All files / src account.ts

90.47% Statements 38/42
84.61% Branches 33/39
85% Functions 17/20
92.68% Lines 38/41

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195222x                 1x   1x 1x   1x 1x 1x                                                     1x             59x 59x     59x   59x 59x       40x       8x       3x       23x 26x     23x 1x         22x               3x                                 4x                   1x               1x                   1x                 2x 2x 1x   2x               2x 2x 1x   2x               1x             6x                   5x             1x  
import {
    Action,
    API,
    APIClient,
    Asset,
    AssetType,
    Name,
    NameType,
    UInt32Type,
} from '@wharfkit/antelope'
import {Contract} from '@wharfkit/contract'
import {Resources} from '@wharfkit/resources'
import {Token} from '@wharfkit/token'
 
import {Permission} from './permission'
import * as SystemContract from './contracts/eosio'
import {Resource, ResourceType} from './resource'
 
export interface AccountArgs<Data extends API.v1.AccountObject = API.v1.AccountObject> {
    client: APIClient
    contract?: Contract
    data: Data
}
 
export interface BuyramOptions {
    receiver?: NameType
}
 
export interface DelegateOptions {
    from?: NameType
    receiver?: NameType
    cpu?: AssetType
    net?: AssetType
    transfer?: boolean
}
 
export interface UndelegateOptions {
    from?: NameType
    receiver?: NameType
    cpu?: AssetType
    net?: AssetType
}
 
export class Account<Data extends API.v1.AccountObject = API.v1.AccountObject> {
    readonly data: Data
    readonly systemContract: Contract
    readonly client: APIClient
    readonly token: Token
 
    constructor(args: AccountArgs<Data>) {
        this.data = args.data
        Iif (args.contract) {
            this.systemContract = args.contract
        } else {
            this.systemContract = new SystemContract.Contract({client: args.client})
        }
        this.client = args.client
        this.token = new Token({client: args.client})
    }
 
    get accountName() {
        return Name.from(this.data.account_name)
    }
 
    get systemToken() {
        return Asset.Symbol.from(this.data.total_resources.cpu_weight.symbol)
    }
 
    balance(symbol?: Asset.SymbolType, tokenContract?: NameType) {
        return this.token.balance(this.accountName, symbol, tokenContract)
    }
 
    permission(permissionName: NameType): Permission {
        const permission = this.data.permissions.find((permission) =>
            permission.perm_name.equals(permissionName)
        )
 
        if (!permission) {
            throw new Error(
                `Permission ${permissionName} does not exist on account ${this.accountName}.`
            )
        }
 
        return Permission.from(permission)
    }
 
    get permissions() {
        return this.data.permissions.map((permission) => Permission.from(permission))
    }
 
    resource(resourceType: ResourceType): Resource {
        return new Resource(resourceType, this.data)
    }
 
    // TODO: Refactor once resources library is updated
    resources(sampleAccount?: NameType) {
        // Returns an instance of the @wharfkit/resources library
        //   configured for this blockchain/account
        return new Resources({
            api: this.client,
            sampleAccount: sampleAccount ? String(sampleAccount) : undefined,
            symbol: this.data.core_liquid_balance
                ? String(this.data.core_liquid_balance.symbol)
                : undefined,
        })
    }
 
    setPermission(permission: Permission): Action {
        return this.systemContract.action('updateauth', {
            account: this.accountName,
            auth: permission.required_auth,
            authorized_by: '',
            parent: permission.parent,
            permission: permission.perm_name,
        })
    }
 
    removePermission(permissionName: NameType): Action {
        return this.systemContract.action('deleteauth', {
            account: this.accountName,
            authorized_by: '',
            permission: permissionName,
        })
    }
 
    linkauth(contract: NameType, action: NameType, requiredPermission: NameType): Action {
        return this.systemContract.action('linkauth', {
            account: this.accountName,
            code: contract,
            type: action,
            requirement: requiredPermission,
            authorized_by: '',
        })
    }
 
    unlinkauth(contract: NameType, action: NameType): Action {
        return this.systemContract.action('unlinkauth', {
            account: this.accountName,
            code: contract,
            type: action,
            authorized_by: '',
        })
    }
 
    buyRam(amount: AssetType, options?: BuyramOptions): Action {
        let receiver = this.accountName
        if (options && options.receiver) {
            receiver = Name.from(options.receiver)
        }
        return this.systemContract.action('buyram', {
            payer: this.accountName,
            quant: amount,
            receiver,
        })
    }
 
    buyRamBytes(bytes: UInt32Type, options?: BuyramOptions): Action {
        let receiver = this.accountName
        if (options && options.receiver) {
            receiver = Name.from(options.receiver)
        }
        return this.systemContract.action('buyrambytes', {
            bytes,
            payer: this.accountName,
            receiver,
        })
    }
 
    sellRam(bytes: UInt32Type): Action {
        return this.systemContract.action('sellram', {
            account: this.accountName,
            bytes,
        })
    }
 
    delegate(value: DelegateOptions): Action {
        return this.systemContract.action('delegatebw', {
            from: value.from || this.accountName,
            receiver: value.receiver || this.accountName,
            stake_cpu_quantity: value.cpu || Asset.fromUnits(0, this.systemToken),
            stake_net_quantity: value.net || Asset.fromUnits(0, this.systemToken),
            transfer: value.transfer !== undefined ? value.transfer : false,
        })
    }
 
    undelegate(value: UndelegateOptions): Action {
        return this.systemContract.action('undelegatebw', {
            from: value.from || this.accountName,
            receiver: value.receiver || this.accountName,
            unstake_cpu_quantity: value.cpu || Asset.fromUnits(0, this.systemToken),
            unstake_net_quantity: value.net || Asset.fromUnits(0, this.systemToken),
        })
    }
}