From 191751414ed31adcec15ce82ce6c000eb24cc953 Mon Sep 17 00:00:00 2001 From: Mike Carson Date: Mon, 31 Aug 2020 11:10:51 -0400 Subject: [PATCH] wallet: add 'spendable_balance' to getwalletinfo RPC --- lib/wallet/rpc.js | 2 ++ test/wallet-rpc-test.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index ff0222aad..41e48984c 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -748,12 +748,14 @@ class RPC extends RPCBase { const wallet = this.wallet; const balance = await wallet.getBalance(); + const spendableBalance = balance.unconfirmed - balance.ulocked; return { walletid: wallet.id, walletversion: 6, balance: Amount.coin(balance.unconfirmed, true), unconfirmed_balance: Amount.coin(balance.unconfirmed, true), + spendable_balance: Amount.coin(spendableBalance, true), txcount: balance.tx, keypoololdest: 0, keypoolsize: 0, diff --git a/test/wallet-rpc-test.js b/test/wallet-rpc-test.js index db952b7cb..1dea9ff10 100644 --- a/test/wallet-rpc-test.js +++ b/test/wallet-rpc-test.js @@ -10,6 +10,7 @@ const Mnemonic = require('../lib/hd/mnemonic'); const HDPrivateKey = require('../lib/hd/private'); const Script = require('../lib/script/script'); const Address = require('../lib/primitives/address'); +const rules = require('../lib/covenants/rules'); const network = Network.get('regtest'); const mnemonics = require('./data/mnemonic-english.json'); // Commonly used test mnemonic @@ -45,6 +46,16 @@ const wclient = new WalletClient({ apiKey: 'bar' }); +const name = rules.grindName(5, 1, network); + +async function mineBlocks(n, addr) { + addr = addr ? addr : new Address().toString('regtest'); + for (let i = 0; i < n; i++) { + const block = await node.miner.mineBlock(null, addr); + await node.chain.add(block); + } +} + describe('Wallet RPC Methods', function() { this.timeout(15000); @@ -324,5 +335,19 @@ describe('Wallet RPC Methods', function() { assert.strictEqual(info.walletid, 'primary'); assert.strictEqual(info.height, node.chain.height); }); + + it('should get spendable balance in wallet info', async () => { + const address = await wclient.execute('getnewaddress'); + await mineBlocks(2, address); + + await wclient.execute('sendopen', [name]); + await mineBlocks(network.names.treeInterval + 2); + + await wclient.execute('sendbid', [name, 1000, 2000]); + + const info = await wclient.execute('getwalletinfo', []); + + assert.strictEqual(info.spendable_balance, info.unconfirmed_balance - 2000); + }); }); });