1 // Copyright (c) 2011-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_QT_TRANSACTIONRECORD_H
6 #define BITCOIN_QT_TRANSACTIONRECORD_H
7 
8 #include "amount.h"
9 #include "uint256.h"
10 
11 #include <QList>
12 #include <QString>
13 
14 class CWallet;
15 class CWalletTx;
16 
17 /** UI model for transaction status. The transaction status is the part of a transaction that will change over time.
18  */
19 class TransactionStatus
20 {
21 public:
TransactionStatus()22     TransactionStatus():
23         countsForBalance(false), sortKey(""),
24         matures_in(0), status(Offline), depth(0), open_for(0), cur_num_blocks(-1)
25     { }
26 
27     enum Status {
28         Confirmed,          /**< Have 6 or more confirmations (normal tx) or fully mature (mined tx) **/
29         /// Normal (sent/received) transactions
30         OpenUntilDate,      /**< Transaction not yet final, waiting for date */
31         OpenUntilBlock,     /**< Transaction not yet final, waiting for block */
32         Offline,            /**< Not sent to any other nodes **/
33         Unconfirmed,        /**< Not yet mined into a block **/
34         Confirming,         /**< Confirmed, but waiting for the recommended number of confirmations **/
35         Conflicted,         /**< Conflicts with other transaction or mempool **/
36         Abandoned,          /**< Abandoned from the wallet **/
37         /// Generated (mined) transactions
38         Immature,           /**< Mined but waiting for maturity */
39         MaturesWarning,     /**< Transaction will likely not mature because no nodes have confirmed */
40         NotAccepted         /**< Mined but not accepted */
41     };
42 
43     /// Transaction counts towards available balance
44     bool countsForBalance;
45     /// Sorting key based on status
46     std::string sortKey;
47 
48     /** @name Generated (mined) transactions
49        @{*/
50     int matures_in;
51     /**@}*/
52 
53     /** @name Reported status
54        @{*/
55     Status status;
56     qint64 depth;
57     qint64 open_for; /**< Timestamp if status==OpenUntilDate, otherwise number
58                       of additional blocks that need to be mined before
59                       finalization */
60     /**@}*/
61 
62     /** Current number of blocks (to know whether cached status is still valid) */
63     int cur_num_blocks;
64 };
65 
66 /** UI model for a transaction. A core transaction can be represented by multiple UI transactions if it has
67     multiple outputs.
68  */
69 class TransactionRecord
70 {
71 public:
72     enum Type
73     {
74         Other,
75         Generated,
76         SendToAddress,
77         SendToOther,
78         RecvWithAddress,
79         RecvFromOther,
80         SendToSelf
81     };
82 
83     /** Number of confirmation recommended for accepting a transaction */
84     static const int RecommendedNumConfirmations = 6;
85 
TransactionRecord()86     TransactionRecord():
87             hash(), time(0), type(Other), address(""), debit(0), credit(0), idx(0)
88     {
89     }
90 
TransactionRecord(uint256 hash,qint64 time)91     TransactionRecord(uint256 hash, qint64 time):
92             hash(hash), time(time), type(Other), address(""), debit(0),
93             credit(0), idx(0)
94     {
95     }
96 
TransactionRecord(uint256 hash,qint64 time,Type type,const std::string & address,const CAmount & debit,const CAmount & credit)97     TransactionRecord(uint256 hash, qint64 time,
98                 Type type, const std::string &address,
99                 const CAmount& debit, const CAmount& credit):
100             hash(hash), time(time), type(type), address(address), debit(debit), credit(credit),
101             idx(0)
102     {
103     }
104 
105     /** Decompose CWallet transaction to model transaction records.
106      */
107     static bool showTransaction(const CWalletTx &wtx);
108     static QList<TransactionRecord> decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx);
109 
110     /** @name Immutable transaction attributes
111       @{*/
112     uint256 hash;
113     qint64 time;
114     Type type;
115     std::string address;
116     CAmount debit;
117     CAmount credit;
118     /**@}*/
119 
120     /** Subtransaction index, for sort key */
121     int idx;
122 
123     /** Status: can change with block chain update */
124     TransactionStatus status;
125 
126     /** Whether the transaction was sent/received with a watch-only address */
127     bool involvesWatchAddress;
128 
129     /** Return the unique identifier for this transaction (part) */
130     QString getTxID() const;
131 
132     /** Return the output index of the subtransaction  */
133     int getOutputIndex() const;
134 
135     /** Update status from core wallet tx.
136      */
137     void updateStatus(const CWalletTx &wtx);
138 
139     /** Return whether a status update is needed.
140      */
141     bool statusUpdateNeeded();
142 };
143 
144 #endif // BITCOIN_QT_TRANSACTIONRECORD_H
145