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
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 )
2012-07-03 14:37:51 +02:00
return tr ( " Open for %n block(s) " , " " , 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 )
2012-07-03 14:37:51 +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
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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 " ) ;
2012-07-03 14:37:51 +02:00
else if ( nRequests > 0 )
strHTML + = tr ( " , broadcast through %n node(s) " , " " , nRequests ) ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " <br> " ;
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Date " ) + " :</b> " + ( nTime ? GUIUtil : : dateTimeStr ( nTime ) : " " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// From
//
if ( wtx . IsCoinBase ( ) )
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Source " ) + " :</b> " + tr ( " Generated " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else if ( ! wtx . mapValue [ " from " ] . empty ( ) )
{
// Online transaction
if ( ! wtx . mapValue [ " from " ] . empty ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + tr ( " unknown " ) + " <br> " ;
strHTML + = " <b> " + tr ( " 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 ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " ( " + tr ( " own address " ) + " , " + tr ( " label " ) + " : " + GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2012-07-03 14:37:51 +02:00
strHTML + = " ( " + tr ( " own address " ) + " ) " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
break ;
}
}
}
}
//
// To
//
if ( ! wtx . mapValue [ " to " ] . empty ( ) )
{
// Online transaction
2012-07-03 14:37:51 +02:00
std : : string strAddress = wtx . mapValue [ " to " ] ;
strHTML + = " <b> " + tr ( " 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 ) ;
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsInMainChain ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nUnmatured ) + " ( " + tr ( " matures in %n more block(s) " , " " , wtx . GetBlocksToMaturity ( ) ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2012-07-03 14:37:51 +02:00
strHTML + = " ( " + tr ( " not accepted " ) + " ) " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
else if ( nNet > 0 )
{
//
// Credit
//
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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
{
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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> " ;
}
}
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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 ;
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - nValue ) + " <br> " ;
strHTML + = " <b> " + tr ( " 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 )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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 ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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 ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Net amount " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nNet , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Message
//
if ( ! wtx . mapValue [ " message " ] . empty ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <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 ( ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Comment " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " comment " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Transaction ID " ) + " :</b> " + wtx . GetHash ( ) . ToString ( ) . c_str ( ) + " <br> " ;
2012-01-10 19:25:02 +01:00
2011-06-10 15:05:51 +02:00
if ( wtx . IsCoinBase ( ) )
2012-07-03 14:37:51 +02:00
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> " ;
2011-06-10 15:05:51 +02:00
//
// Debug view
//
if ( fDebug )
{
2012-07-03 14:37:51 +02:00
strHTML + = " <hr><br> " + tr ( " 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 ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " 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 ) )
2012-07-03 14:37:51 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " 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
2012-07-03 14:37:51 +02:00
strHTML + = " <br><b> " + tr ( " Inputs " ) + " :</b> " ;
2011-07-29 23:11:40 +02:00
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
}
2012-07-03 14:37:51 +02:00
strHTML = strHTML + " " + tr ( " Amount " ) + " = " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , vout . nValue ) ;
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) ? tr ( " true " ) : tr ( " false " ) ) + " </li> " ;
2011-06-10 15:05:51 +02:00
}
}
}
}
2012-07-03 14:37: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 ;
}