Skip to content

Commit

Permalink
Merged in getextendedbalance (pull request #12)
Browse files Browse the repository at this point in the history
[RPC] Re-add getextendedbalance RPC call

Approved-by: Cevap
  • Loading branch information
FornaxA authored and Cevap committed Feb 9, 2020
2 parents 3b82c33 + 3482d56 commit da47513
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,33 @@ UniValue getunconfirmedbalance(const JSONRPCRequest &request)
return ValueFromAmount(pwallet->GetUnconfirmedBalance());
}

UniValue getextendedbalance(const JSONRPCRequest &request)
{
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
return NullUniValue;
}

if (request.fHelp || request.params.size() > 0)
throw std::runtime_error(
"getextendedbalance\n"
"Returns extended balance information\n");

LOCK2(cs_main, pwallet->cs_wallet);

UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("blocks", (int)chainActive.Height()));
obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
obj.push_back(Pair("balance_locked", ValueFromAmount(pwallet->GetLockedBalance())));
obj.push_back(Pair("balance_unlocked", ValueFromAmount(pwallet->GetUnlockedBalance())));
obj.push_back(Pair("balance_unconfirmed", ValueFromAmount(pwallet->GetUnconfirmedBalance())));
obj.push_back(Pair("balance_immature", ValueFromAmount(pwallet->GetImmatureBalance())));
obj.push_back(Pair("watchonly_balance", ValueFromAmount(pwallet->GetWatchOnlyBalance())));
obj.push_back(Pair("watchonly_balance_unconfirmed", ValueFromAmount(pwallet->GetUnconfirmedWatchOnlyBalance())));
obj.push_back(Pair("watchonly_balance_immature", ValueFromAmount(pwallet->GetImmatureWatchOnlyBalance())));
obj.push_back(Pair("watchonly_balance_locked", ValueFromAmount(pwallet->GetLockedWatchOnlyBalance())));
return obj;
}

UniValue movecmd(const JSONRPCRequest& request)
{
Expand Down Expand Up @@ -3136,6 +3163,7 @@ static const CRPCCommand commands[] =
{ "wallet", "getaccount", &getaccount, true, {"address"} },
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, {"account"} },
{ "wallet", "getbalance", &getbalance, false, {"account","minconf","addlocked","include_watchonly"} },
{ "wallet", "getextendedbalance", &getextendedbalance, false, {} },
{ "wallet", "getnewaddress", &getnewaddress, true, {"account"} },
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, true, {} },
{ "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false, {"account","minconf","addlocked"} },
Expand Down
101 changes: 101 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,68 @@ CAmount CWalletTx::GetDenominatedCredit(bool unconfirmed, bool fUseCache) const
return nCredit;
}

CAmount CWalletTx::GetUnlockedCredit(bool fUseCache, const isminefilter& filter) const
{
if (pwallet == 0)
return 0;

// Must wait until coinbase is safely deep enough in the chain before valuing it
if (IsCoinBase() && GetBlocksToMaturity() > 0)
return 0;

CAmount nCredit = 0;
uint256 hashTx = GetHash();
for (unsigned int i = 0; i < tx->vout.size(); i++) {
const CTxOut& txout = tx->vout[i];

if (pwallet->IsSpent(hashTx, i) || pwallet->IsLockedCoin(hashTx, i)) continue;
if (fMasternodeMode && tx->vout[i].nValue == MASTERNODE_COLLATERAL_AMOUNT) continue; // do not count MN-like outputs

nCredit += pwallet->GetCredit(txout, filter);
if (!MoneyRange(nCredit))
throw std::runtime_error("CWalletTx::GetUnlockedCredit() : value out of range");
}

return nCredit;
}

