8000 Adding private balance value into getBalance rpc result by levonpetrosyan93 · Pull Request #1045 · firoorg/firo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adding private balance value into getBalance rpc result #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 &a 496C mp;confirmed, size_t &unconfirmed) const;
CAmount GetUnconfirmedBalance() const;
CAmount GetImmatureBalance() const;
CAmount GetWatchOnlyBalance() const;
Expand Down
0