All files / src/chain asset.ts

84.32% Statements 113/134
63.15% Branches 24/38
80.35% Functions 45/56
84.21% Lines 112/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376                  1x 1x               83x 2x   81x   3x     3x   78x             78x 78x 2x   76x 76x 76x 74x       3x 3x       1x       9x 9x 9x       5x       23x 23x 23x 5x 5x   23x 35x   23x 17x   23x 23x 5x   23x       92x 92x                 13x       6x       23x       25x 25x       23x       10x       1x     1x 1x     21x 3x   18x 1x   17x 17x 1x   16x 1x   16x 16x       92x       11x       5x           104x 1x   103x 2x   101x               33x       55x       2x       27x               14x               9x       8x       4x           1x 1x     2x     2x 2x   2x       1x                   5x     5x               1x       1x       1x           1x 1x     1x     1x       1x             2x 2x               1x 1x       1x               1x 1x                                                                             159x         135x       136x 394x           92x 92x 92x 92x 92x       94x 94x 94x 285x   94x    
import {ABISerializableObject} from '../serializer/serializable'
import {ABIEncoder} from '../serializer/encoder'
import {ABIDecoder} from '../serializer/decoder'
import {isInstanceOf} from '../utils'
 
import {Int64, Int64Type, Name, NameType, UInt64} from '../'
 
export type AssetType = Asset | string
 
export class Asset implements ABISerializableObject {
    static abiName = 'asset'
 
    units: Int64
    symbol: Asset.Symbol
 
    static from(value: AssetType): Asset
    static from(value: number, symbol: Asset.SymbolType): Asset
    static from(value: AssetType | number, symbol?: Asset.SymbolType) {
        if (isInstanceOf(value, Asset)) {
            return value
        }
        switch (typeof value) {
            case 'number':
                Iif (!symbol) {
                    throw new Error('Symbol is required when creating Asset from number')
                }
                return this.fromFloat(value, symbol)
            case 'string':
                return this.fromString(value)
            default:
                throw new Error('Invalid asset')
        }
    }
 
    static fromString(value: string) {
        const parts = (typeof value === 'string' ? value : '').split(' ')
        if (parts.length !== 2) {
            throw new Error('Invalid asset string')
        }
        const amount = parts[0].replace('.', '')
        const precision = (parts[0].split('.')[1] || '').length
        const symbol = Asset.Symbol.fromParts(parts[1], precision)
        return new Asset(Int64.from(amount), symbol)
    }
 
    static fromFloat(value: number, symbol: Asset.SymbolType) {
        const s = Asset.Symbol.from(symbol)
        return new Asset(s.convertFloat(value), s)
    }
 
    static fromUnits(value: Int64Type, symbol: Asset.SymbolType) {
        return new Asset(Int64.from(value), Asset.Symbol.from(symbol))
    }
 
    static fromABI(decoder: ABIDecoder): Asset {
        const units = Int64.fromABI(decoder)
        const symbol = Asset.Symbol.fromABI(decoder)
        return new Asset(units, symbol)
    }
 
    static abiDefault() {
        return new this(Int64.from(0), Asset.Symbol.abiDefault())
    }
 
    static formatUnits(units: Int64Type, precision: number) {
        const digits = Int64.from(units).toString().split('')
        let negative = false
        if (digits[0] === '-') {
            negative = true
            digits.shift()
        }
        while (digits.length <= precision) {
            digits.unshift('0')
        }
        if (precision > 0) {
            digits.splice(digits.length - precision, 0, '.')
        }
        let rv = digits.join('')
        if (negative) {
            rv = '-' + rv
        }
        return rv
    }
 
    constructor(units: Int64, symbol: Asset.Symbol) {
        this.units = units
        this.symbol = symbol
    }
 
    equals(other: AssetType) {
        const {symbol, units} = Asset.from(other)
        return this.symbol.value.equals(symbol.value) && this.units.equals(units)
    }
 
