From cff0f51c84f8e6c0c1a6884fc9df9952d0e8343d Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 6 Feb 2026 15:09:48 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../usecase/gasless/GetFeeForGaslessUseCase.kt | 18 +++++++++++++----- .../usecase/gasless/TokenFeeCalculator.kt | 13 ++++++++++++- .../v2/feeselector/model/FeeSelectorLogic.kt | 5 ++++- gradle/tangem_dependencies.toml | 2 +- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/GetFeeForGaslessUseCase.kt b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/GetFeeForGaslessUseCase.kt index 1bb6b4074c..8400491228 100644 --- a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/GetFeeForGaslessUseCase.kt +++ b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/GetFeeForGaslessUseCase.kt @@ -1,6 +1,7 @@ package com.tangem.domain.transaction.usecase.gasless import arrow.core.Either +import arrow.core.getOrElse import arrow.core.raise.Raise import arrow.core.raise.catch import arrow.core.raise.either @@ -128,25 +129,32 @@ class GetFeeForGaslessUseCase( } ?: raiseIllegalStateError("native currency not found for network ${network.id}") val nativeBalance = nativeCurrencyStatus.value.amount ?: BigDecimal.ZERO - return if (nativeBalance >= feeValue) { + val nativeCoinSelectedResult = TransactionFeeExtended(transactionFee = initialFee, feeTokenId = nativeCurrencyStatus.currency.id) + return if (nativeBalance >= feeValue) { + nativeCoinSelectedResult } else { findTokensToPayFee( walletManager = walletManager, initialTxFee = initialFee, nativeCurrencyStatus = nativeCurrencyStatus, networkCurrenciesStatuses = networkCurrenciesStatuses, - ) + ).getOrElse { error -> + when (error) { + GaslessError.NotEnoughFunds -> nativeCoinSelectedResult + else -> raise(error) + } + } } } @Suppress("NullableToStringCall") - private suspend fun Raise.findTokensToPayFee( + private suspend fun findTokensToPayFee( walletManager: EthereumWalletManager, initialTxFee: TransactionFee, nativeCurrencyStatus: CryptoCurrencyStatus, networkCurrenciesStatuses: List, - ): TransactionFeeExtended { + ): Either = either { val initialFee = initialTxFee.normal as? Fee.Ethereum ?: raiseIllegalStateError( error = "only Fee.Ethereum supported, but was ${initialTxFee.normal::class.qualifiedName}", @@ -178,6 +186,6 @@ class GetFeeForGaslessUseCase( tokenForPayFeeStatus = tokenForPayFeeStatus, nativeCurrencyStatus = nativeCurrencyStatus, initialFee = initialFee, - ).bind() + ) } } \ No newline at end of file diff --git a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/TokenFeeCalculator.kt b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/TokenFeeCalculator.kt index cec62f3d5c..451be7a94b 100644 --- a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/TokenFeeCalculator.kt +++ b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/gasless/TokenFeeCalculator.kt @@ -6,6 +6,7 @@ import arrow.core.raise.either import com.tangem.blockchain.blockchains.ethereum.EthereumWalletManager import com.tangem.blockchain.blockchains.ethereum.tokenmethods.TransferERC20TokenCallData import com.tangem.blockchain.common.Amount +import com.tangem.blockchain.common.BlockchainSdkError import com.tangem.blockchain.common.Token import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.transaction.Fee @@ -89,6 +90,7 @@ internal class TokenFeeCalculator( } } + @Suppress("LongMethod", "CyclomaticComplexMethod") suspend fun calculateTokenFee( walletManager: EthereumWalletManager, tokenForPayFeeStatus: CryptoCurrencyStatus, @@ -119,8 +121,17 @@ internal class TokenFeeCalculator( ) val feeTransferGasLimit = when (feeTransferGasLimitResult) { - is Result.Failure -> raise(GaslessError.DataError(feeTransferGasLimitResult.error)) is Result.Success -> feeTransferGasLimitResult.data + is Result.Failure -> { + // If there is a dust on the balance, the gas limit estimation will fail with code + if (feeTransferGasLimitResult.error is BlockchainSdkError.WrappedThrowable) { + val cause = feeTransferGasLimitResult.error.cause + if (cause is BlockchainSdkError.Ethereum.InsufficientFundsForOperation) { + raise(GaslessError.NotEnoughFunds) + } + } + raise(GaslessError.DataError(feeTransferGasLimitResult.error)) + } }.increaseByPercent(PERCENT_TO_INCREASE_TRANSFER_GASLIMIT) val baseGas = gaslessTransactionRepository.getBaseGasForTransaction() diff --git a/features/send-v2/impl/src/main/java/com/tangem/features/send/v2/feeselector/model/FeeSelectorLogic.kt b/features/send-v2/impl/src/main/java/com/tangem/features/send/v2/feeselector/model/FeeSelectorLogic.kt index b39a4b4717..a3cbaac500 100644 --- a/features/send-v2/impl/src/main/java/com/tangem/features/send/v2/feeselector/model/FeeSelectorLogic.kt +++ b/features/send-v2/impl/src/main/java/com/tangem/features/send/v2/feeselector/model/FeeSelectorLogic.kt @@ -31,6 +31,8 @@ import com.tangem.features.send.v2.api.subcomponents.feeSelector.FeeSelectorRelo import com.tangem.features.send.v2.api.subcomponents.feeSelector.analytics.CommonSendFeeAnalyticEvents import com.tangem.features.send.v2.api.subcomponents.feeSelector.analytics.CommonSendFeeAnalyticEvents.GasPriceInserter import com.tangem.features.send.v2.feeselector.model.transformers.* +import com.tangem.utils.coroutines.JobHolder +import com.tangem.utils.coroutines.saveIn import com.tangem.utils.transformer.update import dagger.assisted.Assisted import dagger.assisted.AssistedFactory @@ -62,6 +64,7 @@ internal class FeeSelectorLogic @AssistedInject constructor( ) : FeeSelectorIntents { private var appCurrency: AppCurrency = AppCurrency.Default + private val loadFeeJobHolder = JobHolder() val uiState = MutableStateFlow(params.state) val isGaslessEnabled = sendFeatureToggles.isGaslessTransactionsEnabled && @@ -113,7 +116,7 @@ internal class FeeSelectorLogic @AssistedInject constructor( ) }, ) - } + }.saveIn(loadFeeJobHolder) } private fun isFeeApproximate(amountType: AmountType): Boolean { diff --git a/gradle/tangem_dependencies.toml b/gradle/tangem_dependencies.toml index 2318c92aa6..0758ec2945 100644 --- a/gradle/tangem_dependencies.toml +++ b/gradle/tangem_dependencies.toml @@ -5,7 +5,7 @@ # https://github.com/tangem/tangem-sdk-android/ # https://github.com/tangem/vico -tangemBlockchainSdk = "releases-5.33-1413" +tangemBlockchainSdk = "releases-5.33-1421" #tangemBlockchainSdk = "0.0.1" # Keep it! - used for local builds tangemCardSdk = "releases-5.33-576" #tangemCardSdk = "0.0.1" # Keep it! - used for local builds ^