Updated on 2026-08-14

This commit is contained in:
Tangem 2025-02-12 11:46:40 +04:00
parent 90e783441c
commit 5ae5b86980
5 changed files with 78 additions and 39 deletions

View file

@ -35,7 +35,9 @@ internal class DefaultHotCryptoRepository(
scanResponse = userWallet.scanResponse,
imageHost = it.imageHost,
excludedBlockchains = excludedBlockchains,
).convertList(input = it.tokens)
)
.convertList(input = it.tokens)
.filterNotNull()
}
}
}

View file

@ -10,7 +10,9 @@ import com.tangem.domain.models.scan.ScanResponse
import com.tangem.domain.onramp.model.HotCryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.tokens.model.Quote
import com.tangem.utils.converter.Converter
import java.math.BigDecimal
/**
* Converter from [HotCryptoResponse.Token] to [HotCryptoCurrency]
@ -25,44 +27,75 @@ internal class HotCryptoCurrencyConverter(
private val scanResponse: ScanResponse,
private val imageHost: String?,
private val excludedBlockchains: ExcludedBlockchains,
) : Converter<HotCryptoResponse.Token, HotCryptoCurrency> {
) : Converter<HotCryptoResponse.Token, HotCryptoCurrency?> {
private val cryptoCurrencyFactory by lazy { CryptoCurrencyFactory(excludedBlockchains) }
override fun convert(value: HotCryptoResponse.Token): HotCryptoCurrency {
val network = createNetwork(value.networkId)
override fun convert(value: HotCryptoResponse.Token): HotCryptoCurrency? {
val rawId = value.id?.let(CryptoCurrency::RawID) ?: return null
val network = value.networkId?.let(::createNetwork) ?: return null
val currency = createCryptoCurrencyOrNull(rawId, value, network) ?: return null
return HotCryptoCurrency(
cryptoCurrency = currency.setupIconUrl(id = rawId.value),
quote = createQuote(
fiatRate = value.currentPrice,
priceChange = value.priceChangePercentage,
rawCurrencyId = rawId,
),
)
}
private fun createCryptoCurrencyOrNull(
rawId: CryptoCurrency.RawID,
value: HotCryptoResponse.Token,
network: Network,
): CryptoCurrency? {
val name = value.name
val symbol = value.symbol
if (name.isNullOrBlank() || symbol.isNullOrBlank()) return null
val contractAddress = value.contractAddress
val decimals = value.decimalCount
val currency = if (contractAddress != null && decimals != null) {
return if (contractAddress != null && decimals != null) {
cryptoCurrencyFactory.createToken(
network = network,
rawId = CryptoCurrency.RawID(value.id),
name = value.name,
symbol = value.symbol,
rawId = rawId,
name = name,
symbol = symbol,
decimals = decimals,
contractAddress = contractAddress,
)
} else {
cryptoCurrencyFactory.createCoin(network = network)
}
}
return HotCryptoCurrency(
cryptoCurrency = currency.setupIconUrl(id = value.id),
fiatRate = value.currentPrice,
priceChange = value.priceChangePercentage,
private fun createNetwork(networkId: String): Network? {
return getNetwork(
blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)),
extraDerivationPath = null,
scanResponse = scanResponse,
excludedBlockchains = excludedBlockchains,
)
}
private fun createNetwork(networkId: String): Network {
return requireNotNull(
getNetwork(
blockchain = requireNotNull(Blockchain.fromNetworkId(networkId)),
extraDerivationPath = null,
scanResponse = scanResponse,
excludedBlockchains = excludedBlockchains,
),
)
private fun createQuote(
fiatRate: BigDecimal?,
priceChange: BigDecimal?,
rawCurrencyId: CryptoCurrency.RawID,
): Quote {
return if (fiatRate != null && priceChange != null) {
Quote.Value(
rawCurrencyId = rawCurrencyId,
fiatRate = fiatRate,
priceChange = priceChange.movePointLeft(2),
)
} else {
Quote.Empty(rawCurrencyId)
}
}
private fun CryptoCurrency.setupIconUrl(id: String): CryptoCurrency {