From 1f3c68af64b9d962c58196eeca50c8b920b57b78 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 16 Dec 2024 18:35:04 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../swap/domain/models/ui/SwapState.kt | 10 +- .../feature/swap/domain/SwapInteractor.kt | 14 +-- .../feature/swap/domain/SwapInteractorImpl.kt | 113 ++++++++---------- .../feature/swap/viewmodels/SwapViewModel.kt | 13 +- 4 files changed, 65 insertions(+), 85 deletions(-) diff --git a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt index a42a8db9a0..98c167b26c 100644 --- a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt +++ b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/SwapState.kt @@ -96,7 +96,15 @@ sealed class TxFeeState { data class MultipleFeeState( val normalFee: TxFee, val priorityFee: TxFee, - ) : TxFeeState() + ) : TxFeeState() { + + fun getFeeByType(feeType: FeeType): TxFee { + return when (feeType) { + FeeType.NORMAL -> normalFee + FeeType.PRIORITY -> priorityFee + } + } + } data class SingleFeeState( val fee: TxFee, diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt index 871f5f5e70..0eef2da3aa 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractor.kt @@ -67,13 +67,13 @@ interface SwapInteractor { fee: TxFee, ): SwapTransactionState - suspend fun updateQuotesStateWithSelectedFee( - state: SwapState.QuotesLoadedState, - selectedFee: FeeType, - fromToken: CryptoCurrencyStatus, - amountToSwap: String, - reduceBalanceBy: BigDecimal, - ): SwapState.QuotesLoadedState + // suspend fun updateQuotesStateWithSelectedFee( + // state: SwapState.QuotesLoadedState, + // selectedFee: FeeType, + // fromToken: CryptoCurrencyStatus, + // amountToSwap: String, + // reduceBalanceBy: BigDecimal, + // ): SwapState.QuotesLoadedState /** * Returns token in wallet balance 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 05beccce4e..cba72dc72b 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 @@ -294,6 +294,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( amount = amount, reduceBalanceBy = reduceBalanceBy, isBalanceWithoutFeeEnough = isBalanceWithoutFeeEnough, + selectedFee = selectedFee, ) } } @@ -373,6 +374,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( amount: SwapAmount, reduceBalanceBy: BigDecimal, isBalanceWithoutFeeEnough: Boolean, + selectedFee: FeeType, ): Pair { return provider to loadCexQuoteData( networkId = networkId, @@ -383,6 +385,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( isAllowedToSpend = true, isBalanceWithoutFeeEnough = isBalanceWithoutFeeEnough, provider = provider, + selectedFee = selectedFee, ) } @@ -496,45 +499,46 @@ internal class SwapInteractorImpl @AssistedInject constructor( } } - override suspend fun updateQuotesStateWithSelectedFee( - state: SwapState.QuotesLoadedState, - selectedFee: FeeType, - fromToken: CryptoCurrencyStatus, - amountToSwap: String, - reduceBalanceBy: BigDecimal, - ): SwapState.QuotesLoadedState { - val amountDecimal = toBigDecimalOrNull(amountToSwap) - if (amountDecimal == null || amountDecimal.signum() == 0) { - return state - } - val amount = SwapAmount(amountDecimal, fromToken.currency.decimals) - val includeFeeInAmount = getIncludeFeeInAmount( - networkId = fromToken.currency.network.backendId, - txFee = state.txFee, - amount = amount, - reduceBalanceBy = reduceBalanceBy, - fromToken = fromToken.currency, - ) - val fee = when (val txFee = state.txFee) { - TxFeeState.Empty -> BigDecimal.ZERO - is TxFeeState.MultipleFeeState -> txFee.priorityFee.feeIncludeOtherNativeFee - is TxFeeState.SingleFeeState -> txFee.fee.feeIncludeOtherNativeFee - } - val feeState = getFeeState( - fee = fee, - spendAmount = amount, - networkId = fromToken.currency.network.backendId, - fromTokenStatus = fromToken, - ) - return state.copy( - permissionState = PermissionDataState.Empty, - preparedSwapConfigState = state.preparedSwapConfigState.copy( - feeState = feeState, - isBalanceEnough = includeFeeInAmount !is IncludeFeeInAmount.BalanceNotEnough, - includeFeeInAmount = includeFeeInAmount, - ), - ) - } + // override suspend fun updateQuotesStateWithSelectedFee( + // state: SwapState.QuotesLoadedState, + // selectedFee: FeeType, + // fromToken: CryptoCurrencyStatus, + // amountToSwap: String, + // reduceBalanceBy: BigDecimal, + // ): SwapState.QuotesLoadedState { + // val amountDecimal = toBigDecimalOrNull(amountToSwap) + // if (amountDecimal == null || amountDecimal.signum() == 0) { + // return state + // } + // val amount = SwapAmount(amountDecimal, fromToken.currency.decimals) + // val includeFeeInAmount = getIncludeFeeInAmount( + // networkId = fromToken.currency.network.backendId, + // txFee = state.txFee, + // amount = amount, + // reduceBalanceBy = reduceBalanceBy, + // fromToken = fromToken.currency, + // selectedFee = selectedFee, + // ) + // val fee = when (val txFee = state.txFee) { + // TxFeeState.Empty -> BigDecimal.ZERO + // is TxFeeState.MultipleFeeState -> txFee.priorityFee.feeIncludeOtherNativeFee + // is TxFeeState.SingleFeeState -> txFee.fee.feeIncludeOtherNativeFee + // } + // val feeState = getFeeState( + // fee = fee, + // spendAmount = amount, + // networkId = fromToken.currency.network.backendId, + // fromTokenStatus = fromToken, + // ) + // return state.copy( + // permissionState = PermissionDataState.Empty, + // preparedSwapConfigState = state.preparedSwapConfigState.copy( + // feeState = feeState, + // isBalanceEnough = includeFeeInAmount !is IncludeFeeInAmount.BalanceNotEnough, + // includeFeeInAmount = includeFeeInAmount, + // ), + // ) + // } private suspend fun onSwapDex( provider: SwapProvider, @@ -1039,6 +1043,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( provider: SwapProvider, isAllowedToSpend: Boolean, isBalanceWithoutFeeEnough: Boolean, + selectedFee: FeeType, ): SwapState { val fromToken = fromTokenStatus.currency val toToken = toTokenStatus.currency @@ -1061,6 +1066,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( amount = amount, reduceBalanceBy = reduceBalanceBy, fromToken = fromToken, + selectedFee = selectedFee, ) val amountToRequest = if (includeFeeInAmount is IncludeFeeInAmount.Included) { includeFeeInAmount.amountSubtractFee @@ -1214,10 +1220,11 @@ internal class SwapInteractorImpl @AssistedInject constructor( amount: SwapAmount, reduceBalanceBy: BigDecimal, fromToken: CryptoCurrency, + selectedFee: FeeType, ): IncludeFeeInAmount { val feeValue = when (txFee) { TxFeeState.Empty -> BigDecimal.ZERO - is TxFeeState.MultipleFeeState -> txFee.priorityFee.feeIncludeOtherNativeFee + is TxFeeState.MultipleFeeState -> txFee.getFeeByType(selectedFee).feeIncludeOtherNativeFee is TxFeeState.SingleFeeState -> txFee.fee.feeIncludeOtherNativeFee } val feePaidCurrency = getFeePaidCurrency( @@ -1506,20 +1513,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( amount = amount, userWallet = userWallet, cryptoCurrency = cryptoCurrency, - ).mapFeeToForceSingleFeeIfNeeded(cryptoCurrency) - } - - private fun Either.mapFeeToForceSingleFeeIfNeeded( - cryptoCurrency: CryptoCurrency, - ): Either { - return this.map { - val blockchain = Blockchain.fromNetworkId(cryptoCurrency.network.backendId) - if (it is TransactionFee.Choosable && forceSingleFeeBlockchains.contains(blockchain)) { - TransactionFee.Single(normal = it.normal) - } else { - it - } - } + ) } @Suppress("LongParameterList", "LongMethod") @@ -2133,17 +2127,6 @@ internal class SwapInteractorImpl @AssistedInject constructor( private val minDemoFee = "0.0001".toBigDecimal() private val normalDemoFee = "0.0002".toBigDecimal() private val priorityDemoFee = "0.0003".toBigDecimal() - - private val forceSingleFeeBlockchains = listOf( - Bitcoin, BitcoinTestnet, - BitcoinCash, BitcoinCashTestnet, - Litecoin, - Dogecoin, - Dash, - Kaspa, - Ravencoin, RavencoinTestnet, - Ducatus, - ) } @AssistedFactory diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt index bdffaf4f6b..baf2ff720e 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt @@ -1055,21 +1055,10 @@ internal class SwapViewModel @Inject constructor( } }, onSelectFeeType = { - val state = dataState.getCurrentLoadedSwapState() ?: return@UiActions - val fromToken = dataState.fromCryptoCurrency ?: return@UiActions - val amountToSwap = dataState.amount ?: return@UiActions - val selectedProvider = dataState.selectedProvider ?: return@UiActions uiState = stateBuilder.dismissBottomSheet(uiState) dataState = dataState.copy(selectedFee = it) viewModelScope.launch(dispatchers.io) { - val updatedState = swapInteractor.updateQuotesStateWithSelectedFee( - state = state, - selectedFee = it.feeType, - fromToken = fromToken, - amountToSwap = amountToSwap, - reduceBalanceBy = dataState.reduceBalanceBy, - ) - setupLoadedState(selectedProvider, updatedState, fromToken) + startLoadingQuotesFromLastState(false) } }, onProviderClick = { providerId ->