All files / src ram.ts

100% Statements 26/26
75% Branches 6/8
100% Functions 6/6
100% Lines 20/20

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 53105x   1x     1x 1x 1x       1x 1x 1x 1x       1x   4x 4x 4x             1x           4x       1x 7x     4x           4x   1x  
import {Resources} from './'
 
import {Asset, Float64, Int64, Struct} from '@wharfkit/antelope'
 
@Struct.type('connector')
export class Connector extends Struct {
    @Struct.field('asset') balance!: Asset
    @Struct.field('float64') weight!: Float64
}
 
@Struct.type('exchange_state')
export class ExchangeState extends Struct {
    @Struct.field('asset') supply!: Asset
    @Struct.field(Connector) base!: Connector
    @Struct.field(Connector) quote!: Connector
}
 
@Struct.type('ramstate')
export class RAMState extends ExchangeState {
    public price_per(bytes: number): Asset {
        const base = this.base.balance.units
        const quote = this.quote.balance.units
        return Asset.fromUnits(
            this.get_input(base, quote, Int64.from(bytes)),
            this.quote.balance.symbol
        )
    }
 
    public price_per_kb(kilobytes: number): Asset {
        return this.price_per(kilobytes * 1000)
    }
 
    // Derived from https://github.com/EOSIO/eosio.contracts/blob/f6578c45c83ec60826e6a1eeb9ee71de85abe976/contracts/eosio.system/src/exchange_state.cpp#L96
    public get_input(base: Int64, quote: Int64, value: Int64): Int64 {
        // (quote * value) / (base - value), using 'ceil' to round up
        return quote.multiplying(value).dividing(base.subtracting(value), 'ceil')
    }
}
 
export class RAMAPI {
    constructor(private parent: Resources) {}
 
    async get_state() {
        const response = await this.parent.api.v1.chain.get_table_rows({
            code: 'eosio',
            scope: 'eosio',
            table: 'rammarket',
            type: RAMState,
        })
        return response.rows[0]
    }
}