From bffb591447cbc2ec1ec494d5042963b90c379ad5 Mon Sep 17 00:00:00 2001 From: Preschian Febryantara Date: Fri, 1 Dec 2023 17:53:46 +0700 Subject: [PATCH 1/2] chore: cache daily --- price/src/utils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/price/src/utils.ts b/price/src/utils.ts index aa0102a..9624bec 100644 --- a/price/src/utils.ts +++ b/price/src/utils.ts @@ -3,9 +3,8 @@ export function cacheKey() { const year = date.getUTCFullYear(); const month = date.getUTCMonth() + 1; const day = date.getUTCDate(); - const hours = date.getUTCHours(); - return `${year}-${month}-${day}-${hours}`; + return `${year}-${month}-${day}`; } // return same format as coingecko https://api.coingecko.com/api/v3/simple/price?ids=kusama&vs_currencies=usd From 5831bc07027a81cfc2df782dcb22ad0eccbe824d Mon Sep 17 00:00:00 2001 From: Preschian Febryantara Date: Sat, 2 Dec 2023 07:47:18 +0700 Subject: [PATCH 2/2] * fix(index.ts): add missing variable declaration for latestPrice * fix(index.ts): add support for caching latest price for each chain * fix(utils.ts): add error handling for sub.id and kraken API calls --- price/src/index.ts | 9 ++++++++- price/src/utils.ts | 39 ++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/price/src/index.ts b/price/src/index.ts index f7b07d2..0acdcb1 100644 --- a/price/src/index.ts +++ b/price/src/index.ts @@ -19,6 +19,7 @@ app.on(['GET', 'OPTIONS'], '/price/:chain', async (c) => { const key = `${cacheKey()}-${token}`; const value = await c.env.TOKENPRICE.get(key); + const latestPrice = await c.env.TOKENPRICE.get(chain); console.log(key, value); @@ -28,11 +29,17 @@ app.on(['GET', 'OPTIONS'], '/price/:chain', async (c) => { console.log(chain, usd); c.executionCtx.waitUntil(c.env.TOKENPRICE.put(key, usd)); + c.executionCtx.waitUntil(c.env.TOKENPRICE.put(chain, usd)); return c.json(formatPrice(chain, usd)); } catch (error) { console.log(error); - return c.json({ error }, 500); + + if (latestPrice) { + return c.json(formatPrice(chain, latestPrice)); + } + + return c.json({ error: (error as Error).message }, 500); } } diff --git a/price/src/utils.ts b/price/src/utils.ts index 9624bec..977a362 100644 --- a/price/src/utils.ts +++ b/price/src/utils.ts @@ -3,8 +3,9 @@ export function cacheKey() { const year = date.getUTCFullYear(); const month = date.getUTCMonth() + 1; const day = date.getUTCDate(); + const hours = date.getUTCHours(); - return `${year}-${month}-${day}`; + return `${year}-${month}-${day}-${hours}`; } // return same format as coingecko https://api.coingecko.com/api/v3/simple/price?ids=kusama&vs_currencies=usd @@ -29,14 +30,18 @@ export async function getPrice( // fetch sub.id API const subid = await fetch('https://sub.id/api/v1/prices'); if (subid.status === 200) { - const data = await subid.json(); - const findToken = (data as { id: string; current_price: number }[]).find( - (p) => p.id === chain, - ); - const price = findToken?.current_price; - - if (price) { - return price.toString(); + try { + const data = (await subid.json()) as { + prices: { id: string; current_price: number }[]; + }; + const findToken = data.prices.find((p) => p.id === chain); + const price = findToken?.current_price; + + if (price) { + return price.toString(); + } + } catch (error) { + console.log('skip sub.id', error); } } @@ -46,13 +51,17 @@ export async function getPrice( `https://api.kraken.com/0/public/Ticker?pair=${pair}`, ); if (kraken.status === 200) { - const data = (await kraken.json()) as { - result: { [key: string]: { a: [string] } }; - }; - const price = data.result[pair].a[0]; + try { + const data = (await kraken.json()) as { + result: { [key: string]: { a: [string] } }; + }; + const price = data.result[pair].a[0]; - if (price) { - return price; + if (price) { + return price; + } + } catch (error) { + console.log('skip kraken', error); } }