Updated on 2026-08-14

This commit is contained in:
Tangem 2025-02-12 15:41:23 +04:00
parent 62b5e25567
commit 9ba3231bf2
3 changed files with 49 additions and 11 deletions

View file

@ -1,6 +1,8 @@
package com.tangem.data.onramp package com.tangem.data.onramp
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchainsdk.utils.ExcludedBlockchains import com.tangem.blockchainsdk.utils.ExcludedBlockchains
import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.core.analytics.models.event.MainScreenAnalyticsEvent import com.tangem.core.analytics.models.event.MainScreenAnalyticsEvent
import com.tangem.data.onramp.converters.HotCryptoCurrencyConverter import com.tangem.data.onramp.converters.HotCryptoCurrencyConverter
@ -15,8 +17,12 @@ import com.tangem.datasource.local.preferences.AppPreferencesStore
import com.tangem.datasource.local.preferences.PreferencesKeys import com.tangem.datasource.local.preferences.PreferencesKeys
import com.tangem.datasource.local.preferences.utils.getObject import com.tangem.datasource.local.preferences.utils.getObject
import com.tangem.datasource.local.userwallet.UserWalletsStore import com.tangem.datasource.local.userwallet.UserWalletsStore
import com.tangem.domain.common.extensions.canHandleBlockchain
import com.tangem.domain.common.extensions.canHandleToken
import com.tangem.domain.common.util.cardTypesResolver
import com.tangem.domain.onramp.model.HotCryptoCurrency import com.tangem.domain.onramp.model.HotCryptoCurrency
import com.tangem.domain.onramp.repositories.HotCryptoRepository import com.tangem.domain.onramp.repositories.HotCryptoRepository
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.utils.coroutines.CoroutineDispatcherProvider import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.coroutines.JobHolder import com.tangem.utils.coroutines.JobHolder
@ -64,9 +70,9 @@ internal class DefaultHotCryptoRepository(
) { walletsWithTokens, hotCrypto -> ) { walletsWithTokens, hotCrypto ->
hotCrypto ?: return@combine emptyMap() hotCrypto ?: return@combine emptyMap()
walletsWithTokens.mapValues { (_, tokens) -> walletsWithTokens
hotCrypto.filterTokens(tokens) .mapValues { hotCrypto.filterTokens(it) }
} .mapKeys { it.key.walletId }
} }
.onEach(hotCryptoResponseStore::store) .onEach(hotCryptoResponseStore::store)
.launchIn(scope = coroutineScope) .launchIn(scope = coroutineScope)
@ -91,13 +97,13 @@ internal class DefaultHotCryptoRepository(
} }
} }
private fun getWalletsWithTokensFlow(): Flow<Map<UserWalletId, List<UserTokensResponse.Token>>> { private fun getWalletsWithTokensFlow(): Flow<Map<UserWallet, List<UserTokensResponse.Token>>> {
return userWalletsStore.userWallets.flatMapLatest { userWallets -> return userWalletsStore.userWallets.flatMapLatest { userWallets ->
val flows = userWallets.map { userWallet -> val flows = userWallets.map { userWallet ->
appPreferencesStore.getObject<UserTokensResponse>( appPreferencesStore.getObject<UserTokensResponse>(
key = PreferencesKeys.getUserTokensKey(userWallet.walletId.stringValue), key = PreferencesKeys.getUserTokensKey(userWallet.walletId.stringValue),
) )
.map { userWallet.walletId to it?.tokens.orEmpty() } .map { userWallet to it?.tokens.orEmpty() }
} }
combine(flows) { it.toMap() } combine(flows) { it.toMap() }
@ -129,14 +135,42 @@ internal class DefaultHotCryptoRepository(
} }
} }
private fun HotCryptoResponse.filterTokens(tokens: List<UserTokensResponse.Token>): HotCryptoResponse { private fun HotCryptoResponse.filterTokens(
walletWithTokens: Map.Entry<UserWallet, List<UserTokensResponse.Token>>,
): HotCryptoResponse {
val (userWallet, tokens) = walletWithTokens
return copy( return copy(
tokens = this.tokens.filter { hotToken -> tokens = this.tokens.filter { hotToken ->
tokens.none { !tokens.hasAlreadyAdded(hotToken) && userWallet.canHandleHotCrypto(hotToken)
it.id == hotToken.id && it.contractAddress == hotToken.contractAddress &&
it.networkId == hotToken.networkId
}
}, },
) )
} }
private fun List<UserTokensResponse.Token>.hasAlreadyAdded(hotToken: HotCryptoResponse.Token): Boolean {
return any { token ->
token.id == hotToken.id && token.contractAddress == hotToken.contractAddress &&
token.networkId == hotToken.networkId
}
}
// TODO: [REDACTED_JIRA]
private fun UserWallet.canHandleHotCrypto(hotToken: HotCryptoResponse.Token): Boolean {
val isToken = hotToken.contractAddress != null && hotToken.decimalCount != null
val blockchain = hotToken.networkId?.let { Blockchain.fromNetworkId(it) } ?: return false
return if (isToken) {
scanResponse.card.canHandleToken(
blockchain = blockchain,
cardTypesResolver = cardTypesResolver,
excludedBlockchains = excludedBlockchains,
)
} else {
scanResponse.card.canHandleBlockchain(
blockchain = blockchain,
cardTypesResolver = cardTypesResolver,
excludedBlockchains = excludedBlockchains,
)
}
}
} }

View file

@ -74,8 +74,10 @@ internal class HotCryptoCurrencyConverter(
} }
private fun createNetwork(networkId: String): Network? { private fun createNetwork(networkId: String): Network? {
val blockchain = Blockchain.fromNetworkId(networkId) ?: return null
return getNetwork( return getNetwork(
blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)), blockchain = blockchain,
extraDerivationPath = null, extraDerivationPath = null,
scanResponse = scanResponse, scanResponse = scanResponse,
excludedBlockchains = excludedBlockchains, excludedBlockchains = excludedBlockchains,

View file

@ -9,6 +9,8 @@ import com.tangem.domain.common.TapWorkarounds.isTestCard
import com.tangem.domain.common.configs.CardConfig import com.tangem.domain.common.configs.CardConfig
import com.tangem.domain.models.scan.CardDTO import com.tangem.domain.models.scan.CardDTO
// TODO: refactor [REDACTED_JIRA]
/** /**
[REDACTED_AUTHOR] [REDACTED_AUTHOR]
*/ */