|
23 | 23 | #include <psbt.h> |
24 | 24 | #include <util/translation.h> |
25 | 25 | #include <wallet/coincontrol.h> |
| 26 | +#include <wallet/types.h> |
26 | 27 | #include <wallet/wallet.h> |
27 | 28 |
|
28 | 29 | #include <cstdint> |
@@ -149,6 +150,8 @@ bool WalletModel::validateAddress(const QString& address) const |
149 | 150 |
|
150 | 151 | WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl) |
151 | 152 | { |
| 153 | + transaction.getWtx() = nullptr; // reset tx output |
| 154 | + |
152 | 155 | CAmount total = 0; |
153 | 156 | bool fSubtractFeeFromAmount = false; |
154 | 157 | QList<SendCoinsRecipient> recipients = transaction.getRecipients(); |
@@ -199,27 +202,21 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact |
199 | 202 | } |
200 | 203 |
|
201 | 204 | try { |
202 | | - CAmount nFeeRequired = 0; |
203 | | - int nChangePosRet = -1; |
204 | | - |
205 | 205 | auto& newTx = transaction.getWtx(); |
206 | | - const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), nChangePosRet, nFeeRequired); |
207 | | - newTx = res ? *res : nullptr; |
208 | | - transaction.setTransactionFee(nFeeRequired); |
209 | | - if (fSubtractFeeFromAmount && newTx) |
210 | | - transaction.reassignAmounts(nChangePosRet); |
211 | | - |
212 | | - if(!newTx) |
213 | | - { |
214 | | - if(!fSubtractFeeFromAmount && (total + nFeeRequired) > nBalance) |
215 | | - { |
216 | | - return SendCoinsReturn(AmountWithFeeExceedsBalance); |
217 | | - } |
| 206 | + const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), /*change_pos=*/std::nullopt); |
| 207 | + if (!res) { |
218 | 208 | Q_EMIT message(tr("Send Coins"), QString::fromStdString(util::ErrorString(res).translated), |
219 | | - CClientUIInterface::MSG_ERROR); |
| 209 | + CClientUIInterface::MSG_ERROR); |
220 | 210 | return TransactionCreationFailed; |
221 | 211 | } |
222 | 212 |
|
| 213 | + newTx = res->tx; |
| 214 | + CAmount nFeeRequired = res->fee; |
| 215 | + transaction.setTransactionFee(nFeeRequired); |
| 216 | + if (fSubtractFeeFromAmount && newTx) { |
| 217 | + transaction.reassignAmounts(static_cast<int>(res->change_pos.value_or(-1))); |
| 218 | + } |
| 219 | + |
223 | 220 | // Reject absurdly high fee. (This can never happen because the |
224 | 221 | // wallet never creates transactions with fee greater than |
225 | 222 | // m_default_max_tx_fee. This merely a belt-and-suspenders check). |
|
0 commit comments