Updated on 2026-08-14

This commit is contained in:
Tangem 2025-09-22 18:47:08 +05:00
parent 9860ecc4cb
commit 5c11931241
21 changed files with 541 additions and 63 deletions

View file

@ -1,7 +1,13 @@
package com.tangem.data.transaction
import com.tangem.blockchain.blockchains.ethereum.EthereumWalletManager
import com.tangem.blockchain.blockchains.ethereum.eip1559.isSupportEIP1559
import com.tangem.blockchain.blockchains.ethereum.network.EthereumFeeHistory
import com.tangem.blockchain.common.Amount
import com.tangem.blockchain.common.AmountType
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.TransactionData
import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchainsdk.utils.toBlockchain
@ -12,6 +18,8 @@ import com.tangem.domain.models.network.Network
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.transaction.FeeRepository
import com.tangem.domain.walletmanager.WalletManagersFacade
import java.math.BigDecimal
import java.math.BigInteger
internal class DefaultFeeRepository(
private val walletManagersFacade: WalletManagersFacade,
@ -22,6 +30,53 @@ internal class DefaultFeeRepository(
return networkId.toBlockchain().isFeeApproximate(amountType)
}
override suspend fun getEthereumFeeWithoutGas(
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
): Fee.Ethereum {
val walletManager = walletManagersFacade.getOrCreateWalletManager(
userWalletId = userWallet.walletId,
network = cryptoCurrency.network,
)
val blockchain = cryptoCurrency.network.toBlockchain()
val ethereumWalletManager = walletManager as? EthereumWalletManager
?: error("Not supported for ${cryptoCurrency.network}")
val fee = if (blockchain.isSupportEIP1559) {
val gasHistory = when (val gasHistory = ethereumWalletManager.getGasHistory()) {
is Result.Failure -> throw gasHistory.error
is Result.Success -> gasHistory.data
}
val marketPriorityFee = when (gasHistory) {
is EthereumFeeHistory.Common -> gasHistory.marketPriorityFee
is EthereumFeeHistory.Fallback -> gasHistory.gasPrice.toBigDecimal() * MULTIPLIER_GAS_PRICE_NORMAL_FEE
}
val maxFeePerGas = gasHistory.baseFee * MULTIPLIER_GAS_PRICE_NORMAL_FEE + marketPriorityFee
getEthEip1559Fee(
maxFeePerGas = maxFeePerGas.toBigInteger(),
priorityFee = marketPriorityFee.toBigInteger(),
blockchain = blockchain,
)
} else {
val gasPrice = when (val gasPrice = ethereumWalletManager.getGasPrice()) {
is Result.Failure -> throw gasPrice.error
is Result.Success -> gasPrice.data
}
getEthLegacyFee(
gasPrice = gasPrice,
blockchain = blockchain,
)
}
return fee
}
override suspend fun calculateFee(
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
@ -54,4 +109,37 @@ internal class DefaultFeeRepository(
?: error("WalletManager is null"),
)
}
private fun getEthLegacyFee(gasPrice: BigInteger, blockchain: Blockchain): Fee.Ethereum.Legacy {
val amount = Amount(
value = BigDecimal.ZERO,
blockchain = blockchain,
)
return Fee.Ethereum.Legacy(
amount = amount,
gasLimit = BigInteger.ZERO,
gasPrice = gasPrice,
)
}
private fun getEthEip1559Fee(
maxFeePerGas: BigInteger,
priorityFee: BigInteger,
blockchain: Blockchain,
): Fee.Ethereum.EIP1559 {
val amount = Amount(
value = BigDecimal.ZERO,
blockchain = blockchain,
)
return Fee.Ethereum.EIP1559(
amount = amount,
gasLimit = BigInteger.ZERO,
maxFeePerGas = maxFeePerGas,
priorityFee = priorityFee,
)
}
private companion object {
val MULTIPLIER_GAS_PRICE_NORMAL_FEE = "1.2".toBigDecimal() // 120%
}
}