Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-06 15:09:48 +03:00
parent eec7f2ffc0
commit cff0f51c84
4 changed files with 30 additions and 8 deletions

View file

@ -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<GetFeeError>.findTokensToPayFee(
private suspend fun findTokensToPayFee(
walletManager: EthereumWalletManager,
initialTxFee: TransactionFee,
nativeCurrencyStatus: CryptoCurrencyStatus,
networkCurrenciesStatuses: List<CryptoCurrencyStatus>,
): TransactionFeeExtended {
): Either<GetFeeError, TransactionFeeExtended> = 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()
)
}
}

View file

@ -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()