diff --git a/lib/exchange/provider/near_Intents_exchange_provider.dart b/lib/exchange/provider/near_Intents_exchange_provider.dart index f2ebe1006..2ac942def 100644 --- a/lib/exchange/provider/near_Intents_exchange_provider.dart +++ b/lib/exchange/provider/near_Intents_exchange_provider.dart @@ -271,15 +271,17 @@ class NearIntentsExchangeProvider extends ExchangeProvider { throw Exception('Failed to parse to currency from assetId: $toAssetId'); } - final from = CryptoCurrency.safeParseCurrencyFromString(fromCurrency.$1, tag: fromCurrency.$2); - final to = CryptoCurrency.safeParseCurrencyFromString(toCurrency.$1, tag: toCurrency.$2); + final from = CryptoCurrency.safeParseCurrencyFromString(fromCurrency.$1, + tag: fromCurrency.$2); + final to = CryptoCurrency.safeParseCurrencyFromString(toCurrency.$1, + tag: toCurrency.$2); final trade = Trade( id: depositAddress, // Using deposit address as trade ID from: request.fromCurrency, - to: request.fromCurrency, + to: request.toCurrency, provider: description, providerName: title, state: TradeState.created, diff --git a/lib/view_model/exchange/exchange_trade_view_model.dart b/lib/view_model/exchange/exchange_trade_view_model.dart index 58540f8fc..154c1ab96 100644 --- a/lib/view_model/exchange/exchange_trade_view_model.dart +++ b/lib/view_model/exchange/exchange_trade_view_model.dart @@ -214,11 +214,11 @@ abstract class ExchangeTradeViewModelBase with Store { final canSendError = checkIfCanSend(trade, wallet); if (canSendError != null) { + _logCanSendError(trade, wallet, canSendError); sendViewModel.state = FailureState(canSendError); return; } - final selected = trade.from; if (selected == null) { printV('No selectable currency for trade ${trade.id}'); @@ -354,62 +354,55 @@ abstract class ExchangeTradeViewModelBase with Store { } String? checkIfCanSend(Trade? trade, WalletBase wallet) { - try { + if (trade == null) return 'Trade is null'; - if (trade == null) throw Exception('Trade is null'); + final tradeFrom = trade.from; + if (tradeFrom == null) return 'Trade from currency is null'; - final tradeFrom = trade.from; - if (tradeFrom == null) throw Exception('Trade from currency is null'); + bool _sameCurrency(CryptoCurrency a, CryptoCurrency b) => a.titleAndTagEqual(b); - bool _sameCurrency(CryptoCurrency a, CryptoCurrency b) => a.titleAndTagEqual(b); - - bool _isTokenBelongingToWallet(CryptoCurrency cur) { - final chainTag = cur.tag ?? cur.title; - return wallet.currency == cur && - tradeFrom.tag?.toUpperCase() == chainTag.toUpperCase(); - } - - final canSend = _sameCurrency(tradeFrom, wallet.currency) || - (_sameCurrency(tradeFrom, CryptoCurrency.btcln) && - wallet.currency == CryptoCurrency.btc) || - trade.provider == ExchangeProviderDescription.xmrto || - _isTokenBelongingToWallet(CryptoCurrency.eth) || - _isTokenBelongingToWallet(CryptoCurrency.maticpoly) || - _isTokenBelongingToWallet(CryptoCurrency.baseEth) || - _isTokenBelongingToWallet(CryptoCurrency.arbEth) || - _isTokenBelongingToWallet(CryptoCurrency.trx) || - _isTokenBelongingToWallet(CryptoCurrency.sol) || - _isTokenBelongingToWallet(CryptoCurrency.bnb); - - if (!canSend) { - throw Exception( - 'Wallet currency ${wallet.currency.title} does not match trade from currency ${tradeFrom.title} or is not a supported token for this wallet.', - ); - } - - return null; - } catch (e, s) { - final trade = tradesStore.trade; - - ExchangeProviderLogger.logError( - provider: trade?.provider, - function: '_checkIfCanSend', - error: e, - stackTrace: s, - requestData: { - 'tradeId': trade?.id, - 'tradeFrom': trade?.from?.title, - 'tradeFromTag': trade?.from?.tag, - 'tradeTo': trade?.to?.title, - 'tradeToTag': trade?.to?.tag, - 'walletName': wallet.name, - 'walletCurrency': wallet.currency.title, - 'walletCurrencyTag': wallet.currency.tag, - }, - ); - - return e.toString(); + bool _isTokenBelongingToWallet(CryptoCurrency cur) { + final chainTag = cur.tag ?? cur.title; + return wallet.currency == cur && + (tradeFrom.tag?.toUpperCase() == chainTag.toUpperCase() || + tradeFrom.title.toUpperCase() == chainTag.toUpperCase()); } + + final canSend = _sameCurrency(tradeFrom, wallet.currency) || + (_sameCurrency(tradeFrom, CryptoCurrency.btcln) && + wallet.currency == CryptoCurrency.btc) || + trade.provider == ExchangeProviderDescription.xmrto || + _isTokenBelongingToWallet(CryptoCurrency.eth) || + _isTokenBelongingToWallet(CryptoCurrency.maticpoly) || + _isTokenBelongingToWallet(CryptoCurrency.baseEth) || + _isTokenBelongingToWallet(CryptoCurrency.arbEth) || + _isTokenBelongingToWallet(CryptoCurrency.trx) || + _isTokenBelongingToWallet(CryptoCurrency.sol) || + _isTokenBelongingToWallet(CryptoCurrency.bnb); + + if (!canSend) { + return 'Wallet currency ${wallet.currency.title} does not match trade from currency ${tradeFrom.title} or is not a supported token for this wallet.'; + } + + return null; + } + + void _logCanSendError(Trade? trade, WalletBase wallet, String error) { + ExchangeProviderLogger.logError( + provider: trade?.provider, + function: '_checkIfCanSend', + error: error, + requestData: { + 'tradeId': trade?.id, + 'tradeFrom': trade?.from?.title, + 'tradeFromTag': trade?.from?.tag, + 'tradeTo': trade?.to?.title, + 'tradeToTag': trade?.to?.tag, + 'walletName': wallet.name, + 'walletCurrency': wallet.currency.title, + 'walletCurrencyTag': wallet.currency.tag, + }, + ); } static bool _checkIfSwapsXYZCanSendFromExternal(Trade trade, WalletBase wallet) {