From 15147af1af8f03e6a6ba9573ceb09dbc23dc966d Mon Sep 17 00:00:00 2001 From: naz_dou <41945483+nduchak@users.noreply.github.com> Date: Tue, 10 Sep 2019 12:23:28 +0300 Subject: [PATCH] feat(Chain): get balance method (#655) --- es/chain/index.js | 3 ++- es/chain/node.js | 11 +++++++++++ test/integration/accounts.js | 8 ++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/es/chain/index.js b/es/chain/index.js index ca3417a630..3fa9da34be 100644 --- a/es/chain/index.js +++ b/es/chain/index.js @@ -42,7 +42,7 @@ const Chain = Oracle.compose({ deepConf: { Ae: { methods: [ - 'sendTransaction', 'height', 'awaitHeight', 'poll', 'balance', 'tx', + 'sendTransaction', 'height', 'awaitHeight', 'poll', 'balance', 'getBalance', 'tx', 'mempool', 'topBlock', 'getTxInfo', 'txDryRun', 'getName', 'getNodeInfo', 'getAccount' ] } @@ -55,6 +55,7 @@ const Chain = Oracle.compose({ topBlock: required, poll: required, balance: required, + getBalance: required, tx: required, getTxInfo: required, mempool: required, diff --git a/es/chain/node.js b/es/chain/node.js index e5e8e16e64..42845eca6e 100644 --- a/es/chain/node.js +++ b/es/chain/node.js @@ -64,12 +64,22 @@ async function getAccount (address, { height, hash } = {}) { return this.api.getAccountByPubkey(address) } +/** + * @function + * @deprecated + */ async function balance (address, { height, hash, format = false } = {}) { const { balance } = await this.getAccount(address, { hash, height }) return format ? formatBalance(balance) : balance.toString() } +async function getBalance (address, { height, hash, format = false } = {}) { + const { balance } = await this.getAccount(address, { hash, height }).catch(_ => ({ balance: 0 })) + + return format ? formatBalance(balance) : balance.toString() +} + async function tx (hash, info = false) { const tx = await this.api.getTransactionByHash(hash) if (['ContractCreateTx', 'ContractCallTx'].includes(tx.tx.type) && info) { @@ -195,6 +205,7 @@ const ChainNode = Chain.compose(Oracle, TransactionValidator, NodePool, { methods: { sendTransaction, balance, + getBalance, getAccount, topBlock, tx, diff --git a/test/integration/accounts.js b/test/integration/accounts.js index 6473156cfe..abb2859b66 100644 --- a/test/integration/accounts.js +++ b/test/integration/accounts.js @@ -39,10 +39,14 @@ describe('Accounts', function () { wallet.setKeypair(generateKeyPair()) }) - it('determining the balance', async () => { + it('determining the balance using deprecated `balance` method', async () => { return wallet.balance(await wallet.address()).should.be.rejectedWith(Error) }) + it('determining the balance', async () => { + return wallet.getBalance(await wallet.address()).should.eventually.be.equal('0') + }) + it('spending tokens', async () => { return wallet.spend(1, receiver).should.be.rejectedWith(Error) }) @@ -56,7 +60,7 @@ describe('Accounts', function () { }) }) - it('determines the balance', async () => { + it('determines the balance using `balance`', async () => { return wallet.balance(await wallet.address()).should.eventually.be.a('string') })