Updated on 2026-08-14

This commit is contained in:
Tangem 2025-11-07 13:54:47 +04:00
parent 3088258c74
commit 50c633e873
11 changed files with 119 additions and 33 deletions

View file

@ -8,7 +8,10 @@ import com.tangem.datasource.local.token.UserTokensResponseStore
import com.tangem.domain.account.featuretoggle.AccountsFeatureToggles
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.retryer.Retryer
import com.tangem.utils.retryer.RetryerPool
import kotlinx.coroutines.withContext
import timber.log.Timber
class UserTokensSaver(
private val tangemTechApi: TangemTechApi,
@ -16,6 +19,7 @@ class UserTokensSaver(
private val dispatchers: CoroutineDispatcherProvider,
private val addressesEnricher: UserTokensResponseAddressesEnricher,
private val accountsFeatureToggles: AccountsFeatureToggles,
private val pushTokensRetryerPool: RetryerPool,
) {
private val userTokensBackwardCompatibility = UserTokensBackwardCompatibility()
@ -58,6 +62,23 @@ class UserTokensSaver(
}
}
suspend fun pushWithRetryer(
userWalletId: UserWalletId,
response: UserTokensResponse,
useEnricher: Boolean = true,
onFailSend: () -> Unit = {},
) {
push(
userWalletId = userWalletId,
response = response,
useEnricher = useEnricher,
onFailSend = {
pushTokensRetryerPool + createPushTokensRetryer(userWalletId, response)
onFailSend()
},
)
}
private fun UserTokensResponse.applyCompatibility(): UserTokensResponse {
return userTokensBackwardCompatibility.applyCompatibilityAndGetUpdated(userTokensResponse = this)
}
@ -86,4 +107,24 @@ class UserTokensSaver(
private fun UserTokensResponse.enrichByAccountId(userWalletId: UserWalletId): UserTokensResponse {
return UserTokensResponseAccountIdEnricher(userWalletId = userWalletId, response = this)
}
private fun createPushTokensRetryer(userWalletId: UserWalletId, response: UserTokensResponse): Retryer {
return Retryer(attempt = 3) { iteration ->
var isSuccess = true
push(
userWalletId = userWalletId,
response = response,
onFailSend = {
Timber.e(
"Retryer: Failed to push updated tokens on attempt ${iteration + 1} for $userWalletId",
)
isSuccess = false
},
)
isSuccess
}
}
}

View file

@ -16,10 +16,13 @@ import com.tangem.domain.demo.models.DemoConfig
import com.tangem.domain.networks.multi.MultiNetworkStatusSupplier
import com.tangem.domain.wallets.repository.WalletsRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.retryer.RetryerPool
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
@Module
@ -76,6 +79,9 @@ internal object DataCommonModule {
dispatchers = dispatchers,
addressesEnricher = addressesEnricher,
accountsFeatureToggles = accountsFeatureToggles,
pushTokensRetryerPool = RetryerPool(
coroutineScope = CoroutineScope(SupervisorJob() + dispatchers.default),
),
)
}

View file

@ -30,6 +30,7 @@ class UserTokensSaverTest {
dispatchers = TestingCoroutineDispatcherProvider(),
addressesEnricher = enricher,
accountsFeatureToggles = accountsFeatureToggles,
pushTokensRetryerPool = mockk(),
)
@BeforeEach