Skip to content

Commit 702ae1e

Browse files
committed
[RPC] [wallet] allow getbalance to use min_conf and watch_only without accounts.
1 parent cf15761 commit 702ae1e

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,9 @@ static UniValue getbalance(const JSONRPCRequest& request)
853853
return NullUniValue;
854854
}
855855

856-
if (request.fHelp || (request.params.size() > 3 && IsDeprecatedRPCEnabled("accounts")) || (request.params.size() != 0 && !IsDeprecatedRPCEnabled("accounts")))
856+
if (request.fHelp || (request.params.size() > 3 ))
857857
throw std::runtime_error(
858+
(IsDeprecatedRPCEnabled("accounts") ? std::string(
858859
"getbalance ( \"account\" minconf include_watchonly )\n"
859860
"\nIf account is not specified, returns the server's total available balance.\n"
860861
"The available balance is what the wallet considers currently spendable, and is\n"
@@ -876,8 +877,17 @@ static UniValue getbalance(const JSONRPCRequest& request)
876877
" balances. In general, account balance calculation is not considered\n"
877878
" reliable and has resulted in confusing outcomes, so it is recommended to\n"
878879
" avoid passing this argument.\n"
879-
"2. minconf (numeric, optional, default=1) DEPRECATED. Only valid when an account is specified. This argument will be removed in V0.18. To use this deprecated argument, start bitcoind with -deprecatedrpc=accounts. Only include transactions confirmed at least this many times.\n"
880-
"3. include_watchonly (bool, optional, default=false) DEPRECATED. Only valid when an account is specified. This argument will be removed in V0.18. To use this deprecated argument, start bitcoind with -deprecatedrpc=accounts. Also include balance in watch-only addresses (see 'importaddress')\n"
880+
"2. minconf (numeric, optional) Only include transactions confirmed at least this many times. \n"
881+
" The default is 1 if an account is provided or 0 if no account is provided\n")
882+
: std::string(
883+
"getbalance ( \"(dummy)\" minconf include_watchonly )\n"
884+
"\nReturns the total available balance.\n"
885+
"The available balance is what the wallet considers currently spendable, and is\n"
886+
"thus affected by options which limit spendability such as -spendzeroconfchange.\n"
887+
"\nArguments:\n"
888+
"1. (dummy) (string, optional) Remains for backward compatibility. Must be excluded or set to \"*\".\n"
889+
"2. minconf (numeric, optional, default=0) Only include transactions confirmed at least this many times.\n")) +
890+
"3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')\n"
881891
"\nResult:\n"
882892
"amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this account.\n"
883893
"\nExamples:\n"
@@ -895,38 +905,35 @@ static UniValue getbalance(const JSONRPCRequest& request)
895905

896906
LOCK2(cs_main, pwallet->cs_wallet);
897907

898-
if (IsDeprecatedRPCEnabled("accounts")) {
899-
const UniValue& account_value = request.params[0];
900-
const UniValue& minconf = request.params[1];
901-
const UniValue& include_watchonly = request.params[2];
908+
const UniValue& account_value = request.params[0];
902909

903-
if (account_value.isNull()) {
904-
if (!minconf.isNull()) {
905-
throw JSONRPCError(RPC_INVALID_PARAMETER,
906-
"getbalance minconf option is only currently supported if an account is specified");
907-
}
908-
if (!include_watchonly.isNull()) {
909-
throw JSONRPCError(RPC_INVALID_PARAMETER,
910-
"getbalance include_watchonly option is only currently supported if an account is specified");
911-
}
912-
return ValueFromAmount(pwallet->GetBalance());
913-
}
910+
int min_depth = 0;
911+
if (IsDeprecatedRPCEnabled("accounts") && !account_value.isNull()) {
912+
// Default min_depth to 1 when an account is provided.
913+
min_depth = 1;
914+
}
915+
if (!request.params[1].isNull()) {
916+
min_depth = request.params[1].get_int();
917+
}
918+
919+
isminefilter filter = ISMINE_SPENDABLE;
920+
if (!request.params[2].isNull() && request.params[2].get_bool()) {
921+
filter = filter | ISMINE_WATCH_ONLY;
922+
}
923+
924+
if (!account_value.isNull()) {
914925

915926
const std::string& account_param = account_value.get_str();
916927
const std::string* account = account_param != "*" ? &account_param : nullptr;
917928

918-
int nMinDepth = 1;
919-
if (!minconf.isNull())
920-
nMinDepth = minconf.get_int();
921-
isminefilter filter = ISMINE_SPENDABLE;
922-
if(!include_watchonly.isNull())
923-
if(include_watchonly.get_bool())
924-
filter = filter | ISMINE_WATCH_ONLY;
925-
926-
return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
929+
if (!IsDeprecatedRPCEnabled("accounts") && account_param != "*") {
930+
throw JSONRPCError(RPC_METHOD_DEPRECATED, "dummy first argument must be excluded or set to \"*\".");
931+
} else if (IsDeprecatedRPCEnabled("accounts")) {
932+
return ValueFromAmount(pwallet->GetLegacyBalance(filter, min_depth, account));
933+
}
927934
}
928935

929-
return ValueFromAmount(pwallet->GetBalance());
936+
return ValueFromAmount(pwallet->GetBalance(filter, min_depth));
930937
}
931938

932939
static UniValue getunconfirmedbalance(const JSONRPCRequest &request)
@@ -4416,7 +4423,7 @@ static const CRPCCommand commands[] =
44164423
{ "wallet", "dumpwallet", &dumpwallet, {"filename"} },
44174424
{ "wallet", "encryptwallet", &encryptwallet, {"passphrase"} },
44184425
{ "wallet", "getaddressinfo", &getaddressinfo, {"address"} },
4419-
{ "wallet", "getbalance", &getbalance, {"account","minconf","include_watchonly"} },
4426+
{ "wallet", "getbalance", &getbalance, {"account|dummy","minconf","include_watchonly"} },
44204427
{ "wallet", "getnewaddress", &getnewaddress, {"label|account","address_type"} },
44214428
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, {"address_type"} },
44224429
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, {"address","minconf"} },

test/functional/wallet_basic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def run_test(self):
6464
assert_equal(self.nodes[1].getbalance(), 50)
6565
assert_equal(self.nodes[2].getbalance(), 0)
6666

67+
# Check getbalance with different arguments
68+
assert_equal(self.nodes[0].getbalance("*"), 50)
69+
assert_equal(self.nodes[0].getbalance("*", 1), 50)
70+
assert_equal(self.nodes[0].getbalance("*", 1, True), 50)
71+
assert_equal(self.nodes[0].getbalance(minconf=1), 50)
72+
73+
# first argument of getbalance must be excluded or set to "*"
74+
assert_raises_rpc_error(-32, "dummy first argument must be excluded or set to \"*\"", self.nodes[0].getbalance, "")
75+
6776
# Check that only first and second nodes have UTXOs
6877
utxos = self.nodes[0].listunspent()
6978
assert_equal(len(utxos), 1)

0 commit comments

Comments
 (0)