From 5cdbf5b2d7bb560b1a370f225db1dd5a614130c9 Mon Sep 17 00:00:00 2001 From: romandvo <50524575+romandvo@users.noreply.github.com> Date: Thu, 16 Mar 2023 14:11:25 +0200 Subject: [PATCH] Avoid CoinGecko's API rate limit with time delays Updating token prices with ./mev prices fetch-all can fail due to CoinGecko's API rate limits https://www.coingecko.com/en/api/pricing_2 (10-30 requests a minute) Error message: ValueError: {'status': {'error_code': 429, 'error_message': "You've exceeded the Rate Limit. Please visit https://www.coingecko.com/en/api/pricing to subscribe to our API plans for higher rate limits."}} Currently, TOKEN_ADDRESSES length is 14 which can (and does) fail the function, depending on the dynamic limit By introducing time delays between API queries we can avoid hitting the limit and failing the price update. --- mev_inspect/prices.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mev_inspect/prices.py b/mev_inspect/prices.py index 62b3508..3f90325 100644 --- a/mev_inspect/prices.py +++ b/mev_inspect/prices.py @@ -1,16 +1,20 @@ from datetime import datetime from typing import List +import time from pycoingecko import CoinGeckoAPI from mev_inspect.schemas.prices import COINGECKO_ID_BY_ADDRESS, TOKEN_ADDRESSES, Price +SLEEP_TIME = 10 def fetch_prices() -> List[Price]: coingecko_api = CoinGeckoAPI() prices = [] for token_address in TOKEN_ADDRESSES: + # Avoid Coingecko's API rate limits + time.sleep(SLEEP_TIME) coingecko_price_data = coingecko_api.get_coin_market_chart_by_id( id=COINGECKO_ID_BY_ADDRESS[token_address], vs_currency="usd",