Skip to content

Commit

Permalink
make coingecko and moralis api keys optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fhenneke committed Oct 11, 2024
1 parent 83d00b9 commit 3f493a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/price_providers/coingecko_pricing.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions src/price_providers/moralis_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3f493a2

Please sign in to comment.