From 1ff1a50481d1bd45345c6c4d3c597337c8ce82cd Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 01:54:09 +0200 Subject: [PATCH 1/5] Add genesis config rpc provider (#298) --- lib/providers/json-rpc-provider.d.ts | 7 ++++++- lib/providers/json-rpc-provider.js | 7 +++++++ lib/providers/provider.d.ts | 7 +++++++ src/providers/json-rpc-provider.ts | 11 ++++++++++- src/providers/provider.ts | 9 +++++++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/providers/json-rpc-provider.d.ts b/lib/providers/json-rpc-provider.d.ts index 8f938d4215..e9b0dedebe 100644 --- a/lib/providers/json-rpc-provider.d.ts +++ b/lib/providers/json-rpc-provider.d.ts @@ -1,4 +1,4 @@ -import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, BlockResult, ChunkId, ChunkResult, EpochValidatorInfo } from './provider'; +import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, BlockResult, ChunkId, ChunkResult, EpochValidatorInfo, GenesisConfig } from './provider'; import { Network } from '../utils/network'; import { ConnectionInfo } from '../utils/web'; import { TypedError } from '../utils/errors'; @@ -59,6 +59,11 @@ export declare class JsonRpcProvider extends Provider { * @param blockId Block hash or height, or null for latest. */ validators(blockId: BlockId): Promise; + /** + * Gets EXPERIMENTAL_genesis_config from RPC + * @returns {Promise} + */ + genesisConfig(): Promise; /** * Directly call the RPC specifying the method and params * @param method RPC method diff --git a/lib/providers/json-rpc-provider.js b/lib/providers/json-rpc-provider.js index 2a291ead47..188c001a8f 100644 --- a/lib/providers/json-rpc-provider.js +++ b/lib/providers/json-rpc-provider.js @@ -89,6 +89,13 @@ class JsonRpcProvider extends provider_1.Provider { async validators(blockId) { return this.sendJsonRpc('validators', [blockId]); } + /** + * Gets EXPERIMENTAL_genesis_config from RPC + * @returns {Promise} + */ + async genesisConfig() { + return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); + } /** * Directly call the RPC specifying the method and params * @param method RPC method diff --git a/lib/providers/provider.d.ts b/lib/providers/provider.d.ts index ddf5db0b74..ef7b838de3 100644 --- a/lib/providers/provider.d.ts +++ b/lib/providers/provider.d.ts @@ -133,6 +133,12 @@ export interface ValidatorStakeView { public_key: string; stake: string; } +export interface GenesisConfig { + runtime_config: RuntimeConfig; +} +export interface RuntimeConfig { + storage_amount_per_byte: string; +} export interface EpochValidatorInfo { next_validators: NextEpochValidatorInfo[]; current_validators: CurrentEpochValidatorInfo[]; @@ -151,6 +157,7 @@ export declare abstract class Provider { abstract block(blockId: BlockId): Promise; abstract chunk(chunkId: ChunkId): Promise; abstract validators(blockId: BlockId): Promise; + abstract genesisConfig(): Promise; } export declare function getTransactionLastResult(txResult: FinalExecutionOutcome): any; export declare function adaptTransactionResult(txResult: any): FinalExecutionOutcome; diff --git a/src/providers/json-rpc-provider.ts b/src/providers/json-rpc-provider.ts index a8e9fda43f..1a650c22ff 100644 --- a/src/providers/json-rpc-provider.ts +++ b/src/providers/json-rpc-provider.ts @@ -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'; @@ -106,6 +107,14 @@ export class JsonRpcProvider extends Provider { return this.sendJsonRpc('validators', [blockId]); } + /** + * Gets EXPERIMENTAL_genesis_config from RPC + * @returns {Promise} + */ + async genesisConfig(): Promise { + return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); + } + /** * Directly call the RPC specifying the method and params * @param method RPC method diff --git a/src/providers/provider.ts b/src/providers/provider.ts index f2a0c7b371..6546151177 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -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[]; @@ -183,6 +191,7 @@ export abstract class Provider { abstract async block(blockId: BlockId): Promise; abstract async chunk(chunkId: ChunkId): Promise; abstract async validators(blockId: BlockId): Promise; + abstract async genesisConfig(): Promise; } export function getTransactionLastResult(txResult: FinalExecutionOutcome): any { From 8651f7ad849fdce4e73693c32fa8cf35fb990447 Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 01:54:24 +0200 Subject: [PATCH 2/5] 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() + }; + } } From 409a0f0a3b66a4da9ce55911a312187a52d49fd5 Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 23:10:50 +0200 Subject: [PATCH 3/5] Refactor getAccountBalance (#298) --- lib/account.d.ts | 3 +-- lib/account.js | 8 ++++---- src/account.ts | 11 +++++------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/account.d.ts b/lib/account.d.ts index 663afc6e1a..00e2eb7793 100644 --- a/lib/account.d.ts +++ b/lib/account.d.ts @@ -10,7 +10,6 @@ export interface AccountState { code_hash: string; storage_usage: number; locked: string; - balance: AccountBalance; } export interface AccountBalance { total: string; @@ -132,5 +131,5 @@ export declare class Account { * Returns calculated account balance * @returns {Promise} */ - private getAccountBalance; + getAccountBalance(): Promise; } diff --git a/lib/account.js b/lib/account.js index 62661b6a71..8cdaca3f25 100644 --- a/lib/account.js +++ b/lib/account.js @@ -50,7 +50,6 @@ class Account { */ async state() { await this.ready; - this._state.balance = await this.getAccountBalance(); return this._state; } printLogs(contractId, logs) { @@ -286,10 +285,11 @@ class Account { */ async getAccountBalance() { const genesisConfig = await this.connection.provider.genesisConfig(); + const state = await this.state(); 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 stateStaked = new bn_js_1.default(state.storage_usage).mul(costPerByte); + const staked = new bn_js_1.default(state.locked); + const totalBalance = new bn_js_1.default(state.amount).add(staked); const availableBalance = totalBalance.sub(staked).sub(stateStaked); return { total: totalBalance.toString(), diff --git a/src/account.ts b/src/account.ts index b114cc9e59..9ce4f651c6 100644 --- a/src/account.ts +++ b/src/account.ts @@ -38,7 +38,6 @@ export interface AccountState { code_hash: string; storage_usage: number; locked: string; - balance: AccountBalance; } export interface AccountBalance { @@ -80,7 +79,6 @@ export class Account { */ async state(): Promise { await this.ready; - this._state.balance = await this.getAccountBalance(); return this._state; } @@ -340,13 +338,14 @@ export class Account { * Returns calculated account balance * @returns {Promise} */ - private async getAccountBalance(): Promise { + async getAccountBalance(): Promise { 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 { From c532e506f9b4c3f56a6615781e6fdf60bf9925bc Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 23:17:58 +0200 Subject: [PATCH 4/5] Change genesisConfig to experimental_genesisConfig (#298) --- lib/account.js | 2 +- lib/providers/json-rpc-provider.d.ts | 2 +- lib/providers/json-rpc-provider.js | 2 +- lib/providers/provider.d.ts | 2 +- src/account.ts | 2 +- src/providers/json-rpc-provider.ts | 2 +- src/providers/provider.ts | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/account.js b/lib/account.js index 8cdaca3f25..dc92b0ae8a 100644 --- a/lib/account.js +++ b/lib/account.js @@ -284,7 +284,7 @@ class Account { * @returns {Promise} */ async getAccountBalance() { - const genesisConfig = await this.connection.provider.genesisConfig(); + const genesisConfig = await this.connection.provider.experimental_genesisConfig(); const state = await this.state(); const costPerByte = new bn_js_1.default(genesisConfig.runtime_config.storage_amount_per_byte); const stateStaked = new bn_js_1.default(state.storage_usage).mul(costPerByte); diff --git a/lib/providers/json-rpc-provider.d.ts b/lib/providers/json-rpc-provider.d.ts index e9b0dedebe..f363284f14 100644 --- a/lib/providers/json-rpc-provider.d.ts +++ b/lib/providers/json-rpc-provider.d.ts @@ -63,7 +63,7 @@ export declare class JsonRpcProvider extends Provider { * Gets EXPERIMENTAL_genesis_config from RPC * @returns {Promise} */ - genesisConfig(): Promise; + experimental_genesisConfig(): Promise; /** * Directly call the RPC specifying the method and params * @param method RPC method diff --git a/lib/providers/json-rpc-provider.js b/lib/providers/json-rpc-provider.js index 188c001a8f..a398508905 100644 --- a/lib/providers/json-rpc-provider.js +++ b/lib/providers/json-rpc-provider.js @@ -93,7 +93,7 @@ class JsonRpcProvider extends provider_1.Provider { * Gets EXPERIMENTAL_genesis_config from RPC * @returns {Promise} */ - async genesisConfig() { + async experimental_genesisConfig() { return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); } /** diff --git a/lib/providers/provider.d.ts b/lib/providers/provider.d.ts index ef7b838de3..f7d9238e51 100644 --- a/lib/providers/provider.d.ts +++ b/lib/providers/provider.d.ts @@ -157,7 +157,7 @@ export declare abstract class Provider { abstract block(blockId: BlockId): Promise; abstract chunk(chunkId: ChunkId): Promise; abstract validators(blockId: BlockId): Promise; - abstract genesisConfig(): Promise; + abstract experimental_genesisConfig(): Promise; } export declare function getTransactionLastResult(txResult: FinalExecutionOutcome): any; export declare function adaptTransactionResult(txResult: any): FinalExecutionOutcome; diff --git a/src/account.ts b/src/account.ts index 9ce4f651c6..7027b4360a 100644 --- a/src/account.ts +++ b/src/account.ts @@ -339,7 +339,7 @@ export class Account { * @returns {Promise} */ async getAccountBalance(): Promise { - const genesisConfig = await this.connection.provider.genesisConfig(); + const genesisConfig = await this.connection.provider.experimental_genesisConfig(); const state = await this.state(); const costPerByte = new BN(genesisConfig.runtime_config.storage_amount_per_byte); diff --git a/src/providers/json-rpc-provider.ts b/src/providers/json-rpc-provider.ts index 1a650c22ff..927f5b2cb8 100644 --- a/src/providers/json-rpc-provider.ts +++ b/src/providers/json-rpc-provider.ts @@ -111,7 +111,7 @@ export class JsonRpcProvider extends Provider { * Gets EXPERIMENTAL_genesis_config from RPC * @returns {Promise} */ - async genesisConfig(): Promise { + async experimental_genesisConfig(): Promise { return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); } diff --git a/src/providers/provider.ts b/src/providers/provider.ts index 6546151177..c278e26d4e 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -191,7 +191,7 @@ export abstract class Provider { abstract async block(blockId: BlockId): Promise; abstract async chunk(chunkId: ChunkId): Promise; abstract async validators(blockId: BlockId): Promise; - abstract async genesisConfig(): Promise; + abstract async experimental_genesisConfig(): Promise; } export function getTransactionLastResult(txResult: FinalExecutionOutcome): any { From 3ae4d9174cf082e0f5778cd9228e6188d2718013 Mon Sep 17 00:00:00 2001 From: marcinbodnardesktop Date: Wed, 13 May 2020 23:18:59 +0200 Subject: [PATCH 5/5] Cleanup for account (#298) --- lib/account.d.ts | 2 -- src/account.ts | 2 -- 2 files changed, 4 deletions(-) diff --git a/lib/account.d.ts b/lib/account.d.ts index 00e2eb7793..969cfc7289 100644 --- a/lib/account.d.ts +++ b/lib/account.d.ts @@ -4,9 +4,7 @@ import { FinalExecutionOutcome } from './providers'; import { Connection } from './connection'; import { PublicKey } from './utils/key_pair'; export interface AccountState { - account_id: string; amount: string; - staked: string; code_hash: string; storage_usage: number; locked: string; diff --git a/src/account.ts b/src/account.ts index 7027b4360a..d8c60dbf8d 100644 --- a/src/account.ts +++ b/src/account.ts @@ -32,9 +32,7 @@ function sleep(millis: number): Promise { } export interface AccountState { - account_id: string; amount: string; - staked: string; code_hash: string; storage_usage: number; locked: string;