All files / src/objects sale.ts

100% Statements 30/30
71.42% Branches 10/14
100% Functions 18/18
100% Lines 29/29

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 11652x 1x   1x 1x 1x     1x               16x 16x 16x       16x 16x 16x 16x   16x                       9x       3x       2x       3x       6x                   4x       2x       1x       1x       1x   1x             1x           1x                     1x             1x  
import type {Action, NameType, UInt64Type} from '@wharfkit/antelope'
import {Asset as AntelopeAsset, ExtendedAsset, Name, UInt64} from '@wharfkit/antelope'
import type {SaleObject} from '../types'
import {SaleState} from '../types'
import {Collection} from './collection'
import {Asset} from './asset'
import type {KitUtility} from '../utility'
 
export class Sale {
    readonly data: SaleObject
    readonly collection: Collection
    readonly assets: Asset[]
    readonly price: ExtendedAsset
    readonly utility: KitUtility
 
    static from(saleObject: SaleObject, utility: KitUtility) {
        const collection = new Collection(utility, saleObject.collection)
        const assets = saleObject.assets.map((a) => Asset.from(a, utility))
        return new this(utility, saleObject, collection, assets)
    }
 
    constructor(utility: KitUtility, data: SaleObject, 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 saleId() {
        return UInt64.from(this.data.sale_id)
    }
 
    get seller() {
        return Name.from(this.data.seller)
    }
 
    get buyer() {
        return this.data.buyer ? Name.from(this.data.buyer) : null
    }
 
    get listingPrice() {
        return AntelopeAsset.fromUnits(this.data.listing_price, this.listingSymbol)
    }
 
    get listingSymbol() {
        return this.price.quantity.symbol
    }
 
    /**
     * 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 state() {
        return SaleState[SaleState[this.data.state.toNumber()]]
    }
 
    get isSellerContract() {
        return this.data.is_seller_contract
    }
 
    assert(): Action {
        return this.utility.marketContract.action('assertsale', {
            sale_id: this.saleId,
            asset_ids_to_assert: this.assets.map((a) => a.assetId),
            listing_price_to_assert: this.listingPrice,
            settlement_symbol_to_assert: this.listingSymbol,
        })
    }
 
    cancel(): Action {
        return this.utility.marketContract.action('cancelsale', {
            sale_id: this.saleId,
        })
    }
 
    payram(payer: NameType): Action {
        return this.utility.marketContract.action('paysaleram', {
            payer,
            sale_id: this.saleId,
        })
    }
 
    purchase(
        buyer: NameType,
        intendedDelphiMedian: UInt64Type,
        takerMarketplace: NameType
    ): Action {
        return this.utility.marketContract.action('purchasesale', {
            buyer,
            sale_id: this.saleId,
            intended_delphi_median: intendedDelphiMedian,
            taker_marketplace: takerMarketplace,
        })
    }
}