diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSendTransactionUseCase.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSendTransactionUseCase.kt index ccce4e7bad..4b80dbfbd4 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSendTransactionUseCase.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSendTransactionUseCase.kt @@ -3,7 +3,7 @@ package com.tangem.data.walletconnect.network.ethereum import arrow.core.left import com.tangem.blockchain.blockchains.ethereum.EthereumTransactionExtras import com.tangem.blockchain.blockchains.ethereum.tokenmethods.ApprovalERC20TokenCallData -import com.tangem.blockchain.common.Amount +import com.tangem.blockchain.common.Amount as BlockchainAmount import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.transaction.Fee import com.tangem.blockchain.extensions.formatHex @@ -15,7 +15,9 @@ import com.tangem.data.walletconnect.sign.SignStateConverter.toResult import com.tangem.data.walletconnect.sign.WcMethodUseCaseContext import com.tangem.data.walletconnect.utils.BlockAidVerificationDelegate import com.tangem.domain.core.lce.LceFlow +import com.tangem.domain.tokens.model.Amount import com.tangem.domain.transaction.usecase.SendTransactionUseCase +import com.tangem.domain.walletconnect.model.WcApprovedAmount import com.tangem.domain.walletconnect.model.WcEthMethod import com.tangem.domain.walletconnect.usecase.method.* import dagger.assisted.Assisted @@ -35,7 +37,7 @@ internal class WcEthSendTransactionUseCase @AssistedInject constructor( WcApproval, WcMutableFee { - private var approvalAmount: Amount? = null + private var approvalAmount: WcApprovedAmount? = null private var dAppFee = WcEthTxHelper.getDAppFee( network = context.network, txParams = method.transaction, @@ -53,13 +55,19 @@ internal class WcEthSendTransactionUseCase @AssistedInject constructor( val amount = WcEthTxHelper.getApprovedAmount(method.transaction.data, result) ?: return@map BlockAidTransactionCheck.Result.Plain(result) val tokenInfo = amount.tokenInfo - if (!amount.isUnlimited) { - this@WcEthSendTransactionUseCase.approvalAmount = Amount( - currencySymbol = tokenInfo.symbol, - decimals = tokenInfo.decimals, - value = amount.approvedAmount, - ) - } + this@WcEthSendTransactionUseCase.approvalAmount = WcApprovedAmount( + amount = if (!amount.isUnlimited) { + Amount( + currencySymbol = tokenInfo.symbol, + decimals = tokenInfo.decimals, + value = amount.approvedAmount, + ) + } else { + null + }, + logoUrl = tokenInfo.logoUrl, + chainId = tokenInfo.chainId, + ) BlockAidTransactionCheck.Result.Approval( result = result, approval = this, @@ -90,7 +98,9 @@ internal class WcEthSendTransactionUseCase @AssistedInject constructor( val extras = uncompiled.extras as EthereumTransactionExtras val callData = ApprovalERC20TokenCallData( spenderAddress = uncompiled.sourceAddress, - amount = action.amount, + amount = action.amount?.amount?.let { + BlockchainAmount(currencySymbol = it.currencySymbol, decimals = it.decimals, value = it.value) + }, ) approvalAmount = action.amount dAppFee = null @@ -119,11 +129,11 @@ internal class WcEthSendTransactionUseCase @AssistedInject constructor( emitAll(delegate.invoke(transactionData)) } - override fun getAmount(): Amount? { + override fun getAmount(): WcApprovedAmount? { return approvalAmount } - override fun updateAmount(amount: Amount?) { + override fun updateAmount(amount: WcApprovedAmount?) { middleAction(WcEthTxAction.UpdateApprovalAmount(amount)) } diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSignTransactionUseCase.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSignTransactionUseCase.kt index ed984c971c..b86988cbae 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSignTransactionUseCase.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthSignTransactionUseCase.kt @@ -3,7 +3,7 @@ package com.tangem.data.walletconnect.network.ethereum import arrow.core.left import com.tangem.blockchain.blockchains.ethereum.EthereumTransactionExtras import com.tangem.blockchain.blockchains.ethereum.tokenmethods.ApprovalERC20TokenCallData -import com.tangem.blockchain.common.Amount +import com.tangem.blockchain.common.Amount as BlockchainAmount import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.transaction.Fee import com.tangem.blockchain.extensions.formatHex @@ -15,13 +15,11 @@ import com.tangem.data.walletconnect.sign.SignCollector import com.tangem.data.walletconnect.sign.SignStateConverter.toResult import com.tangem.data.walletconnect.sign.WcMethodUseCaseContext import com.tangem.data.walletconnect.utils.BlockAidVerificationDelegate +import com.tangem.domain.tokens.model.Amount import com.tangem.domain.transaction.usecase.PrepareForSendUseCase +import com.tangem.domain.walletconnect.model.WcApprovedAmount import com.tangem.domain.walletconnect.model.WcEthMethod -import com.tangem.domain.walletconnect.usecase.method.BlockAidTransactionCheck -import com.tangem.domain.walletconnect.usecase.method.WcApproval -import com.tangem.domain.walletconnect.usecase.method.WcMutableFee -import com.tangem.domain.walletconnect.usecase.method.WcSignState -import com.tangem.domain.walletconnect.usecase.method.WcTransactionUseCase +import com.tangem.domain.walletconnect.usecase.method.* import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject @@ -43,7 +41,7 @@ internal class WcEthSignTransactionUseCase @AssistedInject constructor( WcApproval, WcMutableFee { - private var approvalAmount: Amount? = null + private var approvalAmount: WcApprovedAmount? = null private var dAppFee = WcEthTxHelper.getDAppFee( network = context.network, txParams = method.transaction, @@ -61,10 +59,14 @@ internal class WcEthSignTransactionUseCase @AssistedInject constructor( ?: return@map BlockAidTransactionCheck.Result.Plain(result) val tokenInfo = amount.tokenInfo if (!amount.isUnlimited) { - approvalAmount = Amount( - currencySymbol = tokenInfo.symbol, - decimals = tokenInfo.decimals, - value = amount.approvedAmount, + approvalAmount = WcApprovedAmount( + amount = Amount( + currencySymbol = tokenInfo.symbol, + decimals = tokenInfo.decimals, + value = amount.approvedAmount, + ), + logoUrl = tokenInfo.logoUrl, + chainId = tokenInfo.chainId, ) } BlockAidTransactionCheck.Result.Approval( @@ -98,7 +100,9 @@ internal class WcEthSignTransactionUseCase @AssistedInject constructor( val extras = uncompiled.extras as EthereumTransactionExtras val callData = ApprovalERC20TokenCallData( spenderAddress = uncompiled.sourceAddress, - amount = action.amount, + amount = action.amount?.amount?.let { + BlockchainAmount(currencySymbol = it.currencySymbol, decimals = it.decimals, value = it.value) + }, ) approvalAmount = action.amount dAppFee = null @@ -127,11 +131,11 @@ internal class WcEthSignTransactionUseCase @AssistedInject constructor( return dAppFee } - override fun getAmount(): Amount? { + override fun getAmount(): WcApprovedAmount? { return approvalAmount } - override fun updateAmount(amount: Amount?) { + override fun updateAmount(amount: WcApprovedAmount?) { middleAction(WcEthTxAction.UpdateApprovalAmount(amount)) } diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthTxHelper.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthTxHelper.kt index a2fc3d4224..268633a213 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthTxHelper.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/network/ethereum/WcEthTxHelper.kt @@ -16,6 +16,7 @@ import com.tangem.blockchain.extensions.hexToBigDecimal import com.tangem.blockchainsdk.utils.toBlockchain import com.tangem.common.extensions.hexToBytes import com.tangem.domain.models.network.Network +import com.tangem.domain.walletconnect.model.WcApprovedAmount import com.tangem.domain.walletconnect.model.WcEthTransactionParams import java.math.BigDecimal @@ -81,5 +82,5 @@ internal object WcEthTxHelper { sealed interface WcEthTxAction { data class UpdateFee(val fee: Fee) : WcEthTxAction - data class UpdateApprovalAmount(val amount: Amount?) : WcEthTxAction + data class UpdateApprovalAmount(val amount: WcApprovedAmount?) : WcEthTxAction } \ No newline at end of file diff --git a/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcApprovedAmount.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcApprovedAmount.kt new file mode 100644 index 0000000000..5b7dc55b23 --- /dev/null +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcApprovedAmount.kt @@ -0,0 +1,9 @@ +package com.tangem.domain.walletconnect.model + +import com.tangem.domain.tokens.model.Amount + +data class WcApprovedAmount( + val amount: Amount?, + val chainId: Int?, + val logoUrl: String?, +) \ No newline at end of file diff --git a/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/usecase/method/WcTransactionUseCase.kt b/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/usecase/method/WcTransactionUseCase.kt index c4410c3a7f..acc4686c4d 100644 --- a/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/usecase/method/WcTransactionUseCase.kt +++ b/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/usecase/method/WcTransactionUseCase.kt @@ -1,8 +1,8 @@ package com.tangem.domain.walletconnect.usecase.method -import com.tangem.blockchain.common.Amount import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.transaction.Fee +import com.tangem.domain.walletconnect.model.WcApprovedAmount /** * Base UseCase for wc methods with single TransactionData @@ -40,10 +40,10 @@ interface WcMutableFee { /** * Wc Approval Method - * If [Amount] is null it means unlimited + * If [WcApprovedAmount.amount] is null it means unlimited * [updateAmount] triggered a new [TransactionData] emit */ interface WcApproval { - fun getAmount(): Amount? - fun updateAmount(amount: Amount?) + fun getAmount(): WcApprovedAmount? + fun updateAmount(amount: WcApprovedAmount?) } \ No newline at end of file diff --git a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/model/WcSendTransactionModel.kt b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/model/WcSendTransactionModel.kt index 3f9908da90..4cde43d227 100644 --- a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/model/WcSendTransactionModel.kt +++ b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/model/WcSendTransactionModel.kt @@ -105,11 +105,11 @@ internal class WcSendTransactionModel @Inject constructor( this@WcSendTransactionModel.signState = signState val isSecurityCheckContent = securityCheck is Lce.Content - var isApprovalMethod = isSecurityCheckContent && + val isApprovalMethod = isSecurityCheckContent && securityCheck.content is BlockAidTransactionCheck.Result.Approval wcApproval = useCase as? WcApproval sign = { useCase.sign() } - buildUiState(securityCheck, useCase, signState) + buildUiState(securityCheck, useCase, signState, isApprovalMethod) } } else -> unknownMethodRunnable() @@ -144,10 +144,14 @@ internal class WcSendTransactionModel @Inject constructor( securityCheck: Lce, useCase: WcSignUseCase<*>, signState: WcSignState<*>, + isApproval: Boolean, ) { val blockAidState = when (securityCheck) { is Lce.Content -> blockAidUiConverter.convert( - WcSendAndReceiveBlockAidUiConverter.Input(securityCheck.content.result, wcApproval?.getAmount()), + WcSendAndReceiveBlockAidUiConverter.Input( + securityCheck.content.result, + if (isApproval) wcApproval?.getAmount() else null, + ), ) is Lce.Error -> WcSendReceiveTransactionCheckResultsUM(isLoading = false) is Lce.Loading -> WcSendReceiveTransactionCheckResultsUM(isLoading = true) @@ -188,9 +192,9 @@ internal class WcSendTransactionModel @Inject constructor( } fun onClickDoneCustomAllowance(value: BigDecimal, isUnlimited: Boolean) { - val maxValue = if (isUnlimited) Double.MAX_VALUE.toBigDecimal() else value + val newValue = if (isUnlimited) null else value wcApproval?.getAmount()?.let { currentAmount -> - wcApproval?.updateAmount(currentAmount.copy(maxValue = maxValue)) + wcApproval?.updateAmount(currentAmount.copy(amount = currentAmount.amount?.copy(value = newValue))) } } diff --git a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSendAndReceiveBlockAidUiConverter.kt b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSendAndReceiveBlockAidUiConverter.kt index 14d3abede0..aa9eb45eb3 100644 --- a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSendAndReceiveBlockAidUiConverter.kt +++ b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSendAndReceiveBlockAidUiConverter.kt @@ -5,10 +5,10 @@ import com.domain.blockaid.models.transaction.SimulationResult import com.domain.blockaid.models.transaction.ValidationResult import com.domain.blockaid.models.transaction.simultation.AmountInfo import com.domain.blockaid.models.transaction.simultation.SimulationData -import com.tangem.blockchain.common.Amount import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.format.bigdecimal.crypto import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.domain.walletconnect.model.WcApprovedAmount import com.tangem.features.walletconnect.impl.R import com.tangem.features.walletconnect.transaction.entity.blockaid.WcEstimatedWalletChangeUM import com.tangem.features.walletconnect.transaction.entity.blockaid.WcEstimatedWalletChangesUM @@ -86,7 +86,7 @@ internal class WcSendAndReceiveBlockAidUiConverter @Inject constructor( when (data) { is SimulationData.SendAndReceive -> null is SimulationData.Approve -> value.approvedAmount?.let { - data.approvedAmounts.map { spendAllowanceUMConverter.convert(it) }.firstOrNull() + spendAllowanceUMConverter.convert(it) } } }, @@ -95,7 +95,7 @@ internal class WcSendAndReceiveBlockAidUiConverter @Inject constructor( data class Input( val result: CheckTransactionResult, - val approvedAmount: Amount?, + val approvedAmount: WcApprovedAmount?, ) } diff --git a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSpendAllowanceUMConverter.kt b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSpendAllowanceUMConverter.kt index 96d797734c..4f7bf34268 100644 --- a/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSpendAllowanceUMConverter.kt +++ b/features/walletconnect/impl/src/main/kotlin/com/tangem/features/walletconnect/transaction/ui/blockaid/WcSpendAllowanceUMConverter.kt @@ -1,25 +1,29 @@ package com.tangem.features.walletconnect.transaction.ui.blockaid -import com.domain.blockaid.models.transaction.simultation.ApprovedAmount import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.getActiveIconRes +import com.tangem.domain.walletconnect.model.WcApprovedAmount import com.tangem.features.walletconnect.impl.R import com.tangem.features.walletconnect.transaction.entity.approve.WcSpendAllowanceUM import com.tangem.utils.converter.Converter import javax.inject.Inject -internal class WcSpendAllowanceUMConverter @Inject constructor() : Converter { +internal class WcSpendAllowanceUMConverter @Inject constructor() : Converter { - override fun convert(value: ApprovedAmount) = WcSpendAllowanceUM( - amountValue = value.approvedAmount, - amountText = if (value.isUnlimited) { - TextReference.Res(R.string.wc_common_unlimited) - } else { - TextReference.Str(value.approvedAmount.amountText()) - }, - isUnlimited = value.isUnlimited, - tokenSymbol = value.tokenInfo.symbol, - tokenImageUrl = value.tokenInfo.logoUrl, - networkIconRes = value.tokenInfo.chainId?.toString()?.let { getActiveIconRes(it) }, - ) + override fun convert(value: WcApprovedAmount): WcSpendAllowanceUM { + val amount = value.amount?.value ?: 0.0.toBigDecimal() + val isUnlimited = value.amount?.value == null + return WcSpendAllowanceUM( + amountValue = amount, + amountText = if (value.amount?.value == null) { + TextReference.Res(R.string.wc_common_unlimited) + } else { + TextReference.Str(amount.amountText()) + }, + isUnlimited = isUnlimited, + tokenSymbol = value.amount?.currencySymbol ?: "", + tokenImageUrl = value.logoUrl, + networkIconRes = value.chainId?.toString()?.let { getActiveIconRes(it) }, + ) + } } \ No newline at end of file