All files / src resource.ts

92% Statements 23/25
56.25% Branches 9/16
66.66% Functions 2/3
91.66% Lines 22/24

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 6526x                   1x                       3x 3x 3x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x                                   1x  
import {API, Int64} from '@wharfkit/antelope'
 
export type ResourceType = 'cpu' | 'net' | 'ram'
 
export interface ResourceValues {
    available: Int64
    used: Int64
    max: Int64
}
 
export class Resource {
    public resource: ResourceType
    public data: API.v1.AccountObject
 
    readonly available: Int64
    readonly used: Int64
    readonly max: Int64
 
    readonly current_used?: Int64
    readonly weight?: Int64
 
    constructor(resource: ResourceType, data: API.v1.AccountObject) {
        this.resource = resource
        this.data = data
        switch (resource) {
            case 'cpu': {
                this.available = this.data.cpu_limit.available
                this.current_used = this.data.cpu_limit.current_used
                this.used = this.data.cpu_limit.used
                this.max = this.data.cpu_limit.max
                this.weight = this.data.cpu_weight
                break
            }
            case 'net': {
                this.available = this.data.net_limit.available
                this.current_used = this.data.net_limit.current_used
                this.used = this.data.net_limit.used
                this.max = this.data.net_limit.max
                this.weight = this.data.net_weight
                break
            }
            case 'ram': {
                this.available = this.data.ram_quota.subtracting(this.data.ram_usage)
                this.used = Int64.from(this.data.ram_usage)
                this.max = this.data.ram_quota
                break
            }
            default: {
                throw new Error(`Unknown resource type (${resource}).`)
            }
        }
    }
 
    toJSON() {
        return {
            resource: this.resource,
            available: this.available,
            current_used: this.current_used ? this.current_used : this.current_used,
            used: this.used,
            max: this.max,
            weight: this.weight ? this.weight : undefined,
        }
    }
}