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 (wtx.IsCoinStake())
37 parts.append(TransactionRecord(hash, nTime, TransactionRecord::StakeMint, "", -nDebit, wtx.GetValueOut()));
39 else if (nNet > 0 || wtx.IsCoinBase())
44 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
46 if(wallet->IsMine(txout))
48 TransactionRecord sub(hash, nTime);
49 CTxDestination address;
50 sub.idx = parts.size(); // sequence number
51 sub.credit = txout.nValue;
52 if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
54 // Received by Bitcoin Address
55 sub.type = TransactionRecord::RecvWithAddress;
56 sub.address = CBitcoinAddress(address).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"];
67 sub.type = TransactionRecord::Generated;
76 bool fAllFromMe = true;
77 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
78 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
81 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
82 fAllToMe = fAllToMe && wallet->IsMine(txout);
84 if (fAllFromMe && fAllToMe)
87 int64 nChange = wtx.GetChange();
89 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
90 -(nDebit - nChange), nCredit - nChange));
97 int64 nTxFee = nDebit - wtx.GetValueOut();
99 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
101 const CTxOut& txout = wtx.vout[nOut];
102 TransactionRecord sub(hash, nTime);
103 sub.idx = parts.size();
105 if(wallet->IsMine(txout))
107 // Ignore parts sent to self, as this is usually the change
108 // from a transaction sent back to our own address.
112 CTxDestination address;
113 if (ExtractDestination(txout.scriptPubKey, address))
115 // Sent to Bitcoin Address
116 sub.type = TransactionRecord::SendToAddress;
117 sub.address = CBitcoinAddress(address).ToString();
121 // Sent to IP, or other non-address transaction like OP_EVAL
122 sub.type = TransactionRecord::SendToOther;
123 sub.address = mapValue["to"];
126 int64 nValue = txout.nValue;
127 /* Add fee to first output */
141 // Mixed debit transaction, can't break down payees
143 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
150 void TransactionRecord::updateStatus(const CWalletTx &wtx)
152 // Determine transaction status
154 // Find the block the tx is in
155 CBlockIndex* pindex = NULL;
156 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
157 if (mi != mapBlockIndex.end())
158 pindex = (*mi).second;
160 // Sort order, unrecorded transactions sort to the top
161 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
162 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
163 (wtx.IsCoinBase() ? 1 : 0),
166 status.confirmed = wtx.IsConfirmed();
167 status.depth = wtx.GetDepthInMainChain();
168 status.cur_num_blocks = nBestHeight;
172 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
174 status.status = TransactionStatus::OpenUntilBlock;
175 status.open_for = nBestHeight - wtx.nLockTime;
179 status.status = TransactionStatus::OpenUntilDate;
180 status.open_for = wtx.nLockTime;
185 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
187 status.status = TransactionStatus::Offline;
189 else if (status.depth < NumConfirmations)
191 status.status = TransactionStatus::Unconfirmed;
195 status.status = TransactionStatus::HaveConfirmations;
199 // For generated transactions, determine maturity
200 if(type == TransactionRecord::Generated || type == TransactionRecord::StakeMint)
202 int64 nCredit = wtx.GetCredit(true);
205 status.maturity = TransactionStatus::Immature;
207 if (wtx.IsInMainChain())
209 status.matures_in = wtx.GetBlocksToMaturity();
211 // Check if the block was requested by anyone
212 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
213 status.maturity = TransactionStatus::MaturesWarning;
217 status.maturity = TransactionStatus::NotAccepted;
222 status.maturity = TransactionStatus::Mature;
227 bool TransactionRecord::statusUpdateNeeded()
229 return status.cur_num_blocks != nBestHeight;
232 std::string TransactionRecord::getTxID()
234 return hash.ToString() + strprintf("-%03d", idx);