Skip to content

Commit b20bc52

Browse files
committed
[RPC] define getdelegatedbalance
1 parent d81ecdc commit b20bc52

File tree

3 files changed

+61
-67
lines changed

3 files changed

+61
-67
lines changed

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ static const CRPCCommand vRPCCommands[] =
410410
{"wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, false, true},
411411
{"wallet", "getbalance", &getbalance, false, false, true},
412412
{"wallet", "getcoldstakingbalance", &getcoldstakingbalance, false, false, true},
413+
{"wallet", "getdelegatedbalance", &getdelegatedbalance, false, false, true},
413414
{"wallet", "getnewaddress", &getnewaddress, true, false, true},
414415
{"wallet", "getnewstakingaddress", &getnewstakingaddress, true, false, true},
415416
{"wallet", "getrawchangeaddress", &getrawchangeaddress, true, false, true},

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ extern UniValue getreceivedbyaddress(const UniValue& params, bool fHelp);
230230
extern UniValue getreceivedbyaccount(const UniValue& params, bool fHelp);
231231
extern UniValue getbalance(const UniValue& params, bool fHelp);
232232
extern UniValue getcoldstakingbalance(const UniValue& params, bool fHelp);
233+
extern UniValue getdelegatedbalance(const UniValue& params, bool fHelp);
233234
extern UniValue getunconfirmedbalance(const UniValue& params, bool fHelp);
234235
extern UniValue movecmd(const UniValue& params, bool fHelp);
235236
extern UniValue sendfrom(const UniValue& params, bool fHelp);

src/wallet/rpcwallet.cpp

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -963,16 +963,36 @@ CAmount GetAccountBalance(CWalletDB& walletdb, const std::string& strAccount, in
963963
if (!IsFinalTx(wtx) || wtx.GetBlocksToMaturity() > 0 || depth < 0 || fConflicted)
964964
continue;
965965

966-
CAmount nReceived, nSent, nFee;
967-
wtx.GetAccountAmounts(strAccount, nReceived, nSent, nFee, filter);
966+
if (strAccount == "*") {
967+
// Calculate total balance a different way from GetBalance()
968+
// (GetBalance() sums up all unspent TxOuts)
969+
CAmount allFee;
970+
std::string strSentAccount;
971+
std::list<COutputEntry> listReceived;
972+
std::list<COutputEntry> listSent;
973+
wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount, filter);
974+
if (wtx.GetDepthInMainChain() >= nMinDepth) {
975+
for (const COutputEntry& r : listReceived)
976+
nBalance += r.amount;
977+
}
978+
for (const COutputEntry& s : listSent)
979+
nBalance -= s.amount;
980+
nBalance -= allFee;
981+
982+
} else {
983+
984+
CAmount nReceived, nSent, nFee;
985+
wtx.GetAccountAmounts(strAccount, nReceived, nSent, nFee, filter);
968986

969-
if (nReceived != 0 && depth >= nMinDepth)
970-
nBalance += nReceived;
971-
nBalance -= nSent + nFee;
987+
if (nReceived != 0 && depth >= nMinDepth)
988+
nBalance += nReceived;
989+
nBalance -= nSent + nFee;
990+
}
972991
}
973992

974993
// Tally internal accounting entries
975-
nBalance += walletdb.GetAccountCreditDebit(strAccount);
994+
if (strAccount != "*")
995+
nBalance += walletdb.GetAccountCreditDebit(strAccount);
976996

977997
return nBalance;
978998
}
@@ -1029,40 +1049,8 @@ UniValue getbalance(const UniValue& params, bool fHelp)
10291049
if ( !(params.size() > 3) || params[3].get_bool() )
10301050
filter = filter | ISMINE_SPENDABLE_DELEGATED;
10311051

1032-
if (params[0].get_str() == "*") {
1033-
// Calculate total balance a different way from GetBalance()
1034-
// (GetBalance() sums up all unspent TxOuts)
1035-
// getbalance and "getbalance * 1 true" should return the same number
1036-
CAmount nBalance = 0;
1037-
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
1038-
const CWalletTx& wtx = (*it).second;
1039-
bool fConflicted;
1040-
int depth = wtx.GetDepthAndMempool(fConflicted);
1041-
1042-
if (!IsFinalTx(wtx) || wtx.GetBlocksToMaturity() > 0 || depth < 0 || fConflicted)
1043-
continue;
1044-
1045-
CAmount allFee;
1046-
std::string strSentAccount;
1047-
std::list<COutputEntry> listReceived;
1048-
std::list<COutputEntry> listSent;
1049-
wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount, filter);
1050-
if (depth >= nMinDepth) {
1051-
for (const COutputEntry& r : listReceived)
1052-
nBalance += r.amount;
1053-
}
1054-
for (const COutputEntry& s : listSent)
1055-
nBalance -= s.amount;
1056-
nBalance -= allFee;
1057-
}
1058-
return ValueFromAmount(nBalance);
1059-
}
1060-
1061-
std::string strAccount = AccountFromValue(params[0]);
1062-
1063-
CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, filter);
1064-
1065-
return ValueFromAmount(nBalance);
1052+
std::string strAccount = params[0].get_str();
1053+
return ValueFromAmount(GetAccountBalance(strAccount, nMinDepth, filter));
10661054
}
10671055

