From 842b3e494ad39fec2ea76155c98fb47c0f57b6a7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 21 Aug 2024 18:11:39 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../walletconnect/WalletConnectSdkHelper.kt | 60 ++++++++++++------- .../dialogs/TransactionDialog.kt | 4 +- gradle/dependencies.toml | 2 +- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect/WalletConnectSdkHelper.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect/WalletConnectSdkHelper.kt index 3e8529f150..3914707cc3 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect/WalletConnectSdkHelper.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect/WalletConnectSdkHelper.kt @@ -50,9 +50,7 @@ class WalletConnectSdkHelper { @Suppress("MagicNumber") suspend fun prepareTransactionData(data: EthTransactionData): WcTransactionData { val transaction = data.transaction - val blockchain = requireNotNull(Blockchain.fromNetworkId(data.networkId)) { - "Blockchain not found" - } + val blockchain = requireNotNull(Blockchain.fromNetworkId(data.networkId)) { "Blockchain not found" } val walletManager = requireNotNull(getWalletManager(blockchain, data.rawDerivationPath)) { "WalletManager not found" } @@ -73,27 +71,31 @@ class WalletConnectSdkHelper { "Transaction amount is null" } + // TODO move fee calculation to SDK getFee() [REDACTED_JIRA] val gasLimit = getGasLimitFromTx(value, walletManager, transaction) + val gasPrice = getGasPrice(walletManager, transaction) - val gasPrice = transaction.gasPrice?.hexToBigDecimal() - ?: when (val result = (walletManager as? EthereumGasLoader)?.getGasPrice()) { - is Result.Success -> result.data.toBigDecimal() - is Result.Failure -> { - (result.error as? Throwable)?.let { Timber.e(it, "getGasPrice failed") } - - error("Unable to get gas price: ${result.error}") - } - null -> error("Gas price is null") - } - - val fee = (gasLimit * gasPrice).movePointLeft(decimals) - val total = value + fee + val feeDecimal = (gasLimit * gasPrice).movePointLeft(decimals) + val total = value + feeDecimal + val feeAmount = Amount(feeDecimal, wallet.blockchain) val destinationAddress = requireNotNull(transaction.to) { "Destination address is null" } + val fee = if (blockchain.isEvm()) { + // TODO [REDACTED_JIRA] + // workaround for Mantle, remove after [REDACTED_JIRA] + val patchedAmount = if (blockchain == Blockchain.Mantle) { + feeAmount.copy(value = feeAmount.value?.multiply(MANTLE_FEE_ESTIMATE_MULTIPLIER)) + } else { + feeAmount + } + Fee.Ethereum(patchedAmount, gasLimit.toBigInteger(), gasPrice.toBigInteger()) + } else { + Fee.Common(feeAmount) + } val transactionData = TransactionData.Uncompiled( amount = Amount(value, wallet.blockchain), - fee = Fee.Common(Amount(fee, wallet.blockchain)), + fee = fee, sourceAddress = transaction.from, destinationAddress = destinationAddress, extras = EthereumTransactionExtras( @@ -107,7 +109,7 @@ class WalletConnectSdkHelper { dAppName = data.metaName, dAppUrl = data.metaUrl, amount = value.toFormattedString(decimals), - gasAmount = fee.toFormattedString(decimals), + feeAmount = feeDecimal.toFormattedString(decimals), totalAmount = total.toFormattedString(decimals), balance = balance.toFormattedString(decimals), isEnoughFundsToSend = balance - total >= BigDecimal.ZERO, @@ -148,6 +150,22 @@ class WalletConnectSdkHelper { } } + private suspend fun getGasPrice(walletManager: WalletManager, transaction: WcEthereumTransaction): BigDecimal { + val txGasPrice = transaction.gasPrice?.hexToBigDecimal() + if (txGasPrice != null) { + return txGasPrice + } + return when (val result = (walletManager as? EthereumGasLoader)?.getGasPrice()) { + is Result.Success -> result.data.toBigDecimal() + is Result.Failure -> { + (result.error as? Throwable)?.let { Timber.e(it, "getGasPrice failed") } + + error("Unable to get gas price: ${result.error}") + } + null -> error("Gas price is null") + } + } + private suspend fun getGasLimitFromTx( value: BigDecimal, walletManager: WalletManager, @@ -155,14 +173,14 @@ class WalletConnectSdkHelper { ): BigDecimal { return transaction.gas?.hexToBigDecimal() ?: transaction.gasLimit?.hexToBigDecimal() - ?: getGaLimitFromBlockchain( + ?: getGasLimitFromBlockchain( value = value, walletManager = walletManager, transaction = transaction, ) } - private suspend fun getGaLimitFromBlockchain( + private suspend fun getGasLimitFromBlockchain( value: BigDecimal, walletManager: WalletManager, transaction: WcEthereumTransaction, @@ -418,5 +436,7 @@ class WalletConnectSdkHelper { const val ETH_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n" const val HEX_PREFIX = "0x" const val DEFAULT_MAX_GASLIMIT = 350000 + // TODO remove after [REDACTED_JIRA] + private val MANTLE_FEE_ESTIMATE_MULTIPLIER = BigDecimal("1.6") } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/walletconnect/dialogs/TransactionDialog.kt b/app/src/main/java/com/tangem/tap/features/details/ui/walletconnect/dialogs/TransactionDialog.kt index 3ec2d8bfda..4fd49641fb 100644 --- a/app/src/main/java/com/tangem/tap/features/details/ui/walletconnect/dialogs/TransactionDialog.kt +++ b/app/src/main/java/com/tangem/tap/features/details/ui/walletconnect/dialogs/TransactionDialog.kt @@ -18,7 +18,7 @@ object TransactionDialog { data.dAppName, data.dAppUrl, data.amount, - data.gasAmount, + data.feeAmount, data.totalAmount, data.balance, ) @@ -51,7 +51,7 @@ data class TransactionRequestDialogData( val dAppName: String, val dAppUrl: String, val amount: String, - val gasAmount: String, + val feeAmount: String, val totalAmount: String, val balance: String, val isEnoughFundsToSend: Boolean, diff --git a/gradle/dependencies.toml b/gradle/dependencies.toml index 6a52565547..b259900751 100644 --- a/gradle/dependencies.toml +++ b/gradle/dependencies.toml @@ -88,7 +88,7 @@ markdown = "0.7.2" # endregion Other libraries # region Tangem -tangemBlockchainSdk = "release-app_5.14-742" +tangemBlockchainSdk = "release-app_5.14-747" #tangemBlockchainSdk = "0.0.1" # Keep it! - used for local builds tangemCardSdk = "release-app_5.14-379" #tangemCardSdk = "0.0.1" # Keep it! - used for local builds ^