Skip to content

Commit

Permalink
wallet: (Refactor) Compute used balance from GetBalance() Function
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonOdiwuor committed May 11, 2024
1 parent ba7c67f commit 1cc2a81
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/wallet/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,20 @@ Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse)
const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
const CAmount tx_credit_mine{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_SPENDABLE | reuse_filter)};
const CAmount tx_credit_watchonly{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_WATCH_ONLY | reuse_filter)};
const CAmount tx_credit_mine_used{CachedTxGetAvailableCredit(wallet, wtx, ISMINE_SPENDABLE | ISMINE_USED)};
if (is_trusted) { // min_depth == 0 when calculating used balance and tx_depth >= 0
ret.m_mine_used += tx_credit_mine_used;
}
if (is_trusted && tx_depth >= min_depth) {
ret.m_mine_trusted += tx_credit_mine;
ret.m_watchonly_trusted += tx_credit_watchonly;
ret.m_mine_used -= tx_credit_mine; // remove overlap from used_balance
}
if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
ret.m_mine_untrusted_pending += tx_credit_mine;
ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
ret.m_mine_used += tx_credit_mine_used;
ret.m_mine_used -= tx_credit_mine; // remove overlap from used_balance
}
ret.m_mine_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_SPENDABLE);
ret.m_watchonly_immature += CachedTxGetImmatureCredit(wallet, wtx, ISMINE_WATCH_ONLY);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/receive.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct Balance {
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
CAmount m_mine_immature{0}; //!< Immature coinbases in the main chain
CAmount m_mine_used{0}; //!< Used balance
CAmount m_watchonly_trusted{0};
CAmount m_watchonly_untrusted_pending{0};
CAmount m_watchonly_immature{0};
Expand Down
5 changes: 1 addition & 4 deletions src/wallet/rpc/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ RPCHelpMan getbalances()
balances_mine.pushKV("untrusted_pending", ValueFromAmount(bal.m_mine_untrusted_pending));
balances_mine.pushKV("immature", ValueFromAmount(bal.m_mine_immature));
if (wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
// If the AVOID_REUSE flag is set, bal has been set to just the un-reused address balance. Get
// the total balance, and then subtract bal to get the reused address balance.
const auto full_bal = GetBalance(wallet, 0, false);
balances_mine.pushKV("used", ValueFromAmount(full_bal.m_mine_trusted + full_bal.m_mine_untrusted_pending - bal.m_mine_trusted - bal.m_mine_untrusted_pending));
balances_mine.pushKV("used", ValueFromAmount(bal.m_mine_used));
}
balances.pushKV("mine", balances_mine);
}
Expand Down

0 comments on commit 1cc2a81

Please sign in to comment.