Updated on 2026-08-14

This commit is contained in:
Tangem 2024-10-14 14:43:41 +05:00
parent 3f647e8180
commit b9db5dce07
4 changed files with 68 additions and 30 deletions

View file

@ -87,6 +87,26 @@ internal class DefaultCurrenciesRepository(
storeAndPushTokens(userWalletId, response)
}
override suspend fun saveNewCurrenciesList(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) {
val savedResponse = requireNotNull(
value = getSavedUserTokensResponseSync(key = userWalletId),
lazyMessage = { "Saved tokens empty. Can not perform add currencies action" },
)
val newCoins = createCoinsForNewTokenList(
userWalletId = userWalletId,
newTokens = currencies.filterIsInstance<CryptoCurrency.Token>(),
)
val newCurrencies = (newCoins + currencies).distinct()
val updatedResponse = savedResponse.copy(
tokens = newCurrencies.map(userTokensResponseFactory::createResponseToken),
)
storeAndPushTokens(
userWalletId = userWalletId,
response = updatedResponse,
)
fetchExchangeableUserMarketCoinsByIds(userWalletId, updatedResponse)
}
override suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) {
return withContext(dispatchers.io) {
val savedCurrencies = requireNotNull(
@ -125,6 +145,19 @@ internal class DefaultCurrenciesRepository(
}
}
private suspend fun createCoinsForNewTokenList(
userWalletId: UserWalletId,
newTokens: List<CryptoCurrency.Token>,
): List<CryptoCurrency.Coin> {
return newTokens.mapNotNull {
cryptoCurrencyFactory.createCoin(
blockchain = getBlockchain(networkId = it.network.id),
extraDerivationPath = it.network.derivationPath.value,
scanResponse = getUserWallet(userWalletId).scanResponse,
)
}.distinct()
}
private suspend fun createCoinsForNewTokens(
userWalletId: UserWalletId,
newTokens: List<CryptoCurrency.Token>,

View file

@ -26,21 +26,36 @@ class SaveManagedTokensUseCase(
currenciesToRemove: Map<ManagedCryptoCurrency.Token, Set<Network>>,
): Either<Throwable, Unit> {
return Either.catch {
// TODO: Currently order is matter. [REDACTED_JIRA]
removeCurrencies(userWalletId, currenciesToRemove)
addCurrencies(userWalletId, currenciesToAdd)
val removingCurrencies = currenciesToRemove.mapToCryptoCurrencies(userWalletId)
val addingCurrencies = currenciesToAdd.mapToCryptoCurrencies(userWalletId)
derivationsRepository.derivePublicKeysByNetworks(
userWalletId = userWalletId,
networks = currenciesToAdd.values.flatten(),
)
val newCurrenciesList = currenciesRepository
.getMultiCurrencyWalletCurrenciesSync(userWalletId)
.filterNot(removingCurrencies::contains)
.toMutableList()
.also { it.addAll(addingCurrencies) }
currenciesRepository.saveNewCurrenciesList(userWalletId, newCurrenciesList)
val existingCurrencies = currenciesRepository.getMultiCurrencyWalletCurrenciesSync(userWalletId)
removeCurrenciesFromWalletManager(
userWalletId = userWalletId,
currencies = removingCurrencies.filterNot(existingCurrencies::contains),
)
refreshUpdatedNetworks(
userWalletId = userWalletId,
existingCurrencies = existingCurrencies,
currenciesToAdd = addingCurrencies,
)
}
}
private suspend fun removeCurrencies(
private suspend fun removeCurrenciesFromWalletManager(
userWalletId: UserWalletId,
currenciesToRemove: Map<ManagedCryptoCurrency.Token, Set<Network>>,
currencies: List<CryptoCurrency>,
) {
if (currenciesToRemove.isEmpty()) return
val currencies = currenciesToRemove.mapToCryptoCurrencies(userWalletId)
currenciesRepository.removeCurrencies(userWalletId = userWalletId, currencies = currencies)
walletManagersFacade.remove(
userWalletId = userWalletId,
networks = currencies
@ -54,26 +69,6 @@ class SaveManagedTokensUseCase(
)
}
private suspend fun addCurrencies(
userWalletId: UserWalletId,
currenciesToAdd: Map<ManagedCryptoCurrency.Token, Set<Network>>,
) {
if (currenciesToAdd.isEmpty()) return
val existingCurrencies = currenciesRepository.getMultiCurrencyWalletCurrenciesSync(userWalletId)
val currencies = currenciesToAdd.mapToCryptoCurrencies(userWalletId)
derivationsRepository.derivePublicKeysByNetworks(
userWalletId = userWalletId,
networks = currenciesToAdd.values.flatten(),
)
currenciesRepository.addCurrencies(userWalletId, currencies)
refreshUpdatedNetworks(
userWalletId = userWalletId,
existingCurrencies = existingCurrencies,
currenciesToAdd = currencies,
)
}
private suspend fun refreshUpdatedNetworks(
userWalletId: UserWalletId,
currenciesToAdd: List<CryptoCurrency>,

View file

@ -34,6 +34,14 @@ interface CurrenciesRepository {
isSortedByBalance: Boolean,
)
/**
* Saves the given list of cryptocurrencies for a specific multi-currency user wallet.
*
* @param userWalletId The unique identifier of the user wallet.
* @param currencies The list of cryptocurrencies to be saved.
*/
suspend fun saveNewCurrenciesList(userWalletId: UserWalletId, currencies: List<CryptoCurrency>)
/**
* Add currencies to a specific user wallet.
*

View file

@ -47,6 +47,8 @@ internal class MockCurrenciesRepository(
isTokensSortedByBalanceAfterSortingApply = isSortedByBalance
}
override suspend fun saveNewCurrenciesList(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) = Unit
override suspend fun addCurrencies(userWalletId: UserWalletId, currencies: List<CryptoCurrency>) = Unit
override suspend fun removeCurrency(userWalletId: UserWalletId, currency: CryptoCurrency) {