Bitcoin ABC  0.26.3
P2P Digital Currency
transactionrecord.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 #include <qt/transactionrecord.h>
6 
7 #include <cashaddrenc.h>
8 #include <chain.h> // For MAX_BLOCK_TIME_GAP
9 #include <chainparams.h> // For Params()
10 #include <interfaces/wallet.h>
11 #include <key_io.h>
12 #include <wallet/ismine.h>
13 
14 #include <QDateTime>
15 
16 #include <cstdint>
17 
22  // There are currently no cases where we hide transactions, but we may want
23  // to use this in the future for things like RBF.
24  return true;
25 }
26 
30 QList<TransactionRecord>
32  QList<TransactionRecord> parts;
33  int64_t nTime = wtx.time;
34  Amount nCredit = wtx.credit;
35  Amount nDebit = wtx.debit;
36  Amount nNet = nCredit - nDebit;
37  const TxId &txid = wtx.tx->GetId();
38  std::map<std::string, std::string> mapValue = wtx.value_map;
39 
40  if (nNet > Amount::zero() || wtx.is_coinbase) {
41  //
42  // Credit
43  //
44  for (size_t i = 0; i < wtx.tx->vout.size(); i++) {
45  const CTxOut &txout = wtx.tx->vout[i];
46  isminetype mine = wtx.txout_is_mine[i];
47  if (mine) {
48  TransactionRecord sub(txid, nTime);
49  sub.idx = i; // vout index
50  sub.credit = txout.nValue;
52  if (wtx.txout_address_is_mine[i]) {
53  // Received by Bitcoin Address
55  sub.address =
57  } else {
58  // Received by IP connection (deprecated features), or a
59  // multisignature or other non-simple transaction
61  sub.address = mapValue["from"];
62  }
63  if (wtx.is_coinbase) {
64  // Generated
66  }
67 
68  parts.append(sub);
69  }
70  }
71  } else {
72  bool involvesWatchAddress = false;
73  isminetype fAllFromMe = ISMINE_SPENDABLE;
74  for (const isminetype mine : wtx.txin_is_mine) {
75  if (mine & ISMINE_WATCH_ONLY) {
76  involvesWatchAddress = true;
77  }
78  if (fAllFromMe > mine) {
79  fAllFromMe = mine;
80  }
81  }
82 
83  isminetype fAllToMe = ISMINE_SPENDABLE;
84  for (const isminetype mine : wtx.txout_is_mine) {
85  if (mine & ISMINE_WATCH_ONLY) {
86  involvesWatchAddress = true;
87  }
88  if (fAllToMe > mine) {
89  fAllToMe = mine;
90  }
91  }
92 
93  if (fAllFromMe && fAllToMe) {
94  // Payment to self
95  std::string address;
96  for (auto it = wtx.txout_address.begin();
97  it != wtx.txout_address.end(); ++it) {
98  if (it != wtx.txout_address.begin()) {
99  address += ", ";
100  }
101  address += EncodeCashAddr(*it, Params());
102  }
103  Amount nChange = wtx.change;
104  parts.append(TransactionRecord(
106  -(nDebit - nChange), nCredit - nChange));
107 
108  // maybe pass to TransactionRecord as constructor argument
109  parts.last().involvesWatchAddress = involvesWatchAddress;
110  } else if (fAllFromMe) {
111  //
112  // Debit
113  //
114  Amount nTxFee = nDebit - wtx.tx->GetValueOut();
115 
116  for (size_t nOut = 0; nOut < wtx.tx->vout.size(); nOut++) {
117  const CTxOut &txout = wtx.tx->vout[nOut];
118  TransactionRecord sub(txid, nTime);
119  sub.idx = nOut;
121 
122  if (wtx.txout_is_mine[nOut]) {
123  // Ignore parts sent to self, as this is usually the change
124  // from a transaction sent back to our own address.
125  continue;
126  }
127 
128  if (!boost::get<CNoDestination>(&wtx.txout_address[nOut])) {
129  // Sent to Bitcoin Address
131  sub.address =
132  EncodeCashAddr(wtx.txout_address[nOut], Params());
133  } else {
134  // Sent to IP, or other non-address transaction like OP_EVAL
136  sub.address = mapValue["to"];
137  }
138 
139  Amount nValue = txout.nValue;
140  /* Add fee to first output */
141  if (nTxFee > Amount::zero()) {
142  nValue += nTxFee;
143  nTxFee = Amount::zero();
144  }
145  sub.debit = -1 * nValue;
146 
147  parts.append(sub);
148  }
149  } else {
150  //
151  // Mixed debit transaction, can't break down payees
152  //
153  parts.append(TransactionRecord(txid, nTime,
154  TransactionRecord::Other, "", nNet,
155  Amount::zero()));
156  parts.last().involvesWatchAddress = involvesWatchAddress;
157  }
158  }
159 
160  return parts;
161 }
162 
164  const BlockHash &block_hash, int numBlocks,
165  int64_t block_time) {
166  // Determine transaction status
167 
168  // Sort order, unrecorded transactions sort to the top
169  status.sortKey = strprintf("%010d-%01d-%010u-%03d", wtx.block_height,
170  wtx.is_coinbase ? 1 : 0, wtx.time_received, idx);
173  status.m_cur_block_hash = block_hash;
174 
176  // For generated transactions, determine maturity
177  if (wtx.blocks_to_maturity > 0) {
179 
180  if (wtx.is_in_main_chain) {
182  } else {
184  }
185  } else {
187  }
188  } else {
189  if (status.depth < 0) {
191  } else if (status.depth == 0) {
193  if (wtx.is_abandoned) {
195  }
198  } else {
200  }
201  }
202 }
203 
204 bool TransactionRecord::statusUpdateNeeded(const BlockHash &block_hash) const {
205  assert(!block_hash.IsNull());
206  return status.m_cur_block_hash != block_hash;
207 }
208 
209 QString TransactionRecord::getTxID() const {
210  return QString::fromStdString(txid.ToString());
211 }
212 
214  return idx;
215 }
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams &params)
Definition: cashaddrenc.cpp:91
const CChainParams & Params()
Return the currently selected parameters.
An output of a transaction.
Definition: transaction.h:130
Amount nValue
Definition: transaction.h:132
UI model for a transaction.
int idx
Subtransaction index, for sort key.
static QList< TransactionRecord > decomposeTransaction(const interfaces::WalletTx &wtx)
Decompose CWallet transaction to model transaction records.
static const int RecommendedNumConfirmations
Number of confirmation recommended for accepting a transaction.
static bool showTransaction()
Decompose CWallet transaction to model transaction records.
TransactionStatus status
Status: can change with block chain update.
int getOutputIndex() const
Return the output index of the subtransaction
void updateStatus(const interfaces::WalletTxStatus &wtx, const BlockHash &block_hash, int numBlocks, int64_t block_time)
Update status from core wallet tx.
bool statusUpdateNeeded(const BlockHash &block_hash) const
Return whether a status update is needed.
QString getTxID() const
Return the unique identifier for this transaction (part)
bool involvesWatchAddress
Whether the transaction was sent/received with a watch-only address.
bool countsForBalance
Transaction counts towards available balance.
BlockHash m_cur_block_hash
Current block hash (to know whether cached status is still valid)
@ Confirmed
Have 6 or more confirmations (normal tx) or fully mature (mined tx)
@ Unconfirmed
Normal (sent/received) transactions.
@ Immature
Generated (mined) transactions.
@ Confirming
Confirmed, but waiting for the recommended number of confirmations.
@ NotAccepted
Mined but not accepted.
@ Conflicted
Conflicts with other transaction or mempool.
@ Abandoned
Abandoned from the wallet.
std::string sortKey
Sorting key based on status.
std::string ToString() const
Definition: uint256.h:78
bool IsNull() const
Definition: uint256.h:30
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_SPENDABLE
Definition: ismine.h:21
@ ISMINE_WATCH_ONLY
Definition: ismine.h:20
Definition: amount.h:19
static constexpr Amount zero()
Definition: amount.h:42
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
A TxId is the identifier of a transaction.
Definition: txid.h:14
std::vector< CTxDestination > txout_address
Definition: wallet.h:376
std::vector< isminetype > txout_is_mine
Definition: wallet.h:375
CTransactionRef tx
Definition: wallet.h:373
std::map< std::string, std::string > value_map
Definition: wallet.h:382
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:377
std::vector< isminetype > txin_is_mine
Definition: wallet.h:374
Updated transaction status.
Definition: wallet.h:387
unsigned int time_received
Definition: wallet.h:391
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
assert(!tx.IsCoinBase())