Fix transaction type in UI: not all tx'es with "from"/"to" field are necessarily...
[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
68             {
69                 bool foundAddress = false;
70                 // Received by Bitcoin Address
71                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
72                 {
73                     if(wallet->IsMine(txout))
74                     {
75                         CBitcoinAddress address;
76                         if (ExtractAddress(txout.scriptPubKey, address) && wallet->HaveKey(address))
77                         {
78                             sub.type = TransactionRecord::RecvWithAddress;
79                             sub.address = address.ToString();
80                             foundAddress = true;
81                             break;
82                         }
83                     }
84                 }
85                 if(!foundAddress)
86                 {
87                     // Received by IP connection, or other non-address transaction like OP_EVAL
88                     sub.type = TransactionRecord::RecvFromOther;
89                     sub.address = mapValue["from"];
90                 }
91             }
92             parts.append(sub);
93         }
94         else
95         {
96             bool fAllFromMe = true;
97             BOOST_FOREACH(const CTxIn& txin, wtx.vin)
98                 fAllFromMe = fAllFromMe && wallet->IsMine(txin);
99
100             bool fAllToMe = true;
101             BOOST_FOREACH(const CTxOut& txout, wtx.vout)
102                 fAllToMe = fAllToMe && wallet->IsMine(txout);
103
104             if (fAllFromMe && fAllToMe)
105             {
106                 // Payment to self
107                 int64 nChange = wtx.GetChange();
108
109                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
110                                 -(nDebit - nChange), nCredit - nChange));
111             }
112             else if (fAllFromMe)
113             {
114                 //
115                 // Debit
116                 //
117                 int64 nTxFee = nDebit - wtx.GetValueOut();
118
119                 for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
120                 {
121                     const CTxOut& txout = wtx.vout[nOut];
122                     TransactionRecord sub(hash, nTime);
123                     sub.idx = parts.size();
124
125                     if(wallet->IsMine(txout))
126                     {
127                         // Ignore parts sent to self, as this is usually the change
128                         // from a transaction sent back to our own address.
129                         continue;
130                     }
131
132                     CBitcoinAddress address;
133                     if (ExtractAddress(txout.scriptPubKey, address))
134                     {
135                         // Sent to Bitcoin Address
136                         sub.type = TransactionRecord::SendToAddress;
137                         sub.address = address.ToString();
138                     }
139                     else
140                     {
141                         // Sent to IP, or other non-address transaction like OP_EVAL
142                         sub.type = TransactionRecord::SendToOther;
143                         sub.address = mapValue["to"];
144                     }
145
146                     int64 nValue = txout.nValue;
147                     /* Add fee to first output */
148                     if (nTxFee > 0)
149                     {
150                         nValue += nTxFee;
151                         nTxFee = 0;
152                     }
153                     sub.debit = -nValue;
154
155                     parts.append(sub);
156                 }
157             }
158             else
159             {
160                 //
161                 // Mixed debit transaction, can't break down payees
162                 //
163                 bool fAllMine = true;
164                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
165                     fAllMine = fAllMine && wallet->IsMine(txout);
166                 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
167                     fAllMine = fAllMine && wallet->IsMine(txin);
168
169                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
170             }
171         }
172     }
173
174     return parts;
175 }
176
177 void TransactionRecord::updateStatus(const CWalletTx &wtx)
178 {
179     // Determine transaction status
180
181     // Find the block the tx is in
182     CBlockIndex* pindex = NULL;
183     std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
184     if (mi != mapBlockIndex.end())
185         pindex = (*mi).second;
186
187     // Sort order, unrecorded transactions sort to the top
188     status.sortKey = strprintf("%010d-%01d-%010u-%03d",
189         (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
190         (wtx.IsCoinBase() ? 1 : 0),
191         wtx.nTimeReceived,
192         idx);
193     status.confirmed = wtx.IsConfirmed();
194     status.depth = wtx.GetDepthInMainChain();
195     status.cur_num_blocks = nBestHeight;
196
197     if (!wtx.IsFinal())
198     {
199         if (wtx.nLockTime < LOCKTIME_THRESHOLD)
200         {
201             status.status = TransactionStatus::OpenUntilBlock;
202             status.open_for = nBestHeight - wtx.nLockTime;
203         }
204         else
205         {
206             status.status = TransactionStatus::OpenUntilDate;
207             status.open_for = wtx.nLockTime;
208         }
209     }
210     else
211     {
212         if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
213         {
214             status.status = TransactionStatus::Offline;
215         }
216         else if (status.depth < NumConfirmations)
217         {
218             status.status = TransactionStatus::Unconfirmed;
219         }
220         else
221         {
222             status.status = TransactionStatus::HaveConfirmations;
223         }
224     }
225
226     // For generated transactions, determine maturity
227     if(type == TransactionRecord::Generated)
228     {
229         int64 nCredit = wtx.GetCredit(true);
230         if (nCredit == 0)
231         {
232             status.maturity = TransactionStatus::Immature;
233
234             if (wtx.IsInMainChain())
235             {
236                 status.matures_in = wtx.GetBlocksToMaturity();
237
238                 // Check if the block was requested by anyone
239                 if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
240                     status.maturity = TransactionStatus::MaturesWarning;
241             }
242             else
243             {
244                 status.maturity = TransactionStatus::NotAccepted;
245             }
246         }
247         else
248         {
249             status.maturity = TransactionStatus::Mature;
250         }
251     }
252 }
253
254 bool TransactionRecord::statusUpdateNeeded()
255 {
256     return status.cur_num_blocks != nBestHeight;
257 }
258
259 std::string TransactionRecord::getTxID()
260 {
261     return hash.ToString() + strprintf("-%03d", idx);
262 }
263