Skip to content

Commit 9f01849

Browse files
committed
cli: create GetWalletBalances() to fetch multiwallet balances
1 parent 7430775 commit 9f01849

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/bitcoin-cli.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,30 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
452452
return response;
453453
}
454454

455+
/**
456+
* GetWalletBalances calls listwallets; if more than one wallet is loaded, it then
457+
* fetches mine.trusted balances for each loaded wallet and pushes them to `result`.
458+
*
459+
* @param result Reference to UniValue object the wallet names and balances are pushed to.
460+
*/
461+
static void GetWalletBalances(UniValue& result)
462+
{
463+
std::unique_ptr<BaseRequestHandler> rh{MakeUnique<DefaultRequestHandler>()};
464+
const UniValue listwallets = ConnectAndCallRPC(rh.get(), "listwallets", /* args=*/{});
465+
if (!find_value(listwallets, "error").isNull()) return;
466+
const UniValue& wallets = find_value(listwallets, "result");
467+
if (wallets.size() <= 1) return;
468+
469+
UniValue balances(UniValue::VOBJ);
470+
for (const UniValue& wallet : wallets.getValues()) {
471+
const std::string wallet_name = wallet.get_str();
472+
const UniValue getbalances = ConnectAndCallRPC(rh.get(), "getbalances", /* args=*/{}, wallet_name);
473+
const UniValue& balance = find_value(getbalances, "result")["mine"]["trusted"];
474+
balances.pushKV(wallet_name, balance);
475+
}
476+
result.pushKV("balances", balances);
477+
}
478+
455479
static int CommandLineRPC(int argc, char *argv[])
456480
{
457481
std::string strPrint;

0 commit comments

Comments
 (0)