Skip to content

Commit

Permalink
Adding private balance value into getBalance rpc result (#1045)
Browse files Browse the repository at this point in the history
* Adding getprivatebalance and gettotalbalance rpc calls
  • Loading branch information
levonpetrosyan93 authored Jul 5, 2021
1 parent 0243914 commit e3f929c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 55 deletions.
57 changes: 2 additions & 55 deletions src/qt/lelantusmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,12 @@ AutoMintModel* LelantusModel::getAutoMintModel()
std::pair<CAmount, CAmount> LelantusModel::getPrivateBalance()
{
size_t confirmed, unconfirmed;
return getPrivateBalance(confirmed, unconfirmed);
return pwalletMain->GetPrivateBalance(confirmed, unconfirmed);
}

std::pair<CAmount, CAmount> LelantusModel::getPrivateBalance(size_t &confirmed, size_t &unconfirmed)
{
std::pair<CAmount, CAmount> balance = {0, 0};

confirmed = 0;
unconfirmed = 0;

auto zwallet = pwalletMain->zwallet.get();

if(!zwallet)
return balance;

auto coins = zwallet->GetTracker().ListLelantusMints(true, false, false);
for (auto const &c : coins) {

if (c.isUsed || c.isArchived || !c.isSeedCorrect) {
continue;
}

auto conf = c.nHeight > 0
? chainActive.Height() - c.nHeight + 1 : 0;

if (conf >= ZC_MINT_CONFIRMATIONS) {
confirmed++;
balance.first += c.amount;
} else {
unconfirmed++;
balance.second += c.amount;
}
}

auto sigmaCoins = zwallet->GetTracker().ListMints(true, false, false);
for (auto const &c : sigmaCoins) {

if (c.isUsed || c.isArchived || !c.isSeedCorrect) {
continue;
}

CAmount amount;
if (!sigma::DenominationToInteger(c.denom, amount)) {
throw std::runtime_error("Fail to get denomination value");
}

auto conf = c.nHeight > 0
? chainActive.Height() - c.nHeight + 1 : 0;

if (conf >= ZC_MINT_CONFIRMATIONS) {
confirmed++;
balance.first += amount;
} else {
unconfirmed++;
balance.second += amount;
}
}

return balance;
return pwalletMain->GetPrivateBalance(confirmed, unconfirmed);
}

bool LelantusModel::unlockWallet(SecureString const &passphase, size_t msecs)
Expand Down
57 changes: 57 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,61 @@ UniValue getbalance(const JSONRPCRequest& request)
return ValueFromAmount(pwallet->GetLegacyBalance(filter, nMinDepth, account));
}

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

EnsureLelantusWalletIsAvailable();

if (request.fHelp || request.params.size() != 0)
throw runtime_error(
"getprivatebalance\n"
"\nReturns private balance.\n"
"Private balance is the sum of all confirmed sigma/lelantus mints which are created by the wallet.\n"
"\nResult:\n"
"amount (numeric) The confirmed private balance in " + CURRENCY_UNIT + ".\n"
"\nExamples:\n"
"\nThe total amount in the wallet\n"
+ HelpExampleCli("getprivatebalance", "")
+ HelpExampleRpc("getprivatebalance", "")
);

LOCK2(cs_main, pwallet->cs_wallet);

return ValueFromAmount(pwallet->GetPrivateBalance().first);
}

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

EnsureLelantusWalletIsAvailable();

if (request.fHelp || request.params.size() != 0)
throw runtime_error(
"gettotalbalance\n"
"\nReturns total (transparent + private) balance.\n"
"Transparent balance is the sum of coin amounts received as utxo.\n"
"Private balance is the sum of all confirmed sigma/lelantus mints which are created by the wallet.\n"
"\nResult:\n"
"amount (numeric) The total balance in " + CURRENCY_UNIT + " for the wallet.\n"
"\nExamples:\n"
"\nThe total amount in the wallet\n"
+ HelpExampleCli("gettotalbalance", "")
+ HelpExampleRpc("gettotalbalance", "")
);

LOCK2(cs_main, pwallet->cs_wallet);

return ValueFromAmount(pwallet->GetBalance() + pwallet->GetPrivateBalance().first);
}

UniValue getunconfirmedbalance(const JSONRPCRequest &request)
{
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
Expand Down Expand Up @@ -4746,6 +4801,8 @@ static const CRPCCommand commands[] =
{ "wallet", "getaccount", &getaccount, true, {"address"} },
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true, {"account"} },
{ "wallet", "getbalance", &getbalance, false, {"account","minconf","include_watchonly"} },
{ "wallet", "getprivatebalance", &getprivatebalance, false, {} },
{ "wallet", "gettotalbalance", &gettotalbalance, false, {} },
{ "wallet", "getnewaddress", &getnewaddress, true, {"account"} },
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, true, {} },
{ "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false, {"account","minconf"} },
Expand Down
64 changes: 64 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,70 @@ CAmount CWallet::GetBalance(bool fExcludeLocked) const
return nTotal;
}

std::pair<CAmount, CAmount> CWallet::GetPrivateBalance() const
{
size_t confirmed, unconfirmed;
return GetPrivateBalance(confirmed, unconfirmed);
}

std::pair<CAmount, CAmount> CWallet::GetPrivateBalance(size_t &confirmed, size_t &unconfirmed) const
{
std::pair<CAmount, CAmount> balance = {0, 0};

confirmed = 0;
unconfirmed = 0;

auto zwallet = pwalletMain->zwallet.get();

if(!zwallet)
return balance;

auto lelantusCoins = zwallet->GetTracker().ListLelantusMints(true, false, false);
for (auto const &c : lelantusCoins) {

if (c.isUsed || c.isArchived || !c.isSeedCorrect) {
continue;
}

auto conf = c.nHeight > 0
? chainActive.Height() - c.nHeight + 1 : 0;

if (conf >= ZC_MINT_CONFIRMATIONS) {
confirmed++;
balance.first += c.amount;
} else {
unconfirmed++;
balance.second += c.amount;
}
}

auto sigmaCoins = zwallet->GetTracker().ListMints(true, false, false);
for (auto const &c : sigmaCoins) {

if (c.isUsed || c.isArchived || !c.isSeedCorrect) {
continue;
}

CAmount amount;
if (!sigma::DenominationToInteger(c.denom, amount)) {
throw std::runtime_error("Fail to get denomination value");
}

auto conf = c.nHeight > 0
? chainActive.Height() - c.nHeight + 1 : 0;

if (conf >= ZC_MINT_CONFIRMATIONS) {
confirmed++;
balance.first += amount;
} else {
unconfirmed++;
balance.second += amount;
}
}

return balance;
}

std::vector<CRecipient> CWallet::CreateSigmaMintRecipients(
std::vector<sigma::PrivateCoin>& coins,
vector<CHDMint>& vDMints)
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override;
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman);
CAmount GetBalance(bool fExcludeLocked = false) const;
std::pair<CAmount, CAmount> GetPrivateBalance() const;
std::pair<CAmount, CAmount> GetPrivateBalance(size_t &confirmed, size_t &unconfirmed) const;
CAmount GetUnconfirmedBalance() const;
CAmount GetImmatureBalance() const;
CAmount GetWatchOnlyBalance() const;
Expand Down

0 comments on commit e3f929c

Please sign in to comment.