@@ -1457,41 +1457,6 @@ void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,
1457
1457
1458
1458
}
1459
1459
1460
- void CWalletTx::GetAccountAmounts (const std::string& strAccount, CAmount& nReceived,
1461
- CAmount& nSent, CAmount& nFee, const isminefilter& filter) const
1462
- {
1463
- nReceived = nSent = nFee = 0 ;
1464
-
1465
- CAmount allFee;
1466
- std::string strSentAccount;
1467
- std::list<COutputEntry> listReceived;
1468
- std::list<COutputEntry> listSent;
1469
- GetAmounts (listReceived, listSent, allFee, strSentAccount, filter);
1470
-
1471
- if (strAccount == strSentAccount)
1472
- {
1473
- BOOST_FOREACH (const COutputEntry& s, listSent)
1474
- nSent += s.amount ;
1475
- nFee = allFee;
1476
- }
1477
- {
1478
- LOCK (pwallet->cs_wallet );
1479
- BOOST_FOREACH (const COutputEntry& r, listReceived)
1480
- {
1481
- if (pwallet->mapAddressBook .count (r.destination ))
1482
- {
1483
- std::map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook .find (r.destination );
1484
- if (mi != pwallet->mapAddressBook .end () && (*mi).second .name == strAccount)
1485
- nReceived += r.amount ;
1486
- }
1487
- else if (strAccount.empty ())
1488
- {
1489
- nReceived += r.amount ;
1490
- }
1491
- }
1492
- }
1493
- }
1494
-
1495
1460
/* *
1496
1461
* Scan the block chain (starting in pindexStart) for transactions
1497
1462
* from or to us. If fUpdate is true, found transactions that already
@@ -1975,6 +1940,49 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const
1975
1940
return nTotal;
1976
1941
}
1977
1942
1943
+ // Calculate total balance in a different way from GetBalance. The biggest
1944
+ // difference is that GetBalance sums up all unspent TxOuts paying to the
1945
+ // wallet, while this sums up both spent and unspent TxOuts paying to the
1946
+ // wallet, and then subtracts the values of TxIns spending from the wallet. This
1947
+ // also has fewer restrictions on which unconfirmed transactions are considered
1948
+ // trusted.
1949
+ CAmount CWallet::GetLegacyBalance (const isminefilter& filter, int minDepth, const std::string* account) const
1950
+ {
1951
+ LOCK2 (cs_main, cs_wallet);
1952
+
1953
+ CAmount balance = 0 ;
1954
+ for (const auto & entry : mapWallet) {
1955
+ const CWalletTx& wtx = entry.second ;
1956
+ const int depth = wtx.GetDepthInMainChain ();
1957
+ if (depth < 0 || !CheckFinalTx (*wtx.tx ) || wtx.GetBlocksToMaturity () > 0 ) {
1958
+ continue ;
1959
+ }
1960
+
1961
+ // Loop through tx outputs and add incoming payments. For outgoing txs,
1962
+ // treat change outputs specially, as part of the amount debited.
1963
+ CAmount debit = wtx.GetDebit (filter);
1964
+ const bool outgoing = debit > 0 ;
1965
+ for (const CTxOut& out : wtx.tx ->vout ) {
1966
+ if (outgoing && IsChange (out)) {
1967
+ debit -= out.nValue ;
1968
+ } else if (IsMine (out) & filter && depth >= minDepth && (!account || *account == GetAccountName (out.scriptPubKey ))) {
1969
+ balance += out.nValue ;
1970
+ }
1971
+ }
1972
+
1973
+ // For outgoing txs, subtract amount debited.
1974
+ if (outgoing && (!account || *account == wtx.strFromAccount )) {
1975
+ balance -= debit;
1976
+ }
1977
+ }
1978
+
1979
+ if (account) {
1980
+ balance += CWalletDB (*dbw).GetAccountCreditDebit (*account);
1981
+ }
1982
+
1983
+ return balance;
1984
+ }
1985
+
1978
1986
void CWallet::AvailableCoins (std::vector<COutput>& vCoins, bool fOnlySafe , const CCoinControl *coinControl, bool fIncludeZeroValue ) const
1979
1987
{
1980
1988
vCoins.clear ();
@@ -2911,6 +2919,21 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
2911
2919
return CWalletDB (*dbw).EraseName (CBitcoinAddress (address).ToString ());
2912
2920
}
2913
2921
2922
+ const std::string& CWallet::GetAccountName (const CScript& scriptPubKey) const
2923
+ {
2924
+ CTxDestination address;
2925
+ if (ExtractDestination (scriptPubKey, address) && !scriptPubKey.IsUnspendable ()) {
2926
+ auto mi = mapAddressBook.find (address);
2927
+ if (mi != mapAddressBook.end ()) {
2928
+ return mi->second .name ;
2929
+ }
2930
+ }
2931
+ // A scriptPubKey that doesn't have an entry in the address book is
2932
+ // associated with the default account ("").
2933
+ const static std::string DEFAULT_ACCOUNT_NAME;
2934
+ return DEFAULT_ACCOUNT_NAME;
2935
+ }
2936
+
2914
2937
bool CWallet::SetDefaultKey (const CPubKey &vchPubKey)
2915
2938
{
2916
2939
if (!CWalletDB (*dbw).WriteDefaultKey (vchPubKey))
@@ -3257,37 +3280,6 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
3257
3280
return ret;
3258
3281
}
3259
3282
3260
- CAmount CWallet::GetAccountBalance (const std::string& strAccount, int nMinDepth, const isminefilter& filter)
3261
- {
3262
- CWalletDB walletdb (*dbw);
3263
- return GetAccountBalance (walletdb, strAccount, nMinDepth, filter);
3264
- }
3265
-
3266
- CAmount CWallet::GetAccountBalance (CWalletDB& walletdb, const std::string& strAccount, int nMinDepth, const isminefilter& filter)
3267
- {
3268
- CAmount nBalance = 0 ;
3269
-
3270
- // Tally wallet transactions
3271
- for (std::map<uint256, CWalletTx>::iterator it = mapWallet.begin (); it != mapWallet.end (); ++it)
3272
- {
3273
- const CWalletTx& wtx = (*it).second ;
3274
- if (!CheckFinalTx (wtx) || wtx.GetBlocksToMaturity () > 0 || wtx.GetDepthInMainChain () < 0 )
3275
- continue ;
3276
-
3277
- CAmount nReceived, nSent, nFee;
3278
- wtx.GetAccountAmounts (strAccount, nReceived, nSent, nFee, filter);
3279
-
3280
- if (nReceived != 0 && wtx.GetDepthInMainChain () >= nMinDepth)
3281
- nBalance += nReceived;
3282
- nBalance -= nSent + nFee;
3283
- }
3284
-
3285
- // Tally internal accounting entries
3286
- nBalance += walletdb.GetAccountCreditDebit (strAccount);
3287
-
3288
- return nBalance;
3289
- }
3290
-
3291
3283
std::set<CTxDestination> CWallet::GetAccountAddresses (const std::string& strAccount) const
3292
3284
{
3293
3285
LOCK (cs_wallet);
0 commit comments