add amount validation (#3076)

Serhii committed Mar 13, 2026 at 17:06 UTC 6617fba738b3d5e0b604ebc47087b161ff394225
1 file changed +32 -15
lib/view_model/send/send_view_model.dart
+32 -15
@@ -814,30 +814,47 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
814
815 // Regular flow
816
817 +
818 + final isSendAll = outputs.any((output) => output.sendAll);
819 +
820 + if (!isSendAll) {
821 + final estimateTxAmountDouble = outputs.fold<double>(0, (acc, output) =>
822 + acc + (double.tryParse(output.cryptoAmount) ?? 0));
823 + if (estimateTxAmountDouble <= 0) throw Exception(
824 + 'Amount must be greater than 0');
825 + }
826 +
827 pendingTransaction = await wallet.createTransaction(_credentials(provider));
828
819 - if (trade?.isSendAll == true) {
820 - if (provider is NearIntentsExchangeProvider) {
821 - final txAmountDouble = double.tryParse(pendingTransaction?.amountFormatted ?? '0') ?? 0.0;
822 - final tradeAmountDouble = double.tryParse(trade?.amount ?? '0') ?? 0.0;
823 - if (txAmountDouble != tradeAmountDouble) {
824 - throw Exception(
825 - 'Transaction amount $txAmountDouble does not match expected trade amount $tradeAmountDouble');
829 + final txAmountDouble = double.tryParse(pendingTransaction?.amountFormatted ?? '0') ?? 0.0;
830 + final bool isTradeTx = trade != null && provider != null;
831 +
832 + if (isTradeTx) {
833 + final tradeAmountDouble = double.tryParse(trade.amount) ?? 0.0;
834 + if (tradeAmountDouble <= 0) throw Exception('Trade amount must be greater than 0');
835 +
836 + if (trade.isSendAll == true) {
837 + if (provider is NearIntentsExchangeProvider) {
838 + if (txAmountDouble != tradeAmountDouble) {
839 + throw Exception(
840 + 'Transaction amount $txAmountDouble does not match expected trade amount $tradeAmountDouble');
841 + }
842 }
843 }
828 - }
844
830 - if (provider is ThorChainExchangeProvider) {
831 - final outputCount = pendingTransaction?.outputCount ?? 0;
832 - if (outputCount > 10) {
833 - throw Exception("THORChain does not support more than 10 outputs");
834 - }
845 + if (provider is ThorChainExchangeProvider) {
846 + final outputCount = pendingTransaction?.outputCount ?? 0;
847 + if (outputCount > 10) {
848 + throw Exception("THORChain does not support more than 10 outputs");
849 + }
850
836 - if (_hasTaprootInput(pendingTransaction)) {
837 - throw Exception("THORChain does not support Taproot addresses");
851 + if (_hasTaprootInput(pendingTransaction)) {
852 + throw Exception("THORChain does not support Taproot addresses");
853 + }
854 }
855 }
856
857 +
858 if (wallet.type == WalletType.bitcoin) {
859 final updatedOutputs = bitcoin!.updateOutputs(pendingTransaction!, outputs);
860