Skip to content

Commit bef9483

Browse files
authored
Merge pull request #207 from code-payments/fix/exchange-use-provided-expiry-time-when-success
fix(exchange): use provided expiry time for exchange rates to determi…
2 parents 7dc6e9b + 0a31e64 commit bef9483

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

  • api/src/main/java/com/getcode/network/exchange

api/src/main/java/com/getcode/network/exchange/Exchange.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import java.util.Date
2525
import javax.inject.Inject
2626
import javax.inject.Singleton
2727
import kotlin.coroutines.resume
28+
import kotlin.time.Duration
29+
import kotlin.time.Duration.Companion.convert
2830
import kotlin.time.Duration.Companion.minutes
31+
import kotlin.time.DurationUnit
32+
import kotlin.time.ExperimentalTime
2933

3034
interface Exchange {
3135
val localRate: Rate
@@ -41,7 +45,7 @@ interface Exchange {
4145
fun rateForUsd(): Rate?
4246
}
4347

44-
class ExchangeNull: Exchange {
48+
class ExchangeNull : Exchange {
4549
override val localRate: Rate
4650
get() = Rate.oneToOne
4751

@@ -73,7 +77,7 @@ class CodeExchange @Inject constructor(
7377
private val currencyApi: CurrencyApi,
7478
private val networkOracle: NetworkOracle,
7579
private val defaultCurrency: () -> Currency?,
76-
): Exchange, CoroutineScope by CoroutineScope(Dispatchers.IO) {
80+
) : Exchange, CoroutineScope by CoroutineScope(Dispatchers.IO) {
7781

7882
private val db = Database.getInstance()
7983

@@ -168,7 +172,13 @@ class CodeExchange @Inject constructor(
168172
val localCurrency = defaultCurrency()
169173
val rate = localCurrency?.let { rates.rateFor(it) }
170174
_localRate.value = if (rate != null) {
171-
Timber.d("Updated the entry currency: $localCurrency, Staleness ${System.currentTimeMillis() - rates.dateMillis} ms, Date: ${Date(rates.dateMillis)}")
175+
Timber.d(
176+
"Updated the entry currency: $localCurrency, Staleness ${System.currentTimeMillis() - rates.dateMillis} ms, Date: ${
177+
Date(
178+
rates.dateMillis
179+
)
180+
}"
181+
)
172182
rate
173183
} else {
174184
Timber.d("Rate for $localCurrency not found. Defaulting to USD.")
@@ -178,7 +188,13 @@ class CodeExchange @Inject constructor(
178188

179189
val entryRate = entryCurrency?.let { rates.rateFor(it) }
180190
this.entryRate = if (entryRate != null) {
181-
Timber.d("Updated the entry currency: $entryCurrency, Staleness ${System.currentTimeMillis() - rates.dateMillis} ms, Date: ${Date(rates.dateMillis)}")
191+
Timber.d(
192+
"Updated the entry currency: $entryCurrency, Staleness ${System.currentTimeMillis() - rates.dateMillis} ms, Date: ${
193+
Date(
194+
rates.dateMillis
195+
)
196+
}"
197+
)
182198
entryRate
183199
} else {
184200
Timber.d("Rate for $entryCurrency not found. Defaulting to USD.")
@@ -187,6 +203,7 @@ class CodeExchange @Inject constructor(
187203

188204
}
189205

206+
@OptIn(ExperimentalTime::class)
190207
@SuppressLint("CheckResult")
191208
private suspend fun fetchExchangeRates() = suspendCancellableCoroutine { cont ->
192209
Timber.d("fetching rates")
@@ -201,7 +218,14 @@ class CodeExchange @Inject constructor(
201218
if (rates.none { it.currency == CurrencyCode.KIN }) {
202219
rates.add(Rate(fx = 1.0, currency = CurrencyCode.KIN))
203220
}
204-
cont.resume(rates.toList() to System.currentTimeMillis())
221+
222+
cont.resume(
223+
rates.toList() to convert(
224+
value = response.asOf.seconds.toDouble(),
225+
sourceUnit = DurationUnit.SECONDS,
226+
targetUnit = DurationUnit.MILLISECONDS
227+
).toLong()
228+
)
205229
}, {
206230
ErrorUtils.handleError(it)
207231
cont.resume(emptyList<Rate>() to System.currentTimeMillis())
@@ -212,7 +236,9 @@ class CodeExchange @Inject constructor(
212236
}
213237

214238
private data class RatesBox(val dateMillis: Long, val rates: Map<CurrencyCode, Rate>) {
215-
constructor(dateMillis: Long, rates: List<Rate>): this(dateMillis, rates.associateBy { it.currency })
239+
constructor(dateMillis: Long, rates: List<Rate>) : this(
240+
dateMillis,
241+
rates.associateBy { it.currency })
216242

217243
val isEmpty: Boolean
218244
get() = rates.isEmpty()

0 commit comments

Comments
 (0)