Updated on 2026-08-14

This commit is contained in:
Tangem 2023-08-11 08:26:23 +03:00
parent b284a8195a
commit 9b70e5bc82
14 changed files with 116 additions and 77 deletions

View file

@ -13,13 +13,13 @@ internal class MockQuotesRepository : QuotesRepository {
override fun getQuotes(currenciesIds: Set<CryptoCurrency.ID>, refresh: Boolean): Flow<Set<Quote>> {
return channelFlow {
val quotes = currenciesIds.map {
val quotes = currenciesIds.mapNotNullTo(hashSetOf()) { id ->
Quote(
currencyId = it,
rawCurrencyId = id.rawCurrencyId ?: return@mapNotNullTo null,
fiatRate = BigDecimal.ZERO,
priceChange = BigDecimal.ZERO,
)
}.toSet()
}
delay(Random.nextLong(from = 200, until = 2_000))

View file

@ -39,7 +39,7 @@ internal class NetworkStatusFactory {
is CryptoCurrencyAmount.Coin -> currencies.singleOrNull { it is CryptoCurrency.Coin }
is CryptoCurrencyAmount.Token -> currencies.firstOrNull {
it is CryptoCurrency.Token &&
getTokenIdString(it.id) == amount.id &&
it.id.rawCurrencyId == amount.id &&
it.contractAddress == amount.tokenContractAddress
}
}

View file

@ -13,7 +13,7 @@ import com.tangem.blockchain.common.Token as SdkToken
internal class ResponseCurrenciesFactory(private val demoConfig: DemoConfig) {
fun createCurrency(currencyId: CryptoCurrency.ID, response: UserTokensResponse, card: CardDTO): CryptoCurrency {
val responseTokenId = getTokenIdString(currencyId)
val responseTokenId = currencyId.rawCurrencyId
val token = requireNotNull(response.tokens.firstOrNull { it.id == responseTokenId }) {
"Unable find a token with provided ID: $responseTokenId"

View file

@ -6,21 +6,21 @@ import com.tangem.domain.common.TapWorkarounds.derivationStyle
import com.tangem.domain.common.extensions.toCoinId
import com.tangem.domain.common.extensions.toNetworkId
import com.tangem.domain.models.scan.CardDTO
import com.tangem.domain.tokens.models.CryptoCurrency
import com.tangem.domain.tokens.models.CryptoCurrency.ID
import com.tangem.domain.tokens.models.Network
import com.tangem.blockchain.common.Token as SdkToken
import com.tangem.domain.tokens.models.CryptoCurrency.ID.Prefix.COIN_PREFIX as COIN_ID_PREFIX
import com.tangem.domain.tokens.models.CryptoCurrency.ID.Prefix.CUSTOM_TOKEN_PREFIX as CUSTOM_TOKEN_ID_PREFIX
import com.tangem.domain.tokens.models.CryptoCurrency.ID.Prefix.TOKEN_PREFIX as TOKEN_ID_PREFIX
import com.tangem.domain.tokens.models.CryptoCurrency.ID.Suffix.ContractAddress as CustomCurrencyIdSuffix
import com.tangem.domain.tokens.models.CryptoCurrency.ID.Suffix.RawID as CurrencyIdSuffix
private const val DEFAULT_TOKENS_ICONS_HOST = "https://s3.eu-central-1.amazonaws.com/tangem.api/coins"
private const val TOKEN_ICON_SIZE = "large"
private const val TOKEN_ICON_EXT = "png"
private const val COIN_ID_PREFIX = "coin_"
private const val TOKEN_ID_PREFIX = "token_"
private const val CUSTOM_TOKEN_ID_PREFIX = "custom_token_"
private const val TOKEN_ID_DELIMITER = '#'
internal fun isCustomToken(tokenId: CryptoCurrency.ID): Boolean {
return tokenId.value.startsWith(CUSTOM_TOKEN_ID_PREFIX)
internal fun isCustomToken(tokenId: ID): Boolean {
return tokenId.rawCurrencyId == null
}
internal fun getDerivationPath(blockchain: Blockchain, card: CardDTO): String? {
@ -41,24 +41,14 @@ internal fun getNetworkId(blockchain: Blockchain): Network.ID {
return Network.ID(value)
}
internal fun getCoinId(blockchain: Blockchain): CryptoCurrency.ID {
internal fun getCoinId(blockchain: Blockchain): ID {
return getTokenOrCoinId(blockchain, token = null)
}
internal fun getTokenId(blockchain: Blockchain, token: SdkToken): CryptoCurrency.ID {
internal fun getTokenId(blockchain: Blockchain, token: SdkToken): ID {
return getTokenOrCoinId(blockchain, token)
}
internal fun getTokenIdString(currencyId: CryptoCurrency.ID): String? {
val idValue = currencyId.value
return if (idValue.startsWith(CUSTOM_TOKEN_ID_PREFIX)) {
null
} else {
idValue.substringAfter(TOKEN_ID_DELIMITER)
}
}
internal fun getTokenIconUrl(blockchain: Blockchain, token: SdkToken): String? {
val tokenId = token.id
@ -79,22 +69,15 @@ internal fun getCoinIconUrl(blockchain: Blockchain): String? {
return coinId?.let(::getTokenIconUrlFromDefaultHost)
}
private fun getTokenOrCoinId(blockchain: Blockchain, token: SdkToken?): CryptoCurrency.ID {
private fun getTokenOrCoinId(blockchain: Blockchain, token: SdkToken?): ID {
val sdkTokenId = token?.id
val (prefix, suffix) = when {
token == null -> COIN_ID_PREFIX to blockchain.toCoinId()
sdkTokenId == null -> CUSTOM_TOKEN_ID_PREFIX to token.contractAddress
else -> TOKEN_ID_PREFIX to sdkTokenId
token == null -> COIN_ID_PREFIX to CurrencyIdSuffix(rawId = blockchain.toCoinId())
sdkTokenId == null -> CUSTOM_TOKEN_ID_PREFIX to CustomCurrencyIdSuffix(contractAddress = token.contractAddress)
else -> TOKEN_ID_PREFIX to CurrencyIdSuffix(rawId = sdkTokenId)
}
val value = buildString {
append(prefix)
append(blockchain.id)
append(TOKEN_ID_DELIMITER)
append(suffix.lowercase())
}
return CryptoCurrency.ID(value)
return ID(prefix, getNetworkId(blockchain), suffix)
}
private fun getTokenIconUrlFromDefaultHost(tokenId: String): String {

View file

@ -30,7 +30,7 @@ internal class UserTokensResponseFactory {
val blockchain = getBlockchain(currency.networkId)
return UserTokensResponse.Token(
id = getTokenIdString(currency.id),
id = currency.id.rawCurrencyId,
networkId = blockchain.toNetworkId(),
derivationPath = currency.derivationPath,
name = currency.name,