Skip to content

Commit

Permalink
cli: add -getinfo multiwallet total balance
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#19092
Rebased-From: f47067c
  • Loading branch information
jonatack authored and luke-jr committed Dec 4, 2020
1 parent 403507d commit cd87fec
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/bitcoin-cli.cpp
Expand Up @@ -719,7 +719,8 @@ static UniValue ValueFromAmount(const CAmount& amount)

/**
* GetWalletBalances calls listwallets; if more than one wallet is loaded, it then
* fetches mine.trusted balances for each loaded wallet and pushes them to `result`.
* fetches mine.trusted balances for each loaded wallet and pushes them to `result`,
* preceded by the total balance.
*
* @param result Reference to UniValue object the wallet names and balances are pushed to.
*/
Expand All @@ -732,13 +733,17 @@ static void GetWalletBalances(UniValue& result)
if (wallets.size() <= 1) return;

UniValue balances(UniValue::VOBJ);
CAmount total_balance{0};
for (const UniValue& wallet : wallets.getValues()) {
const std::string wallet_name = wallet.get_str();
const UniValue getbalances = ConnectAndCallRPC(&rh, "getbalances", /* args=*/{}, wallet_name);
if (!find_value(getbalances, "error").isNull()) continue;
const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
total_balance += AmountFromValue(balance);
balances.pushKV(wallet_name, balance);
}
result.pushKV("balances", balances);
result.pushKV("total_balance", ValueFromAmount(total_balance));
}

/**
Expand Down

0 comments on commit cd87fec

Please sign in to comment.