Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch global market data to new endpoint #7494

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ dependencies {
implementation 'com.github.horizontalsystems:ethereum-kit-android:3d83900'
implementation 'com.github.horizontalsystems:blockchain-fee-rate-kit-android:1d3bd49'
implementation 'com.github.horizontalsystems:binance-chain-kit-android:c1509a2'
implementation 'com.github.horizontalsystems:market-kit-android:b9abcef'
implementation 'com.github.horizontalsystems:market-kit-android:6928d76'
implementation 'com.github.horizontalsystems:solana-kit-android:ec238b4'
implementation 'com.github.horizontalsystems:tron-kit-android:dc3dca7'
// Zcash SDK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class MarketKitWrapper(

fun marketOverviewSingle(currencyCode: String) = marketKit.marketOverviewSingle(currencyCode)

fun marketGlobalSingle(currencyCode: String) = marketKit.marketGlobalSingle(currencyCode)

fun topPairsSingle(currencyCode: String, page: Int, limit: Int) = marketKit.topPairsSingle(currencyCode, page, limit)

fun topMoversSingle(currencyCode: String) = marketKit.topMoversSingle(currencyCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package io.horizontalsystems.bankwallet.modules.market

import android.util.Log
import androidx.lifecycle.viewModelScope
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.ILocalStorage
import io.horizontalsystems.bankwallet.core.IMarketStorage
import io.horizontalsystems.bankwallet.core.ViewModelUiState
import io.horizontalsystems.bankwallet.core.managers.CurrencyManager
import io.horizontalsystems.bankwallet.core.managers.MarketKitWrapper
import io.horizontalsystems.bankwallet.core.providers.Translator
import io.horizontalsystems.bankwallet.entities.Currency
import io.horizontalsystems.bankwallet.entities.LaunchPage
import io.horizontalsystems.bankwallet.modules.market.MarketModule.MarketOverviewViewItem
import io.horizontalsystems.bankwallet.modules.market.MarketModule.Tab
import io.horizontalsystems.bankwallet.modules.metricchart.MetricsType
import io.horizontalsystems.marketkit.models.GlobalMarketPoint
import io.horizontalsystems.marketkit.models.MarketGlobal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -53,12 +55,10 @@ class MarketViewModel(
marketOverviewJob?.cancel()
marketOverviewJob = viewModelScope.launch(Dispatchers.IO) {
try {
val marketOverview =
marketKit.marketOverviewSingle(currencyManager.baseCurrency.code).await()
marketOverview?.globalMarketPoints?.let {
marketOverviewItems = getMarketMetrics(it, currencyManager.baseCurrency)
emitState()
}
val marketGlobal =
marketKit.marketGlobalSingle(currencyManager.baseCurrency.code).await()
marketOverviewItems = getMarketMetrics(marketGlobal, currencyManager.baseCurrency)
emitState()
} catch (e: Throwable) {
Log.e("TAG", "updateMarketOverview: ", e)
}
Expand All @@ -72,76 +72,49 @@ class MarketViewModel(
}

private fun getMarketMetrics(
globalMarketPoints: List<GlobalMarketPoint>,
globalMarket: MarketGlobal,
baseCurrency: Currency
): List<MarketOverviewViewItem> {
var marketCap: BigDecimal? = null
var marketCapDiff: BigDecimal? = null
var defiMarketCap: BigDecimal? = null
var defiMarketCapDiff: BigDecimal? = null
var volume24h: BigDecimal? = null
var volume24hDiff: BigDecimal? = null
var tvl: BigDecimal? = null
var tvlDiff: BigDecimal? = null
var btcDominance: BigDecimal? = null
var btcDominanceDiff: BigDecimal? = null

if (globalMarketPoints.isNotEmpty()) {
val startingPoint = globalMarketPoints.first()
val endingPoint = globalMarketPoints.last()

marketCap = endingPoint.marketCap
marketCapDiff = diff(startingPoint.marketCap, marketCap)

defiMarketCap = endingPoint.defiMarketCap
defiMarketCapDiff = diff(startingPoint.defiMarketCap, defiMarketCap)

volume24h = endingPoint.volume24h
volume24hDiff = diff(startingPoint.volume24h, volume24h)

btcDominance = endingPoint.btcDominance
btcDominanceDiff = diff(startingPoint.btcDominance, btcDominance)

tvl = endingPoint.tvl
tvlDiff = diff(startingPoint.tvl, tvl)
}

val metrics: List<MarketOverviewViewItem> = listOf(
MarketOverviewViewItem(
"Total.Cap",
marketCap?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
marketCapDiff?.let { getDiff(it) } ?: "----",
marketCapDiff?.let { it > BigDecimal.ZERO } ?: false,
Translator.getString(R.string.MarketGlobalMetrics_TotalMarketCap),
globalMarket.marketCap?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
globalMarket.marketCapChange?.let { getDiff(it) } ?: "----",
globalMarket.marketCapChange?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.TotalMarketCap
),
MarketOverviewViewItem(
"24h Vol.",
volume24h?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
volume24hDiff?.let { getDiff(it) } ?: "----",
volume24hDiff?.let { it > BigDecimal.ZERO } ?: false,
Translator.getString(R.string.MarketGlobalMetrics_Volume),
globalMarket.volume?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
globalMarket.volumeChange?.let { getDiff(it) } ?: "----",
globalMarket.volumeChange?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.Volume24h
),
MarketOverviewViewItem(
"BTC Dominance",
btcDominance?.let {
Translator.getString(R.string.MarketGlobalMetrics_BtcDominance),
globalMarket.btcDominance?.let {
App.numberFormatter.format(it, 0, 2, suffix = "%")
} ?: "-",
btcDominanceDiff?.let { getDiff(it) } ?: "----",
btcDominanceDiff?.let { it > BigDecimal.ZERO } ?: false,
globalMarket.btcDominance?.let { getDiff(it) } ?: "----",
globalMarket.btcDominance?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.TotalMarketCap
),
MarketOverviewViewItem(
"ETF",
defiMarketCap?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
defiMarketCapDiff?.let { getDiff(it) } ?: "----",
defiMarketCapDiff?.let { it > BigDecimal.ZERO } ?: false,
Translator.getString(R.string.MarketGlobalMetrics_EtfInflow),
globalMarket.etfTotalInflow?.let { formatFiatShortened(it, baseCurrency.symbol) }
?: "-",
globalMarket.etfDailyInflow?.let {
val sign = if (it >= BigDecimal.ZERO) "+" else "-"
"$sign${formatFiatShortened(it, baseCurrency.symbol)}"
} ?: "----",
globalMarket.etfDailyInflow?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.Etf
),
MarketOverviewViewItem(
"TVL",
tvl?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
tvlDiff?.let { getDiff(it) } ?: "----",
tvlDiff?.let { it > BigDecimal.ZERO } ?: false,
Translator.getString(R.string.MarketGlobalMetrics_TvlInDefi),
globalMarket.tvl?.let { formatFiatShortened(it, baseCurrency.symbol) } ?: "-",
globalMarket.tvlChange?.let { getDiff(it) } ?: "----",
globalMarket.tvlChange?.let { it > BigDecimal.ZERO } ?: false,
MetricsType.TvlInDefi
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.horizontalsystems.bankwallet.core.stats.statPage
import io.horizontalsystems.bankwallet.entities.Currency
import io.horizontalsystems.bankwallet.entities.CurrencyValue
import io.horizontalsystems.bankwallet.entities.ViewState
import io.horizontalsystems.bankwallet.modules.market.ImageSource
import io.horizontalsystems.bankwallet.modules.market.MarketDataValue
import io.horizontalsystems.bankwallet.modules.market.MarketModule
import io.horizontalsystems.bankwallet.modules.market.filters.TimePeriod
Expand Down Expand Up @@ -45,13 +46,26 @@ class MetricsPageViewModel(

private val toggleButtonTitle = when (metricsType) {
MetricsType.Volume24h -> Translator.getString(R.string.Market_Volume)
else -> Translator.getString(R.string.Market_MarketCap)
MetricsType.TotalMarketCap -> Translator.getString(R.string.Market_MarketCap)
else -> throw Exception("MetricsType not supported")
}

private val title = when(metricsType) {
MetricsType.Volume24h -> R.string.MarketGlobalMetrics_Volume
MetricsType.TotalMarketCap -> R.string.MarketGlobalMetrics_TotalMarketCap
else -> throw Exception("MetricsType not supported")
}

private val description = when(metricsType) {
MetricsType.Volume24h -> R.string.MarketGlobalMetrics_VolumeDescription
MetricsType.TotalMarketCap -> R.string.MarketGlobalMetrics_TotalMarketCapDescription
else -> throw Exception("MetricsType not supported")
}

private val header = MarketModule.Header(
title = Translator.getString(metricsType.title),
description = Translator.getString(metricsType.description),
icon = metricsType.headerIcon
title = Translator.getString(title),
description = Translator.getString(description),
icon = ImageSource.Remote("https://cdn.blocksdecoded.com/header-images/[email protected]")
)

override fun createState(): MetricsPageModule.UiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import io.horizontalsystems.bankwallet.core.stats.statPage
import io.horizontalsystems.bankwallet.modules.market.overview.MarketOverviewModule
import io.horizontalsystems.bankwallet.modules.metricchart.MetricsType
import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme
import io.horizontalsystems.bankwallet.ui.compose.components.caption_grey
import io.horizontalsystems.bankwallet.ui.extensions.MetricData
import io.horizontalsystems.chartview.ChartMinimal
import java.math.BigDecimal
Expand Down Expand Up @@ -59,7 +58,7 @@ private fun ChartView(metricsData: MetricData, navController: NavController) {
Column(
modifier = Modifier.padding(12.dp)
) {
caption_grey(text = stringResource(metricsData.type.title))
// caption_grey(text = stringResource(metricsData.type.title))
Spacer(modifier = Modifier.height(10.dp))
if (metricsData.value != null) {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package io.horizontalsystems.bankwallet.modules.market.tvl
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.providers.Translator
import io.horizontalsystems.bankwallet.core.stats.StatEvent
import io.horizontalsystems.bankwallet.core.stats.StatPage
import io.horizontalsystems.bankwallet.core.stats.stat
import io.horizontalsystems.bankwallet.entities.ViewState
import io.horizontalsystems.bankwallet.modules.market.ImageSource
import io.horizontalsystems.bankwallet.modules.market.MarketModule
import io.horizontalsystems.bankwallet.modules.market.tvl.TvlModule.SelectorDialogState
import io.horizontalsystems.bankwallet.modules.market.tvl.TvlModule.TvlDiffType
import io.horizontalsystems.bankwallet.modules.metricchart.MetricsType
import io.horizontalsystems.bankwallet.ui.compose.Select
import io.horizontalsystems.marketkit.models.HsTimePeriod
import kotlinx.coroutines.delay
Expand All @@ -29,7 +30,6 @@ class TvlViewModel(
tvlDiffTypeLiveData.postValue(value)
}
private var tvlItems: List<TvlModule.MarketTvlItem> = listOf()
private val metricsType = MetricsType.TvlInDefi

val isRefreshingLiveData = MutableLiveData<Boolean>()
val tvlLiveData = MutableLiveData<TvlModule.TvlData>()
Expand All @@ -38,9 +38,9 @@ class TvlViewModel(
val chainSelectorDialogStateLiveData = MutableLiveData<SelectorDialogState>()

var header = MarketModule.Header(
title = Translator.getString(metricsType.title),
description = Translator.getString(metricsType.description),
icon = metricsType.headerIcon
title = Translator.getString(R.string.MarketGlobalMetrics_TvlInDefi),
description = Translator.getString(R.string.MarketGlobalMetrics_TvlInDefiDescription),
icon = ImageSource.Remote("https://cdn.blocksdecoded.com/header-images/[email protected]")
)

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
package io.horizontalsystems.bankwallet.modules.metricchart

import android.os.Parcelable
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.modules.market.ImageSource
import kotlinx.parcelize.Parcelize

@Parcelize
enum class MetricsType : Parcelable {
TotalMarketCap, Volume24h, Etf, TvlInDefi;

val title: Int
get() = when (this) {
TotalMarketCap -> R.string.MarketGlobalMetrics_TotalMarketCap
Volume24h -> R.string.MarketGlobalMetrics_Volume
Etf -> R.string.MarketGlobalMetrics_Etf
TvlInDefi -> R.string.MarketGlobalMetrics_TvlInDefi
}

val description: Int
get() = when (this) {
TotalMarketCap -> R.string.MarketGlobalMetrics_TotalMarketCapDescription
Volume24h -> R.string.MarketGlobalMetrics_VolumeDescription
Etf -> R.string.MarketEtf_Description
TvlInDefi -> R.string.MarketGlobalMetrics_TvlInDefiDescription
}

val headerIcon: ImageSource
get() {
val imageName = when (this) {
TotalMarketCap,
Volume24h -> "total_volume"
Etf -> "defi_cap"
TvlInDefi -> "tvl"
}

return ImageSource.Remote("https://cdn.blocksdecoded.com/header-images/[email protected]")
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@

<!--Market Global Metrics-->
<string name="MarketGlobalMetrics_Chart">Chart</string>
<string name="MarketGlobalMetrics_TotalMarketCap">Total M.Cap</string>
<string name="MarketGlobalMetrics_BtcDominance">Bitcoin Dominance</string>
<string name="MarketGlobalMetrics_Volume">24h Volume</string>
<string name="MarketGlobalMetrics_TotalMarketCap">Total Cap</string>
<string name="MarketGlobalMetrics_BtcDominance">BTC Dominance</string>
<string name="MarketGlobalMetrics_Volume">24h Vol.</string>
<string name="MarketGlobalMetrics_DefiCap">DeFi Cap</string>
<string name="MarketGlobalMetrics_TvlInDefi">TVL in DeFi</string>
<string name="MarketGlobalMetrics_EtfInflow">ETF Inflow</string>
<string name="MarketGlobalMetrics_TotalMarketCapDescription">Total market value of all cryptocurrencies.</string>
<string name="MarketGlobalMetrics_BtcDominanceDescription">The historical change in ratio between the market cap of Bitcoin to the rest of the cryptocurrency markets. \n\nTypically, when Bitcoin dominance is trending higher, the rest of the crypto market trends downwards and vice versa.</string>
<string name="MarketGlobalMetrics_VolumeDescription">The 24h trading volume of the crypto market.</string>
Expand All @@ -279,7 +280,6 @@
<string name="MarketGlobalMetrics_ChainSelectorAll">All</string>
<string name="MarketGlobalMetrics_CoinNotSupported">This coin is not supported yet</string>
<string name="MarketGlobalMetrics_NoCoin">This project doesn\'t have a coin</string>
<string name="MarketGlobalMetrics_Etf">Etf</string>

<!-- ETF-->

Expand Down
Loading