Updated on 2026-08-14

This commit is contained in:
Tangem 2023-02-14 15:39:43 +03:00
commit c27049f0ea
4 changed files with 50 additions and 41 deletions

View file

@ -79,7 +79,6 @@ internal class DefaultWalletCurrenciesManager(
.filter { it !in currenciesToRemove }
val remainingBlockchains = remainingCurrencies
.filterIsInstance<Currency.Blockchain>()
.map { it.blockchain }
walletStoresRepository.deleteDifference(userWallet.walletId, remainingBlockchains)
.flatMap {
@ -116,43 +115,50 @@ internal class DefaultWalletCurrenciesManager(
}
private fun List<Currency>.addMissingBlockchains(card: CardDTO): List<Currency> {
val newCurrencies = arrayListOf<Currency>()
if (this.isEmpty()) return this
val currencies = this.asSequence()
this
.groupBy { it.blockchain }
.forEach { (blockchain, currencies) ->
val rawDerivationPath: String?
val blockchainCurrency = currencies
.firstOrNull { it is Currency.Blockchain }
as? Currency.Blockchain
// Add blockchain currency
if (blockchainCurrency != null) {
rawDerivationPath = findDerivationPath(blockchainCurrency, card.derivationStyle)
newCurrencies.add(blockchainCurrency.copy(derivationPath = rawDerivationPath))
} else {
rawDerivationPath = findDerivationPath(currencies.first(), card.derivationStyle)
newCurrencies.add(
Currency.Blockchain(
blockchain = blockchain,
derivationPath = rawDerivationPath,
),
)
}
// Add tokens currencies
currencies
.filterIsInstance<Currency.Token>()
.forEach { currency ->
newCurrencies.add(currency.copy(derivationPath = rawDerivationPath))
}
return currencies
.groupBy { currency ->
findBlockchainCurrency(currency, currencies, card.derivationStyle)
}
.mapValues { (blockchainCurrency, blockchainCurrencies) ->
findBlockchainTokens(blockchainCurrency, blockchainCurrencies)
}
.flatMap { (blockchainCurrency, blockchainTokens) ->
arrayListOf(blockchainCurrency) + blockchainTokens
}
return newCurrencies
}
private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? {
return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath
private fun findBlockchainCurrency(
currency: Currency,
currencies: Sequence<Currency>,
cardDerivationStyle: DerivationStyle?,
): Currency.Blockchain {
return currencies
.filterIsInstance<Currency.Blockchain>()
.firstOrNull {
it.blockchain == currency.blockchain &&
it.derivationPath == currency.derivationPath
}
?: Currency.Blockchain(
blockchain = currency.blockchain,
derivationPath = currency.derivationPath
?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath,
)
}
private fun findBlockchainTokens(
blockchainCurrency: Currency.Blockchain,
blockchainCurrencies: List<Currency>,
): List<Currency.Token> {
return blockchainCurrencies
.filterIsInstance<Currency.Token>()
.map { token ->
token.copy(
derivationPath = token.derivationPath ?: blockchainCurrency.derivationPath,
)
}
}
private suspend fun updateWalletStores(

View file

@ -18,6 +18,7 @@ import com.tangem.tap.domain.walletStores.WalletStoresManager
import com.tangem.tap.domain.walletStores.repository.WalletAmountsRepository
import com.tangem.tap.domain.walletStores.repository.WalletManagersRepository
import com.tangem.tap.domain.walletStores.repository.WalletStoresRepository
import com.tangem.tap.features.wallet.models.Currency
import com.tangem.tap.features.wallet.models.toBlockchainNetworks
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
@ -126,7 +127,9 @@ internal class DefaultWalletStoresManager(
.also { blockchainNetworks ->
walletStoresRepository.deleteDifference(
userWalletId = userWalletId,
currentBlockchains = blockchainNetworks.map { it.blockchain },
currentBlockchains = blockchainNetworks.map {
Currency.Blockchain(it.blockchain, it.derivationPath)
},
)
}
.map { blockchainNetwork ->

View file

@ -1,9 +1,9 @@
package com.tangem.tap.domain.walletStores.repository
import com.tangem.blockchain.common.Blockchain
import com.tangem.common.CompletionResult
import com.tangem.domain.common.util.UserWalletId
import com.tangem.tap.domain.model.WalletStoreModel
import com.tangem.tap.features.wallet.models.Currency
import kotlinx.coroutines.flow.Flow
interface WalletStoresRepository {
@ -17,7 +17,7 @@ interface WalletStoresRepository {
suspend fun deleteDifference(
userWalletId: UserWalletId,
currentBlockchains: List<Blockchain>,
currentBlockchains: List<Currency.Blockchain>,
): CompletionResult<Unit>
suspend fun clear(): CompletionResult<Unit>

View file

@ -1,6 +1,5 @@
package com.tangem.tap.domain.walletStores.repository.implementation
import com.tangem.blockchain.common.Blockchain
import com.tangem.common.CompletionResult
import com.tangem.common.catching
import com.tangem.domain.common.util.UserWalletId
@ -10,6 +9,7 @@ import com.tangem.tap.domain.walletStores.repository.implementation.utils.isSame
import com.tangem.tap.domain.walletStores.repository.implementation.utils.replaceWalletStore
import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithSelf
import com.tangem.tap.domain.walletStores.storage.WalletStoresStorage
import com.tangem.tap.features.wallet.models.Currency
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
@ -43,13 +43,13 @@ internal class DefaultWalletStoresRepository : WalletStoresRepository {
override suspend fun deleteDifference(
userWalletId: UserWalletId,
currentBlockchains: List<Blockchain>,
currentBlockchains: List<Currency.Blockchain>,
): CompletionResult<Unit> = catching {
if (currentBlockchains != getSync(userWalletId).map { it.blockchain }) {
if (currentBlockchains != getSync(userWalletId)) {
walletStoresStorage.update { prevStores ->
prevStores.apply {
this[userWalletId] = this[userWalletId]
?.filter { it.blockchain in currentBlockchains }
?.filter { it.blockchainWalletData.currency in currentBlockchains }
.orEmpty()
}
}