All files / src index.ts

95% Statements 38/40
82.35% Branches 14/17
100% Functions 5/5
94.87% Lines 37/39

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 93589x 1x   1x 1x 1x 1x   1x 1x 1x                             1x   1x 1x         7x     7x       7x 1x     7x       7x 6x 1x 1x           7x             20x 20x 20x   20x 20x   20x               1x 40x 40x 40x 40x 40x   40x     2x 180x    
import {API, APIClient, APIClientOptions, FetchProvider, Int, UInt128} from '@wharfkit/antelope'
import BN from 'bn.js'
 
import {PowerUpAPI} from './powerup'
import {RAMAPI} from './ram'
import {REXAPI} from './rex'
import bigDecimal from 'js-big-decimal'
 
export * from './powerup'
export * from './ram'
export * from './rex'
 
interface ResourcesOptions extends APIClientOptions {
    api?: APIClient
    sampleAccount?: string
    symbol?: string
    url?: string
}
 
export interface SampleUsage {
    account: API.v1.AccountObject
    cpu: UInt128
    net: UInt128
}
 
export const BNPrecision = new BN(100 * 1000 * 1000)
 
export class Resources {
    static __className = 'Resources'
 
    readonly api: APIClient
 
    // the account to use when sampling usage
    sampleAccount = 'greymassfuel'
 
    // token precision/symbol
    symbol = '4,EOS'
 
    constructor(options: ResourcesOptions) {
        // Allow overriding of the sample account name
        if (options.sampleAccount) {
            this.sampleAccount = options.sampleAccount
        }
        // Allow overriding of the system token symbol
        Iif (options.symbol) {
            this.symbol = options.symbol
        }
        // Allow variations on how to specify the API configuration
        if (options.api) {
            this.api = options.api
        } else if (options.url) {
            this.api = new APIClient({provider: new FetchProvider(options.url, options)})
        } else E{
            throw new Error('Missing url or api client')
        }
    }
 
    v1 = {
        powerup: new PowerUpAPI(this),
        ram: new RAMAPI(this),
        rex: new REXAPI(this),
    }
 
    async getSampledUsage(): Promise<SampleUsage> {
        const account = await this.api.v1.chain.get_account(this.sampleAccount)
        const us = UInt128.from(account.cpu_limit.max.value.mul(BNPrecision))
        const byte = UInt128.from(account.net_limit.max.value.mul(BNPrecision))
 
        const cpu_weight = UInt128.from(account.cpu_weight.value)
        const net_weight = UInt128.from(account.net_weight.value)
 
        return {
            account,
            cpu: divCeil(us.value, cpu_weight.value),
            net: divCeil(byte.value, net_weight.value),
        }
    }
}
 
function divCeil(num: BN, den: BN): UInt128 {
    let v: BN = num.div(den)
    const zero = new BN(0)
    const one = new BN(1)
    if (num.mod(den).gt(zero) && v.gt(one)) {
        v = v.sub(one)
    }
    return UInt128.from(String(v))
}
 
export function intToBigDecimal(value: Int | number) {
    return new bigDecimal(String(value))
}