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 if (nNet > 0 || wtx.IsCoinBase() || wtx.IsCoinStake())
41 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
43 if(wallet->IsMine(txout))
45 TransactionRecord sub(hash, nTime);
46 sub.idx = parts.size(); // sequence number
47 sub.credit = txout.nValue;
50 std::vector<valtype> vSolutions;
51 if (Solver(txout.scriptPubKey, whichType, vSolutions))
53 CTxDestination address;
54 if (whichType == TX_PUBKEY)
57 address = CPubKey(vSolutions[0]).GetID();
58 sub.type = TransactionRecord::RecvWithAddress;
59 sub.address = CBitcoinAddress(address).ToString();
61 else if (whichType == TX_PUBKEYHASH)
64 address = CKeyID(uint160(vSolutions[0]));
65 sub.type = TransactionRecord::RecvWithAddress;
66 sub.address = CBitcoinAddress(address).ToString();
68 else if (whichType == TX_SCRIPTHASH)
71 address = CScriptID(uint160(vSolutions[0]));
72 sub.type = TransactionRecord::RecvWithAddress;
73 sub.address = CBitcoinAddress(address).ToString();
75 else if (whichType == TX_PUBKEY_DROP)
78 sub.type = TransactionRecord::RecvWithAddress;
80 CMalleableKeyView view;
81 pwalletMain->CheckOwnership(CPubKey(vSolutions[0]), CPubKey(vSolutions[1]), view);
82 sub.address = view.GetMalleablePubKey().ToString();
86 // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
87 sub.type = TransactionRecord::RecvFromOther;
88 sub.address = mapValue["from"];
93 // Generated (proof-of-work)
94 sub.type = TransactionRecord::Generated;
96 if (wtx.IsCoinStake())
98 // Generated (proof-of-stake)
100 if (hashPrev == hash)
101 continue; // last coinstake output
103 sub.type = TransactionRecord::Generated;
104 sub.credit = nNet > 0 ? nNet : wtx.GetValueOut() - nDebit;
114 bool fAllFromMe = true;
115 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
116 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
118 bool fAllToMe = true;
119 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
120 fAllToMe = fAllToMe && wallet->IsMine(txout);
122 if (fAllFromMe && fAllToMe)
125 int64_t nChange = wtx.GetChange();
127 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
128 -(nDebit - nChange), nCredit - nChange));
135 int64_t nTxFee = nDebit - wtx.GetValueOut();
137 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
139 const CTxOut& txout = wtx.vout[nOut];
140 TransactionRecord sub(hash, nTime);
141 sub.idx = parts.size();
143 if(wallet->IsMine(txout))
145 // Ignore parts sent to self, as this is usually the change
146 // from a transaction sent back to our own address.
150 CTxDestination address;
151 if (ExtractDestination(txout.scriptPubKey, address))
153 // Sent to Bitcoin Address
154 sub.type = TransactionRecord::SendToAddress;
155 sub.address = CBitcoinAddress(address).ToString();
159 // Sent to IP, or other non-address transaction like OP_EVAL
160 sub.type = TransactionRecord::SendToOther;
161 sub.address = mapValue["to"];
164 int64_t nValue = txout.nValue;
165 /* Add fee to first output */
179 // Mixed debit transaction, can't break down payees
181 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
188 void TransactionRecord::updateStatus(const CWalletTx &wtx)
190 // Determine transaction status
192 // Find the block the tx is in
193 CBlockIndex* pindex = NULL;
194 std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
195 if (mi != mapBlockIndex.end())
196 pindex = (*mi).second;
198 // Sort order, unrecorded transactions sort to the top
199 status.sortKey = strprintf("%010d-%01d-%010u-%03d",
200 (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
201 (wtx.IsCoinBase() ? 1 : 0),
204 status.confirmed = wtx.IsTrusted();
205 status.depth = wtx.GetDepthInMainChain();
206 status.cur_num_blocks = nBestHeight;
210 if (wtx.nLockTime < LOCKTIME_THRESHOLD)
212 status.status = TransactionStatus::OpenUntilBlock;
213 status.open_for = nBestHeight - wtx.nLockTime;
217 status.status = TransactionStatus::OpenUntilDate;
218 status.open_for = wtx.nLockTime;
223 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
225 status.status = TransactionStatus::Offline;
227 else if (status.depth < NumConfirmations)
229 status.status = TransactionStatus::Unconfirmed;
233 status.status = TransactionStatus::HaveConfirmations;
237 // For generated transactions, determine maturity
238 if(type == TransactionRecord::Generated)
240 if (wtx.GetBlocksToMaturity() > 0)
242 status.maturity = TransactionStatus::Immature;
244 if (wtx.IsInMainChain())
246 status.matures_in = wtx.GetBlocksToMaturity();
248 // Check if the block was requested by anyone
249 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
250 status.maturity = TransactionStatus::MaturesWarning;
254 status.maturity = TransactionStatus::NotAccepted;
259 status.maturity = TransactionStatus::Mature;
264 bool TransactionRecord::statusUpdateNeeded()
266 return status.cur_num_blocks != nBestHeight;
269 std::string TransactionRecord::getTxID()
271 return hash.ToString() + strprintf("-%03d", idx);