| 243 | } |
| 244 | |
| 245 | Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse, bool include_nonmempool) |
| 246 | { |
| 247 | Balance ret; |
| 248 | bool allow_used_addresses = !avoid_reuse || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE); |
| 249 | { |
| 250 | LOCK(wallet.cs_wallet); |
| 251 | std::set<Txid> trusted_parents; |
| 252 | for (const auto& [outpoint, txo] : wallet.GetTXOs()) { |
| 253 | const CWalletTx& wtx = txo.GetWalletTx(); |
| 254 | |
| 255 | const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)}; |
| 256 | const int tx_depth{wallet.GetTxDepthInMainChain(wtx)}; |
| 257 | |
| 258 | bool nonmempool_spent = false; |
| 259 | switch (wallet.HowSpent(outpoint)) { |
| 260 | case CWallet::SpendType::CONFIRMED: |
| 261 | case CWallet::SpendType::MEMPOOL: |
| 262 | // treat as spent; ignore |
| 263 | break; |
| 264 | case CWallet::SpendType::NONMEMPOOL: |
| 265 | if (!include_nonmempool) break; |
| 266 | nonmempool_spent = true; |
| 267 | [[fallthrough]]; |
| 268 | case CWallet::SpendType::UNSPENT: |
| 269 | CAmount* bucket = nullptr; |
| 270 | |
| 271 | // Set the amounts in the return object |
| 272 | if (wallet.IsTxImmatureCoinBase(wtx) && wtx.isConfirmed()) { |
| 273 | bucket = &ret.m_mine_immature; |
| 274 | } else if (is_trusted && tx_depth >= min_depth) { |
| 275 | bucket = &ret.m_mine_trusted; |
| 276 | } else if (!is_trusted && wtx.InMempool()) { |
| 277 | bucket = &ret.m_mine_untrusted_pending; |
| 278 | } |
| 279 | if (bucket) { |
| 280 | // Get the amounts for mine |
| 281 | CAmount credit_mine = txo.GetTxOut().nValue; |
| 282 | |
| 283 | if (!allow_used_addresses && wallet.IsSpentKey(txo.GetTxOut().scriptPubKey)) { |
| 284 | bucket = &ret.m_mine_used; |
| 285 | } |
| 286 | *bucket += credit_mine; |
| 287 | if (nonmempool_spent) { |
| 288 | ret.m_mine_nonmempool -= credit_mine; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | std::map<CTxDestination, CAmount> GetAddressBalances(const CWallet& wallet) |
| 298 | { |