Updated on 2026-08-14

This commit is contained in:
Tangem 2023-10-26 11:32:43 +04:00
parent 6d9c467b20
commit 40f24bd65b
13 changed files with 191 additions and 84 deletions

View file

@ -1,5 +1,6 @@
package com.tangem.feature.swap.domain
import com.tangem.domain.tokens.AddCryptoCurrenciesUseCase
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.tokens.repository.CurrenciesRepository
import com.tangem.domain.tokens.repository.NetworksRepository
@ -36,6 +37,11 @@ internal class SwapInteractorImpl @Inject constructor(
private val getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase,
) : SwapInteractor {
// TODO: Move to DI
private val addCryptoCurrenciesUseCase by lazy(LazyThreadSafetyMode.NONE) {
AddCryptoCurrenciesUseCase(currenciesRepository, networksRepository)
}
private val swapCurrencyConverter = SwapCurrencyConverter()
private val amountFormatter = AmountFormatter()
private var derivationPath: String? = null
@ -164,7 +170,7 @@ internal class SwapInteractorImpl @Inject constructor(
): SwapState {
syncWalletBalanceForTokens(networkId, listOf(fromToken, toToken))
val amountDecimal = toBigDecimalOrNull(amountToSwap)
if (amountDecimal == null || amountDecimal.compareTo(BigDecimal.ZERO) == 0) {
if (amountDecimal == null || amountDecimal.signum() == 0) {
return createEmptyAmountState(networkId, fromToken, toToken)
}
val amount = SwapAmount(amountDecimal, getTokenDecimals(fromToken))
@ -283,15 +289,7 @@ internal class SwapInteractorImpl @Inject constructor(
private suspend fun getAndAddCryptoCurrency(userWallet: UserWallet, currency: Currency, network: Network) {
repository.getCryptoCurrency(userWallet, currency, network)?.let { cryptoCurrency ->
currenciesRepository.addCurrencies(
userWallet.walletId,
listOf(cryptoCurrency),
)
networksRepository.getNetworkStatusesSync(
userWalletId = userWallet.walletId,
networks = setOf(cryptoCurrency.network),
refresh = true,
)
addCryptoCurrenciesUseCase(userWallet.walletId, cryptoCurrency)
}
}
@ -567,7 +565,7 @@ internal class SwapInteractorImpl @Inject constructor(
quotesLoadedState: SwapState.QuotesLoadedState,
): SwapState.QuotesLoadedState {
// if token balance ZERO not show permission state to avoid user to spend money for fee
val isTokenZeroBalance = getTokenBalance(networkId, fromToken).value.compareTo(BigDecimal.ZERO) == 0
val isTokenZeroBalance = getTokenBalance(networkId, fromToken).value.signum() == 0
if (isTokenZeroBalance) {
return quotesLoadedState.copy(
permissionState = PermissionDataState.Empty,
@ -711,7 +709,7 @@ internal class SwapInteractorImpl @Inject constructor(
return false
}
val nativeTokenBalance = userWalletManager.getNativeTokenBalance(networkId, derivationPath)
val percentsToFeeIncrease = BigDecimal.valueOf(INCREASE_FEE_TO_CHECK_ENOUGH_PERCENT)
val percentsToFeeIncrease = BigDecimal.ONE
return when (fromToken) {
is Currency.NativeToken -> {
nativeTokenBalance?.let { balance ->
@ -768,6 +766,8 @@ internal class SwapInteractorImpl @Inject constructor(
private const val DEFAULT_SLIPPAGE = 2
private const val ZERO_BALANCE = "0"
private const val DEFAULT_BLOCKCHAIN_INCH_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
@Suppress("UnusedPrivateMember")
private const val INCREASE_FEE_TO_CHECK_ENOUGH_PERCENT = 1.0 // if need to increase fee when check isEnough
private const val INCREASE_GAS_LIMIT_BY = 112 // 12%
private const val USDT_SYMBOL = "USDT"