From 3f493a23fabe9a37e8fdcd0fa6d24256077e65ad Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Fri, 11 Oct 2024 11:39:59 +0200 Subject: [PATCH] make coingecko and moralis api keys optional --- src/price_providers/coingecko_pricing.py | 16 ++++++++++------ src/price_providers/moralis_pricing.py | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/price_providers/coingecko_pricing.py b/src/price_providers/coingecko_pricing.py index 9933e69..069e209 100644 --- a/src/price_providers/coingecko_pricing.py +++ b/src/price_providers/coingecko_pricing.py @@ -1,8 +1,9 @@ import os import time + import requests -import json from web3 import Web3 + from src.price_providers.pricing_model import AbstractPriceProvider from src.helpers.config import logger, get_web3_instance from src.helpers.helper_functions import get_finalized_block_number, extract_params @@ -31,11 +32,14 @@ def __init__(self) -> None: def name(self) -> str: return "coingecko" - def fetch_coingecko_list(self) -> list[dict]: + def fetch_coingecko_list(self) -> list[dict] | None: """ Fetch and filter the list of tokens (currently filters only Ethereum) from the Coingecko API. """ + if not coingecko_api_key: + logger.warning("Coingecko API key is not set.") + return None url = ( f"https://pro-api.coingecko.com/api/v3/coins/" f"list?include_platform=true&status=active" @@ -47,7 +51,7 @@ def fetch_coingecko_list(self) -> list[dict]: headers["x-cg-pro-api-key"] = coingecko_api_key response = requests.get(url, headers=headers) - tokens_list = json.loads(response.text) + tokens_list = response.json() return [ {"id": item["id"], "platforms": {"ethereum": item["platforms"]["ethereum"]}} for item in tokens_list @@ -82,9 +86,6 @@ def fetch_api_price( """ Makes call to Coingecko API to fetch price, between a start and end timestamp. """ - if not coingecko_api_key: - logger.warning("Coingecko API key is not set.") - return None # price of token is returned in ETH url = ( f"https://pro-api.coingecko.com/api/v3/coins/{token_id}/market_chart/range" @@ -122,6 +123,9 @@ def get_price(self, price_params: dict) -> float | None: Function returns coingecko price for a token address, closest to and at least as large as the block timestamp for a given tx hash. """ + if not coingecko_api_key: + logger.warning("Coingecko API key is not set.") + return None token_address, block_number = extract_params(price_params, is_block=True) block_start_timestamp = self.web3.eth.get_block(block_number)["timestamp"] if self.price_not_retrievable(block_start_timestamp): diff --git a/src/price_providers/moralis_pricing.py b/src/price_providers/moralis_pricing.py index bc4bb57..affc793 100644 --- a/src/price_providers/moralis_pricing.py +++ b/src/price_providers/moralis_pricing.py @@ -37,6 +37,9 @@ def get_price(self, price_params: dict) -> float | None: Price returned is closest to and at least as large as block timestamp. """ try: + if os.getenv("MORALIS_API_KEY") is None: + self.logger.warning("Moralis API key is not set.") + return None token_address, block_number = extract_params(price_params, is_block=True) params = { "chain": "eth",