Skip to content

Commit

Permalink
feat(Chain): get balance method (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Sep 10, 2019
1 parent e64462f commit 15147af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion es/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
}
Expand All @@ -55,6 +55,7 @@ const Chain = Oracle.compose({
topBlock: required,
poll: required,
balance: required,
getBalance: required,
tx: required,
getTxInfo: required,
mempool: required,
Expand Down
11 changes: 11 additions & 0 deletions es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -195,6 +205,7 @@ const ChainNode = Chain.compose(Oracle, TransactionValidator, NodePool, {
methods: {
sendTransaction,
balance,
getBalance,
getAccount,
topBlock,
tx,
Expand Down
8 changes: 6 additions & 2 deletions test/integration/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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')
})

Expand Down

0 comments on commit 15147af

Please sign in to comment.