Updated on 2026-08-14

This commit is contained in:
Tangem 2025-09-23 20:21:48 +05:00
parent 043602ebd6
commit dd362fb32c
21 changed files with 133 additions and 184 deletions

View file

@ -1,6 +1,6 @@
package com.tangem.feature.swap.domain.models.domain
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.currency.CryptoCurrencyStatus
import com.tangem.feature.swap.domain.models.ui.RequestApproveStateData
import com.tangem.feature.swap.domain.models.ui.TxFee
@ -9,14 +9,14 @@ import com.tangem.feature.swap.domain.models.ui.TxFee
*
* @param approveData tx data to give approve, it loaded from 1inch in findBestQuote if needed
* @param forTokenContractAddress token contract address for which needs permission
* @param fromToken which token will be swapping
* @param fromTokenStatus which token will be swapping
* @param approveType unlimited or tx amount approve
* @param txFee fee for tx
*/
data class PermissionOptions(
val approveData: RequestApproveStateData,
val forTokenContractAddress: String,
val fromToken: CryptoCurrency,
val fromTokenStatus: CryptoCurrencyStatus,
val spenderAddress: String,
val approveType: SwapApproveType,
val txFee: TxFee,

View file

@ -1,5 +1,6 @@
package com.tangem.feature.swap.domain
import android.util.Base64
import arrow.core.Either
import arrow.core.getOrElse
import com.tangem.blockchain.common.Amount
@ -11,6 +12,7 @@ import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.blockchainsdk.utils.toBlockchain
import com.tangem.blockchainsdk.utils.toNetworkId
import com.tangem.core.ui.format.bigdecimal.fiat
import com.tangem.core.ui.format.bigdecimal.format
import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase
@ -52,8 +54,6 @@ import timber.log.Timber
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import android.util.Base64
import com.tangem.blockchainsdk.utils.toNetworkId
@Suppress("LargeClass", "LongParameterList")
internal class SwapInteractorImpl @AssistedInject constructor(
@ -227,7 +227,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
val approveTransaction = createApprovalTransactionUseCase(
fee = permissionOptions.txFee.fee,
userWalletId = userWalletId,
cryptoCurrency = permissionOptions.fromToken as CryptoCurrency.Token,
cryptoCurrencyStatus = permissionOptions.fromTokenStatus,
amount = amount?.value,
contractAddress = permissionOptions.forTokenContractAddress,
spenderAddress = permissionOptions.spenderAddress,
@ -239,7 +239,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
val result = sendTransactionUseCase(
txData = approveTransaction,
userWallet = userWallet,
network = permissionOptions.fromToken.network,
network = permissionOptions.fromTokenStatus.currency.network,
)
return result.fold(
ifRight = { hash ->
@ -546,7 +546,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
)
val result = validateTransactionUseCase(
amount = amount.value.convertToSdkAmount(currency),
amount = amount.value.convertToSdkAmount(fromToken),
fee = fee,
memo = null,
destination = getTokenAddress(fromToken.currency),
@ -842,7 +842,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
}
val txData = createTransferTransactionUseCase(
amount = amount.value.convertToSdkAmount(currencyToSend.currency),
amount = amount.value.convertToSdkAmount(currencyToSend),
fee = txFee.fee,
memo = exchangeDataCex.txExtraId,
destination = exchangeDataCex.txTo,
@ -1022,7 +1022,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
val txFeeResult = getUnhandledFee(
amount = amount.value,
userWallet = userWallet,
cryptoCurrency = fromToken,
cryptoCurrencyStatus = fromTokenStatus,
)
val txFee = if (provider.type == ExchangeProviderType.CEX) {
@ -1520,12 +1520,12 @@ internal class SwapInteractorImpl @AssistedInject constructor(
private suspend fun getUnhandledFee(
amount: BigDecimal,
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
cryptoCurrencyStatus: CryptoCurrencyStatus,
): Either<GetFeeError, TransactionFee> {
return estimateFeeUseCase(
amount = amount,
userWallet = userWallet,
cryptoCurrency = cryptoCurrency,
cryptoCurrencyStatus = cryptoCurrencyStatus,
)
}
@ -1560,7 +1560,7 @@ internal class SwapInteractorImpl @AssistedInject constructor(
// setting up amount for approve with given amount for swap [SwapApproveType.Limited]
val callData = SmartContractCallDataProviderFactory.getApprovalCallData(
spenderAddress = requireNotNull(spenderAddress) { "Spender address is null" },
amount = swapAmount.value.convertToSdkAmount(fromToken),
amount = swapAmount.value.convertToSdkAmount(fromTokenStatus),
blockchain = fromToken.network.toBlockchain(),
)
val feeData = try {

View file

@ -700,9 +700,11 @@ internal class SwapModel @Inject constructor(
private fun givePermissionsToSwap() {
modelScope.launch(dispatchers.main) {
runCatching {
val fromToken = requireNotNull(dataState.fromCryptoCurrency?.currency) {
"dataState.fromCurrency might not be null"
val fromCryptoCurrency = requireNotNull(dataState.fromCryptoCurrency) {
"dataState.fromCryptoCurrency might not be null"
}
val fromToken = fromCryptoCurrency.currency
val approveDataModel = requireNotNull(dataState.approveDataModel) {
"dataState.approveDataModel.spenderAddress shouldn't be null"
}
@ -725,7 +727,7 @@ internal class SwapModel @Inject constructor(
permissionOptions = PermissionOptions(
approveData = approveDataModel,
forTokenContractAddress = (fromToken as? CryptoCurrency.Token)?.contractAddress.orEmpty(),
fromToken = fromToken,
fromTokenStatus = fromCryptoCurrency,
approveType = approveType,
txFee = feeForPermission,
spenderAddress = approveDataModel.spenderAddress,