Export functionality for transaction list
[novacoin.git] / src / qt / transactionrecord.cpp
1 #include "transactionrecord.h"
2
3 #include "headers.h"
4
5 /* Return positive answer if transaction should be shown in list.
6  */
7 bool TransactionRecord::showTransaction(const CWalletTx &wtx)
8 {
9     if (wtx.IsCoinBase())
10     {
11         // Don't show generated coin until confirmed by at least one block after it
12         // so we don't get the user's hopes up until it looks like it's probably accepted.
13         //
14         // It is not an error when generated blocks are not accepted.  By design,
15         // some percentage of blocks, like 10% or more, will end up not accepted.
16         // This is the normal mechanism by which the network copes with latency.
17         //
18         // We display regular transactions right away before any confirmation
19         // because they can always get into some block eventually.  Generated coins
20         // are special because if their block is not accepted, they are not valid.
21         //
22         if (wtx.GetDepthInMainChain() < 2)
23         {
24             return false;
25         }
26     }
27     return true;
28 }
29
30 /*
31  * Decompose CWallet transaction to model transaction records.
32  */
33 QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
34 {
35     QList<TransactionRecord> parts;
36     int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
37     int64 nCredit = wtx.GetCredit(true);
38     int64 nDebit = wtx.GetDebit();
39     int64 nNet = nCredit - nDebit;
40     uint256 hash = wtx.GetHash();
41     std::map<std::string, std::string> mapValue = wtx.mapValue;
42
43     if (showTransaction(wtx))
44     {
45         if (nNet > 0 || wtx.IsCoinBase())
46         {
47             //
48             // Credit
49             //
50             TransactionRecord sub(hash, nTime);
51
52             sub.credit = nNet;
53
54             if (wtx.IsCoinBase())
55             {
56                 // Generated
57                 sub.type = TransactionRecord::Generated;
58
59                 if (nCredit == 0)
60                 {
61                     int64 nUnmatured = 0;
62                     BOOST_FOREACH(const CTxOut& txout, wtx.vout)
63                         nUnmatured += wallet->GetCredit(txout);
64                     sub.credit = nUnmatured;
65                 }
66             }
67             else if (!mapValue["from"].empty() || !mapValue["message"].empty())
68             {
69                 // Received by IP connection
70                 sub.type = TransactionRecord::RecvFromIP;
71                 if (!mapValue["from"].empty())
72                     sub.address = mapValue["from"];
73             }
74             else
75             {
76                 // Received by Bitcoin Address
77                 sub.type = TransactionRecord::RecvWithAddress;
78                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
79                 {
80                     if(wallet->IsMine(txout))
81                     {
82                         std::vector<unsigned char> vchPubKey;
83                         if (ExtractPubKey(txout.scriptPubKey, wallet, vchPubKey))
84                         {
85                             sub.address = PubKeyToAddress(vchPubKey);
86                         }
87                         break;
88                     }
89                 }
90             }
91             parts.append(sub);
92         }
93         else
94         {
95             bool fAllFromMe = true;
96             BOOST_FOREACH(const CTxIn& txin, wtx.vin)
97                 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
98
99             bool fAllToMe = true;
100             BOOST_FOREACH(const CTxOut& txout, wtx.vout)
101                 fAllToMe = fAllToMe && wallet->IsMine(txout);
102
103             if (fAllFromMe && fAllToMe)
104             {
105                 // Payment to self
106                 int64 nChange = wtx.GetChange();
107
108                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
109                                 -(nDebit - nChange), nCredit - nChange));
110             }
111             else if (fAllFromMe)
112             {
113                 //
114                 // Debit
115                 //
116                 int64 nTxFee = nDebit - wtx.GetValueOut();
117
118                 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
119                 {
120                     const CTxOut& txout = wtx.vout[nOut];
121                     TransactionRecord sub(hash, nTime);
122                     sub.idx = parts.size();
123
124                     if(wallet->IsMine(txout))
125                     {
126                         // Ignore parts sent to self, as this is usually the change
127                         // from a transaction sent back to our own address.
128                         continue;
129                     }
130                     else if(!mapValue["to"].empty())
131                     {
132                         // Sent to IP
133                         sub.type = TransactionRecord::SendToIP;
134                         sub.address = mapValue["to"];
135                     }
136                     else
137                     {
138                         // Sent to Bitcoin Address
139                         sub.type = TransactionRecord::SendToAddress;
140                         uint160 hash160;
141                         if (ExtractHash160(txout.scriptPubKey, hash160))
142                             sub.address = Hash160ToAddress(hash160);
143                     }
144
145                     int64 nValue = txout.nValue;
146                     /* Add fee to first output */
147                     if (nTxFee > 0)
148                     {
149                         nValue += nTxFee;
150                         nTxFee = 0;
151                     }
152                     sub.debit = -nValue;
153
154                     parts.append(sub);
155                 }
156             }
157             else
158             {
159                 //
160                 // Mixed debit transaction, can't break down payees
161                 //
162                 bool fAllMine = true;
163                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
164                     fAllMine = fAllMine && wallet->IsMine(txout);
165                 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
166                     fAllMine = fAllMine && wallet->IsMine(txin);
167
168                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
169             }
170         }
171     }
172
173     return parts;
174 }
175
176 void TransactionRecord::updateStatus(const CWalletTx &wtx)
177 {
178     // Determine transaction status
179
180     // Find the block the tx is in
181     CBlockIndex* pindex = NULL;
182     std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
183     if (mi != mapBlockIndex.end())
184         pindex = (*mi).second;
185
186     // Sort order, unrecorded transactions sort to the top
187     status.sortKey = strprintf("%010d-%01d-%010u-%03d",
188         (pindex ? pindex->nHeight : INT_MAX),
189         (wtx.IsCoinBase() ? 1 : 0),
190         wtx.nTimeReceived,
191         idx);
192     status.confirmed = wtx.IsConfirmed();
193     status.depth = wtx.GetDepthInMainChain();
194     status.cur_num_blocks = nBestHeight;
195
196     if (!wtx.IsFinal())
197     {
198         if (wtx.nLockTime < 500000000)
199         {
200             status.status = TransactionStatus::OpenUntilBlock;
201             status.open_for = nBestHeight - wtx.nLockTime;
202         }
203         else
204         {
205             status.status = TransactionStatus::OpenUntilDate;
206             status.open_for = wtx.nLockTime;
207         }
208     }
209     else
210     {
211         if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
212         {
213             status.status = TransactionStatus::Offline;
214         }
215         else if (status.depth < NumConfirmations)
216         {
217             status.status = TransactionStatus::Unconfirmed;
218         }
219         else
220         {
221             status.status = TransactionStatus::HaveConfirmations;
222         }
223     }
224
225     // For generated transactions, determine maturity
226     if(type == TransactionRecord::Generated)
227     {
228         int64 nCredit = wtx.GetCredit(true);
229         if (nCredit == 0)
230         {
231             status.maturity = TransactionStatus::Immature;
232
233             if (wtx.IsInMainChain())
234             {
235                 status.matures_in = wtx.GetBlocksToMaturity();
236
237                 // Check if the block was requested by anyone
238                 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
239                     status.maturity = TransactionStatus::MaturesWarning;
240             }
241             else
242             {
243                 status.maturity = TransactionStatus::NotAccepted;
244             }
245         }
246         else
247         {
248             status.maturity = TransactionStatus::Mature;
249         }
250     }
251 }
252
253 bool TransactionRecord::statusUpdateNeeded()
254 {
255     return status.cur_num_blocks != nBestHeight;
256 }
257
258 std::string TransactionRecord::getTxID()
259 {
260     return hash.ToString() + strprintf("-%03d", idx);
261 }
262