Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-03 11:12:10 +04:00
parent 42b4c894bb
commit bec5334529
2 changed files with 25 additions and 26 deletions

View file

@ -55,7 +55,8 @@ internal class CustomTokenFormModel @Inject constructor(
private val params: CustomTokenFormComponent.Params = paramsContainer.require()
private var createdCurrency: CryptoCurrency? = null
private var useCasesFacade: CustomTokenFormUseCasesFacade = customTokenFormUseCasesFacadeFactory.create(params.mode)
private val useCasesFacade: CustomTokenFormUseCasesFacade =
customTokenFormUseCasesFacadeFactory.create(params.mode.userWalletId)
private val customCurrencyValidator = CustomCurrencyValidator(
userWalletId = params.mode.userWalletId,
useCasesFacade = useCasesFacade,

View file

@ -18,9 +18,9 @@ import com.tangem.domain.models.account.AccountId
import com.tangem.domain.models.account.DerivationIndex
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.tokens.AddCryptoCurrenciesUseCase
import com.tangem.domain.wallets.usecase.DerivePublicKeysUseCase
import com.tangem.features.managetokens.component.AddCustomTokenMode
import com.tangem.lib.crypto.derivation.AccountNodeRecognizer
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
@ -29,7 +29,7 @@ import timber.log.Timber
@Suppress("LongParameterList")
internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
@Assisted private val mode: AddCustomTokenMode,
@Assisted private val userWalletId: UserWalletId,
private val addCryptoCurrenciesUseCase: AddCryptoCurrenciesUseCase,
private val derivePublicKeysUseCase: DerivePublicKeysUseCase,
private val checkIsCurrencyNotAddedUseCase: CheckIsCurrencyNotAddedUseCase,
@ -39,26 +39,24 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
private val accountsFeatureToggles: AccountsFeatureToggles,
) {
suspend fun addCryptoCurrenciesUseCase(currency: CryptoCurrency): Either<Throwable, Unit> = when (mode) {
is AddCustomTokenMode.Account -> either {
val accountId = getAccountId(currency)
suspend fun addCryptoCurrenciesUseCase(currency: CryptoCurrency): Either<Throwable, Unit> {
return if (accountsFeatureToggles.isFeatureEnabled) {
either {
val accountId = getAccountId(currency)
manageCryptoCurrenciesUseCase(accountId = accountId, add = currency).bind()
}
is AddCustomTokenMode.Wallet -> {
addCryptoCurrenciesUseCase.invoke(
userWalletId = mode.userWalletId,
currency = currency,
)
manageCryptoCurrenciesUseCase(accountId = accountId, add = currency).bind()
}
} else {
addCryptoCurrenciesUseCase.invoke(userWalletId = userWalletId, currency = currency)
}
}
suspend fun derivePublicKeysUseCase(currencies: List<CryptoCurrency>): Either<Throwable, Unit> = when (mode) {
is AddCustomTokenMode.Account -> Unit.right()
is AddCustomTokenMode.Wallet -> derivePublicKeysUseCase.invoke(
userWalletId = mode.userWalletId,
currencies = currencies,
)
suspend fun derivePublicKeysUseCase(currencies: List<CryptoCurrency>): Either<Throwable, Unit> {
return if (accountsFeatureToggles.isFeatureEnabled) {
Unit.right()
} else {
derivePublicKeysUseCase.invoke(userWalletId = userWalletId, currencies = currencies)
}
}
suspend fun checkIsCurrencyNotAddedUseCase(
@ -67,7 +65,7 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
contractAddress: String?,
): Either<Throwable, Boolean> = if (accountsFeatureToggles.isFeatureEnabled) {
getAccountCurrencyStatusUseCase.invokeSync(
userWalletId = mode.userWalletId,
userWalletId = userWalletId,
networkId = networkId,
derivationPath = derivationPath,
contractAddress = contractAddress,
@ -76,7 +74,7 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
.right()
} else {
checkIsCurrencyNotAddedUseCase.invoke(
userWalletId = mode.userWalletId,
userWalletId = userWalletId,
networkId = networkId,
derivationPath = derivationPath,
contractAddress = contractAddress,
@ -85,15 +83,15 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
private suspend fun Raise<Throwable>.getAccountId(currency: CryptoCurrency): AccountId {
val accountList = singleAccountListSupplier.getSyncOrNull(
params = SingleAccountListProducer.Params(userWalletId = mode.userWalletId),
params = SingleAccountListProducer.Params(userWalletId = userWalletId),
)
ensureNotNull(accountList) {
IllegalStateException("Account list not found: ${mode.userWalletId}")
IllegalStateException("Account list not found: $userWalletId")
}
if (accountList.activeAccounts == 1) {
return AccountId.forMainCryptoPortfolio(userWalletId = mode.userWalletId)
return AccountId.forMainCryptoPortfolio(userWalletId = userWalletId)
}
val currencyAccountIndex = currency.getAccountIndex().bind()
@ -104,7 +102,7 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
cryptoPortfolioAccount?.derivationIndex?.value == currencyAccountIndex
}
return account?.accountId ?: AccountId.forMainCryptoPortfolio(userWalletId = mode.userWalletId)
return account?.accountId ?: AccountId.forMainCryptoPortfolio(userWalletId = userWalletId)
}
private fun CryptoCurrency.getAccountIndex(): Either<Throwable, Int> = either {
@ -140,6 +138,6 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor(
@AssistedFactory
interface Factory {
fun create(mode: AddCustomTokenMode): CustomTokenFormUseCasesFacade
fun create(userWalletId: UserWalletId): CustomTokenFormUseCasesFacade
}
}