diff --git a/domain/account/status/src/main/java/com/tangem/domain/account/status/usecase/ManageCryptoCurrenciesUseCase.kt b/domain/account/status/src/main/java/com/tangem/domain/account/status/usecase/ManageCryptoCurrenciesUseCase.kt index 2bef64e9b1..2774fa49fe 100644 --- a/domain/account/status/src/main/java/com/tangem/domain/account/status/usecase/ManageCryptoCurrenciesUseCase.kt +++ b/domain/account/status/src/main/java/com/tangem/domain/account/status/usecase/ManageCryptoCurrenciesUseCase.kt @@ -84,7 +84,7 @@ class ManageCryptoCurrenciesUseCase( withContext(NonCancellable) { val accountStatus = getAccountStatus(accountId = accountId) - val modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies() + var modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies() .modify(add = add, remove = remove) if (!modifiedCurrencyList.hasChanges) { @@ -92,12 +92,21 @@ class ManageCryptoCurrenciesUseCase( return@withContext } + val derivingResult = derivePublicKeys(userWalletId = userWalletId, currencies = modifiedCurrencyList.added) + + if (!skipDerivationErrors && derivingResult.isLeft()) { + modifiedCurrencyList = accountStatus.tokenList.flattenCurrencies() + .modify(add = emptyList(), remove = remove) + + if (!modifiedCurrencyList.hasChanges) { + derivingResult.bind() + } + } + saveAccount( account = accountStatus.account.copy(cryptoCurrencies = modifiedCurrencyList.total), ) - val result = derivePublicKeys(userWalletId = userWalletId, currencies = modifiedCurrencyList.added) - parallelUpdatingScope.launch { syncTokens(userWalletId, modifiedCurrencyList) @@ -107,7 +116,7 @@ class ManageCryptoCurrenciesUseCase( } if (!skipDerivationErrors) { - result.bind() + derivingResult.bind() } } } diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt index 1ce00a7b83..d741fc5ed0 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/model/CustomTokenFormModel.kt @@ -1,6 +1,7 @@ package com.tangem.features.managetokens.model import arrow.core.getOrElse +import com.tangem.common.core.TangemSdkError import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.decompose.di.ModelScoped import com.tangem.core.decompose.model.Model @@ -8,6 +9,7 @@ import com.tangem.core.decompose.model.ParamsContainer import com.tangem.core.decompose.ui.UiMessageSender import com.tangem.core.ui.extensions.resourceReference import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.extensions.wrappedList import com.tangem.core.ui.message.DialogMessage import com.tangem.domain.managetokens.CreateCryptoCurrencyUseCase import com.tangem.domain.managetokens.FindTokenUseCase @@ -146,7 +148,7 @@ internal class CustomTokenFormModel @Inject constructor( is CustomCurrencyValidator.Status.Validating, -> Unit is CustomCurrencyValidator.Status.SearchingToken -> updateStateWithProgress() - is CustomCurrencyValidator.Status.UnexpectedException -> showErrorDialog() + is CustomCurrencyValidator.Status.UnexpectedException -> showErrorDialog(validatorState.cause) is CustomCurrencyValidator.Status.FormValidationException -> updateStateWithExceptions( exceptions = validatorState.exceptions, ) @@ -236,9 +238,17 @@ internal class CustomTokenFormModel @Inject constructor( } } - private fun showErrorDialog() { + private fun showErrorDialog(throwable: Throwable) { + Timber.e(throwable) + val message = when (throwable) { + is TangemSdkError -> resourceReference( + R.string.generic_error_code, + wrappedList(throwable.code.toString()), + ) + else -> resourceReference(R.string.common_unknown_error) + } val dialog = DialogMessage( - message = resourceReference(R.string.common_unknown_error), + message = message, ) messageSender.send(dialog) @@ -341,8 +351,17 @@ internal class CustomTokenFormModel @Inject constructor( ) { val currency = createdCurrency if (currency == null) { - Timber.e("Trying to add currency without validation") - showErrorDialog() + showErrorDialog(IllegalStateException("Trying to add currency without validation")) + return@resource + } + + useCasesFacade.derivePublicKeysUseCase(listOf(currency)).getOrElse { + showErrorDialog(IllegalStateException("Failed to derive public keys")) + return@resource + } + + useCasesFacade.addCryptoCurrenciesUseCase(currency).getOrElse { throwable -> + showErrorDialog(throwable) return@resource } @@ -353,18 +372,6 @@ internal class CustomTokenFormModel @Inject constructor( ) analyticsEventHandler.send(event) - useCasesFacade.derivePublicKeysUseCase(listOf(currency)).getOrElse { - Timber.e(it, "Failed to derive public keys") - showErrorDialog() - return@resource - } - - useCasesFacade.addCryptoCurrenciesUseCase(currency).getOrElse { - Timber.e(it, "Failed to add currency") - showErrorDialog() - return@resource - } - params.onCurrencyAdded(currency) } diff --git a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/list/CustomTokenFormUseCasesFacade.kt b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/list/CustomTokenFormUseCasesFacade.kt index 51a46bd0e0..d446dd0141 100644 --- a/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/list/CustomTokenFormUseCasesFacade.kt +++ b/features/manage-tokens/impl/src/main/kotlin/com/tangem/features/managetokens/utils/list/CustomTokenFormUseCasesFacade.kt @@ -44,7 +44,11 @@ internal class CustomTokenFormUseCasesFacade @AssistedInject constructor( either { val accountId = getAccountId(currency) - manageCryptoCurrenciesUseCase(accountId = accountId, add = currency).bind() + manageCryptoCurrenciesUseCase( + accountId = accountId, + add = currency, + skipDerivationErrors = false, + ).bind() } } else { addCryptoCurrenciesUseCase.invoke(userWalletId = userWalletId, currency = currency)