@@ -113,7 +113,7 @@ export async function getExchangeRates(
113
113
cryptoCurrencies = cryptoCurrencies . map ( ( currency ) => currency . toLowerCase ( ) as FiatApiSupportedCryptoCurrency ) ;
114
114
115
115
// Check for bridged currencies and fetch their USD exchange rates
116
- const bridgedExchangeRatesPromises : Array < Promise < [ FiatApiBridgedFiatCurrency , number ] > > = [ ] ;
116
+ const bridgedExchangeRatesPromises : Array < Promise < [ FiatApiBridgedFiatCurrency , number | undefined ] > > = [ ] ;
117
117
for ( const vsCurrency of vsCurrencies ) {
118
118
if ( ! _isBridgedFiatCurrency ( vsCurrency ) ) continue ;
119
119
bridgedExchangeRatesPromises . push ( _getBridgedFiatCurrencyExchangeRate ( vsCurrency )
@@ -167,7 +167,7 @@ export async function getHistoricExchangeRatesByRange(
167
167
to : number , // in milliseconds
168
168
) : Promise < Array < [ number , number ] > > {
169
169
let bridgedCurrency : FiatApiHistorySupportedBridgedFiatCurrency | undefined ;
170
- let bridgedExchangeRatePromise : Promise < Record < string , number > > | undefined ;
170
+ let bridgedExchangeRatePromise : Promise < Record < string , number | undefined > > | undefined ;
171
171
if ( _isBridgedFiatCurrency ( vsCurrency ) ) {
172
172
bridgedCurrency = vsCurrency ;
173
173
bridgedExchangeRatePromise = _getHistoricBridgedFiatCurrencyExchangeRatesByRange ( bridgedCurrency , from , to ) ;
@@ -180,26 +180,25 @@ export async function getHistoricExchangeRatesByRange(
180
180
from = Math . floor ( from / 1000 ) ;
181
181
to = Math . ceil ( to / 1000 ) ;
182
182
const [
183
- { prices : result } ,
184
- bridgedExchangeRates ,
183
+ { prices : coingeckoHistoricRates } ,
184
+ bridgedHistoricRates ,
185
185
] = await Promise . all ( [
186
186
_fetch < { prices : Array < [ number , number ] > } > (
187
187
`${ API_URL } /coins/${ coinId } /market_chart/range?vs_currency=${ vsCurrency } &from=${ from } &to=${ to } ` ,
188
188
) ,
189
189
bridgedExchangeRatePromise ,
190
190
] ) ;
191
191
192
- if ( bridgedCurrency && bridgedExchangeRates ) {
193
- return result . map ( ( [ timestamp , coinUsdPrice ] ) => [
194
- timestamp ,
195
- coinUsdPrice * bridgedExchangeRates [ _getDateString (
196
- timestamp ,
197
- HISTORY_SUPPORTED_BRIDGED_CURRENCY_TIMEZONES [ bridgedCurrency ! ] ,
198
- ) ] ,
199
- ] ) ;
192
+ if ( bridgedCurrency && bridgedHistoricRates ) {
193
+ // Convert exchange rates to bridged currency and omit entries for which no bridged exchange rate is available.
194
+ return coingeckoHistoricRates . map ( ( [ timestamp , coinUsdPrice ] ) => {
195
+ const date = _getDateString ( timestamp , HISTORY_SUPPORTED_BRIDGED_CURRENCY_TIMEZONES [ bridgedCurrency ! ] ) ;
196
+ const bridgedHistoricRate = bridgedHistoricRates [ date ] ;
197
+ return bridgedHistoricRate ? [ timestamp , coinUsdPrice * bridgedHistoricRate ] : null ;
198
+ } ) . filter ( ( entry ) : entry is [ number , number ] => entry !== null ) ;
200
199
}
201
200
202
- return result ;
201
+ return coingeckoHistoricRates ;
203
202
}
204
203
205
204
/**
@@ -365,16 +364,17 @@ function _isBridgedFiatCurrency(currency: unknown): currency is FiatApiBridgedFi
365
364
}
366
365
367
366
/**
368
- * Get today's exchange rate to USD.
367
+ * Get today's exchange rate to USD. Can be undefined if the user's clock is in the future.
369
368
*/
370
- async function _getBridgedFiatCurrencyExchangeRate ( bridgedFiatCurrency : FiatApiBridgedFiatCurrency ) : Promise < number > {
369
+ async function _getBridgedFiatCurrencyExchangeRate ( bridgedFiatCurrency : FiatApiBridgedFiatCurrency )
370
+ : Promise < number | undefined > {
371
371
switch ( bridgedFiatCurrency ) {
372
372
case FiatApiBridgedFiatCurrency . CRC : {
373
373
const crcExchangeRates = await _getHistoricBridgedFiatCurrencyExchangeRatesByRange (
374
374
FiatApiBridgedFiatCurrency . CRC ,
375
375
Date . now ( ) ,
376
376
) ;
377
- // There is only a single exchange rate entry which is for the current date
377
+ // There is only a single exchange rate entry, if any, which is for the current date.
378
378
return Object . values ( crcExchangeRates ) [ 0 ] ;
379
379
}
380
380
case FiatApiBridgedFiatCurrency . GMD :
@@ -397,14 +397,15 @@ async function _getHistoricBridgedFiatCurrencyExchangeRatesByRange(
397
397
bridgedFiatCurrency : FiatApiHistorySupportedBridgedFiatCurrency ,
398
398
from : number , // in milliseconds, inclusive
399
399
to : number = from , // in milliseconds, inclusive
400
- ) : Promise < Record < string , number > > {
400
+ ) : Promise < Record < string , number | undefined > > {
401
401
if ( bridgedFiatCurrency !== FiatApiBridgedFiatCurrency . CRC ) {
402
402
// Currently only suported for CRC. Check for users that don't use typescript.
403
403
throw new Error ( `Unsupported bridged currency for historic rates: ${ bridgedFiatCurrency } ` ) ;
404
404
}
405
405
const timezone = HISTORY_SUPPORTED_BRIDGED_CURRENCY_TIMEZONES [ bridgedFiatCurrency ] ;
406
406
const fromDate = _getDateString ( from , timezone ) ;
407
407
const toDate = to === from ? fromDate : _getDateString ( to , timezone ) ;
408
+ // Note: entries for future dates are omitted and thus basically undefined which is reflected in the return type.
408
409
return _fetch < Record < string , number > > ( `https://usd-crc-historic-rate.deno.dev/api/rates/${ fromDate } /${ toDate } ` ) ;
409
410
}
410
411
0 commit comments