Validate swap amounts before creating trade (#3296)
Serhii committed
Jun 4, 2026 at 18:45 UTC
ea4bdc709b2bcfa7f083deebf0c804fe7d672ddc
1 file changed
+33
lib/view_model/exchange/exchange_view_model.dart
+33
@@ -1025,6 +1025,39 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
1025
1026
@action
1027
Future<void> createTrade() async {
1028
+ final depositAmountValue =
1029
+ double.tryParse(_depositAmount.replaceAll(',', '.')) ?? 0.0;
1030
+
1031
+ final receiveAmountValue =
1032
+ double.tryParse(_receiveAmount.replaceAll(',', '.')) ?? 0.0;
1033
+
1034
+ if (depositAmountValue <= 0 || receiveAmountValue <= 0) {
1035
+ ExchangeProviderLogger.logError(
1036
+ provider: forcedProvider?.description ?? bestRateProvider?.description,
1037
+ function: 'createTrade',
1038
+ error: 'Invalid swap amount: deposit=$_depositAmount receive=$_receiveAmount',
1039
+ requestData: {
1040
+ 'from': depositCurrency.title,
1041
+ 'to': receiveCurrency.title,
1042
+ 'fromAmount': _depositAmount,
1043
+ 'toAmount': _receiveAmount,
1044
+ 'isFixedRateMode': isFixedRateMode,
1045
+ 'forcedProvider': forcedProvider?.title,
1046
+ 'bestRateProvider': bestRateProvider?.title,
1047
+ },
1048
+ );
1049
+
1050
+ final invalidAmountError = depositAmountValue <= 0
1051
+ ? '$depositAmountValue is not a valid amount for depositAmount'
1052
+ : '$receiveAmountValue is not a valid amount for receiveAmount';
1053
+
1054
+ tradeState = TradeIsCreatedFailure(
1055
+ title: S.current.trade_not_created,
1056
+ error: invalidAmountError,
1057
+ );
1058
+ return;
1059
+ }
1060
+
1061
if (isSendAllEnabled) {
1062
await calculateDepositAllAmount();
1063
final amount = double.tryParse(_depositAmount);