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