CAmount CWalletTx::GetLockedCredit(bool fUseCache, const isminefilter& filter) const
{
if (pwallet == 0)
return 0;

// Must wait until coinbase is safely deep enough in the chain before valuing it
if (IsCoinBase() && GetBlocksToMaturity() > 0)
return 0;

CAmount nCredit = 0;
uint256 hashTx = GetHash();
for (unsigned int i = 0; i < tx->vout.size(); i++) {
const CTxOut& txout = tx->vout[i];

// Skip spent coins
if (pwallet->IsSpent(hashTx, i)) continue;

if (pwallet->IsLockedCoin(hashTx, i)) {
// Add locked coins
nCredit += pwallet->GetCredit(txout, filter);
} else if (fMasternodeMode && tx->vout[i].nValue == MASTERNODE_COLLATERAL_AMOUNT) {
// Add masternode collaterals which are handled like locked coins
nCredit += pwallet->GetCredit(txout, filter);
}

if (!MoneyRange(nCredit))
throw std::runtime_error("CWalletTx::GetUnlockedCredit() : value out of range");
}

return nCredit;
}

CAmount CWalletTx::GetLockedWatchOnlyCredit(const bool& fUseCache) const
{
return GetLockedCredit(fUseCache, ISMINE_WATCH_ONLY);
}

CAmount CWalletTx::GetChange() const
{
if (fChangeCached)
Expand Down Expand Up @@ -2772,6 +2834,45 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const
return nTotal;
}

CAmount CWallet::GetUnlockedBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() > 0)
nTotal += pcoin->GetUnlockedCredit();
}
}
return nTotal;
}

CAmount CWallet::GetLockedBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() > 0)
nTotal += pcoin->GetLockedCredit();
}
}
return nTotal;
}

CAmount CWallet::GetLockedWatchOnlyBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted() && pcoin->GetDepthInMainChain() > 0)
nTotal += pcoin->GetLockedWatchOnlyCredit();
}
}
return nTotal;
}

// Calculate total balance in a different way from GetBalance. The biggest
// difference is that GetBalance sums up all unspent TxOuts paying to the
// wallet, while this sums up both spent and unspent TxOuts paying to the
Expand Down
11 changes: 11 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static const int64_t TIMESTAMP_MIN = 0;
//! if set, all keys will be derived by using BIP39/BIP44
static const bool DEFAULT_USE_HD_WALLET = false;

//! Masternode coin amount
static const CAmount MASTERNODE_COLLATERAL_AMOUNT = 20000 * COIN;

bool AutoBackupWallet (CWallet* wallet, const std::string& strWalletFile_, std::string& strBackupWarningRet, std::string& strBackupErrorRet);

class CBlockIndex;
Expand Down Expand Up @@ -511,6 +514,10 @@ class CWalletTx : public CMerkleTx
CAmount GetAnonymizedCredit(bool fUseCache=true) const;
CAmount GetDenominatedCredit(bool unconfirmed, bool fUseCache=true) const;

CAmount GetUnlockedCredit(bool fUseCache=true, const isminefilter& filter = ISMINE_SPENDABLE) const;
CAmount GetLockedCredit(bool fUseCache=true, const isminefilter& filter = ISMINE_SPENDABLE) const;
CAmount GetLockedWatchOnlyCredit(const bool& fUseCache=true) const;

void GetAmounts(std::list<COutputEntry>& listReceived,
std::list<COutputEntry>& listSent, CAmount& nFee, std::string& strSentAccount, const isminefilter& filter) const;

Expand Down Expand Up @@ -1082,6 +1089,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
CAmount GetImmatureWatchOnlyBalance() const;
CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const std::string* account, const bool fAddLocked) const;

CAmount GetUnlockedBalance() const;
CAmount GetLockedBalance() const;
CAmount GetLockedWatchOnlyBalance() const;

CAmount GetAnonymizableBalance(bool fSkipDenominated = false, bool fSkipUnconfirmed = true) const;
CAmount GetAnonymizedBalance() const;
float GetAverageAnonymizedRounds() const;
Expand Down

0 comments on commit da47513

Please sign in to comment.