All files / src/objects buyoffer.ts

100% Statements 29/29
71.42% Branches 10/14
100% Functions 17/17
100% Lines 28/28

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 11148x 1x   1x 1x 1x     1x               14x 26x 14x                 14x 14x 14x 14x   14x                       9x       2x       3x                   4x       2x       1x       1x       1x       1x   2x             1x           1x             1x         1x  
import type {Action, NameType} from '@wharfkit/antelope'
import {Asset as AntelopeAsset, ExtendedAsset, Name, UInt64} from '@wharfkit/antelope'
import type {BuyofferObject} from '../types'
import {BuyofferState} from '../types'
import {Collection} from './collection'
import {Asset} from './asset'
import type {KitUtility} from '../utility'
 
export class Buyoffer {
    readonly data: BuyofferObject
    readonly collection: Collection
    readonly assets: Asset[]
    readonly price: ExtendedAsset
    readonly utility: KitUtility
 
    static from(buyofferObject: BuyofferObject, utility: KitUtility) {
        const collection = new Collection(utility, buyofferObject.collection)
        const assets = buyofferObject.assets.map((a) => Asset.from(a, utility))
        return new this(utility, buyofferObject, collection, assets)
    }
 
    constructor(
        utility: KitUtility,
        data: BuyofferObject,
        collection: Collection,
        assets: Asset[]
    ) {
        this.utility = utility
        this.data = data
        this.collection = collection
        this.assets = assets
 
        this.price = ExtendedAsset.from({
            quantity: AntelopeAsset.fromUnits(
                this.data.price.amount,
                AntelopeAsset.Symbol.from(
                    `${this.data.price.token_precision},${this.data.price.token_symbol}`
                )
            ),
            contract: this.data.price.token_contract,
        })
    }
 
    get buyofferId() {
        return UInt64.from(this.data.buyoffer_id)
    }
 
    get seller() {
        return this.data.seller ? Name.from(this.data.seller) : null
    }
 
    get buyer() {
        return Name.from(this.data.buyer)
    }
 
    /**
     * Always a Name, unlike takerMarketplace, which is null until a listing
     * executes. A maker marketplace is fixed when the listing is created, and
     * the API reports the default marketplace as null rather than the empty
     * name it holds on chain.
     */
    get makerMarketplace() {
        return Name.from(this.data.maker_marketplace ?? '')
    }
 
    get takerMarketplace() {
        return this.data.taker_marketplace ? Name.from(this.data.taker_marketplace) : null
    }
 
    get memo() {
        return this.data.memo
    }
 
    get declineMemo() {
        return this.data.decline_memo
    }
 
    get state() {
        return BuyofferState[BuyofferState[this.data.state.toNumber()]]
    }
 
    accept(takerMarketplace: NameType): Action {
        return this.utility.marketContract.action('acceptbuyo', {
            buyoffer_id: this.buyofferId,
            expected_asset_ids: this.assets.map((a) => a.assetId),
            expected_price: this.price.quantity,
            taker_marketplace: takerMarketplace,
        })
    }
 
    cancel(): Action {
        return this.utility.marketContract.action('cancelbuyo', {
            buyoffer_id: this.buyofferId,
        })
    }
 
    decline(memo: string): Action {
        return this.utility.marketContract.action('declinebuyo', {
            buyoffer_id: this.buyofferId,
            decline_memo: memo,
        })
    }
 
    payram(payer: NameType): Action {
        return this.utility.marketContract.action('paybuyoram', {
            payer,
            buyoffer_id: this.buyofferId,
        })
    }
}