Skip to content

Commit

Permalink
database: add retries to coingecko
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Jul 4, 2024
1 parent 4943c13 commit 7bd4f94
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions database/src/coingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const createConfig = (apiKey: string) => ({
});

export const fetchPrices = async (coinIds: string[], apiKey?: string): Promise<CoinGeckoPrices> => {
const FIVE_MINUTES_IN_MS = 5 * 60 * 1000;
const [baseUrl, config] = apiKey
? [COIN_GECKO_PRO_API_BASE_URL, createConfig(apiKey)]
: [COIN_GECKO_API_BASE_URL, undefined];
Expand All @@ -70,13 +71,34 @@ export const fetchPrices = async (coinIds: string[], apiKey?: string): Promise<C
console.log(`fetching ${chunks.length} chunks of prices`);
for (const chunk of chunks) {
console.log(`fetching chunk of ${chunk.length} prices`);
let currentBackoff = COIN_GECKO_API_SLEEP_MS;
const url = `${baseUrl}/simple/price?ids=${chunk.join(',')}&vs_currencies=usd`;
const response = await axios.get<CoinGeckoPrices>(url, config);
console.log(`fetched ${Object.keys(response.data).length} prices`);
prices = {
...prices,
...response.data,
};
let done: boolean = false;
while (!done) {
try {
const response = await axios.get<CoinGeckoPrices>(url, config);
console.log(`fetched ${Object.keys(response.data).length} prices`);
prices = {
...prices,
...response.data,
};
done = true;
} catch (e) {
console.error(`failed to fetch prices: ${e}`);
if (isAxiosError(e) && e.response?.status === 429) {
if (currentBackoff > FIVE_MINUTES_IN_MS) {
console.error('Exceeded max backoff time querying CoinGecko API, giving up.');
throw e;
}
console.log(`backing off for ${currentBackoff}ms`);
await sleep(currentBackoff);
currentBackoff *= 2;
} else {
// Only want to retry on 429s
throw e;
}
}
}
await sleep(COIN_GECKO_API_SLEEP_MS);
}
return prices;
Expand Down

0 comments on commit 7bd4f94

Please sign in to comment.