|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/transactionrbfadapter.h> |
| 6 | + |
| 7 | +#include <util/translation.h> |
| 8 | + |
| 9 | +#include <QObject> |
| 10 | + |
| 11 | +namespace { |
| 12 | +constexpr unsigned int DEFAULT_RBF_CONFIRM_TARGET{6}; |
| 13 | +constexpr qint64 ZERO_SATS{0}; |
| 14 | +} |
| 15 | + |
| 16 | +std::optional<Txid> TransactionRbfAdapter::ParseTxid(const QString& txid_text) |
| 17 | +{ |
| 18 | + const QString normalized = txid_text.trimmed(); |
| 19 | + if (normalized.isEmpty()) { |
| 20 | + return std::nullopt; |
| 21 | + } |
| 22 | + |
| 23 | + return Txid::FromHex(normalized.toStdString()); |
| 24 | +} |
| 25 | + |
| 26 | +QString TransactionRbfAdapter::ErrorMessage(const std::vector<bilingual_str>& errors, |
| 27 | + const QString& fallback) |
| 28 | +{ |
| 29 | + if (errors.empty()) { |
| 30 | + return fallback; |
| 31 | + } |
| 32 | + |
| 33 | + const bilingual_str& first_error = errors.front(); |
| 34 | + if (!first_error.translated.empty()) { |
| 35 | + return QString::fromStdString(first_error.translated); |
| 36 | + } |
| 37 | + |
| 38 | + if (!first_error.original.empty()) { |
| 39 | + return QString::fromStdString(first_error.original); |
| 40 | + } |
| 41 | + |
| 42 | + return fallback; |
| 43 | +} |
| 44 | + |
| 45 | +bool TransactionRbfAdapter::CanBump(const TransactionRbfBackend& backend, const QString& txid_text) |
| 46 | +{ |
| 47 | + const auto txid = ParseTxid(txid_text); |
| 48 | + return txid.has_value() && backend.transactionCanBeBumped(*txid); |
| 49 | +} |
| 50 | + |
| 51 | +bool TransactionRbfAdapter::CanAbandon(const TransactionRbfBackend& backend, const QString& txid_text) |
| 52 | +{ |
| 53 | + const auto txid = ParseTxid(txid_text); |
| 54 | + return txid.has_value() && backend.transactionCanBeAbandoned(*txid); |
| 55 | +} |
| 56 | + |
| 57 | +QString TransactionRbfAdapter::BumpIneligibleReason() |
| 58 | +{ |
| 59 | + return QObject::tr("Only unconfirmed replaceable transactions can be sped up."); |
| 60 | +} |
| 61 | + |
| 62 | +QString TransactionRbfAdapter::AbandonIneligibleReason() |
| 63 | +{ |
| 64 | + return QObject::tr("Only unconfirmed transactions without descendants can be canceled."); |
| 65 | +} |
| 66 | + |
| 67 | +TransactionRbfPreview TransactionRbfAdapter::PrepareBump(TransactionRbfBackend& backend, |
| 68 | + const QString& txid_text, |
| 69 | + const unsigned int target_blocks, |
| 70 | + const bool custom_fee_enabled, |
| 71 | + const qint64 custom_fee_rate_sat_per_kvb) |
| 72 | +{ |
| 73 | + TransactionRbfPreview preview; |
| 74 | + |
| 75 | + const auto txid = ParseTxid(txid_text); |
| 76 | + if (!txid.has_value()) { |
| 77 | + preview.message = QObject::tr("Invalid transaction ID."); |
| 78 | + return preview; |
| 79 | + } |
| 80 | + |
| 81 | + if (!backend.transactionCanBeBumped(*txid)) { |
| 82 | + preview.message = BumpIneligibleReason(); |
| 83 | + return preview; |
| 84 | + } |
| 85 | + |
| 86 | + const unsigned int normalized_target = target_blocks > 0 ? target_blocks : DEFAULT_RBF_CONFIRM_TARGET; |
| 87 | + std::optional<CAmount> custom_fee_sat_per_kvb{}; |
| 88 | + |
| 89 | + if (custom_fee_enabled) { |
| 90 | + if (custom_fee_rate_sat_per_kvb <= ZERO_SATS) { |
| 91 | + preview.message = QObject::tr("Custom fee must be greater than zero."); |
| 92 | + return preview; |
| 93 | + } |
| 94 | + custom_fee_sat_per_kvb = custom_fee_rate_sat_per_kvb; |
| 95 | + } |
| 96 | + |
| 97 | + std::vector<bilingual_str> errors; |
| 98 | + if (!backend.createBumpTransaction(*txid, |
| 99 | + normalized_target, |
| 100 | + custom_fee_sat_per_kvb, |
| 101 | + errors, |
| 102 | + preview.old_fee, |
| 103 | + preview.new_fee, |
| 104 | + preview.replacement)) { |
| 105 | + preview.message = ErrorMessage(errors, QObject::tr("Could not prepare fee bump.")); |
| 106 | + return preview; |
| 107 | + } |
| 108 | + |
| 109 | + preview.success = true; |
| 110 | + preview.message = QObject::tr("Review the updated fee and confirm to broadcast."); |
| 111 | + return preview; |
| 112 | +} |
| 113 | + |
| 114 | +TransactionRbfActionResult TransactionRbfAdapter::CommitBump(TransactionRbfBackend& backend, |
| 115 | + const QString& txid_text, |
| 116 | + CMutableTransaction&& replacement_tx) |
| 117 | +{ |
| 118 | + TransactionRbfActionResult result; |
| 119 | + |
| 120 | + const auto txid = ParseTxid(txid_text); |
| 121 | + if (!txid.has_value()) { |
| 122 | + result.message = QObject::tr("Invalid transaction ID."); |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + if (!backend.signBumpTransaction(replacement_tx)) { |
| 127 | + result.message = QObject::tr("Could not sign replacement transaction."); |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + std::vector<bilingual_str> errors; |
| 132 | + Txid replacement_txid; |
| 133 | + if (!backend.commitBumpTransaction(*txid, |
| 134 | + std::move(replacement_tx), |
| 135 | + errors, |
| 136 | + replacement_txid)) { |
| 137 | + result.message = ErrorMessage(errors, QObject::tr("Could not broadcast replacement transaction.")); |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + result.success = true; |
| 142 | + result.replacement_txid = QString::fromStdString(replacement_txid.ToString()); |
| 143 | + result.message = QObject::tr("Replacement transaction broadcast."); |
| 144 | + return result; |
| 145 | +} |
| 146 | + |
| 147 | +TransactionRbfActionResult TransactionRbfAdapter::Abandon(TransactionRbfBackend& backend, |
| 148 | + const QString& txid_text) |
| 149 | +{ |
| 150 | + TransactionRbfActionResult result; |
| 151 | + |
| 152 | + const auto txid = ParseTxid(txid_text); |
| 153 | + if (!txid.has_value()) { |
| 154 | + result.message = QObject::tr("Invalid transaction ID."); |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + if (!backend.transactionCanBeAbandoned(*txid)) { |
| 159 | + result.message = AbandonIneligibleReason(); |
| 160 | + return result; |
| 161 | + } |
| 162 | + |
| 163 | + if (!backend.abandonTransaction(*txid)) { |
| 164 | + result.message = QObject::tr("Could not cancel transaction."); |
| 165 | + return result; |
| 166 | + } |
| 167 | + |
| 168 | + result.success = true; |
| 169 | + result.message = QObject::tr("Transaction canceled."); |
| 170 | + return result; |
| 171 | +} |
0 commit comments