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