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 118f753dc5..f5dbf2a745 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 @@ -805,6 +805,13 @@ internal class SwapInteractorImpl @Inject constructor( if (fromToken is CryptoCurrency.Token) { return IncludeFeeInAmount.Excluded } + + val tokenForFeeBalance = + userWalletManager.getNativeTokenBalance(networkId, derivationPath) ?: ProxyAmount.empty() + + if (amount.value > tokenForFeeBalance.value) { + return IncludeFeeInAmount.BalanceNotEnough + } val feeValue = when (txFee) { TxFeeState.Empty -> BigDecimal.ZERO is TxFeeState.MultipleFeeState -> if (selectedFee == FeeType.NORMAL) { @@ -815,8 +822,6 @@ internal class SwapInteractorImpl @Inject constructor( is TxFeeState.SingleFeeState -> txFee.fee.feeValue } - val tokenForFeeBalance = - userWalletManager.getNativeTokenBalance(networkId, derivationPath) ?: ProxyAmount.empty() val amountWithFee = amount.value + feeValue return if (amountWithFee < tokenForFeeBalance.value) { IncludeFeeInAmount.Excluded diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt index 625f477f4a..0f31b4c6d7 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt @@ -784,6 +784,36 @@ internal class StateBuilder( ) } + fun updateProvidersBottomSheetContent( + uiState: SwapStateHolder, + tokenSwapInfoForProviders: Map, + ): SwapStateHolder { + val config = uiState.bottomSheetConfig?.content as? ChooseProviderBottomSheetConfig + return if (config != null) { + val providers = config.providers + uiState.copy( + bottomSheetConfig = uiState.bottomSheetConfig.copy( + content = config.copy( + providers = providers.map { + val tokenInfo = tokenSwapInfoForProviders[it.id] + if (it is ProviderState.Content && tokenInfo != null) { + val rateString = tokenInfo.tokenAmount + .getFormattedCryptoAmount(tokenInfo.cryptoCurrencyStatus.currency) + it.copy( + subtitle = stringReference(rateString), + ) + } else { + it + } + }.toImmutableList(), + ), + ), + ) + } else { + uiState + } + } + fun updateSelectedProvider(uiState: SwapStateHolder, selectedProviderId: String): SwapStateHolder { val config = uiState.bottomSheetConfig?.content as? ChooseProviderBottomSheetConfig return if (config != null) { 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 7f2ce1b8ed..c87574b344 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 @@ -271,6 +271,13 @@ internal class SwapViewModel @Inject constructor( if (providersState.isNotEmpty()) { val (provider, state) = updateLoadedQuotes(providersState) setupLoadedState(provider, state, fromToken) + uiState = stateBuilder.updateProvidersBottomSheetContent( + uiState = uiState, + tokenSwapInfoForProviders = providersState + .getLastLoadedSuccessStates() + .entries + .associate { it.key.providerId to it.value.toTokenInfo }, + ) } else { Timber.e("Accidentally empty quotes list") } @@ -483,6 +490,7 @@ internal class SwapViewModel @Inject constructor( when (it) { is TxState.TxSent -> { uiState = stateBuilder.loadingPermissionState(uiState) + uiState = stateBuilder.dismissBottomSheet(uiState) } is TxState.UserCancelled -> Unit else -> {