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.
This commit is contained in:
parent
51c7345d26
commit
5cdbf5b2d7
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user