10681056
UniValue getcoldstakingbalance(const UniValue& params, bool fHelp)
@@ -1094,38 +1082,42 @@ UniValue getcoldstakingbalance(const UniValue& params, bool fHelp)
10941082
if (params.size() == 0)
10951083
return ValueFromAmount(pwalletMain->GetColdStakingBalance());
10961084

1097-
const int nMinDepth = 1;
1085+
std::string strAccount = params[0].get_str();
1086+
return ValueFromAmount(GetAccountBalance(strAccount, /*nMinDepth*/ 1, ISMINE_COLD));
1087+
}
10981088

1099-
if (params[0].get_str() == "*") {
1100-
// Calculate total balance a different way from GetBalance()
1101-
// (GetBalance() sums up all unspent TxOuts)
1102-
CAmount nBalance = 0;
1103-
for (std::map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) {
1104-
const CWalletTx& wtx = (*it).second;
1105-
if (!IsFinalTx(wtx) || wtx.GetBlocksToMaturity() > 0 || wtx.GetDepthInMainChain() < 0)
1106-
continue;
1089+
UniValue getdelegatedbalance(const UniValue& params, bool fHelp)
1090+
{
1091+
if (fHelp || params.size() > 1)
1092+
throw std::runtime_error(
1093+
"getdelegatedbalance ( \"account\" )\n"
1094+
"\nIf account is not specified, returns the server's total available delegated balance (sum of all utxos delegated\n"
1095+
"to a cold staking address to stake on behalf of addresses of this wallet).\n"
1096+
"If account is specified, returns the cold balance in the account.\n"
1097+
"Note that the account \"\" is not the same as leaving the parameter out.\n"
1098+
"The server total may be different to the balance in the default \"\" account.\n"
11071099

1108-
CAmount allFee;
1109-
std::string strSentAccount;
1110-
std::list<COutputEntry> listReceived;
1111-
std::list<COutputEntry> listSent;
1112-
wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount, ISMINE_COLD);
1113-
if (wtx.GetDepthInMainChain() >= nMinDepth) {
1114-
for (const COutputEntry& r : listReceived)
1115-
nBalance += r.amount;
1116-
}
1117-
for (const COutputEntry& s : listSent)
1118-
nBalance -= s.amount;
1119-
nBalance -= allFee;
1120-
}
1121-
return ValueFromAmount(nBalance);
1122-
}
1100+
"\nArguments:\n"
1101+
"1. \"account\" (string, optional) The selected account, or \"*\" for entire wallet. It may be the default account using \"\".\n"
11231102

1124-
std::string strAccount = AccountFromValue(params[0]);
1103+
"\nResult:\n"
1104+
"amount (numeric) The total amount in PIV received for this account in P2CS contracts.\n"
11251105

1126-
CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, ISMINE_COLD);
1106+
"\nExamples:\n"
1107+
"\nThe total amount in the server across all accounts\n" +
1108+
HelpExampleCli("getdelegatedbalance", "") +
1109+
"\nThe total amount in the account named tabby\n" +
1110+
HelpExampleCli("getdelegatedbalance", "\"tabby\"") +
1111+
"\nAs a json rpc call\n" +
1112+
HelpExampleRpc("getdelegatedbalance", "\"tabby\""));
1113+
1114+
LOCK2(cs_main, pwalletMain->cs_wallet);
1115+
1116+
if (params.size() == 0)
1117+
return ValueFromAmount(pwalletMain->GetDelegatedBalance());
11271118

1128-
return ValueFromAmount(nBalance);
1119+
std::string strAccount = params[0].get_str();
1120+
return ValueFromAmount(GetAccountBalance(strAccount, /*nMinDepth*/ 1, ISMINE_SPENDABLE_DELEGATED));
11291121
}
11301122

11311123
UniValue getunconfirmedbalance(const UniValue &params, bool fHelp)

0 commit comments

Comments
 (0)