From b3ea7fa2328cc1d5ebd30b66357ed1584b7c6132 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 16 Jul 2025 17:20:28 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../feature/swap/domain/SwapInteractorImpl.kt | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt index cc21bc6469..ffec280d8a 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt @@ -21,6 +21,7 @@ import com.tangem.domain.models.currency.CryptoCurrency import com.tangem.domain.models.network.Network import com.tangem.domain.models.quote.QuoteStatus import com.tangem.domain.quotes.QuotesRepository +import com.tangem.domain.quotes.multi.MultiQuoteStatusFetcher import com.tangem.domain.tokens.FetchCurrencyStatusUseCase import com.tangem.domain.tokens.GetCurrencyCheckUseCase import com.tangem.domain.tokens.GetMultiCryptoCurrencyStatusUseCase @@ -66,6 +67,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( private val createApprovalTransactionUseCase: CreateApprovalTransactionUseCase, private val isDemoCardUseCase: IsDemoCardUseCase, private val quotesRepository: QuotesRepository, + private val multiQuoteStatusFetcher: MultiQuoteStatusFetcher, private val swapTransactionRepository: SwapTransactionRepository, private val currencyChecksRepository: CurrencyChecksRepository, private val appCurrencyRepository: AppCurrencyRepository, @@ -324,7 +326,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( ifRight = { quotes -> quotes.allowanceContract?.let { isAllowedToSpend(networkId, fromToken.currency, amount, it) - } ?: true + } != false }, ifLeft = { false }, ) @@ -1279,7 +1281,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( network = network, userWallet = userWallet, ).getOrNull() ?: error("unable to calculate fee") - } catch (e: IllegalStateException) { + } catch (_: IllegalStateException) { getEthSpecificFeeUseCase( userWallet = userWallet, cryptoCurrency = fromToken, @@ -1796,20 +1798,41 @@ internal class SwapInteractorImpl @AssistedInject constructor( } private suspend fun getQuotes(vararg ids: CryptoCurrency.ID): Map { - val set = ids.mapNotNull { it.rawCurrencyId } - .toSet() + val set = ids.mapNotNullTo(destination = hashSetOf(), transform = CryptoCurrency.ID::rawCurrencyId) .getQuotesOrEmpty() return ids .mapNotNull { id -> - set.find { it.rawCurrencyId == id.rawCurrencyId && it.value is QuoteStatus.Data } - ?.let { id to (it.value as QuoteStatus.Data).fiatRate } + val found = set.find { it.rawCurrencyId == id.rawCurrencyId && it.value is QuoteStatus.Data } + ?: return@mapNotNull null + + id to (found.value as QuoteStatus.Data).fiatRate } .toMap() } private suspend fun Set.getQuotesOrEmpty(): Set { - return runCatching { quotesRepository.getMultiQuoteSyncOrNull(currenciesIds = this) } + return runCatching { + val cachedQuotes = quotesRepository.getMultiQuoteSyncOrNull(currenciesIds = this) + + val allQuotesFound = cachedQuotes?.all { it.value !is QuoteStatus.Empty } == true + + if (allQuotesFound) return@runCatching cachedQuotes + + val currenciesIds = if (cachedQuotes.isNullOrEmpty()) { + this + } else { + cachedQuotes.mapNotNullTo(hashSetOf()) { + if (it.value is QuoteStatus.Empty) it.rawCurrencyId else null + } + } + + multiQuoteStatusFetcher( + params = MultiQuoteStatusFetcher.Params(currenciesIds = currenciesIds, appCurrencyId = null), + ) + + quotesRepository.getMultiQuoteSyncOrNull(currenciesIds = this) + } .getOrNull() .orEmpty() }