From c6a23088841137e269f0534b01fc032043eb9187 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 1 Aug 2026 14:07:55 -0600 Subject: [PATCH] maybe --- .../electrumx_interface.dart | 67 +++++++++++++++---- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index 8c1c8014c1..100aa9f9fe 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -569,23 +569,62 @@ mixin ElectrumXInterface feeForOneOutput = overrideFeeAmount; } - final satoshiAmountToSend = satoshisBeingUsed - feeForOneOutput; + late TxData data; + if (txData.type == TxType.mwebPegIn) { + while (true) { + final satoshiAmountToSend = satoshisBeingUsed - feeForOneOutput; + if (satoshiAmountToSend.isNegative) { + throw Exception( + "Estimated fee ($feeForOneOutput sats) is greater than balance!", + ); + } - if (satoshiAmountToSend.isNegative) { - throw Exception( - "Estimated fee ($feeForOneOutput sats) is greater than balance!", - ); - } + data = await buildTransaction( + txData: txData.copyWith( + recipients: await helperRecipientsConvert( + [recipientAddress], + [satoshiAmountToSend], + ), + ), + inputsWithKeys: inputsWithKeys, + ); - final data = await buildTransaction( - txData: txData.copyWith( - recipients: await helperRecipientsConvert( - [recipientAddress], - [satoshiAmountToSend], + if (overrideFeeAmount != null) { + break; + } + + // Signing can change vSize, so calculate the fee from the final tx. + final vSize = BigInt.from(data.vSize!); + final feeForFinalVSize = BigInt.from( + satsPerVByte != null + ? satsPerVByte * data.vSize! + : estimateTxFee(vSize: data.vSize!, feeRatePerKB: feeRatePerKB), + ); + final requiredFee = feeForFinalVSize > vSize ? feeForFinalVSize : vSize; + if (feeForOneOutput >= requiredFee) { + break; + } + feeForOneOutput = requiredFee; + } + } else { + final satoshiAmountToSend = satoshisBeingUsed - feeForOneOutput; + + if (satoshiAmountToSend.isNegative) { + throw Exception( + "Estimated fee ($feeForOneOutput sats) is greater than balance!", + ); + } + + data = await buildTransaction( + txData: txData.copyWith( + recipients: await helperRecipientsConvert( + [recipientAddress], + [satoshiAmountToSend], + ), ), - ), - inputsWithKeys: inputsWithKeys, - ); + inputsWithKeys: inputsWithKeys, + ); + } return data.copyWith( fee: Amount(