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