@@ -1975,6 +1975,49 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const
1975
1975
return nTotal;
1976
1976
}
1977
1977
1978
+ // Calculate total balance in a different way from GetBalance. The biggest
1979
+ // difference is that GetBalance sums up all unspent TxOuts paying to the
1980
+ // wallet, while this sums up both spent and unspent TxOuts paying to the
1981
+ // wallet, and then subtracts the values of TxIns spending from the wallet. This
1982
+ // also has fewer restrictions on which unconfirmed transactions are considered
1983
+ // trusted.
1984
+ CAmount CWallet::GetLegacyBalance (const isminefilter& filter, int minDepth, const std::string* account) const
1985
+ {
1986
+ LOCK2 (cs_main, cs_wallet);
1987
+
1988
+ CAmount balance = 0 ;
1989
+ for (const auto & entry : mapWallet) {
1990
+ const CWalletTx& wtx = entry.second ;
1991
+ const int depth = wtx.GetDepthInMainChain ();
1992
+ if (depth < 0 || !CheckFinalTx (*wtx.tx ) || wtx.GetBlocksToMaturity () > 0 ) {
1993
+ continue ;
1994
+ }
1995
+
1996
+ // Loop through tx outputs and add incoming payments. For outgoing txs,
1997
+ // treat change outputs specially, as part of the amount debited.
1998
+ CAmount debit = wtx.GetDebit (filter);
1999
+ const bool outgoing = debit > 0 ;
2000
+ for (const CTxOut& out : wtx.tx ->vout ) {
2001
+ if (outgoing && IsChange (out)) {
2002
+ debit -= out.nValue ;
2003
+ } else if (IsMine (out) & filter && depth >= minDepth && (!account || *account == GetAccountName (out.scriptPubKey ))) {
2004
+ balance += out.nValue ;
2005
+ }
2006
+ }
2007
+
2008
+ // For outgoing txs, subtract amount debited.
2009
+ if (outgoing && (!account || *account == wtx.strFromAccount )) {
2010
+ balance -= debit;
2011
+ }
2012
+ }
2013
+
2014
+ if (account) {
2015
+ balance += CWalletDB (*dbw).GetAccountCreditDebit (*account);
2016
+ }
2017
+
2018
+ return balance;
2019
+ }
2020
+
1978
2021
void CWallet::AvailableCoins (std::vector<COutput>& vCoins, bool fOnlySafe , const CCoinControl *coinControl, bool fIncludeZeroValue ) const
1979
2022
{
1980
2023
vCoins.clear ();
@@ -2911,6 +2954,21 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
2911
2954
return CWalletDB (*dbw).EraseName (CBitcoinAddress (address).ToString ());
2912
2955
}
2913
2956
2957
+ const std::string& CWallet::GetAccountName (const CScript& scriptPubKey) const
2958
+ {
2959
+ CTxDestination address;
2960
+ if (ExtractDestination (scriptPubKey, address) && !scriptPubKey.IsUnspendable ()) {
2961
+ auto mi = mapAddressBook.find (address);
2962
+ if (mi != mapAddressBook.end ()) {
2963
+ return mi->second .name ;
2964
+ }
2965
+ }
2966
+ // A scriptPubKey that doesn't have an entry in the address book is
2967
+ // associated with the default account ("").
2968
+ const static std::string DEFAULT_ACCOUNT_NAME;
2969
+ return DEFAULT_ACCOUNT_NAME;
2970
+ }
2971
+
2914
2972
bool CWallet::SetDefaultKey (const CPubKey &vchPubKey)
2915
2973
{
2916
2974
if (!CWalletDB (*dbw).WriteDefaultKey (vchPubKey))
0 commit comments