Skip to content

Commit 091a23f

Browse files
committed
fixup! Update coinrank error handling & fallback
1 parent e8c8595 commit 091a23f

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

src/exchangeRateRouter.ts

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ const getRedisMarkets = async (
338338

339339
// Only parse if jsonString is not null/undefined
340340
let redisResult: CoinrankRedis | undefined
341-
if (jsonString !== null && jsonString !== undefined) {
341+
if (jsonString != null) {
342342
try {
343343
redisResult = JSON.parse(jsonString)
344344

@@ -357,78 +357,78 @@ const getRedisMarkets = async (
357357
}
358358
}
359359

360+
// For USD requests, return the result immediately (could be undefined if not found)
361+
if (fiatCode === defaultFiatCode) {
362+
return redisResult
363+
}
364+
360365
// If we need to convert from USD (either no data or expired data)
361-
if (fiatCode !== defaultFiatCode) {
362-
try {
363-
// Get exchange rate
364-
const result = await fetch(
365-
`${ratesServerAddress}/v2/exchangeRate?currency_pair=${defaultFiatCode}_${fiatCode}`
366-
)
367-
if (!result.ok) {
368-
throw new Error(`Exchange rate API returned status ${result.status}`)
369-
}
366+
try {
367+
// Get exchange rate
368+
const result = await fetch(
369+
`${ratesServerAddress}/v2/exchangeRate?currency_pair=${defaultFiatCode}_${fiatCode}`
370+
)
371+
if (!result.ok) {
372+
throw new Error(`Exchange rate API returned status ${result.status}`)
373+
}
370374

371-
const resultJson = await result.json()
372-
const { exchangeRate } = asExchangeRateResponse(resultJson)
373-
const rate = Number(exchangeRate)
375+
const resultJson = await result.json()
376+
const { exchangeRate } = asExchangeRateResponse(resultJson)
377+
const rate = Number(exchangeRate)
374378

375-
// Validate the rate
376-
if (rate == null || isNaN(rate) || rate <= 0) {
377-
throw new Error(`Invalid exchange rate: ${exchangeRate}`)
378-
}
379+
// Validate the rate
380+
if (rate == null || isNaN(rate) || rate <= 0) {
381+
throw new Error(`Invalid exchange rate: ${exchangeRate}`)
382+
}
379383

380-
// Get USD rankings
381-
const usdJsonString = await getAsync(
382-
`${REDIS_COINRANK_KEY_PREFIX}_${defaultFiatCode}`
383-
)
384-
if (usdJsonString == null) {
385-
throw new Error(`No USD data available in Redis`)
386-
}
384+
// Get USD rankings
385+
const usdJsonString = await getAsync(
386+
`${REDIS_COINRANK_KEY_PREFIX}_${defaultFiatCode}`
387+
)
388+
if (usdJsonString == null) {
389+
throw new Error(`No USD data available in Redis`)
390+
}
387391

388-
const usdRedisResult = JSON.parse(usdJsonString)
389-
let { markets } = usdRedisResult
390-
391-
// Modify fiat-related fields with the forex rate
392-
markets = markets.map(m => ({
393-
...m,
394-
marketCap: m.marketCap * rate,
395-
price: m.price * rate,
396-
volume24h: m.volume24h * rate,
397-
high24h: m.high24h * rate,
398-
low24h: m.low24h * rate,
399-
priceChange24h: m.priceChange24h * rate,
400-
marketCapChange24h: m.marketCapChange24h * rate,
401-
circulatingSupply: m.circulatingSupply * rate,
402-
totalSupply: m.totalSupply * rate,
403-
maxSupply: m.maxSupply * rate,
404-
allTimeHigh: m.allTimeHigh * rate,
405-
allTimeLow: m.allTimeLow * rate
406-
}))
407-
408-
// Update redis cache
409-
const redisData: CoinrankRedis = {
410-
markets,
411-
lastUpdate: now.toISOString()
412-
}
413-
await setAsync(
414-
`${REDIS_COINRANK_KEY_PREFIX}_${fiatCode}`,
415-
JSON.stringify(redisData)
416-
)
392+
const usdRedisResult = JSON.parse(usdJsonString)
393+
const { markets } = usdRedisResult
394+
395+
// Modify fiat-related fields with the forex rate
396+
const convertedMarkets = markets.map(m => ({
397+
...m,
398+
marketCap: m.marketCap * rate,
399+
price: m.price * rate,
400+
volume24h: m.volume24h * rate,
401+
high24h: m.high24h * rate,
402+
low24h: m.low24h * rate,
403+
priceChange24h: m.priceChange24h * rate,
404+
marketCapChange24h: m.marketCapChange24h * rate,
405+
circulatingSupply: m.circulatingSupply * rate,
406+
totalSupply: m.totalSupply * rate,
407+
maxSupply: m.maxSupply * rate,
408+
allTimeHigh: m.allTimeHigh * rate,
409+
allTimeLow: m.allTimeLow * rate
410+
}))
411+
412+
// Update redis cache
413+
const redisData: CoinrankRedis = {
414+
markets: convertedMarkets,
415+
lastUpdate: redisResult?.lastUpdate ?? now.toISOString()
416+
}
417+
await setAsync(
418+
`${REDIS_COINRANK_KEY_PREFIX}_${fiatCode}`,
419+
JSON.stringify(redisData)
420+
)
417421

418-
return redisData
419-
} catch (e) {
420-
logger(`Error converting USD data to ${fiatCode}: ${e}`)
421-
// If conversion fails but we have cached data (even if expired), return that
422-
if (redisResult != null) {
423-
logger(`Falling back to cached data for ${fiatCode}`)
424-
return redisResult
425-
}
426-
// Only return undefined if we have no cached data at all
427-
return undefined
422+
return redisData
423+
} catch (e) {
424+
logger(`Error converting USD data to ${fiatCode}: ${e}`)
425+
// If conversion fails but we have cached data (even if expired), return that
426+
if (redisResult != null) {
427+
logger(`Falling back to cached data for ${fiatCode}`)
428+
return redisResult
428429
}
429-
} else {
430-
// For USD requests when data is missing or expired
431-
return redisResult
430+
// Only return undefined if we have no cached data at all
431+
return undefined
432432
}
433433
} catch (e) {
434434
logger(`Error in getRedisMarkets for ${fiatCode}: ${e}`)

0 commit comments

Comments
 (0)