Skip to content

Commit

Permalink
Limit price range to 1 year to satisfy coingecko constrains (novasama…
Browse files Browse the repository at this point in the history
  • Loading branch information
valentunn authored Apr 15, 2024
1 parent 9fd0efe commit 8f364bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import io.novafoundation.nova.feature_wallet_api.data.source.CoinPriceRemoteData
import io.novafoundation.nova.feature_wallet_api.domain.interfaces.CoinPriceRepository
import io.novafoundation.nova.feature_wallet_api.domain.model.CoinRateChange
import io.novafoundation.nova.feature_wallet_api.domain.model.HistoricalCoinRate
import java.util.Calendar
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

private const val START_SYNCING_YEAR = 2010
private val MAX_ALLOWED_PRICE_RANGE = 365.days
private const val BASE_OFFSET = 1

class CoinPriceRepositoryImpl(
Expand Down Expand Up @@ -66,11 +65,9 @@ class CoinPriceRepositoryImpl(
}

private suspend fun loadAndCacheForAllTime(priceId: String, currency: Currency): List<HistoricalCoinRate> {
val startCalendar = Calendar.getInstance()
startCalendar.set(START_SYNCING_YEAR, 0, 0)
val from = startCalendar.time.time.milliseconds.inWholeSeconds
val to = System.currentTimeMillis().milliseconds.inWholeSeconds
val coinRate = remoteCoinPriceDataSource.getCoinPriceRange(priceId, currency, from, to)
val to = System.currentTimeMillis().milliseconds
val from = to - MAX_ALLOWED_PRICE_RANGE
val coinRate = remoteCoinPriceDataSource.getCoinPriceRange(priceId, currency, from.inWholeSeconds, to.inWholeSeconds)
if (coinRate.isNotEmpty()) {
cacheCoinPriceDataSource.updateCoinPrice(priceId, currency, coinRate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.novafoundation.nova.feature_wallet_impl.data.source
import io.novafoundation.nova.common.data.network.HttpExceptionHandler
import io.novafoundation.nova.common.utils.asQueryParam
import io.novafoundation.nova.common.utils.orZero
import io.novafoundation.nova.common.utils.second
import io.novafoundation.nova.feature_currency_api.domain.model.Currency
import io.novafoundation.nova.feature_wallet_api.data.network.coingecko.CoingeckoApi
import io.novafoundation.nova.feature_wallet_api.data.source.CoinPriceRemoteDataSource
Expand All @@ -18,10 +17,11 @@ class CoingeckoCoinPriceDataSource(

override suspend fun getCoinPriceRange(priceId: String, currency: Currency, fromTimestamp: Long, toTimestamp: Long): List<HistoricalCoinRate> {
val response = coingeckoApi.getCoinRange(priceId, currency.coingeckoId, fromTimestamp, toTimestamp)
return response.prices.map {

return response.prices.map { (timestampRaw, rateRaw) ->
HistoricalCoinRate(
it.first().toLong().milliseconds.inWholeSeconds,
it.second()
timestamp = timestampRaw.toLong().milliseconds.inWholeSeconds,
rate = rateRaw
)
}
}
Expand Down

0 comments on commit 8f364bf

Please sign in to comment.