    get value(): number {
        return this.symbol.convertUnits(this.units)
    }
 
    set value(newValue: number) {
        this.units = this.symbol.convertFloat(newValue)
    }
 
    get quantity(): string {
        return Asset.formatUnits(this.units, this.symbol.precision)
    }
 
    toABI(encoder: ABIEncoder) {
        this.units.toABI(encoder)
        this.symbol.toABI(encoder)
    }
 
    toString() {
        return this.quantity + ' ' + this.symbol.name
    }
 
    toJSON() {
        return this.toString()
    }
}
 
export namespace Asset {
    export type SymbolType = Symbol | UInt64 | string
    export class Symbol implements ABISerializableObject {
        static abiName = 'symbol'
        static maxPrecision = 18
 
        static from(value: SymbolType) {
            if (isInstanceOf(value, Symbol)) {
                return value
            }
            if (isInstanceOf(value, UInt64)) {
                return new Symbol(value)
            }
            const parts = value.split(',')
            if (parts.length !== 2 && value !== '0,') {
                throw new Error('Invalid symbol string')
            }
            if (value === '0,') {
                parts.push('')
            }
            const precision = Number.parseInt(parts[0])
            return Symbol.fromParts(parts[1], precision)
        }
 
        static fromParts(name: string, precision: number) {
            return new Symbol(toRawSymbol(name, precision))
        }
 
        static fromABI(decoder: ABIDecoder): Symbol {
            return new Symbol(UInt64.fromABI(decoder))
        }
 
        static abiDefault() {
            return this.from('4,SYS') // CORE_SYMBOL = 4,CORE_SYMBOL_NAME
        }
 
        value: UInt64
 
        constructor(value: UInt64) {
            if (toSymbolPrecision(value) > Symbol.maxPrecision) {
                throw new Error('Invalid asset symbol, precision too large')
            }
            if (!value.equals(0) && !SymbolCode.pattern.test(toSymbolName(value))) {
                throw new Error('Invalid asset symbol, name must be uppercase A-Z')
            }
            this.value = value
        }
 
        equals(other: SymbolType) {
            return this.value.equals(Symbol.from(other).value)
        }
 
        get name(): string {
            return toSymbolName(this.value)
        }
 
        get precision(): number {
            return toSymbolPrecision(this.value)
        }
 
        get code(): SymbolCode {
            return new SymbolCode(UInt64.from(this.value.value.clone().iushrn(8)))
        }
 
        toABI(encoder: ABIEncoder) {
            this.value.toABI(encoder)
        }
 
        /**
         * Convert units to floating point number according to symbol precision.
         * @throws If the given units can't be represented in 53 bits.
         **/
        convertUnits(units: Int64): number {
            return units.value.toNumber() / Math.pow(10, this.precision)
        }
 
        /**
         * Convert floating point to units according to symbol precision.
         * Note that the value will be rounded to closest precision.
         **/
        convertFloat(float: number): Int64 {
            return Int64.from(float.toFixed(this.precision).replace('.', ''))
        }
 
        toString() {
            return `${this.precision},${this.name}`
        }
 
        toJSON() {
            return this.toString()
        }
    }
 
    export type SymbolCodeType = SymbolCode | UInt64 | string | number
    export class SymbolCode implements ABISerializableObject {
        static abiName = 'symbol_code'
        static pattern = /^[A-Z]{0,7}$/
 
        static from(value: SymbolCodeType) {
            Iif (isInstanceOf(value, SymbolCode)) {
                return value
            }
            if (typeof value === 'string') {
                value = UInt64.from(toRawSymbolCode(value))
            }
            return new this(UInt64.from(value))
        }
 
        static fromABI(decoder: ABIDecoder) {
            return new SymbolCode(UInt64.fromABI(decoder))
        }
 
        static abiDefault() {
            return this.from('SYS') // CORE_SYMBOL_NAME
        }
 
        value: UInt64
 
