rpc, test: add "total" field to RPC getbalances

This commit is contained in:
Jon Atack 2024-11-22 07:59:28 -06:00
parent 19a4892871
commit 1b03a82cff
2 changed files with 7 additions and 3 deletions

View file

@ -447,6 +447,7 @@ RPCHelpMan getbalances()
{RPCResult::Type::STR_AMOUNT, "immature", "balance from immature coinbase outputs"},
}},
RESULT_LAST_PROCESSED_BLOCK,
{RPCResult::Type::STR_AMOUNT, "total", "total of all balances returned by this RPC"},
}
},
RPCExamples{
@ -466,6 +467,7 @@ RPCHelpMan getbalances()
const auto bal = GetBalance(wallet);
UniValue balances{UniValue::VOBJ};
const Balance full_bal{GetBalance(wallet, /*min_depth=*/0, /*avoid_reuse=*/false)};
{
UniValue balances_mine{UniValue::VOBJ};
balances_mine.pushKV("trusted", ValueFromAmount(bal.m_mine_trusted));
@ -474,7 +476,6 @@ RPCHelpMan getbalances()
if (wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
// If the AVOID_REUSE flag is set, bal has been set to just the un-reused address balance. Get
// the total balance, and then subtract bal to get the reused address balance.
const auto full_bal = GetBalance(wallet, 0, false);
balances_mine.pushKV("used", ValueFromAmount(full_bal.m_mine_trusted + full_bal.m_mine_untrusted_pending - bal.m_mine_trusted - bal.m_mine_untrusted_pending));
}
balances.pushKV("mine", std::move(balances_mine));
@ -489,6 +490,7 @@ RPCHelpMan getbalances()
}
AppendLastProcessedBlock(balances, wallet);
balances.pushKV("total", ValueFromAmount(full_bal.m_total));
return balances;
},
};

View file

@ -176,10 +176,12 @@ class WalletTest(BitcoinTestFramework):
'untrusted_pending': Decimal('60.0')},
'watchonly': {'immature': Decimal('5000'),
'trusted': Decimal('50.0'),
'untrusted_pending': Decimal('0E-8')}}
'untrusted_pending': Decimal('0E-8')},
'total': Decimal('69.99' if self.options.descriptors else '5119.99')}
expected_balances_1 = {'mine': {'immature': Decimal('0E-8'),
'trusted': Decimal('0E-8'), # node 1's send had an unsafe input
'untrusted_pending': Decimal('30.0') - fee_node_1}} # Doesn't include output of node 0's send since it was spent
'untrusted_pending': Decimal('30.0') - fee_node_1}, # Doesn't include output of node 0's send since it was spent
'total': Decimal('30.0') - fee_node_1}
if self.options.descriptors:
del expected_balances_0["watchonly"]
balances_0 = self.nodes[0].getbalances()