Skip to content

Commit 745d2e3

Browse files
committed
Clean up getbalance RPC parameter handling
Only change in behavior is that unsupported combinations of parameters now trigger more specific error messages instead of the vague "JSON value is not a string as expected" error.
1 parent fd5d71e commit 745d2e3

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,18 +768,31 @@ UniValue getbalance(const JSONRPCRequest& request)
768768

769769
LOCK2(cs_main, pwallet->cs_wallet);
770770

771-
if (request.params[0].isNull() && request.params[1].isNull() && request.params[2].isNull())
772-
return ValueFromAmount(pwallet->GetBalance());
771+
const UniValue& account_value = request.params[0];
772+
const UniValue& minconf = request.params[1];
773+
const UniValue& include_watchonly = request.params[2];
774+
775+
if (account_value.isNull()) {
776+
if (!minconf.isNull()) {
777+
throw JSONRPCError(RPC_INVALID_PARAMETER,
778+
"getbalance minconf option is only currently supported if an account is specified");
779+
}
780+
if (!include_watchonly.isNull()) {
781+
throw JSONRPCError(RPC_INVALID_PARAMETER,
782+
"getbalance include_watchonly option is only currently supported if an account is specified");
783+
}
784+
return ValueFromAmount(pwallet->GetBalance());
785+
}
773786

774-
const std::string& account_param = request.params[0].get_str();
787+
const std::string& account_param = account_value.get_str();
775788
const std::string* account = account_param != "*" ? &account_param : nullptr;
776789

777790
int nMinDepth = 1;
778-
if (!request.params[1].isNull())
779-
nMinDepth = request.params[1].get_int();
791+
if (!minconf.isNull())
792+
nMinDepth = minconf.get_int();
780793
isminefilter filter = ISMINE_SPENDABLE;
781-
if(!request.params[2].isNull())
782-
if(request.params[2].get_bool())
794+
if(!include_watchonly.isNull())
795+
if(include_watchonly.get_bool())
783796
filter = filter | ISMINE_WATCH_ONLY;
784797

785798
return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));

0 commit comments

Comments
 (0)