From 6617fba738b3d5e0b604ebc47087b161ff394225 Mon Sep 17 00:00:00 2001 From: Serhii <17529954+serhii-bor@users.noreply.github.com> Date: Fri, 13 Mar 2026 17:06:40 +0200 Subject: [PATCH] add amount validation (#3076) --- lib/view_model/send/send_view_model.dart | 51 ++++++++++++++++-------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index ecc0672ac..66242a5ea 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -814,29 +814,46 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor // Regular flow + + final isSendAll = outputs.any((output) => output.sendAll); + + if (!isSendAll) { + final estimateTxAmountDouble = outputs.fold(0, (acc, output) => + acc + (double.tryParse(output.cryptoAmount) ?? 0)); + if (estimateTxAmountDouble <= 0) throw Exception( + 'Amount must be greater than 0'); + } + pendingTransaction = await wallet.createTransaction(_credentials(provider)); - if (trade?.isSendAll == true) { - if (provider is NearIntentsExchangeProvider) { - final txAmountDouble = double.tryParse(pendingTransaction?.amountFormatted ?? '0') ?? 0.0; - final tradeAmountDouble = double.tryParse(trade?.amount ?? '0') ?? 0.0; - if (txAmountDouble != tradeAmountDouble) { - throw Exception( - 'Transaction amount $txAmountDouble does not match expected trade amount $tradeAmountDouble'); + final txAmountDouble = double.tryParse(pendingTransaction?.amountFormatted ?? '0') ?? 0.0; + final bool isTradeTx = trade != null && provider != null; + + if (isTradeTx) { + final tradeAmountDouble = double.tryParse(trade.amount) ?? 0.0; + if (tradeAmountDouble <= 0) throw Exception('Trade amount must be greater than 0'); + + if (trade.isSendAll == true) { + if (provider is NearIntentsExchangeProvider) { + if (txAmountDouble != tradeAmountDouble) { + throw Exception( + 'Transaction amount $txAmountDouble does not match expected trade amount $tradeAmountDouble'); + } + } + } + + if (provider is ThorChainExchangeProvider) { + final outputCount = pendingTransaction?.outputCount ?? 0; + if (outputCount > 10) { + throw Exception("THORChain does not support more than 10 outputs"); + } + + if (_hasTaprootInput(pendingTransaction)) { + throw Exception("THORChain does not support Taproot addresses"); } } } - if (provider is ThorChainExchangeProvider) { - final outputCount = pendingTransaction?.outputCount ?? 0; - if (outputCount > 10) { - throw Exception("THORChain does not support more than 10 outputs"); - } - - if (_hasTaprootInput(pendingTransaction)) { - throw Exception("THORChain does not support Taproot addresses"); - } - } if (wallet.type == WalletType.bitcoin) { final updatedOutputs = bitcoin!.updateOutputs(pendingTransaction!, outputs);