Skip to content

Commit

Permalink
wallet: add 'spendable_balance' to getwalletinfo RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ca98am79 committed Aug 31, 2020
1 parent 97737f1 commit 1917514
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/wallet/rpc.js
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions test/wallet-rpc-test.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 1917514

Please sign in to comment.