All files / src/objects offer.ts

100% Statements 27/27
75% Branches 6/8
100% Functions 14/14
100% Lines 27/27

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 9550x 1x   1x 1x     1x             15x 15x   15x       30x 15x       15x                 15x 15x 15x 15x       9x       3x       3x       2x       1x       1x       1x       1x           1x           1x           1x         1x  
import type {Action, NameType} from '@wharfkit/antelope'
import {Name} from '@wharfkit/antelope'
import type {OfferObject} from '../types'
import {OfferState} from '../types'
import {Asset} from './asset'
import type {KitUtility} from '../utility'
 
export class Offer {
    readonly data: OfferObject
    readonly sender_assets: Asset[]
    readonly recipient_assets: Asset[]
    readonly utility: KitUtility
 
    static from(offerObject: OfferObject, utility: KitUtility) {
        const sender_assets: Asset[] = []
        const recipient_assets: Asset[] = []
 
        for (const item of [
            {ass: sender_assets, objs: offerObject.sender_assets},
            {ass: recipient_assets, objs: offerObject.recipient_assets},
        ]) {
            for (const assetObject of item.objs) {
                item.ass.push(Asset.from(assetObject, utility))
            }
        }
 
        return new this(utility, offerObject, sender_assets, recipient_assets)
    }
 
    constructor(
        utility: KitUtility,
        data: OfferObject,
        sender_assets: Asset[],
        recipient_assets: Asset[]
    ) {
        this.utility = utility
        this.data = data
        this.sender_assets = sender_assets
        this.recipient_assets = recipient_assets
    }
 
    get offerId() {
        return this.data.offer_id
    }
 
    get senderName() {
        return Name.from(this.data.sender_name)
    }
 
    get recipientName() {
        return Name.from(this.data.recipient_name)
    }
 
    get memo() {
        return this.data.memo
    }
 
    get state() {
        return OfferState[OfferState[this.data.state.toNumber()]]
    }
 
    get isSenderContract() {
        return this.data.is_sender_contract
    }
 
    get isRecipientContract() {
        return this.data.is_recipient_contract
    }
 
    cancel(): Action {
        return this.utility.assetsContract.action('canceloffer', {
            offer_id: this.offerId,
        })
    }
 
    decline(): Action {
        return this.utility.assetsContract.action('declineoffer', {
            offer_id: this.offerId,
        })
    }
 
    accept(): Action {
        return this.utility.assetsContract.action('acceptoffer', {
            offer_id: this.offerId,
        })
    }
 
    payram(payer: NameType): Action {
        return this.utility.assetsContract.action('payofferram', {
            payer,
            offer_id: this.offerId,
        })
    }
}