From 02c11f91fda12c61ae7007131861773374248f08 Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 6 Mar 2026 12:29:33 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../fetcher/DefaultWalletAccountsFetcher.kt | 25 +++++++++++++++++-- .../tokens/UserTokensBackwardCompatibility.kt | 17 ++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/data/account/src/main/kotlin/com/tangem/data/account/fetcher/DefaultWalletAccountsFetcher.kt b/data/account/src/main/kotlin/com/tangem/data/account/fetcher/DefaultWalletAccountsFetcher.kt index b75e956b6e..f7901863e9 100644 --- a/data/account/src/main/kotlin/com/tangem/data/account/fetcher/DefaultWalletAccountsFetcher.kt +++ b/data/account/src/main/kotlin/com/tangem/data/account/fetcher/DefaultWalletAccountsFetcher.kt @@ -10,6 +10,7 @@ import com.tangem.data.common.account.WalletAccountsSaver import com.tangem.data.common.api.safeApiCall import com.tangem.data.common.cache.etag.ETagsStore import com.tangem.data.common.currency.UserTokensSaver +import com.tangem.data.common.tokens.UserTokensBackwardCompatibility import com.tangem.datasource.api.common.response.ApiResponse import com.tangem.datasource.api.common.response.ApiResponseError.HttpException.Code import com.tangem.datasource.api.common.response.ETAG_HEADER @@ -56,6 +57,8 @@ internal class DefaultWalletAccountsFetcher @Inject constructor( private val mainAccountTokensMigration: DefaultMainAccountTokensMigration, ) : WalletAccountsFetcher, WalletAccountsSaver { + private val userTokensBackwardCompatibility = UserTokensBackwardCompatibility() + override suspend fun fetch(userWalletId: UserWalletId): GetWalletAccountsResponse { val savedAccountsResponse = getAccountsResponseStore(userWalletId = userWalletId).getSyncOrNull() val fetchResult = fetchWalletAccounts(userWalletId, savedAccountsResponse) @@ -90,7 +93,7 @@ internal class DefaultWalletAccountsFetcher @Inject constructor( override suspend fun store(userWalletId: UserWalletId, response: GetWalletAccountsResponse) { val store = getAccountsResponseStore(userWalletId = userWalletId) - store.updateData { response } + store.updateData { response.applyTokensCompatibility() } } override suspend fun update( @@ -99,7 +102,9 @@ internal class DefaultWalletAccountsFetcher @Inject constructor( ) { val store = getAccountsResponseStore(userWalletId = userWalletId) - store.updateData { transform(it) } + store.updateData { + transform(it).applyTokensCompatibility() + } } override suspend fun push( @@ -272,6 +277,22 @@ internal class DefaultWalletAccountsFetcher @Inject constructor( ) } + private fun GetWalletAccountsResponse?.applyTokensCompatibility(): GetWalletAccountsResponse? { + if (this == null) return null + + return copy( + accounts = accounts.map { accountDTO -> + val tokens = accountDTO.tokens + + if (tokens.isNullOrEmpty()) return@map accountDTO + + accountDTO.copy( + tokens = userTokensBackwardCompatibility.applyCompatibilityAndGetUpdated(tokens), + ) + }, + ) + } + private fun getAccountsResponseStore(userWalletId: UserWalletId): AccountsResponseStore { return accountsResponseStoreFactory.create(userWalletId = userWalletId) } diff --git a/data/common/src/main/kotlin/com/tangem/data/common/tokens/UserTokensBackwardCompatibility.kt b/data/common/src/main/kotlin/com/tangem/data/common/tokens/UserTokensBackwardCompatibility.kt index 54e69b23a1..f9c12f4d03 100644 --- a/data/common/src/main/kotlin/com/tangem/data/common/tokens/UserTokensBackwardCompatibility.kt +++ b/data/common/src/main/kotlin/com/tangem/data/common/tokens/UserTokensBackwardCompatibility.kt @@ -9,7 +9,7 @@ import com.tangem.datasource.api.tangemTech.models.UserTokensResponse * Helper to apply compatibility changes for [UserTokensResponse] to support old saved tokens * in new application with new IDs */ -internal class UserTokensBackwardCompatibility { +class UserTokensBackwardCompatibility { fun applyCompatibilityAndGetUpdated(userTokensResponse: UserTokensResponse): UserTokensResponse { return userTokensResponse.copy( @@ -28,6 +28,21 @@ internal class UserTokensBackwardCompatibility { ) } + fun applyCompatibilityAndGetUpdated(tokens: List): List { + return tokens.map { token -> + val oldSavedId = NETWORKS_TO_OLD_SAVED_IDS[token.networkId] + if (oldSavedId != null && token.id == oldSavedId) { + Blockchain.fromNetworkId(token.networkId)?.let { blockchain -> + token.copy( + id = blockchain.toCoinId(), + ) + } ?: token + } else { + token + } + } + } + companion object { private val NETWORKS_TO_OLD_SAVED_IDS = mapOf( "optimistic-ethereum" to "ethereum",