transactiondesc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // ECOin - Copyright (c) - 2014/2024 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "transactiondesc.h"
  3. #include "guiutil.h"
  4. #include "ecoinunits.h"
  5. #include "main.h"
  6. #include "wallet.h"
  7. #include "txdb.h"
  8. #include "ui_interface.h"
  9. #include "base58.h"
  10. #include <QString>
  11. #include <QUrl>
  12. QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx)
  13. {
  14. if (!wtx.IsFinal())
  15. {
  16. if (wtx.nLockTime < LOCKTIME_THRESHOLD)
  17. return tr("Open for %n block(s)", "", nBestHeight - wtx.nLockTime);
  18. else
  19. return tr("Open until %1").arg(GUIUtil::dateTimeStr(wtx.nLockTime));
  20. }
  21. else
  22. {
  23. int nDepth = wtx.GetDepthInMainChain();
  24. if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
  25. return tr("%1/offline").arg(nDepth);
  26. else if (nDepth < 6)
  27. return tr("%1/unconfirmed").arg(nDepth);
  28. else
  29. return tr("%1 confirmations").arg(nDepth);
  30. }
  31. }
  32. QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
  33. {
  34. QString strHTML;
  35. {
  36. LOCK(wallet->cs_wallet);
  37. strHTML.reserve(4000);
  38. strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
  39. int64 nTime = wtx.GetTxTime();
  40. int64 nCredit = wtx.GetCredit();
  41. int64 nDebit = wtx.GetDebit();
  42. int64 nNet = nCredit - nDebit;
  43. strHTML += "<b>" + tr("Status") + ":</b> " + FormatTxStatus(wtx);
  44. int nRequests = wtx.GetRequestCount();
  45. if (nRequests != -1)
  46. {
  47. if (nRequests == 0)
  48. strHTML += tr(", has not been successfully broadcast yet");
  49. else if (nRequests > 0)
  50. strHTML += tr(", broadcast through %n node(s)", "", nRequests);
  51. }
  52. strHTML += "<br>";
  53. strHTML += "<b>" + tr("Date") + ":</b> " + (nTime ? GUIUtil::dateTimeStr(nTime) : "") + "<br>";
  54. // From
  55. if (wtx.IsCoinBase())
  56. {
  57. strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
  58. }
  59. else if (wtx.mapValue.count("from") && !wtx.mapValue["from"].empty())
  60. {
  61. // Online transaction
  62. strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(wtx.mapValue["from"]) + "<br>";
  63. }
  64. else
  65. {
  66. // Offline transaction
  67. if (nNet > 0)
  68. {
  69. // Credit
  70. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  71. {
  72. if (wallet->IsMine(txout))
  73. {
  74. CTxDestination address;
  75. if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
  76. {
  77. if (wallet->mapAddressBook.count(address))
  78. {
  79. strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>";
  80. strHTML += "<b>" + tr("To") + ":</b> ";
  81. strHTML += GUIUtil::HtmlEscape(CEcoinAddress(address).ToString());
  82. if (!wallet->mapAddressBook[address].empty())
  83. strHTML += " (" + tr("own address") + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + ")";
  84. else
  85. strHTML += " (" + tr("own address") + ")";
  86. strHTML += "<br>";
  87. }
  88. }
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. // To
  95. if (wtx.mapValue.count("to") && !wtx.mapValue["to"].empty())
  96. {
  97. // Online transaction
  98. std::string strAddress = wtx.mapValue["to"];
  99. strHTML += "<b>" + tr("To") + ":</b> ";
  100. CTxDestination dest = CEcoinAddress(strAddress).Get();
  101. if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].empty())
  102. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest]) + " ";
  103. strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
  104. }
  105. // Amount
  106. if (wtx.IsCoinBase() && nCredit == 0)
  107. {
  108. // Coinbase
  109. int64 nUnmatured = 0;
  110. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  111. nUnmatured += wallet->GetCredit(txout);
  112. strHTML += "<b>" + tr("Credit") + ":</b> ";
  113. if (wtx.IsInMainChain())
  114. strHTML += EcoinUnits::formatWithUnit(EcoinUnits::ECO, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")";
  115. else
  116. strHTML += "(" + tr("not accepted") + ")";
  117. strHTML += "<br>";
  118. }
  119. else if (nNet > 0)
  120. {
  121. // Credit
  122. strHTML += "<b>" + tr("Credit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, nNet) + "<br>";
  123. }
  124. else
  125. {
  126. bool fAllFromMe = true;
  127. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  128. fAllFromMe = fAllFromMe && wallet->IsMine(txin);
  129. bool fAllToMe = true;
  130. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  131. fAllToMe = fAllToMe && wallet->IsMine(txout);
  132. if (fAllFromMe)
  133. {
  134. // Debit
  135. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  136. {
  137. if (wallet->IsMine(txout))
  138. continue;
  139. if (!wtx.mapValue.count("to") || wtx.mapValue["to"].empty())
  140. {
  141. // Offline transaction
  142. CTxDestination address;
  143. if (ExtractDestination(txout.scriptPubKey, address))
  144. {
  145. strHTML += "<b>" + tr("To") + ":</b> ";
  146. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
  147. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " ";
  148. strHTML += GUIUtil::HtmlEscape(CEcoinAddress(address).ToString());
  149. strHTML += "<br>";
  150. }
  151. }
  152. strHTML += "<b>" + tr("Debit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, -txout.nValue) + "<br>";
  153. }
  154. if (fAllToMe)
  155. {
  156. // Payment to self
  157. int64 nChange = wtx.GetChange();
  158. int64 nValue = nCredit - nChange;
  159. strHTML += "<b>" + tr("Debit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, -nValue) + "<br>";
  160. strHTML += "<b>" + tr("Credit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, nValue) + "<br>";
  161. }
  162. int64 nTxFee = nDebit - wtx.GetValueOut();
  163. if (nTxFee > 0)
  164. strHTML += "<b>" + tr("Transaction fee") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, -nTxFee) + "<br>";
  165. }
  166. else
  167. {
  168. // Mixed debit transaction
  169. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  170. if (wallet->IsMine(txin))
  171. strHTML += "<b>" + tr("Debit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, -wallet->GetDebit(txin)) + "<br>";
  172. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  173. if (wallet->IsMine(txout))
  174. strHTML += "<b>" + tr("Credit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, wallet->GetCredit(txout)) + "<br>";
  175. }
  176. }
  177. strHTML += "<b>" + tr("Net amount") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, nNet, true) + "<br>";
  178. // Message
  179. if (wtx.mapValue.count("message") && !wtx.mapValue["message"].empty())
  180. strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["message"], true) + "<br>";
  181. if (wtx.mapValue.count("comment") && !wtx.mapValue["comment"].empty())
  182. strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["comment"], true) + "<br>";
  183. strHTML += "<b>" + tr("Transaction ID") + ":</b> " + wtx.GetHash().ToString().c_str() + "<br>";
  184. if (wtx.IsCoinBase() || wtx.IsCoinStake())
  185. strHTML += "<br>" + tr("Generated coins must mature 520 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.") + "<br>";
  186. // Debug view
  187. if (fDebug)
  188. {
  189. strHTML += "<hr><br>" + tr("Debug information") + "<br><br>";
  190. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  191. if(wallet->IsMine(txin))
  192. strHTML += "<b>" + tr("Debit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, -wallet->GetDebit(txin)) + "<br>";
  193. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  194. if(wallet->IsMine(txout))
  195. strHTML += "<b>" + tr("Credit") + ":</b> " + EcoinUnits::formatWithUnit(EcoinUnits::ECO, wallet->GetCredit(txout)) + "<br>";
  196. strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
  197. strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
  198. CTxDB txdb("r"); // To fetch source txouts
  199. strHTML += "<br><b>" + tr("Inputs") + ":</b>";
  200. strHTML += "<ul>";
  201. {
  202. LOCK(wallet->cs_wallet);
  203. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  204. {
  205. COutPoint prevout = txin.prevout;
  206. CTransaction prev;
  207. if(txdb.ReadDiskTx(prevout.hash, prev))
  208. {
  209. if (prevout.n < prev.vout.size())
  210. {
  211. strHTML += "<li>";
  212. const CTxOut &vout = prev.vout[prevout.n];
  213. CTxDestination address;
  214. if (ExtractDestination(vout.scriptPubKey, address))
  215. {
  216. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
  217. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " ";
  218. strHTML += QString::fromStdString(CEcoinAddress(address).ToString());
  219. }
  220. strHTML = strHTML + " " + tr("Amount") + "=" + EcoinUnits::formatWithUnit(EcoinUnits::ECO, vout.nValue);
  221. strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) ? tr("true") : tr("false")) + "</li>";
  222. }
  223. }
  224. }
  225. }
  226. strHTML += "</ul>";
  227. }
  228. strHTML += "</font></html>";
  229. }
  230. return strHTML;
  231. }