From 35af377af9a3b85ad3cf818e1da98f101dff80db Mon Sep 17 00:00:00 2001 From: furszy Date: Tue, 1 Sep 2020 16:53:03 -0300 Subject: [PATCH] re adding RPC getbalance missing filtering functionality. watch only, depth and includeDelegated arguments. --- src/wallet/rpcwallet.cpp | 13 +++++++++---- src/wallet/wallet.cpp | 15 +++++++++++---- src/wallet/wallet.h | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index a800abc0f43b..137dea3d3b12 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -1495,7 +1495,9 @@ UniValue getreceivedbylabel(const JSONRPCRequest& request) UniValue getbalance(const JSONRPCRequest& request) { - if (request.fHelp || (request.params.size() > 4 && IsDeprecatedRPCEnabled("accounts")) || (request.params.size() != 0 && !IsDeprecatedRPCEnabled("accounts"))) + if (request.fHelp || + (request.params.size() > 4 && IsDeprecatedRPCEnabled("accounts")) || + (request.params.size() > 3 && !IsDeprecatedRPCEnabled("accounts"))) throw std::runtime_error( "getbalance ( \"account\" minconf includeWatchonly includeDelegated )\n" "\nIf account is not specified, returns the server's total available balance (excluding zerocoins).\n" @@ -1544,9 +1546,12 @@ UniValue getbalance(const JSONRPCRequest& request) return ValueFromAmount(pwalletMain->GetLegacyBalance(filter, nMinDepth, account)); } - // TODO: re-incorporate the includeDelegated argument - // after 4.2 branch off for 5.0 - return ValueFromAmount(pwalletMain->GetAvailableBalance()); + const int paramsSize = request.params.size(); + const int nMinDepth = (paramsSize > 0 ? request.params[0].get_int() : 0); + isminefilter filter = ISMINE_SPENDABLE | (paramsSize > 1 && request.params[1].get_bool() ? ISMINE_WATCH_ONLY : ISMINE_NO); + filter |= (paramsSize <= 2 || request.params[2].get_bool() ? ISMINE_SPENDABLE_DELEGATED : ISMINE_NO); + + return ValueFromAmount(pwalletMain->GetAvailableBalance(filter, true, nMinDepth)); } UniValue getcoldstakingbalance(const JSONRPCRequest& request) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3f2beaf6ba85..5cd69b37a60b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1814,10 +1814,17 @@ CAmount CWallet::loopTxsBalance(std::function= minDepth) { + nTotal += pcoin.GetAvailableCredit(useCache, filter); } }); } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 11241292ff08..d34a3b3d44c9 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -517,6 +517,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface CAmount loopTxsBalance(std::functionmethod) const; CAmount GetAvailableBalance(bool fIncludeDelegated = true) const; + CAmount GetAvailableBalance(isminefilter& filter, bool useCache = false, int minDepth = 1) const; CAmount GetColdStakingBalance() const; // delegated coins for which we have the staking key CAmount GetImmatureColdStakingBalance() const; CAmount GetStakingBalance(const bool fIncludeColdStaking = true) const;