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(false,true);
29 int64 nDebit = wtx.GetDebit();
30 int64 nNet = nCredit - nDebit;
31 uint256 hash = wtx.GetHash(), hashPrev = 0;
32 std::map<std::string, std::string> mapValue = wtx.mapValue;
34 if (nNet > 0 || wtx.IsCoinBase() || wtx.IsCoinStake())
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"];
61 // Generated (proof-of-work)
62 sub.type = TransactionRecord::Generated;
64 if (wtx.IsCoinStake())
66 // Generated (proof-of-stake)
69 continue; // last coinstake output
71 sub.type = TransactionRecord::Generated;
72 sub.credit = nNet > 0 ? nNet : wtx.GetValueOut() - nDebit;
82 bool fAllFromMe = true;
83 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
84 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
87 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
88 fAllToMe = fAllToMe && wallet->IsMine(txout);
90 if (fAllFromMe && fAllToMe)
93 int64 nChange = wtx.GetChange();
95 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
96 -(nDebit - nChange), nCredit - nChange));
103 int64 nTxFee = nDebit - wtx.GetValueOut();
105 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
107 const CTxOut& txout = wtx.vout[nOut];
108 TransactionRecord sub(hash, nTime);
109 sub.idx = parts.size();
111 if(wallet->IsMine(txout))
113 // Ignore parts sent to self, as this is usually the change
114 // from a transaction sent back to our own address.
118 CTxDestination address;
119 if (ExtractDestination(txout.scriptPubKey, address))
121 // Sent to Bitcoin Address
122 sub.type = TransactionRecord::SendToAddress;
123 sub.address = CBitcoinAddress(address).ToString();
127 // Sent to IP, or other non-address transaction like OP_EVAL
128 sub.type = TransactionRecord::SendToOther;
129 sub.address = mapValue["to"];
132 int64 nValue = txout.nValue;
133 /* Add fee to first output */
147 // Mixed debit transaction, can't break down payees
149 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
156 void TransactionRecord::updateStatus(const CWalletTx &wtx)
158 // Determine transaction status
160 // Find the block the tx is in
161 CBlockIndex* pindex = NULL;
162 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
163 if (mi != mapBlockIndex.end())
164 pindex = (*mi).second;
166 // Sort order, unrecorded transactions sort to the top
167 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
168 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
169 (wtx.IsCoinBase() ? 1 : 0),
172 status.confirmed = wtx.IsTrusted();
173 status.depth = wtx.GetDepthInMainChain();
174 status.cur_num_blocks = nBestHeight;
178 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
180 status.status = TransactionStatus::OpenUntilBlock;
181 status.open_for = nBestHeight - wtx.nLockTime;
185 status.status = TransactionStatus::OpenUntilDate;
186 status.open_for = wtx.nLockTime;
191 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
193 status.status = TransactionStatus::Offline;
195 else if (status.depth < NumConfirmations)
197 status.status = TransactionStatus::Unconfirmed;
201 status.status = TransactionStatus::HaveConfirmations;
205 // For generated transactions, determine maturity
206 if(type == TransactionRecord::Generated)
208 int64 nCredit = wtx.GetCredit(false,true);
211 status.maturity = TransactionStatus::Immature;
213 if (wtx.IsInMainChain())
215 status.matures_in = wtx.GetBlocksToMaturity();
217 // Check if the block was requested by anyone
218 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
219 status.maturity = TransactionStatus::MaturesWarning;
223 status.maturity = TransactionStatus::NotAccepted;
228 status.maturity = TransactionStatus::Mature;
233 bool TransactionRecord::statusUpdateNeeded()
235 return status.cur_num_blocks != nBestHeight;
238 std::string TransactionRecord::getTxID()
240 return hash.ToString() + strprintf("-%03d", idx);