From 1b03a82cffff9bed2557279f27862b2a010d6bd5 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Fri, 22 Nov 2024 07:59:28 -0600 Subject: [PATCH] rpc, test: add "total" field to RPC getbalances --- src/wallet/rpc/coins.cpp | 4 +++- test/functional/wallet_balance.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index f1430a3c60..91f5b8b3ee 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -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; }, }; diff --git a/test/functional/wallet_balance.py b/test/functional/wallet_balance.py index 9da53402a4..27f36b731c 100755 --- a/test/functional/wallet_balance.py +++ b/test/functional/wallet_balance.py @@ -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()