transactionrecord.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "transactionrecord.h"
  3. #include "wallet.h"
  4. #include "base58.h"
  5. bool TransactionRecord::showTransaction(const CWalletTx &wtx)
  6. {
  7. if (wtx.IsCoinBase())
  8. {
  9. // Ensures we show generated coins / mined transactions at depth 1
  10. if (!wtx.IsInMainChain())
  11. {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
  18. {
  19. QList<TransactionRecord> parts;
  20. int64 nTime = wtx.GetTxTime();
  21. int64 nCredit = wtx.GetCredit(true);
  22. int64 nDebit = wtx.GetDebit();
  23. int64 nNet = nCredit - nDebit;
  24. uint256 hash = wtx.GetHash(), hashPrev = 0;
  25. std::map<std::string, std::string> mapValue = wtx.mapValue;
  26. if (nNet > 0 || wtx.IsCoinBase() || wtx.IsCoinStake())
  27. {
  28. // Credit
  29. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  30. {
  31. if(wallet->IsMine(txout))
  32. {
  33. TransactionRecord sub(hash, nTime);
  34. CTxDestination address;
  35. sub.idx = parts.size(); // sequence number
  36. sub.credit = txout.nValue;
  37. if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
  38. {
  39. // Received by Ecoin Address
  40. sub.type = TransactionRecord::RecvWithAddress;
  41. sub.address = CEcoinAddress(address).ToString();
  42. }
  43. else
  44. {
  45. // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
  46. sub.type = TransactionRecord::RecvFromOther;
  47. sub.address = mapValue["from"];
  48. }
  49. if (wtx.IsCoinBase())
  50. {
  51. // Generated (proof-of-work)
  52. sub.type = TransactionRecord::Generated;
  53. }
  54. if (wtx.IsCoinStake())
  55. {
  56. // Generated (proof-of-stake)
  57. if (hashPrev == hash)
  58. continue; // last coinstake output
  59. sub.type = TransactionRecord::Generated;
  60. sub.credit = nNet > 0 ? nNet : wtx.GetValueOut() - nDebit;
  61. hashPrev = hash;
  62. }
  63. parts.append(sub);
  64. }
  65. }
  66. }
  67. else
  68. {
  69. bool fAllFromMe = true;
  70. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  71. fAllFromMe = fAllFromMe && wallet->IsMine(txin);
  72. bool fAllToMe = true;
  73. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  74. fAllToMe = fAllToMe && wallet->IsMine(txout);
  75. if (fAllFromMe && fAllToMe)
  76. {
  77. // Payment to self
  78. int64 nChange = wtx.GetChange();
  79. parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
  80. -(nDebit - nChange), nCredit - nChange));
  81. }
  82. else if (fAllFromMe)
  83. {
  84. // Debit
  85. int64 nTxFee = nDebit - wtx.GetValueOut();
  86. for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
  87. {
  88. const CTxOut& txout = wtx.vout[nOut];
  89. TransactionRecord sub(hash, nTime);
  90. sub.idx = parts.size();
  91. if(wallet->IsMine(txout))
  92. {
  93. continue;
  94. }
  95. CTxDestination address;
  96. if (ExtractDestination(txout.scriptPubKey, address))
  97. {
  98. // Sent to Ecoin Address
  99. sub.type = TransactionRecord::SendToAddress;
  100. sub.address = CEcoinAddress(address).ToString();
  101. }
  102. else
  103. {
  104. // Sent to IP, or other non-address transaction like OP_EVAL
  105. sub.type = TransactionRecord::SendToOther;
  106. sub.address = mapValue["to"];
  107. }
  108. int64 nValue = txout.nValue;
  109. /* Add fee to first output */
  110. if (nTxFee > 0)
  111. {
  112. nValue += nTxFee;
  113. nTxFee = 0;
  114. }
  115. sub.debit = -nValue;
  116. parts.append(sub);
  117. }
  118. }
  119. else
  120. {
  121. // Mixed debit transaction, can't break down payees
  122. parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
  123. }
  124. }
  125. return parts;
  126. }
  127. void TransactionRecord::updateStatus(const CWalletTx &wtx)
  128. {
  129. // Determine transaction status
  130. CBlockIndex* pindex = NULL;
  131. std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
  132. if (mi != mapBlockIndex.end())
  133. pindex = (*mi).second;
  134. // Sort order, unrecorded transactions sort to the top
  135. status.sortKey = strprintf("%010d-%01d-%010u-%03d",
  136. (pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
  137. (wtx.IsCoinBase() ? 1 : 0),
  138. wtx.nTimeReceived,
  139. idx);
  140. status.confirmed = wtx.IsTrusted();
  141. status.depth = wtx.GetDepthInMainChain();
  142. status.cur_num_blocks = nBestHeight;
  143. if (!wtx.IsFinal())
  144. {
  145. if (wtx.nLockTime < LOCKTIME_THRESHOLD)
  146. {
  147. status.status = TransactionStatus::OpenUntilBlock;
  148. status.open_for = nBestHeight - wtx.nLockTime;
  149. }
  150. else
  151. {
  152. status.status = TransactionStatus::OpenUntilDate;
  153. status.open_for = wtx.nLockTime;
  154. }
  155. }
  156. else
  157. {
  158. if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
  159. {
  160. status.status = TransactionStatus::Offline;
  161. }
  162. else if (status.depth < NumConfirmations)
  163. {
  164. status.status = TransactionStatus::Unconfirmed;
  165. }
  166. else
  167. {
  168. status.status = TransactionStatus::HaveConfirmations;
  169. }
  170. }
  171. // For generated transactions, determine maturity
  172. if(type == TransactionRecord::Generated)
  173. {
  174. int64 nCredit = wtx.GetCredit(true);
  175. if (nCredit == 0)
  176. {
  177. status.maturity = TransactionStatus::Immature;
  178. if (wtx.IsInMainChain())
  179. {
  180. status.matures_in = wtx.GetBlocksToMaturity();
  181. // Check if the block was requested by anyone
  182. if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
  183. status.maturity = TransactionStatus::MaturesWarning;
  184. }
  185. else
  186. {
  187. status.maturity = TransactionStatus::NotAccepted;
  188. }
  189. }
  190. else
  191. {
  192. status.maturity = TransactionStatus::Mature;
  193. }
  194. }
  195. }
  196. bool TransactionRecord::statusUpdateNeeded()
  197. {
  198. return status.cur_num_blocks != nBestHeight;
  199. }
  200. std::string TransactionRecord::getTxID()
  201. {
  202. return hash.ToString() + strprintf("-%03d", idx);
  203. }