From 712e764147f3535bc711bcd32583959a734e0a39 Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Fri, 2 Aug 2024 22:32:54 -0400 Subject: [PATCH 1/4] coingecko changes --- src/coingecko_pricing.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/coingecko_pricing.py b/src/coingecko_pricing.py index f3cf5fd..b4c8905 100644 --- a/src/coingecko_pricing.py +++ b/src/coingecko_pricing.py @@ -4,7 +4,8 @@ import json from web3 import Web3 from src.config import logger -from src.constants import NATIVE_ETH_TOKEN_ADDRESS +from src.helper_functions import get_finalized_block_number +from src.constants import NATIVE_ETH_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS coingecko_api_key = os.getenv("COINGECKO_API_KEY") @@ -21,7 +22,7 @@ def get_token_id_by_address(cleaned_token_list, token_address): return None -def get_api_price( +def fetch_api_price( token_id: str, start_timestamp: int, end_timestamp: int ) -> Optional[float]: """ @@ -53,30 +54,53 @@ def get_api_price( return None +def price_retrievable(web3: Web3, block_start_timestamp: int) -> bool: + """ + This function checks if the time elapsed between the latest block and block being processed + is less than 2 days, which is coingecko's time frame for fetching 5-minutely data. + """ + # Time in seconds of 45 hours. + COINGECKO_TIME_LIMIT = 162000 + newest_block_timestamp = web3.eth.get_block(get_finalized_block_number(web3))[ + "timestamp" + ] + if (newest_block_timestamp - block_start_timestamp) > COINGECKO_TIME_LIMIT: + return True + + def get_price(web3: Web3, block_number: int, token_address: str, tx_hash: str): """ Function returns coingecko price for a token address, closest to and at least as large as the block timestamp for a given tx hash. """ + + block_start_timestamp = web3.eth.get_block(block_number)["timestamp"] + if price_retrievable(web3, block_start_timestamp): + return None + # Coingecko doesn't store ETH address, which occurs commonly in imbalances. - if Web3.to_checksum_address(token_address) == NATIVE_ETH_TOKEN_ADDRESS: + # Approximate WETH price as equal to ETH. + if Web3.to_checksum_address(token_address) in ( + NATIVE_ETH_TOKEN_ADDRESS, + WETH_TOKEN_ADDRESS, + ): return 1.0 - token_address = token_address.lower() - data = web3.eth.get_block(block_number) - start_timestamp = data["timestamp"] # We need to provide a sufficient buffer time for fetching 5-minutely prices from coingecko. # If too short, it's possible that no price may be returned. We use the first value returned, # which would be closest to block timestamp BUFFER_TIME = 600 - end_timestamp = start_timestamp + BUFFER_TIME + block_end_timestamp = block_start_timestamp + BUFFER_TIME + token_address = token_address.lower() token_id = get_token_id_by_address(cleaned_token_list, token_address) if not token_id: logger.warning(f"Token ID not found for the given address: {token_address}") return None try: - api_price = get_api_price(token_id, start_timestamp, end_timestamp) + api_price = fetch_api_price( + token_id, block_start_timestamp, block_end_timestamp + ) if api_price is None: logger.warning(f"API returned None for token ID: {token_id}") return None From 656e8f42d28be1528942d9f6396e5ab30fef4881 Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Fri, 2 Aug 2024 22:52:31 -0400 Subject: [PATCH 2/4] type fix --- src/coingecko_pricing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coingecko_pricing.py b/src/coingecko_pricing.py index b4c8905..13cae80 100644 --- a/src/coingecko_pricing.py +++ b/src/coingecko_pricing.py @@ -64,8 +64,8 @@ def price_retrievable(web3: Web3, block_start_timestamp: int) -> bool: newest_block_timestamp = web3.eth.get_block(get_finalized_block_number(web3))[ "timestamp" ] - if (newest_block_timestamp - block_start_timestamp) > COINGECKO_TIME_LIMIT: - return True + return (newest_block_timestamp - block_start_timestamp) > COINGECKO_TIME_LIMIT + def get_price(web3: Web3, block_number: int, token_address: str, tx_hash: str): From 0346cfbe2eb2bb9b9047eb1826f82774ac078716 Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Fri, 2 Aug 2024 22:53:05 -0400 Subject: [PATCH 3/4] black --- src/coingecko_pricing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/coingecko_pricing.py b/src/coingecko_pricing.py index 13cae80..c20c7bf 100644 --- a/src/coingecko_pricing.py +++ b/src/coingecko_pricing.py @@ -67,7 +67,6 @@ def price_retrievable(web3: Web3, block_start_timestamp: int) -> bool: return (newest_block_timestamp - block_start_timestamp) > COINGECKO_TIME_LIMIT - def get_price(web3: Web3, block_number: int, token_address: str, tx_hash: str): """ Function returns coingecko price for a token address, From e7e94e1a2c526fe768635b3e674e8e466ae953db Mon Sep 17 00:00:00 2001 From: Shubh Agarwal Date: Mon, 5 Aug 2024 19:46:39 -0400 Subject: [PATCH 4/4] addessed comments, and issue --- src/coingecko_pricing.py | 4 ++-- src/daemon.py | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/coingecko_pricing.py b/src/coingecko_pricing.py index c20c7bf..dca9ca8 100644 --- a/src/coingecko_pricing.py +++ b/src/coingecko_pricing.py @@ -54,7 +54,7 @@ def fetch_api_price( return None -def price_retrievable(web3: Web3, block_start_timestamp: int) -> bool: +def price_not_retrievable(web3: Web3, block_start_timestamp: int) -> bool: """ This function checks if the time elapsed between the latest block and block being processed is less than 2 days, which is coingecko's time frame for fetching 5-minutely data. @@ -74,7 +74,7 @@ def get_price(web3: Web3, block_number: int, token_address: str, tx_hash: str): """ block_start_timestamp = web3.eth.get_block(block_number)["timestamp"] - if price_retrievable(web3, block_start_timestamp): + if price_not_retrievable(web3, block_start_timestamp): return None # Coingecko doesn't store ETH address, which occurs commonly in imbalances. diff --git a/src/daemon.py b/src/daemon.py index 44017af..104ba62 100644 --- a/src/daemon.py +++ b/src/daemon.py @@ -209,9 +209,6 @@ def process_transactions(chain_name: str) -> None: ) = initialize_connections() rt = RawTokenImbalances(web3, chain_name) start_block = get_start_block(web3, chain_name, solver_slippage_db_connection) - ## this is just saying that the start block should be hardcoded in case the script restarts a few hours after that specific block - if start_block - 12000 < 20420000: - start_block = 20420000 previous_block = start_block unprocessed_txs: List[Tuple[str, int, int]] = []