|
| 1 | +import { CoinGeckoCoinData } from '@model' |
1 | 2 | import { Context } from '@originprotocol/squid-utils' |
2 | 3 |
|
3 | | -import { processCoingeckoData } from './coingecko' |
| 4 | +import { CoingeckoDataOutput, processCoingeckoData } from './coingecko' |
4 | 5 | import { queryClient } from './queryClient' |
5 | 6 |
|
| 7 | +type CoinId = 'origin-dollar' | 'origin-ether' | 'super-oeth' | 'origin-dollar-governance' | 'origin-protocol' |
| 8 | + |
| 9 | +// Maps the CoinGecko coinId to the `product` value coingeckoProcessor writes |
| 10 | +// into CoinGeckoCoinData. Used for the DB fallback below. |
| 11 | +const COIN_TO_PRODUCT: Record<CoinId, string> = { |
| 12 | + 'origin-protocol': 'OGN', |
| 13 | + 'origin-dollar': 'OUSD', |
| 14 | + 'origin-ether': 'OETH', |
| 15 | + 'super-oeth': 'superOETHb', |
| 16 | + 'origin-dollar-governance': 'OGV', |
| 17 | +} |
| 18 | + |
| 19 | +async function readCachedData( |
| 20 | + ctx: Context, |
| 21 | + coinId: CoinId, |
| 22 | + vsCurrency: 'usd' | 'eth', |
| 23 | +): Promise<CoingeckoDataOutput | null> { |
| 24 | + const product = COIN_TO_PRODUCT[coinId] |
| 25 | + const rows = await ctx.store.find(CoinGeckoCoinData, { |
| 26 | + where: { product, vsCurrency: vsCurrency.toUpperCase() as 'USD' | 'ETH' }, |
| 27 | + }) |
| 28 | + if (rows.length === 0) return null |
| 29 | + const result: CoingeckoDataOutput = {} |
| 30 | + for (const row of rows) { |
| 31 | + result[row.date] = { |
| 32 | + prices: row.price, |
| 33 | + market_caps: row.marketCap, |
| 34 | + total_volumes: row.tradingVolume, |
| 35 | + } |
| 36 | + } |
| 37 | + return result |
| 38 | +} |
| 39 | + |
| 40 | +// Persistent throttle: if today's row exists in the DB we already have |
| 41 | +// today's data, so skip the API entirely. Survives process restarts (unlike |
| 42 | +// the in-memory throttle in coingeckoProcessor) and amortizes nicely across |
| 43 | +// the 4-coin burst that runs every batch during catch-up. |
| 44 | +async function todayCached(ctx: Context, coinId: CoinId, vsCurrency: 'usd' | 'eth'): Promise<boolean> { |
| 45 | + const product = COIN_TO_PRODUCT[coinId] |
| 46 | + const today = new Date().toISOString().slice(0, 10) |
| 47 | + const row = await ctx.store.get(CoinGeckoCoinData, `${today}-${product}`) |
| 48 | + return row != null |
| 49 | +} |
| 50 | + |
6 | 51 | export async function getCoingeckoData( |
7 | 52 | ctx: Context, |
8 | 53 | props: { |
9 | | - coinId: 'origin-dollar' | 'origin-ether' | 'super-oeth' | 'origin-dollar-governance' | 'origin-protocol' |
| 54 | + coinId: CoinId |
10 | 55 | vsCurrency?: 'eth' | 'usd' |
11 | 56 | startTimestamp?: number |
12 | 57 | }, |
13 | 58 | ) { |
14 | 59 | const vsCurrency = props.vsCurrency || 'usd' |
| 60 | + |
| 61 | + // If today's data is already cached, return the DB rows without touching |
| 62 | + // the API. Saves one fresh API call per coin per restart during catch-up. |
| 63 | + if (await todayCached(ctx, props.coinId, vsCurrency)) { |
| 64 | + const cached = await readCachedData(ctx, props.coinId, vsCurrency) |
| 65 | + if (cached) return cached |
| 66 | + } |
| 67 | + |
15 | 68 | const coingeckoURL = `https://api.coingecko.com/api/v3/coins/${props.coinId}/market_chart?vs_currency=${vsCurrency}&days=365&interval=daily&precision=18` |
16 | | - const coingeckoJson = await queryClient.fetchQuery({ |
17 | | - queryKey: [coingeckoURL, new Date().toISOString().slice(0, 10)], |
18 | | - queryFn: async () => { |
19 | | - console.log('Fetching Coingecko market data') |
20 | | - const response = await fetch(coingeckoURL) |
21 | | - if (response.status === 429) { |
22 | | - throw new Error('Coingecko rate limited') |
23 | | - } |
24 | | - const result = await response.json() |
25 | | - console.log(`Found ${result.prices.length} prices`) |
26 | | - return result |
27 | | - }, |
28 | | - |
29 | | - staleTime: 600_000, // 10 minutes |
30 | | - }) |
| 69 | + try { |
| 70 | + const coingeckoJson = await queryClient.fetchQuery({ |
| 71 | + queryKey: [coingeckoURL, new Date().toISOString().slice(0, 10)], |
| 72 | + queryFn: async () => { |
| 73 | + console.log('Fetching Coingecko market data') |
| 74 | + const response = await fetch(coingeckoURL) |
| 75 | + if (response.status === 429) { |
| 76 | + throw new Error('Coingecko rate limited') |
| 77 | + } |
| 78 | + const result = await response.json() |
| 79 | + console.log(`Found ${result.prices.length} prices`) |
| 80 | + return result |
| 81 | + }, |
| 82 | + staleTime: 600_000, // 10 minutes |
| 83 | + // Don't burn rate-limit budget on retries — if we're 429'd, retries 429 too. |
| 84 | + retry: 0, |
| 85 | + }) |
31 | 86 |
|
32 | | - const result = processCoingeckoData(coingeckoJson) |
33 | | - return result |
| 87 | + return processCoingeckoData(coingeckoJson) |
| 88 | + } catch (err) { |
| 89 | + // API failed (rate limit, network, etc.). Fall back to whatever the |
| 90 | + // coingeckoProcessor last persisted — better than crashing the processor. |
| 91 | + const cached = await readCachedData(ctx, props.coinId, vsCurrency) |
| 92 | + if (cached) { |
| 93 | + console.log( |
| 94 | + `Coingecko fetch failed for ${props.coinId} (${(err as Error).message}); ` + |
| 95 | + `using ${Object.keys(cached).length} cached rows from DB`, |
| 96 | + ) |
| 97 | + return cached |
| 98 | + } |
| 99 | + throw err |
| 100 | + } |
34 | 101 | } |
0 commit comments