Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/app/features/wallets/model/swap_coin_data.f.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class SwapCoinData with _$SwapCoinData {
NetworkData? buyNetwork,
SwapQuoteInfo? swapQuoteInfo,
@Default(false) bool isQuoteLoading,
@Default(false) bool isQuoteError,
@Default(0) double amount,
BigInt? quoteAmount,
Exception? quoteError,
Expand All @@ -26,4 +25,6 @@ class SwapCoinData with _$SwapCoinData {
const SwapCoinData._();

static const double defaultSlippage = 3;

bool get isQuoteError => quoteError != null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import 'package:ion/app/features/wallets/model/transaction_details.f.dart';
import 'package:ion/app/features/wallets/model/transaction_status.f.dart';
import 'package:ion/app/features/wallets/model/transaction_type.dart';
import 'package:ion/app/features/wallets/model/transfer_result.f.dart';
import 'package:ion/app/features/wallets/providers/send_asset_form_provider.r.dart';
import 'package:ion/app/features/wallets/providers/synced_coins_by_symbol_group_provider.r.dart';
import 'package:ion/app/features/wallets/providers/wallet_view_data_provider.r.dart';
import 'package:ion/app/services/logger/logger.dart';
Expand Down Expand Up @@ -57,8 +56,6 @@ class SendCoinsNotifier extends _$SendCoinsNotifier {
state = const AsyncValue.loading();

state = await AsyncValue.guard(() async {
final form = ref.read(sendAssetFormControllerProvider);

final coinAssetData = _extractCoinAssetData(form);
final (senderWallet, sendableAsset, selectedOption) =
_validateFormComponents(form, coinAssetData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class _ErrorState extends StatelessWidget {
quoteError! as OkxException,
),
InsufficientBalanceException() => context.i18n.error_swap_82000,
final AmountBelowMinimumException ex =>
context.i18n.error_swap_amount_below_min(ex.minAmount, ex.symbol),
_ => context.i18n.error_getting_swap_quote,
};
}
Expand Down Expand Up @@ -212,11 +214,13 @@ class _ErrorState extends StatelessWidget {
size: 16.0.s,
),
SizedBox(width: 5.0.s),
Text(
_getErrorMessage(
context,
Expanded(
child: Text(
_getErrorMessage(
context,
),
style: textStyles.body2.copyWith(),
),
style: textStyles.body2.copyWith(),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// SPDX-License-Identifier: ice License 1.0

final class InsufficientBalanceException implements Exception {}

final class AmountBelowMinimumException implements Exception {
AmountBelowMinimumException({required this.symbol, required this.minAmount});

final String symbol;
final String minAmount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import 'package:ion/app/services/ion_swap_client/ion_swap_client_provider.r.dart
import 'package:ion/app/services/sentry/sentry_service.dart';
import 'package:ion/app/services/storage/local_storage.r.dart';
import 'package:ion_identity_client/ion_identity.dart';
import 'package:ion_swap_client/exceptions/exolix_exceptions.dart';
import 'package:ion_swap_client/exceptions/ion_swap_exception.dart';
import 'package:ion_swap_client/exceptions/okx_exceptions.dart';
import 'package:ion_swap_client/exceptions/relay_exception.dart';
import 'package:ion_swap_client/models/ion_swap_request.dart';
Expand Down Expand Up @@ -55,7 +57,6 @@ class SwapCoinsController extends _$SwapCoinsController {
buyNetwork: null,
swapQuoteInfo: null,
amount: 0,
isQuoteError: false,
isQuoteLoading: false,
quoteAmount: null,
quoteError: null,
Expand Down Expand Up @@ -94,7 +95,6 @@ class SwapCoinsController extends _$SwapCoinsController {

if (sellCoinInWallet.amount < amount) {
state = state.copyWith(
isQuoteError: true,
quoteError: InsufficientBalanceException(),
swapQuoteInfo: null,
);
Expand Down Expand Up @@ -474,31 +474,48 @@ class SwapCoinsController extends _$SwapCoinsController {
swapCoinData: swapCoinParameters,
);

final isValidAmount = _validateAmount(swapCoinParameters.amount, swapQuoteInfo);
if (!isValidAmount) {
return;
}

state = state.copyWith(
isQuoteLoading: false,
swapQuoteInfo: swapQuoteInfo,
isQuoteError: false,
quoteError: null,
);
} catch (e, stackTrace) {
await SentryService.logException(
e,
stackTrace: stackTrace,
tag: 'get_swap_quote_failure',
);

Exception? quoteError;

if (e is OkxException || e is RelayException) {
if (e is ExolixBelowMinimumException) {
state = state.copyWith(
isQuoteLoading: false,
swapQuoteInfo: null,
quoteError: AmountBelowMinimumException(
minAmount: e.minAmount.toString(),
symbol: (state.sellCoin?.abbreviation ?? '').toUpperCase(),
),
);
return;
}

if (e is OkxException || e is RelayException || e is ExolixException) {
quoteError = e as Exception;
}

state = state.copyWith(
isQuoteLoading: false,
swapQuoteInfo: null,
isQuoteError: true,
quoteError: quoteError,
);

if (e is! IonSwapException) {
await SentryService.logException(
e,
stackTrace: stackTrace,
tag: 'get_swap_quote_failure',
);
}
}
}

Expand All @@ -523,6 +540,38 @@ class SwapCoinsController extends _$SwapCoinsController {
return null;
}

bool _validateAmount(String userAmountStr, SwapQuoteInfo quoteInfo) {
final userAmount = double.tryParse(userAmountStr);
if (userAmount == null) return true;

final (minAmountValue, minAmountStr) = switch (quoteInfo.source) {
SwapQuoteInfoSource.exolix when quoteInfo.exolixQuote != null => (
quoteInfo.exolixQuote!.minAmount,
quoteInfo.exolixQuote!.minAmount.toString(),
),
SwapQuoteInfoSource.letsExchange when quoteInfo.letsExchangeQuote != null => (
double.tryParse(quoteInfo.letsExchangeQuote!.minAmount),
quoteInfo.letsExchangeQuote!.minAmount,
),
_ => (null, '0'),
};

if (minAmountValue == null || userAmount >= minAmountValue) {
return true;
}

state = state.copyWith(
swapQuoteInfo: null,
isQuoteLoading: false,
quoteError: AmountBelowMinimumException(
minAmount: minAmountStr,
symbol: (state.sellCoin?.abbreviation ?? '').toUpperCase(),
),
);

return false;
}

Future<bool> getIsIonBscSwap() async {
final (:swapQuoteInfo, :swapCoinParameters, :sellNetwork, :sellCoin) = await _getData();

Expand Down
Loading