        constructor(value: UInt64) {
            Iif (!value.equals(0) && !SymbolCode.pattern.test(toSymbolName(value))) {
                throw new Error('Invalid asset symbol, name must be uppercase A-Z')
            }
            this.value = value
        }
 
        equals(other: SymbolCodeType) {
            return this.value.equals(SymbolCode.from(other).value)
        }
 
        toABI(encoder: ABIEncoder) {
            this.value.toABI(encoder)
        }
 
        toString() {
            return charsToSymbolName(this.value.value.toArray('be'))
        }
 
        toJSON() {
            return this.toString()
        }
    }
}
 
export type ExtendedAssetType = ExtendedAsset | {quantity: AssetType; contract: NameType}
export class ExtendedAsset implements ABISerializableObject {
    static abiName = 'extended_asset'
 
    static from(value: ExtendedAssetType) {
        Iif (isInstanceOf(value, ExtendedAsset)) {
            return value
        }
        return new this(Asset.from(value.quantity), Name.from(value.contract))
    }
 
    static fromABI(decoder: ABIDecoder) {
        return new ExtendedAsset(Asset.fromABI(decoder), Name.fromABI(decoder))
    }
 
    quantity: Asset
    contract: Name
 
    constructor(quantity: Asset, contract: Name) {
        this.quantity = quantity
        this.contract = contract
    }
 
    equals(other: ExtendedAssetType) {
        return this.quantity.equals(other.quantity) && this.contract.equals(other.contract)
    }
 
    toABI(encoder: ABIEncoder) {
        this.quantity.toABI(encoder)
        this.contract.toABI(encoder)
    }
 
    toJSON() {
        return {
            quantity: this.quantity,
            contract: this.contract,
        }
    }
}
 
export type ExtendedSymbolType = ExtendedSymbol | {sym: Asset.SymbolType; contract: NameType}
export class ExtendedSymbol implements ABISerializableObject {
    static abiName = 'extended_symbol'
 
    static from(value: ExtendedSymbolType) {
        Iif (isInstanceOf(value, ExtendedSymbol)) {
            return value
        }
        return new this(Asset.Symbol.from(value.sym), Name.from(value.contract))
    }
 
    static fromABI(decoder: ABIDecoder) {
        return new ExtendedSymbol(Asset.Symbol.fromABI(decoder), Name.fromABI(decoder))
    }
 
    sym: Asset.Symbol
    contract: Name
 
    constructor(sym: Asset.Symbol, contract: Name) {
        this.sym = sym
        this.contract = contract
    }
 
    equals(other: ExtendedSymbolType) {
        return this.sym.equals(other.sym) && this.contract.equals(other.contract)
    }
 
    toABI(encoder: ABIEncoder) {
        this.sym.toABI(encoder)
        this.contract.toABI(encoder)
    }
 
    toJSON() {
        return {
            sym: this.sym,
            contract: this.contract,
        }
    }
}
 
function toSymbolPrecision(rawSymbol: UInt64) {
    return rawSymbol.value.and(UInt64.from(0xff).value).toNumber()
}
 
function toSymbolName(rawSymbol: UInt64) {
    const chars = rawSymbol.value.toArray('be').slice(0, -1)
    return charsToSymbolName(chars)
}
 
function charsToSymbolName(chars: number[]) {
    return chars
        .map((char) => String.fromCharCode(char))
        .reverse()
        .join('')
}
 
function toRawSymbol(name: string, precision: number) {
    const code = toRawSymbolCode(name)
    const bytes = new Uint8Array(code.length + 1)
    bytes[0] = precision
    bytes.set(code, 1)
    return UInt64.from(bytes)
}
 
function toRawSymbolCode(name: string) {
    const length = Math.min(name.length, 7)
    const bytes = new Uint8Array(length)
    for (let i = 0; i < length; i++) {
        bytes[i] = name.charCodeAt(i)
    }
    return bytes
}