Updated on 2026-08-14

This commit is contained in:
Tangem 2025-07-12 20:58:28 +04:00
parent cbd9ad505d
commit 309c9aad6c
4 changed files with 49 additions and 40 deletions

View file

@ -97,31 +97,35 @@ internal class DefaultCurrenciesRepository(
}
}
override suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) {
withContext(dispatchers.io) {
val savedCurrencies = requireNotNull(
value = getSavedUserTokensResponseSync(key = userWalletId),
lazyMessage = { "Saved tokens empty. Can not perform add currencies action" },
)
override suspend fun addCurrencies(
userWalletId: UserWalletId,
currencies: List<CryptoCurrency>,
): List<CryptoCurrency> = withContext(dispatchers.io) {
val savedCurrencies = requireNotNull(
value = getSavedUserTokensResponseSync(key = userWalletId),
lazyMessage = { "Saved tokens empty. Can not perform add currencies action" },
)
val currenciesToAdd = populateCurrenciesWithMissedCoins(
currencies = currencies,
).let {
filterAlreadyAddedCurrencies(savedCurrencies.tokens, it)
}
val updatedResponse = savedCurrencies.copy(
tokens = savedCurrencies.tokens + currenciesToAdd.map(userTokensResponseFactory::createResponseToken),
)
userTokensSaver.storeAndPush(
userWalletId = userWalletId,
response = updatedResponse,
)
val currenciesToAdd = filterAlreadyAddedCurrencies(
savedCurrencies = savedCurrencies.tokens,
currenciesToAdd = populateCurrenciesWithMissedCoins(currencies = currencies),
)
fetchExpressAssetsByNetworkIds(
userWallet = userWalletsStore.getSyncStrict(key = userWalletId),
userTokens = updatedResponse,
)
}
val updatedResponse = savedCurrencies.copy(
tokens = savedCurrencies.tokens + currenciesToAdd.map(userTokensResponseFactory::createResponseToken),
)
userTokensSaver.storeAndPush(
userWalletId = userWalletId,
response = updatedResponse,
)
fetchExpressAssetsByNetworkIds(
userWallet = userWalletsStore.getSyncStrict(key = userWalletId),
userTokens = updatedResponse,
)
currenciesToAdd
}
private fun filterAlreadyAddedCurrencies(
@ -573,7 +577,7 @@ internal class DefaultCurrenciesRepository(
override fun isNetworkFeeZero(userWalletId: UserWalletId, network: Network): Boolean {
val blockchain = Blockchain.fromNetworkId(network.backendId)
return blockchain?.isNetworkFeeZero() ?: false
return blockchain?.isNetworkFeeZero() == true
}
override suspend fun syncTokens(userWalletId: UserWalletId) {

View file

@ -41,16 +41,15 @@ class SaveMarketTokensUseCase(
removedNetworks: Set<TokenMarketInfo.Network>,
): Either<Throwable, Unit> = Either.catch {
if (removedNetworks.isNotEmpty()) {
currenciesRepository.removeCurrencies(
userWalletId = userWalletId,
currencies = removedNetworks.mapNotNull {
marketsTokenRepository.createCryptoCurrency(
userWalletId = userWalletId,
token = tokenMarketParams,
network = it,
)
},
)
val removedCurrencies = removedNetworks.mapNotNull {
marketsTokenRepository.createCryptoCurrency(
userWalletId = userWalletId,
token = tokenMarketParams,
network = it,
)
}
currenciesRepository.removeCurrencies(userWalletId = userWalletId, currencies = removedCurrencies)
}
if (addedNetworks.isNotEmpty()) {
@ -67,13 +66,16 @@ class SaveMarketTokensUseCase(
)
}
currenciesRepository.addCurrencies(userWalletId = userWalletId, currencies = addedCurrencies)
val savedCurrencies = currenciesRepository.addCurrencies(
userWalletId = userWalletId,
currencies = addedCurrencies,
)
refreshUpdatedNetworks(userWalletId, addedCurrencies)
refreshUpdatedNetworks(userWalletId, savedCurrencies)
refreshUpdatedYieldBalances(userWalletId, addedCurrencies)
refreshUpdatedYieldBalances(userWalletId, savedCurrencies)
refreshUpdatedQuotes(addedCurrencies)
refreshUpdatedQuotes(savedCurrencies)
}
}

View file

@ -50,7 +50,7 @@ interface CurrenciesRepository {
* @throws DataError.UserWalletError.WrongUserWallet If single-currency user wallet
* ID provided.
*/
suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>)
suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>): List<CryptoCurrency>
/**
* Removes currency from a specific user wallet.

View file

@ -48,7 +48,10 @@ internal class MockCurrenciesRepository(
override suspend fun saveNewCurrenciesList(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) = Unit
override suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) = Unit
override suspend fun addCurrencies(
userWalletId: UserWalletId,
currencies: List<CryptoCurrency>,
): List<CryptoCurrency> = emptyList()
override suspend fun removeCurrency(userWalletId: UserWalletId, currency: CryptoCurrency) {
removeCurrencyResult.onLeft { throw it }