From f6551294b37e710ac2992020852d5d5ed08fb79c Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 26 Sep 2024 11:24:16 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../features/onboarding/OnboardingHelper.kt | 4 +- .../DefaultCustomTokensRepository.kt | 63 ++++++++++--------- .../repository/DefaultCurrenciesRepository.kt | 1 - 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/onboarding/OnboardingHelper.kt b/app/src/main/java/com/tangem/tap/features/onboarding/OnboardingHelper.kt index 130eddc672..3b3e81c1a1 100644 --- a/app/src/main/java/com/tangem/tap/features/onboarding/OnboardingHelper.kt +++ b/app/src/main/java/com/tangem/tap/features/onboarding/OnboardingHelper.kt @@ -8,9 +8,7 @@ import com.tangem.common.routing.AppRoute import com.tangem.core.analytics.Analytics import com.tangem.core.analytics.models.Basic import com.tangem.data.common.currency.CryptoCurrencyFactory - import com.tangem.domain.common.util.cardTypesResolver -import com.tangem.domain.common.util.derivationStyleProvider import com.tangem.domain.common.util.twinsIsTwinned import com.tangem.domain.models.scan.CardDTO import com.tangem.domain.models.scan.ProductType @@ -152,7 +150,7 @@ object OnboardingHelper { val cryptoCurrency = CryptoCurrencyFactory().createCoin( blockchain = blockchain, extraDerivationPath = null, - derivationStyleProvider = scanResponse.derivationStyleProvider, + scanResponse = scanResponse, ) ?: return val topUpUrl = walletManager.getTopUpUrl(cryptoCurrency) ?: return diff --git a/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt b/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt index a85772f5ce..b24cbc6210 100644 --- a/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt +++ b/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt @@ -18,7 +18,6 @@ import com.tangem.datasource.local.preferences.utils.getObjectSyncOrNull import com.tangem.datasource.local.preferences.utils.storeObject import com.tangem.datasource.local.userwallet.UserWalletsStore import com.tangem.domain.common.extensions.canHandleBlockchain -import com.tangem.domain.common.extensions.canHandleToken import com.tangem.domain.common.extensions.supportedBlockchains import com.tangem.domain.common.util.cardTypesResolver import com.tangem.domain.managetokens.model.AddCustomTokenForm @@ -64,9 +63,12 @@ internal class DefaultCustomTokensRepository( contractAddress: String?, ): Boolean { return withContext(dispatchers.io) { - val storedCurrencies: UserTokensResponse = appPreferencesStore.getObjectSyncOrNull( + val storedCurrencies = appPreferencesStore.getObjectSyncOrNull( key = PreferencesKeys.getUserTokensKey(userWalletId.stringValue), - ) ?: error("User tokens not found") + ) + requireNotNull(storedCurrencies) { + "User tokens not found for user wallet [$userWalletId] while checking if currency is not added" + } storedCurrencies.tokens.none { token -> Blockchain.fromId(networkId.value).toNetworkId() == token.networkId && @@ -82,10 +84,12 @@ internal class DefaultCustomTokensRepository( networkId: Network.ID, derivationPath: Network.DerivationPath, ): CryptoCurrency.Token? = withContext(dispatchers.io) { - val userWallet = userWalletsStore.getSyncOrNull(userWalletId) - ?: error("User wallet not found") - val network = getNetwork(networkId, derivationPath, userWallet.scanResponse) - ?: return@withContext null + val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { + "User wallet [$userWalletId] not found while finding token" + } + val network = requireNotNull(getNetwork(networkId, derivationPath, userWallet.scanResponse)) { + "Network [$networkId] not found while finding token" + } val tokenAddress = tokenAddressConverter.convertTokenAddress( networkId, contractAddress, @@ -130,10 +134,12 @@ internal class DefaultCustomTokensRepository( networkId: Network.ID, derivationPath: Network.DerivationPath, ): CryptoCurrency.Coin { - val userWallet = userWalletsStore.getSyncOrNull(userWalletId) - ?: error("User wallet not found") - val network = getNetwork(networkId, derivationPath, userWallet.scanResponse) - ?: error("Network not found") + val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { + "User wallet [$userWalletId] not found while creating coin" + } + val network = requireNotNull(getNetwork(networkId, derivationPath, userWallet.scanResponse)) { + "Network [$networkId] not found while creating coin" + } return cryptoCurrencyFactory.createCoin(network) } @@ -159,10 +165,12 @@ internal class DefaultCustomTokensRepository( derivationPath: Network.DerivationPath, formValues: AddCustomTokenForm.Validated.All, ): CryptoCurrency.Token { - val userWallet = userWalletsStore.getSyncOrNull(userWalletId) - ?: error("User wallet not found") - val network = getNetwork(networkId, derivationPath, userWallet.scanResponse) - ?: error("Network not found") + val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { + "User wallet [$userWalletId] not found while creating custom token" + } + val network = requireNotNull(getNetwork(networkId, derivationPath, userWallet.scanResponse)) { + "Network [$networkId] not found while creating custom token" + } val tokenAddress = tokenAddressConverter.convertTokenAddress( networkId, formValues.contractAddress, @@ -196,15 +204,17 @@ internal class DefaultCustomTokensRepository( contractAddress = currency.contractAddress, ) } - - val savedCurrencies = requireNotNull( - value = getSavedUserTokensResponseSync(key = userWalletId), - lazyMessage = { "Saved tokens empty. Can not perform remove currency action" }, + val storedCurrencies = appPreferencesStore.getObjectSyncOrNull( + key = PreferencesKeys.getUserTokensKey(userWalletId.stringValue), ) + requireNotNull(storedCurrencies) { + "User tokens not found for user wallet [$userWalletId] while removing currency" + } + val token = userTokensResponseFactory.createResponseToken(cryptoCurrency) storeAndPushTokens( userWalletId = userWalletId, - response = savedCurrencies.copy(tokens = savedCurrencies.tokens.filterNot { it == token }), + response = storedCurrencies.copy(tokens = storedCurrencies.tokens.filterNot { it == token }), ) when (cryptoCurrency) { is CryptoCurrency.Coin -> walletManagersFacade.remove(userWalletId, setOf(cryptoCurrency.network)) @@ -213,8 +223,9 @@ internal class DefaultCustomTokensRepository( } override suspend fun getSupportedNetworks(userWalletId: UserWalletId): List = withContext(dispatchers.io) { - val userWallet = userWalletsStore.getSyncOrNull(userWalletId) - ?: error("User wallet not found") + val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { + "User wallet [$userWalletId] not found while getting supported networks" + } val scanResponse = userWallet.scanResponse Blockchain.entries @@ -251,13 +262,7 @@ internal class DefaultCustomTokensRepository( private suspend fun pushTokens(userWalletId: UserWalletId, response: UserTokensResponse) { safeApiCall({ tangemTechApi.saveUserTokens(userWalletId.stringValue, response).bind() }) { - Timber.e(it, "Unable to save user tokens for: ${userWalletId.stringValue}") + Timber.e(it, "Unable to push user tokens for: ${userWalletId.stringValue}") } } - - private suspend fun getSavedUserTokensResponseSync(key: UserWalletId): UserTokensResponse? { - return appPreferencesStore.getObjectSyncOrNull( - key = PreferencesKeys.getUserTokensKey(key.stringValue), - ) - } } \ No newline at end of file diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt index 5a1ce55276..8d14d7cd8f 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt @@ -26,7 +26,6 @@ import com.tangem.datasource.local.preferences.utils.getObjectSyncOrNull import com.tangem.datasource.local.preferences.utils.storeObject import com.tangem.datasource.local.token.ExpressAssetsStore import com.tangem.datasource.local.userwallet.UserWalletsStore -import com.tangem.domain.common.util.derivationStyleProvider import com.tangem.domain.core.error.DataError import com.tangem.domain.core.lce.LceFlow import com.tangem.domain.core.lce.lceFlow