You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transactiondesc.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "transactiondesc.h"
  2. #include "guiutil.h"
  3. #include "bitcoinunits.h"
  4. #include "main.h"
  5. #include "wallet.h"
  6. #include "db.h"
  7. #include "ui_interface.h"
  8. #include "base58.h"
  9. QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx)
  10. {
  11. if (!wtx.IsFinal())
  12. {
  13. if (wtx.nLockTime < LOCKTIME_THRESHOLD)
  14. return tr("Open for %n block(s)", "", nBestHeight - wtx.nLockTime);
  15. else
  16. return tr("Open until %1").arg(GUIUtil::dateTimeStr(wtx.nLockTime));
  17. }
  18. else
  19. {
  20. int nDepth = wtx.GetDepthInMainChain();
  21. if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
  22. return tr("%1/offline").arg(nDepth);
  23. else if (nDepth < 6)
  24. return tr("%1/unconfirmed").arg(nDepth);
  25. else
  26. return tr("%1 confirmations").arg(nDepth);
  27. }
  28. }
  29. QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
  30. {
  31. QString strHTML;
  32. {
  33. LOCK(wallet->cs_wallet);
  34. strHTML.reserve(4000);
  35. strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
  36. int64 nTime = wtx.GetTxTime();
  37. int64 nCredit = wtx.GetCredit();
  38. int64 nDebit = wtx.GetDebit();
  39. int64 nNet = nCredit - nDebit;
  40. strHTML += "<b>" + tr("Status") + ":</b> " + FormatTxStatus(wtx);
  41. int nRequests = wtx.GetRequestCount();
  42. if (nRequests != -1)
  43. {
  44. if (nRequests == 0)
  45. strHTML += tr(", has not been successfully broadcast yet");
  46. else if (nRequests > 0)
  47. strHTML += tr(", broadcast through %n node(s)", "", nRequests);
  48. }
  49. strHTML += "<br>";
  50. strHTML += "<b>" + tr("Date") + ":</b> " + (nTime ? GUIUtil::dateTimeStr(nTime) : "") + "<br>";
  51. //
  52. // From
  53. //
  54. if (wtx.IsCoinBase())
  55. {
  56. strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
  57. }
  58. else if (!wtx.mapValue["from"].empty())
  59. {
  60. // Online transaction
  61. if (!wtx.mapValue["from"].empty())
  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(CBitcoinAddress(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. //
  95. // To
  96. //
  97. if (!wtx.mapValue["to"].empty())
  98. {
  99. // Online transaction
  100. std::string strAddress = wtx.mapValue["to"];
  101. strHTML += "<b>" + tr("To") + ":</b> ";
  102. CTxDestination dest = CBitcoinAddress(strAddress).Get();
  103. if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].empty())
  104. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest]) + " ";
  105. strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
  106. }
  107. //
  108. // Amount
  109. //
  110. if (wtx.IsCoinBase() && nCredit == 0)
  111. {
  112. //
  113. // Coinbase
  114. //
  115. int64 nUnmatured = 0;
  116. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  117. nUnmatured += wallet->GetCredit(txout);
  118. strHTML += "<b>" + tr("Credit") + ":</b> ";
  119. if (wtx.IsInMainChain())
  120. strHTML += BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")";
  121. else
  122. strHTML += "(" + tr("not accepted") + ")";
  123. strHTML += "<br>";
  124. }
  125. else if (nNet > 0)
  126. {
  127. //
  128. // Credit
  129. //
  130. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nNet) + "<br>";
  131. }
  132. else
  133. {
  134. bool fAllFromMe = true;
  135. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  136. fAllFromMe = fAllFromMe && wallet->IsMine(txin);
  137. bool fAllToMe = true;
  138. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  139. fAllToMe = fAllToMe && wallet->IsMine(txout);
  140. if (fAllFromMe)
  141. {
  142. //
  143. // Debit
  144. //
  145. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  146. {
  147. if (wallet->IsMine(txout))
  148. continue;
  149. if (wtx.mapValue["to"].empty())
  150. {
  151. // Offline transaction
  152. CTxDestination address;
  153. if (ExtractDestination(txout.scriptPubKey, address))
  154. {
  155. strHTML += "<b>" + tr("To") + ":</b> ";
  156. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
  157. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " ";
  158. strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString());
  159. strHTML += "<br>";
  160. }
  161. }
  162. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -txout.nValue) + "<br>";
  163. }
  164. if (fAllToMe)
  165. {
  166. // Payment to self
  167. int64 nChange = wtx.GetChange();
  168. int64 nValue = nCredit - nChange;
  169. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -nValue) + "<br>";
  170. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nValue) + "<br>";
  171. }
  172. int64 nTxFee = nDebit - wtx.GetValueOut();
  173. if (nTxFee > 0)
  174. strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -nTxFee) + "<br>";
  175. }
  176. else
  177. {
  178. //
  179. // Mixed debit transaction
  180. //
  181. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  182. if (wallet->IsMine(txin))
  183. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -wallet->GetDebit(txin)) + "<br>";
  184. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  185. if (wallet->IsMine(txout))
  186. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, wallet->GetCredit(txout)) + "<br>";
  187. }
  188. }
  189. strHTML += "<b>" + tr("Net amount") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nNet, true) + "<br>";
  190. //
  191. // Message
  192. //
  193. if (!wtx.mapValue["message"].empty())
  194. strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["message"], true) + "<br>";
  195. if (!wtx.mapValue["comment"].empty())
  196. strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["comment"], true) + "<br>";
  197. strHTML += "<b>" + tr("Transaction ID") + ":</b> " + wtx.GetHash().ToString().c_str() + "<br>";
  198. if (wtx.IsCoinBase())
  199. strHTML += "<br>" + tr("Generated coins must mature 120 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>";
  200. //
  201. // Debug view
  202. //
  203. if (fDebug)
  204. {
  205. strHTML += "<hr><br>" + tr("Debug information") + "<br><br>";
  206. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  207. if(wallet->IsMine(txin))
  208. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -wallet->GetDebit(txin)) + "<br>";
  209. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  210. if(wallet->IsMine(txout))
  211. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, wallet->GetCredit(txout)) + "<br>";
  212. strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
  213. strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
  214. CTxDB txdb("r"); // To fetch source txouts
  215. strHTML += "<br><b>" + tr("Inputs") + ":</b>";
  216. strHTML += "<ul>";
  217. {
  218. LOCK(wallet->cs_wallet);
  219. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  220. {
  221. COutPoint prevout = txin.prevout;
  222. CTransaction prev;
  223. if(txdb.ReadDiskTx(prevout.hash, prev))
  224. {
  225. if (prevout.n < prev.vout.size())
  226. {
  227. strHTML += "<li>";
  228. const CTxOut &vout = prev.vout[prevout.n];
  229. CTxDestination address;
  230. if (ExtractDestination(vout.scriptPubKey, address))
  231. {
  232. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
  233. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " ";
  234. strHTML += QString::fromStdString(CBitcoinAddress(address).ToString());
  235. }
  236. strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, vout.nValue);
  237. strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) ? tr("true") : tr("false")) + "</li>";
  238. }
  239. }
  240. }
  241. }
  242. strHTML += "</ul>";
  243. }
  244. strHTML += "</font></html>";
  245. }
  246. return strHTML;
  247. }