From 8651f7ad849fdce4e73693c32fa8cf35fb990447 Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 01:54:24 +0200 Subject: [PATCH] Add account balance calculation (#298) --- lib/account.d.ts | 14 ++++++++++++++ lib/account.js | 19 +++++++++++++++++++ src/account.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/lib/account.d.ts b/lib/account.d.ts index 869a5fb0c1..663afc6e1a 100644 --- a/lib/account.d.ts +++ b/lib/account.d.ts @@ -8,6 +8,15 @@ export interface AccountState { amount: string; staked: string; code_hash: string; + storage_usage: number; + locked: string; + balance: AccountBalance; +} +export interface AccountBalance { + total: string; + stateStaked: string; + staked: string; + available: string; } /** * More information on [the Account spec](https://nomicon.io/DataStructures/Account.html) @@ -119,4 +128,9 @@ export declare class Account { * @returns {Promise} */ getAccountDetails(): Promise; + /** + * Returns calculated account balance + * @returns {Promise} + */ + private getAccountBalance; } diff --git a/lib/account.js b/lib/account.js index 71154b5d74..62661b6a71 100644 --- a/lib/account.js +++ b/lib/account.js @@ -50,6 +50,7 @@ class Account { */ async state() { await this.ready; + this._state.balance = await this.getAccountBalance(); return this._state; } printLogs(contractId, logs) { @@ -279,5 +280,23 @@ class Account { }); return result; } + /** + * Returns calculated account balance + * @returns {Promise} + */ + async getAccountBalance() { + const genesisConfig = await this.connection.provider.genesisConfig(); + const costPerByte = new bn_js_1.default(genesisConfig.runtime_config.storage_amount_per_byte); + const stateStaked = new bn_js_1.default(this._state.storage_usage).mul(costPerByte); + const staked = new bn_js_1.default(this._state.locked); + const totalBalance = new bn_js_1.default(this._state.amount).add(staked); + const availableBalance = totalBalance.sub(staked).sub(stateStaked); + return { + total: totalBalance.toString(), + stateStaked: stateStaked.toString(), + staked: staked.toString(), + available: availableBalance.toString() + }; + } } exports.Account = Account; diff --git a/src/account.ts b/src/account.ts index 0bb8886080..b114cc9e59 100644 --- a/src/account.ts +++ b/src/account.ts @@ -36,6 +36,16 @@ export interface AccountState { amount: string; staked: string; code_hash: string; + storage_usage: number; + locked: string; + balance: AccountBalance; +} + +export interface AccountBalance { + total: string; + stateStaked: string; + staked: string; + available: string; } /** @@ -70,6 +80,7 @@ export class Account { */ async state(): Promise { await this.ready; + this._state.balance = await this.getAccountBalance(); return this._state; } @@ -324,4 +335,25 @@ export class Account { }); return result; } + + /** + * Returns calculated account balance + * @returns {Promise} + */ + private async getAccountBalance(): Promise { + const genesisConfig = await this.connection.provider.genesisConfig(); + + const costPerByte = new BN(genesisConfig.runtime_config.storage_amount_per_byte); + const stateStaked = new BN(this._state.storage_usage).mul(costPerByte); + const staked = new BN(this._state.locked); + const totalBalance = new BN(this._state.amount).add(staked); + const availableBalance = totalBalance.sub(staked).sub(stateStaked); + + return { + total: totalBalance.toString(), + stateStaked: stateStaked.toString(), + staked: staked.toString(), + available: availableBalance.toString() + }; + } }