Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: add 'spendable_balance' to getwalletinfo RPC #507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});
});
});