1 #include "transactionrecord.h"
6 extern CWallet* pwalletMain;
8 /* Return positive answer if transaction should be shown in list.
10 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
14 // Ensures we show generated coins / mined transactions at depth 1
15 if (!wtx.IsInMainChain())
24 * Decompose CWallet transaction to model transaction records.
26 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
28 QList<TransactionRecord> parts;
29 int64_t nTime = wtx.GetTxTime();
30 int64_t nCredit = wtx.GetCredit(MINE_ALL);
31 int64_t nDebit = wtx.GetDebit(MINE_ALL);
32 int64_t nNet = nCredit - nDebit;
33 uint256 hash = wtx.GetHash(), hashPrev = 0;
34 std::map<std::string, std::string> mapValue = wtx.mapValue;
36 bool fCoinBase = wtx.IsCoinBase(),
37 fCoinStake = wtx.IsCoinStake();
39 if (nNet > 0 || fCoinBase || fCoinStake)
44 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
46 if(wallet->IsMine(txout))
48 TransactionRecord sub(hash, nTime);
49 sub.idx = parts.size(); // sequence number
50 sub.credit = txout.nValue;
52 CBitcoinAddress addressRet;
53 if (ExtractAddress(*pwalletMain, txout.scriptPubKey, addressRet))
55 sub.type = TransactionRecord::RecvWithAddress;
56 sub.address = addressRet.ToString();
60 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
61 sub.type = TransactionRecord::RecvFromOther;
62 sub.address = mapValue["from"];
65 if (fCoinBase || fCoinStake)
68 sub.type = TransactionRecord::Generated;
73 continue; // last coinstake output
74 sub.credit = nNet > 0 ? nNet : wtx.GetValueOut() - nDebit;
85 bool fAllFromMe = true;
86 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
87 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
90 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
91 fAllToMe = fAllToMe && wallet->IsMine(txout);
93 if (fAllFromMe && fAllToMe)
96 int64_t nChange = wtx.GetChange();
98 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
99 -(nDebit - nChange), nCredit - nChange));
106 int64_t nTxFee = nDebit - wtx.GetValueOut();
108 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
110 const CTxOut& txout = wtx.vout[nOut];
111 TransactionRecord sub(hash, nTime);
112 sub.idx = parts.size();
114 if(wallet->IsMine(txout))
116 // Ignore parts sent to self, as this is usually the change
117 // from a transaction sent back to our own address.
121 CTxDestination address;
122 if (ExtractDestination(txout.scriptPubKey, address))
124 // Sent to Bitcoin Address
125 sub.type = TransactionRecord::SendToAddress;
126 sub.address = CBitcoinAddress(address).ToString();
130 // Sent to IP, or other non-address transaction like OP_EVAL
131 sub.type = TransactionRecord::SendToOther;
132 sub.address = mapValue["to"];
135 int64_t nValue = txout.nValue;
136 /* Add fee to first output */
150 // Mixed debit transaction, can't break down payees
152 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
159 void TransactionRecord::updateStatus(const CWalletTx &wtx)
161 // Determine transaction status
163 // Find the block the tx is in
164 CBlockIndex* pindex = NULL;
165 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
166 if (mi != mapBlockIndex.end())
167 pindex = (*mi).second;
169 // Sort order, unrecorded transactions sort to the top
170 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
171 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
172 (wtx.IsCoinBase() ? 1 : 0),
175 status.confirmed = wtx.IsTrusted();
176 status.depth = wtx.GetDepthInMainChain();
177 status.cur_num_blocks = nBestHeight;
181 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
183 status.status = TransactionStatus::OpenUntilBlock;
184 status.open_for = nBestHeight - wtx.nLockTime;
188 status.status = TransactionStatus::OpenUntilDate;
189 status.open_for = wtx.nLockTime;
194 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
196 status.status = TransactionStatus::Offline;
198 else if (status.depth < NumConfirmations)
200 status.status = TransactionStatus::Unconfirmed;
204 status.status = TransactionStatus::HaveConfirmations;
208 // For generated transactions, determine maturity
209 if(type == TransactionRecord::Generated)
211 if (wtx.GetBlocksToMaturity() > 0)
213 status.maturity = TransactionStatus::Immature;
215 if (wtx.IsInMainChain())
217 status.matures_in = wtx.GetBlocksToMaturity();
219 // Check if the block was requested by anyone
220 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
221 status.maturity = TransactionStatus::MaturesWarning;
225 status.maturity = TransactionStatus::NotAccepted;
230 status.maturity = TransactionStatus::Mature;
235 bool TransactionRecord::statusUpdateNeeded()
237 return status.cur_num_blocks != nBestHeight;
240 std::string TransactionRecord::getTxID()
242 return hash.ToString() + strprintf("-%03d", idx);