2020-10-07 10:36:04 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2020-12-26 19:56:06 +00:00
// Copyright (c) 2020-2021, The Monero Project.
2020-10-07 10:36:04 +00:00
2021-06-27 12:22:54 +00:00
# include "TxInfoDialog.h"
# include "ui_TxInfoDialog.h"
2020-10-07 10:36:04 +00:00
2020-11-08 11:08:15 +00:00
# include <QMessageBox>
2021-03-14 21:12:02 +00:00
# include <QScrollBar>
2020-10-07 10:36:04 +00:00
2021-06-27 14:33:18 +00:00
# include "appcontext.h"
# include "config.h"
2021-07-06 13:31:08 +00:00
# include "libwalletqt/Coins.h"
# include "libwalletqt/CoinsInfo.h"
2021-06-27 14:33:18 +00:00
# include "libwalletqt/TransactionHistory.h"
# include "libwalletqt/Transfer.h"
# include "libwalletqt/WalletManager.h"
# include "model/ModelUtils.h"
2021-07-06 19:36:27 +00:00
# include "Utils.h"
2021-06-27 14:33:18 +00:00
2021-06-27 12:22:54 +00:00
TxInfoDialog : : TxInfoDialog ( QSharedPointer < AppContext > ctx , TransactionInfo * txInfo , QWidget * parent )
2021-05-18 15:59:18 +00:00
: QDialog ( parent )
2021-06-27 12:22:54 +00:00
, ui ( new Ui : : TxInfoDialog )
2021-05-18 15:59:18 +00:00
, m_ctx ( std : : move ( ctx ) )
, m_txInfo ( txInfo )
2021-07-02 17:35:21 +00:00
, m_txProofDialog ( new TxProofDialog ( this , m_ctx , txInfo ) )
2020-10-07 10:36:04 +00:00
{
ui - > setupUi ( this ) ;
2021-03-08 20:03:20 +00:00
m_txid = txInfo - > hash ( ) ;
ui - > label_txid - > setText ( m_txid ) ;
2021-10-12 12:58:54 +00:00
connect ( ui - > btn_copyTxID , & QPushButton : : clicked , this , & TxInfoDialog : : copyTxID ) ;
connect ( ui - > btn_CopyTxKey , & QPushButton : : clicked , this , & TxInfoDialog : : copyTxKey ) ;
connect ( ui - > btn_createTxProof , & QPushButton : : clicked , this , & TxInfoDialog : : createTxProof ) ;
2020-10-07 10:36:04 +00:00
2021-07-03 01:29:13 +00:00
connect ( m_ctx - > wallet , & Wallet : : newBlock , this , & TxInfoDialog : : updateData ) ;
2020-12-14 19:29:58 +00:00
2021-03-14 21:12:02 +00:00
this - > setData ( txInfo ) ;
2020-10-07 10:36:04 +00:00
2021-07-04 21:17:10 +00:00
if ( ( txInfo - > isFailed ( ) | | txInfo - > isPending ( ) ) & & txInfo - > direction ( ) ! = TransactionInfo : : Direction_In ) {
2021-03-14 21:12:02 +00:00
connect ( ui - > btn_rebroadcastTx , & QPushButton : : pressed , [ this ] {
emit resendTranscation ( m_txid ) ;
} ) ;
} else {
ui - > btn_rebroadcastTx - > hide ( ) ;
}
2021-07-10 20:05:04 +00:00
//
// if (txInfo->direction() == TransactionInfo::Direction_Out) {
// // TODO: this will not properly represent coinjoin-like transactions.
// QVector<CoinsInfo*> coins = m_ctx->wallet->coins()->coins_from_txid(m_txid);
// QTextCursor c_i = ui->inputs->textCursor();
// QString inputs_str;
// for (const auto &coin : coins) {
// inputs_str += QString("%1 %2\n").arg(coin->pubKey(), coin->displayAmount());
// }
// ui->inputs->setText(inputs_str);
// ui->label_inputs->setText(QString("Inputs (%1)").arg(QString::number(coins.size())));
// this->adjustHeight(ui->inputs, coins.size());
// } else {
ui - > frameInputs - > hide ( ) ;
// }
2021-03-14 21:12:02 +00:00
2021-07-06 13:31:08 +00:00
QTextCursor cursor = ui - > outputs - > textCursor ( ) ;
auto transfers = txInfo - > transfers ( ) ;
if ( ! transfers . isEmpty ( ) ) {
for ( const auto & transfer : transfers ) {
auto address = transfer - > address ( ) ;
auto amount = WalletManager : : displayAmount ( transfer - > amount ( ) ) ;
auto index = m_ctx - > wallet - > subaddressIndex ( address ) ;
cursor . insertText ( address , Utils : : addressTextFormat ( index , transfer - > amount ( ) ) ) ;
cursor . insertText ( QString ( " %1 " ) . arg ( amount ) , QTextCharFormat ( ) ) ;
cursor . insertBlock ( ) ;
}
ui - > label_outputs - > setText ( QString ( " Destinations (%2) " ) . arg ( QString : : number ( transfers . size ( ) ) ) ) ;
this - > adjustHeight ( ui - > outputs , transfers . size ( ) ) ;
} else {
ui - > frameOutputs - > hide ( ) ;
2020-10-07 10:36:04 +00:00
}
2021-07-06 13:31:08 +00:00
this - > adjustSize ( ) ;
}
void TxInfoDialog : : adjustHeight ( QTextEdit * textEdit , qreal docHeight ) {
2021-03-14 21:12:02 +00:00
QCoreApplication : : processEvents ( ) ;
2021-07-06 13:31:08 +00:00
qreal lineHeight = QFontMetrics ( ui - > outputs - > document ( ) - > defaultFont ( ) ) . height ( ) ;
2021-03-14 21:12:02 +00:00
int h = int ( docHeight * ( lineHeight + 2 ) + 11 ) ;
h = qMin ( qMax ( h , 100 ) , 600 ) ;
2021-07-06 13:31:08 +00:00
textEdit - > setMinimumHeight ( h ) ;
textEdit - > setMaximumHeight ( h ) ;
textEdit - > verticalScrollBar ( ) - > hide ( ) ;
2020-10-07 10:36:04 +00:00
}
2021-06-27 14:35:49 +00:00
void TxInfoDialog : : setData ( TransactionInfo * tx ) {
2021-03-14 21:12:02 +00:00
QString blockHeight = QString : : number ( tx - > blockHeight ( ) ) ;
if ( tx - > isFailed ( ) ) {
ui - > label_status - > setText ( " Status: Failed (node was unable to relay transaction) " ) ;
}
if ( blockHeight = = " 0 " ) {
ui - > label_status - > setText ( " Status: Unconfirmed (in mempool) " ) ;
}
else {
QString dateTimeFormat = QString ( " %1 %2 " ) . arg ( config ( ) - > get ( Config : : dateFormat ) . toString ( ) , config ( ) - > get ( Config : : timeFormat ) . toString ( ) ) ;
QString date = tx - > timestamp ( ) . toString ( dateTimeFormat ) ;
QString statusText = QString ( " Status: Included in block %1 (%2 confirmations) on %3 " ) . arg ( blockHeight , QString : : number ( tx - > confirmations ( ) ) , date ) ;
ui - > label_status - > setText ( statusText ) ;
}
if ( tx - > confirmationsRequired ( ) > tx - > confirmations ( ) ) {
bool mandatoryLock = tx - > confirmationsRequired ( ) = = 10 ;
QString confsRequired = QString : : number ( tx - > confirmationsRequired ( ) - tx - > confirmations ( ) ) ;
ui - > label_lock - > setText ( QString ( " Lock: Outputs become spendable in %1 blocks (%2) " ) . arg ( confsRequired , mandatoryLock ? " consensus rule " : " specified by sender " ) ) ;
} else {
ui - > label_lock - > setText ( " Lock: Outputs are spendable " ) ;
}
QString direction = tx - > direction ( ) = = TransactionInfo : : Direction_In ? " received " : " sent " ;
2021-05-24 12:26:04 +00:00
ui - > label_amount - > setText ( QString ( " Amount %1: %2 XMR " ) . arg ( direction , tx - > displayAmount ( ) ) ) ;
2021-03-14 21:12:02 +00:00
2021-03-27 14:55:13 +00:00
QString fee ;
if ( tx - > isCoinbase ( ) )
fee = " Not applicable " ;
else if ( tx - > direction ( ) = = TransactionInfo : : Direction_In )
fee = " Paid by sender " ;
else if ( tx - > fee ( ) . isEmpty ( ) )
fee = " N/A " ;
else
fee = QString ( " %1 XMR " ) . arg ( tx - > fee ( ) ) ;
ui - > label_fee - > setText ( QString ( " Fee: %1 " ) . arg ( fee ) ) ;
2021-03-14 21:12:02 +00:00
}
2021-06-27 12:22:54 +00:00
void TxInfoDialog : : updateData ( ) {
2021-06-27 14:35:49 +00:00
TransactionInfo * tx = m_ctx - > wallet - > history ( ) - > transaction ( m_txid ) ;
2021-03-14 21:12:02 +00:00
if ( ! tx ) return ;
this - > setData ( tx ) ;
}
2021-10-12 12:58:54 +00:00
void TxInfoDialog : : copyTxID ( ) {
Utils : : copyToClipboard ( m_txid ) ;
}
2021-06-27 12:22:54 +00:00
void TxInfoDialog : : copyTxKey ( ) {
2021-07-02 17:35:21 +00:00
m_ctx - > wallet - > getTxKeyAsync ( m_txid , [ this ] ( QVariantMap map ) {
QString txKey = map . value ( " tx_key " ) . toString ( ) ;
if ( txKey . isEmpty ( ) ) {
QMessageBox : : warning ( this , " Unable to copy transaction key " , " Transaction key unknown " ) ;
} else {
Utils : : copyToClipboard ( txKey ) ;
QMessageBox : : information ( this , " Transaction key copied " , " Transaction key copied to clipboard. " ) ;
}
} ) ;
2021-01-29 14:51:59 +00:00
}
2021-06-27 12:22:54 +00:00
void TxInfoDialog : : createTxProof ( ) {
2021-03-08 20:03:20 +00:00
m_txProofDialog - > show ( ) ;
2021-07-02 17:35:21 +00:00
m_txProofDialog - > getTxKey ( ) ;
2021-03-08 20:03:20 +00:00
}
2021-06-27 12:22:54 +00:00
TxInfoDialog : : ~ TxInfoDialog ( ) = default ;