diff options
author | gavinandresen <gavinandresen@1a98c847-1fd6-4fd8-948a-caf3550aa51b> | 2010-11-30 18:58:11 +0000 |
---|---|---|
committer | gavinandresen <gavinandresen@1a98c847-1fd6-4fd8-948a-caf3550aa51b> | 2010-11-30 18:58:11 +0000 |
commit | bfd471f53e14c4218ae7a1544beb7f1de3e695b2 (patch) | |
tree | ce973160466913efe4a091eb47da3e1f8b90f388 /main.h | |
parent | 84d7c981dc52cc738053f9ec48648ab6fbf9311b (diff) |
JSON methods: listtransactions, gettransaction, move, sendfrom and getbalance <account>
git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@193 1a98c847-1fd6-4fd8-948a-caf3550aa51b
Diffstat (limited to 'main.h')
-rw-r--r-- | main.h | 82 |
1 files changed, 78 insertions, 4 deletions
@@ -345,9 +345,25 @@ public: { if (!MoneyRange(nValue)) throw runtime_error("CTxOut::GetCredit() : value out of range"); - if (IsMine()) - return nValue; - return 0; + return (IsMine() ? nValue : 0); + } + + bool IsChange() const + { + // On a debit transaction, a txout that's mine but isn't in the address book is change + vector<unsigned char> vchPubKey; + if (ExtractPubKey(scriptPubKey, true, vchPubKey)) + CRITICAL_BLOCK(cs_mapAddressBook) + if (!mapAddressBook.count(PubKeyToAddress(vchPubKey))) + return true; + return false; + } + + int64 GetChange() const + { + if (!MoneyRange(nValue)) + throw runtime_error("CTxOut::GetChange() : value out of range"); + return (IsChange() ? nValue : 0); } friend bool operator==(const CTxOut& a, const CTxOut& b) @@ -520,6 +536,20 @@ public: return nCredit; } + int64 GetChange() const + { + if (IsCoinBase()) + return 0; + int64 nChange = 0; + foreach(const CTxOut& txout, vout) + { + nChange += txout.GetChange(); + if (!MoneyRange(nChange)) + throw runtime_error("CTransaction::GetChange() : value out of range"); + } + return nChange; + } + int64 GetValueOut() const { int64 nValueOut = 0; @@ -731,8 +761,10 @@ public: // memory only mutable char fDebitCached; mutable char fCreditCached; + mutable char fChangeCached; mutable int64 nDebitCached; mutable int64 nCreditCached; + mutable int64 nChangeCached; // memory only UI hints mutable unsigned int nTimeDisplayed; @@ -768,8 +800,10 @@ public: strFromAccount.clear(); fDebitCached = false; fCreditCached = false; + fChangeCached = false; nDebitCached = 0; nCreditCached = 0; + nChangeCached = 0; nTimeDisplayed = 0; nLinesDisplayed = 0; fConfirmedDisplayed = false; @@ -808,7 +842,7 @@ public: return nDebitCached; } - int64 GetCredit(bool fUseCache=false) const + int64 GetCredit(bool fUseCache=true) const { // Must wait until coinbase is safely deep enough in the chain before valuing it if (IsCoinBase() && GetBlocksToMaturity() > 0) @@ -822,11 +856,51 @@ public: return nCreditCached; } + int64 GetChange() const + { + if (fChangeCached) + return nChangeCached; + nChangeCached = CTransaction::GetChange(); + fChangeCached = true; + return nChangeCached; + } + bool IsFromMe() const { return (GetDebit() > 0); } + void GetAccountAmounts(string strAccount, const set<CScript>& setPubKey, + int64& nGenerated, int64& nReceived, int64& nSent, int64& nFee) const + { + nGenerated = nReceived = nSent = nFee = 0; + + // Generated blocks count to account "" + if (IsCoinBase()) + { + if (strAccount == "" && GetBlocksToMaturity() == 0) + nGenerated = GetCredit(); + return; + } + + // Received + foreach(const CTxOut& txout, vout) + if (setPubKey.count(txout.scriptPubKey)) + nReceived += txout.nValue; + + // Sent + if (strFromAccount == strAccount) + { + int64 nDebit = GetDebit(); + if (nDebit > 0) + { + int64 nValueOut = GetValueOut(); + nFee = nDebit - nValueOut; + nSent = nValueOut - GetChange(); + } + } + } + bool IsConfirmed() const { // Quick answer in most cases |