Skip to content

Commit

Permalink
Merge pull request #316 from near/issue-298
Browse files Browse the repository at this point in the history
Add account balance to account state (#298)
  • Loading branch information
vgrichina authored May 13, 2020
2 parents 8528a74 + 3ae4d91 commit 3f5e2fd
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
15 changes: 13 additions & 2 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.

7 changes: 6 additions & 1 deletion lib/providers/json-rpc-provider.d.ts

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

7 changes: 7 additions & 0 deletions lib/providers/json-rpc-provider.js

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

7 changes: 7 additions & 0 deletions lib/providers/provider.d.ts

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

33 changes: 31 additions & 2 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ function sleep(millis: number): Promise<any> {
}

export interface AccountState {
account_id: string;
amount: string;
staked: string;
code_hash: string;
storage_usage: number;
locked: string;
}

export interface AccountBalance {
total: string;
stateStaked: string;
staked: string;
available: string;
}

/**
Expand Down Expand Up @@ -324,4 +331,26 @@ export class Account {
});
return result;
}

/**
* Returns calculated account balance
* @returns {Promise<AccountBalance>}
*/
async getAccountBalance(): Promise<AccountBalance> {
const genesisConfig = await this.connection.provider.experimental_genesisConfig();
const state = await this.state();

const costPerByte = new BN(genesisConfig.runtime_config.storage_amount_per_byte);
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 {
total: totalBalance.toString(),
stateStaked: stateStaked.toString(),
staked: staked.toString(),
available: availableBalance.toString()
};
}
}
11 changes: 10 additions & 1 deletion src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Provider, FinalExecutionOutcome, NodeStatusResult, BlockId,
BlockResult, ChunkId, ChunkResult, adaptTransactionResult, EpochValidatorInfo
BlockResult, ChunkId, ChunkResult, adaptTransactionResult, EpochValidatorInfo,
GenesisConfig
} from './provider';
import { Network } from '../utils/network';
import { ConnectionInfo, fetchJson } from '../utils/web';
Expand Down Expand Up @@ -106,6 +107,14 @@ export class JsonRpcProvider extends Provider {
return this.sendJsonRpc('validators', [blockId]);
}

/**
* Gets EXPERIMENTAL_genesis_config from RPC
* @returns {Promise<GenesisConfig>}
*/
async experimental_genesisConfig(): Promise<GenesisConfig> {
return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []);
}

/**
* Directly call the RPC specifying the method and params
* @param method RPC method
Expand Down
9 changes: 9 additions & 0 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ export interface ValidatorStakeView {
stake: string;
}

export interface GenesisConfig {
runtime_config: RuntimeConfig;
}

export interface RuntimeConfig {
storage_amount_per_byte: string;
}

export interface EpochValidatorInfo {
// Validators for the current epoch.
next_validators: NextEpochValidatorInfo[];
Expand Down Expand Up @@ -183,6 +191,7 @@ export abstract class Provider {
abstract async block(blockId: BlockId): Promise<BlockResult>;
abstract async chunk(chunkId: ChunkId): Promise<ChunkResult>;
abstract async validators(blockId: BlockId): Promise<EpochValidatorInfo>;
abstract async experimental_genesisConfig(): Promise<GenesisConfig>;
}

export function getTransactionLastResult(txResult: FinalExecutionOutcome): any {
Expand Down

0 comments on commit 3f5e2fd

Please sign in to comment.