Skip to content

Commit

Permalink
Refactor getAccountBalance (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbodnar committed May 13, 2020
1 parent 8651f7a commit 409a0f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/account.d.ts

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

8 changes: 4 additions & 4 deletions lib/account.js

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

11 changes: 5 additions & 6 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface AccountState {
code_hash: string;
storage_usage: number;
locked: string;
balance: AccountBalance;
}

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

Expand Down Expand Up @@ -340,13 +338,14 @@ export class Account {
* Returns calculated account balance
* @returns {Promise<AccountBalance>}
*/
private async getAccountBalance(): Promise<AccountBalance> {
async getAccountBalance(): Promise<AccountBalance> {
const genesisConfig = await this.connection.provider.genesisConfig();
const state = await this.state();

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 stateStaked = new BN(state.storage_usage).mul(costPerByte);
const staked = new BN(state.locked);
const totalBalance = new BN(state.amount).add(staked);
const availableBalance = totalBalance.sub(staked).sub(stateStaked);

return {
Expand Down

0 comments on commit 409a0f0

Please sign in to comment.