Updated on 2026-08-14

This commit is contained in:
Tangem 2023-03-06 18:19:26 +03:00
parent fec81d0697
commit 25d39f9f0b
11 changed files with 81 additions and 71 deletions

View file

@ -26,7 +26,7 @@ internal class ReferralInteractorImpl(
if (tokensForReferral.isNotEmpty()) {
val currency = tokensConverter.convert(tokensForReferral.first())
deriveOrAddTokens(currency)
val publicAddress = userWalletManager.getWalletAddress(currency.networkId)
val publicAddress = userWalletManager.getWalletAddress(currency.networkId, null)
return repository.startReferral(
walletId = userWalletManager.getWalletId(),
networkId = currency.networkId,
@ -42,8 +42,8 @@ internal class ReferralInteractorImpl(
if (!derivationManager.hasDerivation(currency.networkId)) {
derivationManager.deriveMissingBlockchains(currency)
}
if (!userWalletManager.isTokenAdded(currency)) {
userWalletManager.addToken(currency)
if (!userWalletManager.isTokenAdded(currency, null)) {
userWalletManager.addToken(currency, null)
}
}

View file

@ -11,6 +11,8 @@ import com.tangem.feature.swap.domain.models.ui.TxState
interface SwapInteractor {
fun initDerivationPath(derivationPath: String?)
/**
* Init tokens to swap, load tokens list available to swap for given network
*

View file

@ -39,6 +39,11 @@ internal class SwapInteractorImpl @Inject constructor(
private val cryptoCurrencyConverter = CryptoCurrencyConverter()
private val amountFormatter = AmountFormatter()
private var derivationPath: String? = null
override fun initDerivationPath(derivationPath: String?) {
this.derivationPath = derivationPath
}
override suspend fun initTokensToSwap(initialCurrency: Currency): TokensDataState {
val networkId = initialCurrency.networkId
@ -63,7 +68,7 @@ internal class SwapInteractorImpl @Inject constructor(
.filter {
!loadedOnWalletsMap.contains(it.symbol)
}
val tokensBalance = userWalletManager.getCurrentWalletTokensBalance(networkId, emptyList())
val tokensBalance = userWalletManager.getCurrentWalletTokensBalance(networkId, emptyList(), derivationPath)
.mapValues { SwapAmount(it.value.value, it.value.decimals) }
val appCurrency = userWalletManager.getUserAppCurrency()
val rates = repository.getRates(appCurrency.code, tokensInWallet.map { it.id })
@ -118,11 +123,12 @@ internal class SwapInteractorImpl @Inject constructor(
gasLimit = approveData.gasLimit,
destinationAddress = approveData.approveModel.toAddress,
dataToSign = approveData.approveModel.data,
derivationPath = derivationPath,
)
return when (result) {
is SendTxResult.Success -> {
allowPermissionsHandler.addAddressToInProgress(forTokenContractAddress)
TxState.TxSent(txAddress = userWalletManager.getLastTransactionHash(networkId) ?: "")
TxState.TxSent(txAddress = userWalletManager.getLastTransactionHash(networkId, derivationPath) ?: "")
}
SendTxResult.UserCancelledError -> TxState.UserCancelled
is SendTxResult.BlockchainSdkError -> TxState.BlockchainError
@ -149,7 +155,7 @@ internal class SwapInteractorImpl @Inject constructor(
val isAllowedToSpend = checkAllowance(networkId, fromTokenAddress)
if (isAllowedToSpend && allowPermissionsHandler.isAddressAllowanceInProgress(fromTokenAddress)) {
allowPermissionsHandler.removeAddressFromProgress(fromTokenAddress)
transactionManager.updateWalletManager(networkId)
transactionManager.updateWalletManager(networkId, derivationPath)
}
val isBalanceWithoutFeeEnough = isBalanceEnough(fromToken, amount, null)
return if (isAllowedToSpend && isBalanceWithoutFeeEnough) {
@ -192,10 +198,11 @@ internal class SwapInteractorImpl @Inject constructor(
destinationAddress = swapStateData.swapModel.transaction.toWalletAddress,
dataToSign = swapStateData.swapModel.transaction.data,
isSwap = true,
derivationPath = derivationPath,
)
return when (result) {
is SendTxResult.Success -> {
userWalletManager.addToken(cryptoCurrencyConverter.convert(currencyToGet))
userWalletManager.addToken(cryptoCurrencyConverter.convert(currencyToGet), derivationPath)
userWalletManager.refreshWallet()
TxState.TxSent(
fromAmount = amountFormatter.formatSwapAmountToUI(
@ -206,7 +213,7 @@ internal class SwapInteractorImpl @Inject constructor(
swapStateData.swapModel.toTokenAmount,
currencyToGet.symbol,
),
txAddress = userWalletManager.getLastTransactionHash(networkId) ?: "",
txAddress = userWalletManager.getLastTransactionHash(networkId, derivationPath) ?: "",
)
}
SendTxResult.UserCancelledError -> TxState.UserCancelled
@ -285,7 +292,7 @@ internal class SwapInteractorImpl @Inject constructor(
val allowance = repository.checkTokensSpendAllowance(
networkId = networkId,
tokenAddress = fromTokenAddress,
walletAddress = userWalletManager.getWalletAddress(networkId),
walletAddress = userWalletManager.getWalletAddress(networkId, derivationPath),
)
return allowance.error == DataError.NoError && allowance.dataModel != ZERO_BALANCE
}
@ -396,6 +403,7 @@ internal class SwapInteractorImpl @Inject constructor(
currencyToSend = cryptoCurrencyConverter.convert(fromToken),
destinationAddress = swapData.transaction.toWalletAddress,
data = swapData.transaction.data,
derivationPath = derivationPath,
)
val feeFiat = getFormattedFiatFee(networkId, feeData.fee.value)
val formattedFee = amountFormatter.formatBigDecimalAmountToUI(
@ -513,6 +521,7 @@ internal class SwapInteractorImpl @Inject constructor(
currencyToSend = userWalletManager.getNativeTokenForNetwork(networkId),
destinationAddress = transactionData.toAddress,
data = transactionData.data,
derivationPath = derivationPath,
)
val feeFiat = getFormattedFiatFee(networkId, feeData.fee.value)
val formattedFee = amountFormatter.formatBigDecimalAmountToUI(
@ -543,6 +552,7 @@ internal class SwapInteractorImpl @Inject constructor(
userWalletManager.getCurrentWalletTokensBalance(
networkId = networkId,
extraTokens = tokensToSync.map { cryptoCurrencyConverter.convert(it) },
derivationPath = derivationPath,
)
cache.cacheBalances(tokensBalance.mapValues { SwapAmount(it.value.value, it.value.decimals) })
}
@ -558,7 +568,7 @@ internal class SwapInteractorImpl @Inject constructor(
}
private fun getWalletAddress(networkId: String): String {
return userWalletManager.getWalletAddress(networkId)
return userWalletManager.getWalletAddress(networkId, derivationPath)
}
private fun getTokenAddress(currency: Currency): String {
@ -581,7 +591,7 @@ internal class SwapInteractorImpl @Inject constructor(
if (fee == null) {
return false
}
val nativeTokenBalance = userWalletManager.getNativeTokenBalance(networkId)
val nativeTokenBalance = userWalletManager.getNativeTokenBalance(networkId, derivationPath)
val percentsToFeeIncrease = BigDecimal.valueOf(INCREASE_FEE_TO_CHECK_ENOUGH_PERCENT)
return when (fromToken) {
is Currency.NativeToken -> {

View file

@ -72,5 +72,6 @@ class SwapFragment : Fragment() {
companion object {
const val CURRENCY_BUNDLE_KEY = "swap_currency"
const val DERIVATION_PATH = "DERIVATION_STYLE"
}
}

View file

@ -52,6 +52,7 @@ internal class SwapViewModel @Inject constructor(
savedStateHandle[SwapFragment.CURRENCY_BUNDLE_KEY]
?: error("no expected parameter Currency found"),
)
private val derivationPath = savedStateHandle.get<String>(SwapFragment.DERIVATION_PATH)
private val stateBuilder = StateBuilder(
actions = createUiActions(),
@ -78,6 +79,7 @@ internal class SwapViewModel @Inject constructor(
get() = swapRouter.currentScreen
init {
swapInteractor.initDerivationPath(derivationPath)
initTokens(currency)
}