()
| 77 | } |
| 78 | |
| 79 | async function fetchCryptoRates(): Promise<Record<string, number>> { |
| 80 | const ids = Object.values(CRYPTO_IDS).join(',') |
| 81 | const response = await fetch( |
| 82 | `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`, |
| 83 | ) |
| 84 | |
| 85 | if (!response.ok) { |
| 86 | if (response.status === 429) { |
| 87 | throw new Error('rate_limit') |
| 88 | } |
| 89 | throw new Error( |
| 90 | `Crypto rates request failed with status ${response.status}`, |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | const data = (await response.json()) as Record<string, { usd?: number }> |
| 95 | const rates: Record<string, number> = {} |
| 96 | |
| 97 | for (const [code, coinId] of Object.entries(CRYPTO_IDS)) { |
| 98 | const usdPrice = data[coinId]?.usd |
| 99 | if (usdPrice && usdPrice > 0) { |
| 100 | rates[code] = 1 / usdPrice |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return rates |
| 105 | } |
| 106 | |
| 107 | export async function getCurrencyRates(): Promise<CurrencyRatesPayload> { |
| 108 | const cached = store.currencyRates.get('cache') |
no outgoing calls
no test coverage detected