1 #include "transactionrecord.h"
6 /* Return positive answer if transaction should be shown in list.
8 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
12 // Ensures we show generated coins / mined transactions at depth 1
13 if (!wtx.IsInMainChain())
22 * Decompose CWallet transaction to model transaction records.
24 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
26 QList<TransactionRecord> parts;
27 int64 nTime = wtx.GetTxTime();
28 int64 nCredit = wtx.GetCredit(true);
29 int64 nDebit = wtx.GetDebit();
30 int64 nNet = nCredit - nDebit;
31 uint256 hash = wtx.GetHash();
32 std::map<std::string, std::string> mapValue = wtx.mapValue;
34 if (nNet > 0 || wtx.IsCoinBase())
39 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
41 if(wallet->IsMine(txout))
43 TransactionRecord sub(hash, nTime);
44 CTxDestination address;
45 sub.idx = parts.size(); // sequence number
46 sub.credit = txout.nValue;
47 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
49 // Received by Bitcoin Address
50 sub.type = TransactionRecord::RecvWithAddress;
51 sub.address = CBitcoinAddress(address).ToString();
55 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
56 sub.type = TransactionRecord::RecvFromOther;
57 sub.address = mapValue["from"];
62 sub.type = TransactionRecord::Generated;
71 bool fAllFromMe = true;
72 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
73 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
76 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
77 fAllToMe = fAllToMe && wallet->IsMine(txout);
79 if (fAllFromMe && fAllToMe)
82 int64 nChange = wtx.GetChange();
84 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
85 -(nDebit - nChange), nCredit - nChange));
92 int64 nTxFee = nDebit - wtx.GetValueOut();
94 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
96 const CTxOut& txout = wtx.vout[nOut];
97 TransactionRecord sub(hash, nTime);
98 sub.idx = parts.size();
100 if(wallet->IsMine(txout))
102 // Ignore parts sent to self, as this is usually the change
103 // from a transaction sent back to our own address.
107 CTxDestination address;
108 if (ExtractDestination(txout.scriptPubKey, address))
110 // Sent to Bitcoin Address
111 sub.type = TransactionRecord::SendToAddress;
112 sub.address = CBitcoinAddress(address).ToString();
116 // Sent to IP, or other non-address transaction like OP_EVAL
117 sub.type = TransactionRecord::SendToOther;
118 sub.address = mapValue["to"];
121 int64 nValue = txout.nValue;
122 /* Add fee to first output */
136 // Mixed debit transaction, can't break down payees
138 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
145 void TransactionRecord::updateStatus(const CWalletTx &wtx)
147 // Determine transaction status
149 // Find the block the tx is in
150 CBlockIndex* pindex = NULL;
151 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
152 if (mi != mapBlockIndex.end())
153 pindex = (*mi).second;
155 // Sort order, unrecorded transactions sort to the top
156 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
157 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
158 (wtx.IsCoinBase() ? 1 : 0),
161 status.confirmed = wtx.IsConfirmed();
162 status.depth = wtx.GetDepthInMainChain();
163 status.cur_num_blocks = nBestHeight;
167 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
169 status.status = TransactionStatus::OpenUntilBlock;
170 status.open_for = nBestHeight - wtx.nLockTime;
174 status.status = TransactionStatus::OpenUntilDate;
175 status.open_for = wtx.nLockTime;
180 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
182 status.status = TransactionStatus::Offline;
184 else if (status.depth < NumConfirmations)
186 status.status = TransactionStatus::Unconfirmed;
190 status.status = TransactionStatus::HaveConfirmations;
194 // For generated transactions, determine maturity
195 if(type == TransactionRecord::Generated)
197 int64 nCredit = wtx.GetCredit(true);
200 status.maturity = TransactionStatus::Immature;
202 if (wtx.IsInMainChain())
204 status.matures_in = wtx.GetBlocksToMaturity();
206 // Check if the block was requested by anyone
207 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
208 status.maturity = TransactionStatus::MaturesWarning;
212 status.maturity = TransactionStatus::NotAccepted;
217 status.maturity = TransactionStatus::Mature;
222 bool TransactionRecord::statusUpdateNeeded()
224 return status.cur_num_blocks != nBestHeight;
227 std::string TransactionRecord::getTxID()
229 return hash.ToString() + strprintf("-%03d", idx);