Skip to content

Commit

Permalink
Add account balance calculation (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbodnar committed May 12, 2020
1 parent 1ff1a50 commit 8651f7a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/account.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/account.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -70,6 +80,7 @@ export class Account {
*/
async state(): Promise<AccountState> {
await this.ready;
this._state.balance = await this.getAccountBalance();
return this._state;
}

Expand Down Expand Up @@ -324,4 +335,25 @@ export class Account {
});
return result;
}

/**
* Returns calculated account balance
* @returns {Promise<AccountBalance>}
*/
private async getAccountBalance(): Promise<AccountBalance> {
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()
};
}
}

0 comments on commit 8651f7a

Please sign in to comment.