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