Updated on 2026-08-14

This commit is contained in:
Tangem 2024-09-16 22:34:07 +03:00
parent d3cb89b1a1
commit a94db85dfd
7 changed files with 65 additions and 13 deletions

View file

@ -135,4 +135,13 @@ interface TangemTechApi {
@ReadTimeout(duration = 5, unit = TimeUnit.SECONDS)
@GET("networks/providers")
suspend fun getBlockchainProviders(): Map<String, List<ProviderModel>>
companion object {
val marketsQuoteFields = listOf(
"price",
"priceChange24h",
"priceChange1w",
"priceChange30d",
)
}
}

View file

@ -9,14 +9,17 @@ import com.tangem.data.common.currency.CryptoCurrencyFactory
import com.tangem.data.common.currency.getNetwork
import com.tangem.data.common.utils.retryOnError
import com.tangem.data.markets.analytics.MarketsDataAnalyticsEvent
import com.tangem.data.markets.converters.*
import com.tangem.data.markets.converters.TokenChartConverter
import com.tangem.data.markets.converters.TokenMarketInfoConverter
import com.tangem.data.markets.converters.TokenMarketListConverter
import com.tangem.data.markets.converters.TokenQuotesShortConverter
import com.tangem.data.markets.converters.toRequestParam
import com.tangem.datasource.api.common.response.ApiResponseError
import com.tangem.datasource.api.common.response.getOrThrow
import com.tangem.datasource.api.markets.TangemTechMarketsApi
import com.tangem.datasource.api.tangemTech.TangemTechApi
import com.tangem.datasource.api.tangemTech.TangemTechApi.Companion.marketsQuoteFields
import com.tangem.datasource.local.userwallet.UserWalletsStore
import com.tangem.domain.common.util.derivationStyleProvider
import com.tangem.domain.markets.*
@ -188,16 +191,24 @@ internal class DefaultMarketsTokenRepository(
return TokenMarketInfoConverter.convert(resultResponse)
}
override suspend fun getTokenQuotes(fiatCurrencyCode: String, tokenId: String): TokenQuotes {
// TODO change method when backend is ready
// add error analytics event
val response = marketsApi.getCoinMarketData(
currency = fiatCurrencyCode,
coinId = tokenId,
language = "en",
override suspend fun getTokenQuotes(fiatCurrencyCode: String, tokenId: String, tokenSymbol: String): TokenQuotes {
// for second markets iteration we should use extended api method with all required fields
val response = tangemTechApi.getQuotes(
currencyId = fiatCurrencyCode,
coinIds = tokenId,
fields = marketsQuoteFields.joinToString(separator = ","),
)
return TokenMarketInfoConverter.convert(response.getOrThrow()).quotes
val result = catchApiErrorAndSendEvent(
errorEvent = MarketsDataAnalyticsEvent.Details.Error(
request = MarketsDataAnalyticsEvent.Details.Error.Request.Info,
tokenSymbol = tokenSymbol,
),
) {
response.getOrThrow()
}
return TokenQuotesShortConverter.convert(tokenId, result).toFull()
}
override suspend fun createCryptoCurrency(

View file

@ -11,4 +11,17 @@ data class TokenQuotes(
val m6ChangePercent: BigDecimal?,
val yearChangePercent: BigDecimal?,
val allTimeChangePercent: BigDecimal?,
)
)
fun TokenQuotes.populateWith(quotes: TokenQuotes): TokenQuotes {
return TokenQuotes(
currentPrice = quotes.currentPrice,
h24ChangePercent = quotes.h24ChangePercent ?: this.h24ChangePercent,
weekChangePercent = quotes.weekChangePercent ?: this.weekChangePercent,
monthChangePercent = quotes.monthChangePercent ?: this.monthChangePercent,
m3ChangePercent = quotes.m3ChangePercent ?: this.m3ChangePercent,
m6ChangePercent = quotes.m6ChangePercent ?: this.m6ChangePercent,
yearChangePercent = quotes.yearChangePercent ?: this.yearChangePercent,
allTimeChangePercent = quotes.allTimeChangePercent ?: this.allTimeChangePercent,
)
}

View file

@ -7,4 +7,15 @@ data class TokenQuotesShort(
val h24ChangePercent: BigDecimal,
val weekChangePercent: BigDecimal,
val monthChangePercent: BigDecimal,
)
fun TokenQuotesShort.toFull() = TokenQuotes(
currentPrice = currentPrice,
h24ChangePercent = h24ChangePercent,
weekChangePercent = weekChangePercent,
monthChangePercent = monthChangePercent,
m3ChangePercent = null,
m6ChangePercent = null,
yearChangePercent = null,
allTimeChangePercent = null,
)

View file

@ -7,11 +7,16 @@ import com.tangem.domain.markets.repositories.MarketsTokenRepository
class GetTokenFullQuotesUseCase(
private val marketsTokenRepository: MarketsTokenRepository,
) {
suspend operator fun invoke(appCurrency: AppCurrency, tokenId: String): Either<Unit, TokenQuotes> {
suspend operator fun invoke(
appCurrency: AppCurrency,
tokenId: String,
tokenSymbol: String,
): Either<Unit, TokenQuotes> {
return Either.catch {
marketsTokenRepository.getTokenQuotes(
fiatCurrencyCode = appCurrency.code,
tokenId = tokenId,
tokenSymbol = tokenSymbol,
)
}.mapLeft {}
}

View file

@ -33,7 +33,7 @@ interface MarketsTokenRepository {
languageCode: String,
): TokenMarketInfo
suspend fun getTokenQuotes(fiatCurrencyCode: String, tokenId: String): TokenQuotes
suspend fun getTokenQuotes(fiatCurrencyCode: String, tokenId: String, tokenSymbol: String): TokenQuotes
suspend fun createCryptoCurrency(
userWalletId: UserWalletId,

View file

@ -229,6 +229,7 @@ internal class MarketsTokenDetailsModel @Inject constructor(
val result = getTokenFullQuotesUseCase(
tokenId = params.token.id,
appCurrency = currentAppCurrency.value,
tokenSymbol = params.token.symbol,
)
result.onRight { res ->
@ -389,9 +390,11 @@ internal class MarketsTokenDetailsModel @Inject constructor(
}
private suspend fun updateQuotes(newQuotes: TokenQuotes) {
quotesStateUpdater.updateQuotes(newQuotes)
val populatedNewQuotes = currentQuotes.value.populateWith(newQuotes)
val percent = newQuotes
quotesStateUpdater.updateQuotes(newQuotes = populatedNewQuotes)
val percent = populatedNewQuotes
.getPercentByInterval(interval = state.value.selectedInterval)
chartDataProducer.runTransaction {