2012-03-18 23:14:03 +01:00
# include "transactiondesc.h"
2011-06-10 15:05:51 +02:00
# include "guiutil.h"
2011-08-08 17:38:17 +02:00
# include "bitcoinunits.h"
2011-07-03 20:53:56 +02:00
2012-04-15 22:10:54 +02:00
# include "main.h"
# include "wallet.h"
# include "db.h"
2012-04-04 13:19:30 +02:00
# include "ui_interface.h"
2012-05-14 23:44:52 +02:00
# include "base58.h"
2011-06-10 15:05:51 +02:00
# include <QString>
using namespace std ;
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : FormatTxStatus ( const CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
if ( ! wtx . IsFinal ( ) )
{
2011-08-08 17:38:17 +02:00
if ( wtx . nLockTime < LOCKTIME_THRESHOLD )
return tr ( " Open for %1 blocks " ) . arg ( nBestHeight - wtx . nLockTime ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " Open until %1 " ) . arg ( GUIUtil : : dateTimeStr ( wtx . nLockTime ) ) ;
2011-06-10 15:05:51 +02:00
}
else
{
int nDepth = wtx . GetDepthInMainChain ( ) ;
if ( GetAdjustedTime ( ) - wtx . nTimeReceived > 2 * 60 & & wtx . GetRequestCount ( ) = = 0 )
2011-08-08 17:38:17 +02:00
return tr ( " %1/offline? " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
else if ( nDepth < 6 )
2011-08-08 17:38:17 +02:00
return tr ( " %1/unconfirmed " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " %1 confirmations " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
}
}
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : toHTML ( CWallet * wallet , CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
QString strHTML ;
2012-04-06 18:39:12 +02:00
2011-06-10 15:05:51 +02:00
{
2012-04-06 18:39:12 +02:00
LOCK ( wallet - > cs_wallet ) ;
2011-06-10 15:05:51 +02:00
strHTML . reserve ( 4000 ) ;
strHTML + = " <html><font face='verdana, arial, helvetica, sans-serif'> " ;
2011-12-21 22:33:19 +01:00
int64 nTime = wtx . GetTxTime ( ) ;
int64 nCredit = wtx . GetCredit ( ) ;
int64 nDebit = wtx . GetDebit ( ) ;
int64 nNet = nCredit - nDebit ;
2011-06-10 15:05:51 +02:00
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Status:</b> " ) + FormatTxStatus ( wtx ) ;
2011-06-10 15:05:51 +02:00
int nRequests = wtx . GetRequestCount ( ) ;
if ( nRequests ! = - 1 )
{
if ( nRequests = = 0 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , has not been successfully broadcast yet " ) ;
2011-06-10 15:05:51 +02:00
else if ( nRequests = = 1 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , broadcast through %1 node " ) . arg ( nRequests ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , broadcast through %1 nodes " ) . arg ( nRequests ) ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " <br> " ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Date:</b> " ) + ( nTime ? GUIUtil : : dateTimeStr ( nTime ) : QString ( " " ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// From
//
if ( wtx . IsCoinBase ( ) )
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Source:</b> Generated<br> " ) ;
2011-06-10 15:05:51 +02:00
}
else if ( ! wtx . mapValue [ " from " ] . empty ( ) )
{
// Online transaction
if ( ! wtx . mapValue [ " from " ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = tr ( " <b>From:</b> " ) + GUIUtil : : HtmlEscape ( wtx . mapValue [ " from " ] ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
// Offline transaction
if ( nNet > 0 )
{
// Credit
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
{
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) & & IsMine ( * wallet , address ) )
2011-06-10 15:05:51 +02:00
{
2011-07-26 15:38:31 +02:00
if ( wallet - > mapAddressBook . count ( address ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>From:</b> " ) + tr ( " unknown " ) + " <br> " ;
strHTML + = tr ( " <b>To:</b> " ) ;
2012-05-14 23:44:52 +02:00
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-07-26 15:38:31 +02:00
if ( ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = tr ( " (yours, label: " ) + GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (yours) " ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
break ;
}
}
}
}
//
// To
//
string strAddress ;
if ( ! wtx . mapValue [ " to " ] . empty ( ) )
{
// Online transaction
strAddress = wtx . mapValue [ " to " ] ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>To:</b> " ) ;
2012-05-14 23:44:52 +02:00
CTxDestination dest = CBitcoinAddress ( strAddress ) . Get ( ) ;
if ( wallet - > mapAddressBook . count ( dest ) & & ! wallet - > mapAddressBook [ dest ] . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ dest ] ) + " " ;
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( strAddress ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
//
// Amount
//
if ( wtx . IsCoinBase ( ) & & nCredit = = 0 )
{
//
// Coinbase
//
2011-12-21 22:33:19 +01:00
int64 nUnmatured = 0 ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
nUnmatured + = wallet - > GetCredit ( txout ) ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsInMainChain ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (%1 matures in %2 more blocks) " )
. arg ( BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nUnmatured ) )
. arg ( wtx . GetBlocksToMaturity ( ) ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (not accepted) " ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
else if ( nNet > 0 )
{
//
// Credit
//
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nNet ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
bool fAllFromMe = true ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
fAllFromMe = fAllFromMe & & wallet - > IsMine ( txin ) ;
2011-06-10 15:05:51 +02:00
bool fAllToMe = true ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
fAllToMe = fAllToMe & & wallet - > IsMine ( txout ) ;
2011-06-10 15:05:51 +02:00
if ( fAllFromMe )
{
//
// Debit
//
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
continue ;
if ( wtx . mapValue [ " to " ] . empty ( ) )
{
// Offline transaction
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>To:</b> " ) ;
2011-07-26 15:38:31 +02:00
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " " ;
2012-05-14 23:44:52 +02:00
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - txout . nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
if ( fAllToMe )
{
// Payment to self
2011-12-21 22:33:19 +01:00
int64 nChange = wtx . GetChange ( ) ;
int64 nValue = nCredit - nChange ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - nValue ) + " <br> " ;
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2011-12-21 22:33:19 +01:00
int64 nTxFee = nDebit - wtx . GetValueOut ( ) ;
2011-06-10 15:05:51 +02:00
if ( nTxFee > 0 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Transaction fee:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - nTxFee ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
//
// Mixed debit transaction
//
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Net amount:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nNet , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Message
//
if ( ! wtx . mapValue [ " message " ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = QString ( " <br><b> " ) + tr ( " Message: " ) + " </b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " message " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
if ( ! wtx . mapValue [ " comment " ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = QString ( " <br><b> " ) + tr ( " Comment: " ) + " </b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " comment " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2012-01-10 19:25:02 +01:00
strHTML + = QString ( " <b> " ) + tr ( " Transaction ID: " ) + " </b> " + wtx . GetHash ( ) . ToString ( ) . c_str ( ) + " <br> " ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsCoinBase ( ) )
2012-06-03 13:03:55 +02:00
strHTML + = QString ( " <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, it's 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> " ;
2011-06-10 15:05:51 +02:00
//
// Debug view
//
if ( fDebug )
{
2011-07-29 23:11:40 +02:00
strHTML + = " <hr><br>Debug information<br><br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2011-08-08 17:38:17 +02:00
strHTML + = " <b>Debit:</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-08-08 17:38:17 +02:00
strHTML + = " <b>Credit:</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br><b>Transaction:</b><br> " ;
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( wtx . ToString ( ) , true ) ;
2011-06-10 15:05:51 +02:00
2011-07-29 23:11:40 +02:00
CTxDB txdb ( " r " ) ; // To fetch source txouts
strHTML + = " <br><b>Inputs:</b> " ;
strHTML + = " <ul> " ;
2012-04-06 18:39:12 +02:00
2011-06-10 15:05:51 +02:00
{
2012-04-06 18:39:12 +02:00
LOCK ( wallet - > cs_wallet ) ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
{
COutPoint prevout = txin . prevout ;
2011-07-29 23:11:40 +02:00
CTransaction prev ;
if ( txdb . ReadDiskTx ( prevout . hash , prev ) )
2011-06-10 15:05:51 +02:00
{
if ( prevout . n < prev . vout . size ( ) )
{
2011-07-29 23:11:40 +02:00
strHTML + = " <li> " ;
const CTxOut & vout = prev . vout [ prevout . n ] ;
2012-05-14 23:44:52 +02:00
CTxDestination address ;
if ( ExtractDestination ( vout . scriptPubKey , address ) )
2011-07-29 23:11:40 +02:00
{
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-11-11 10:13:25 +01:00
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " " ;
2012-05-14 23:44:52 +02:00
strHTML + = QString : : fromStdString ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-07-29 23:11:40 +02:00
}
2011-08-08 17:38:17 +02:00
strHTML = strHTML + " Amount= " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , vout . nValue ) ;
2011-07-29 23:11:40 +02:00
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) ? " true " : " false " ) + " </li> " ;
2011-06-10 15:05:51 +02:00
}
}
}
}
2011-07-29 23:11:40 +02:00
strHTML + = " </ul> " ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " </font></html> " ;
}
return strHTML ;
}