MCPcopy Index your code
hub / github.com/massCodeIO/massCode / fetchCryptoRates

Function fetchCryptoRates

src/main/currencyRates.ts:79–105  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

77}
78
79async 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
107export async function getCurrencyRates(): Promise<CurrencyRatesPayload> {
108 const cached = store.currencyRates.get('cache')

Callers 2

getCurrencyRatesFunction · 0.85
refreshCryptoRatesForcedFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected