From f8463108c0aaa70229e76fb131b9b4798c54add9 Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Mon, 19 Aug 2024 19:27:31 -0400 Subject: [PATCH 1/2] dune client improved setup --- src/price_providers/dune_pricing.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/price_providers/dune_pricing.py b/src/price_providers/dune_pricing.py index 436bc4c..9d23eb7 100644 --- a/src/price_providers/dune_pricing.py +++ b/src/price_providers/dune_pricing.py @@ -8,8 +8,6 @@ from src.constants import DUNE_PRICE_QUERY_ID, DUNE_QUERY_BUFFER_TIME dotenv.load_dotenv() -dune_api_key = os.getenv("DUNE_API_KEY") -dune = DuneClient.from_env() class DunePriceProvider(AbstractPriceProvider): @@ -20,17 +18,30 @@ class DunePriceProvider(AbstractPriceProvider): def __init__(self) -> None: self.web3 = get_web3_instance() self.logger = get_logger() + self.dune = self.initialize_dune_client() @property def name(self) -> str: return "Dune" + def initialize_dune_client(self) -> DuneClient | None: + """ + Initializes the Dune client and returns None if API key is not set. + """ + dune_api_key = os.getenv("DUNE_API_KEY") + if not dune_api_key: + self.logger.warning("DUNE_API_KEY is not set.") + return None + return DuneClient.from_env() + def get_price(self, price_params: dict) -> float | None: """ Function returns Dune price for a token address, closest to and at least as large as the block timestamp for a given tx hash. """ try: + if not self.dune: + return None token_address, block_number = extract_params(price_params, is_block=True) start_timestamp = getattr( self.web3.eth.get_block(block_number), "timestamp", None @@ -51,7 +62,7 @@ def get_price(self, price_params: dict) -> float | None: ), ], ) - result = dune.run_query(query=query) # type: ignore[attr-defined] + result = self.dune.run_query(query=query) # type: ignore[attr-defined] if result.result.rows: row = result.result.rows[0] price = row.get("price") From 309283b53f17c5068375c247664d4e49c63887da Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Mon, 19 Aug 2024 19:47:53 -0400 Subject: [PATCH 2/2] mypy --- src/price_providers/dune_pricing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/price_providers/dune_pricing.py b/src/price_providers/dune_pricing.py index 9d23eb7..3e27b3a 100644 --- a/src/price_providers/dune_pricing.py +++ b/src/price_providers/dune_pricing.py @@ -1,3 +1,4 @@ +from typing import cast import dotenv, os from src.price_providers.pricing_model import AbstractPriceProvider from dune_client.types import QueryParameter @@ -32,7 +33,7 @@ def initialize_dune_client(self) -> DuneClient | None: if not dune_api_key: self.logger.warning("DUNE_API_KEY is not set.") return None - return DuneClient.from_env() + return cast(DuneClient, DuneClient.from_env()) def get_price(self, price_params: dict) -> float | None: """ @@ -63,7 +64,7 @@ def get_price(self, price_params: dict) -> float | None: ], ) result = self.dune.run_query(query=query) # type: ignore[attr-defined] - if result.result.rows: + if result and result.result and result.result.rows: row = result.result.rows[0] price = row.get("price") if price is not None: