All files / src/objects asset.ts

86.66% Statements 26/30
83.33% Branches 10/12
75% Functions 12/16
86.66% Lines 26/30

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 114260x 1x     1x 1x 1x       1x                 118x 118x 118x   118x                   118x 118x 118x 118x 118x 118x                       23x                               16x       2x       2x       2x       4x       1x             1x                       1x             1x  
import type {Action, AssetType, NameType} from '@wharfkit/antelope'
import {Asset as AntelopeAsset, ExtendedAsset, Name, UInt64} from '@wharfkit/antelope'
 
import type {AssetObject} from '../types'
import {Collection} from './collection'
import {Schema} from './schema'
import {Template} from './template'
import type {KitUtility} from '../utility'
import type * as AtomicAssetsContract from '../contracts/atomicassets'
 
export class Asset {
    readonly data: AssetObject
    readonly collection: Collection
    readonly schema: Schema
    readonly template: Template
    readonly backedTokens: ExtendedAsset[]
    readonly utility: KitUtility
 
    static from(assetObject: AssetObject, utility: KitUtility) {
        const collection = new Collection(utility, assetObject.collection)
        const schema = new Schema(utility, assetObject.schema, collection)
        const template = new Template(utility, assetObject.template, collection, schema)
 
        return new this(utility, assetObject, collection, schema, template)
    }
 
    constructor(
        utility: KitUtility,
        data: AssetObject,
        collection: Collection,
        schema: Schema,
        template: Template
    ) {
        this.utility = utility
        this.data = data
        this.collection = collection
        this.schema = schema
        this.template = template
        this.backedTokens = this.data.backed_tokens.map((t) =>
            ExtendedAsset.from({
                quantity: AntelopeAsset.fromUnits(
                    t.amount,
                    AntelopeAsset.Symbol.from(`${t.token_precision},${t.token_symbol}`)
                ),
                contract: t.token_contract,
            })
        )
    }
 
    get assetId() {
        return UInt64.from(this.data.asset_id)
    }
 
    get immutableData() {
        return this.data.immutable_data
    }
 
    get mutableData() {
        return this.data.mutable_data
    }
 
    get assetData() {
        return this.data.data
    }
 
    get owner() {
        return this.data.owner ? Name.from(this.data.owner) : null
    }
 
    get transferable() {
        return this.data.is_transferable
    }
 
    get burnable() {
        return this.data.is_burnable
    }
 
    get name() {
        return this.data.name
    }
 
    get burnedByAccount() {
        return this.data.burned_by_account ? Name.from(this.data.burned_by_account) : null
    }
 
    burn(): Action {
        return this.utility.assetsContract.action('burnasset', {
            asset_owner: this.owner,
            asset_id: this.assetId,
        })
    }
 
    back(payer: NameType, backedToken: AssetType): Action {
        return this.utility.assetsContract.action('backasset', {
            payer,
            asset_owner: this.owner,
            asset_id: this.assetId,
            token_to_back: backedToken,
        })
    }
 
    setData(
        authorizedEditor: NameType,
        mutableData: AtomicAssetsContract.ActionParams.Type.pair_string_ATOMIC_ATTRIBUTE[]
    ): Action {
        return this.utility.assetsContract.action('setassetdata', {
            authorized_editor: authorizedEditor,
            asset_owner: this.owner,
            asset_id: this.assetId,
            new_mutable_data: mutableData,
        })
    }
}