From 462560b4e774e9a43e90e8e9688ea1c9bf83aec3 Mon Sep 17 00:00:00 2001 From: harisang Date: Fri, 4 Oct 2024 03:52:41 +0300 Subject: [PATCH 1/6] use multiple price feeds always --- src/price_providers/price_feed.py | 8 +++++--- src/transaction_processor.py | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/price_providers/price_feed.py b/src/price_providers/price_feed.py index afed5ee..4dfc103 100644 --- a/src/price_providers/price_feed.py +++ b/src/price_providers/price_feed.py @@ -1,3 +1,4 @@ +from typing import List from src.price_providers.coingecko_pricing import CoingeckoPriceProvider from src.price_providers.dune_pricing import DunePriceProvider from src.price_providers.moralis_pricing import MoralisPriceProvider @@ -18,13 +19,14 @@ def __init__(self, activate: bool): else: self.providers = [] - def get_price(self, price_params: dict) -> tuple[float, str] | None: + def get_price(self, price_params: dict) -> List[tuple[float, str]]: """Function iterates over list of price provider objects and attempts to get a price.""" + prices = [] for provider in self.providers: try: price = provider.get_price(price_params) if price is not None: - return price, provider.name + prices.append((price, provider.name)) except Exception as e: logger.error(f"Error getting price from provider {provider.name}: {e}") - return None + return prices diff --git a/src/transaction_processor.py b/src/transaction_processor.py index 6ffe120..5791343 100644 --- a/src/transaction_processor.py +++ b/src/transaction_processor.py @@ -1,5 +1,7 @@ +import time from hexbytes import HexBytes from web3 import Web3 +from typing import List from src.helpers.blockchain_data import BlockchainData from src.helpers.database import Database from src.imbalances_script import RawTokenImbalances @@ -7,7 +9,6 @@ from src.helpers.helper_functions import read_sql_file, set_params from src.helpers.config import CHAIN_SLEEP_TIME, logger from src.fees.compute_fees import compute_all_fees_of_batch -import time class TransactionProcessor: @@ -191,7 +192,7 @@ def process_prices_for_tokens( token_imbalances: dict[str, int], block_number: int, tx_hash: str, - ) -> dict[str, tuple[float, str]]: + ) -> dict[str, List[tuple[float, str]]]: """Compute prices for tokens with non-null imbalances.""" prices = {} try: @@ -200,8 +201,10 @@ def process_prices_for_tokens( set_params(token_address, block_number, tx_hash) ) if price_data: - price, source = price_data - prices[token_address] = (price, source) + res = [] + for data_point in price_data: + res.append(data_point) + prices[token_address] = res except Exception as e: logger.error(f"Failed to process prices for transaction {tx_hash}: {e}") @@ -294,11 +297,14 @@ def handle_prices( ) -> None: """Function writes prices to table per token.""" try: - for token_address, (price, source) in prices.items(): - self.db.write_prices( - source, block_number, tx_hash, token_address, price - ) - self.log_message.append(f"Token: {token_address}, Price: {price} ETH") + for token_address, list_of_prices in prices.items(): + for price, source in list_of_prices: + self.db.write_prices( + source, block_number, tx_hash, token_address, price + ) + self.log_message.append( + f"Token: {token_address}, Price: {price} ETH, Source: {source}" + ) except Exception as err: logger.error(f"Error: {err}") From 0f05b8014d9def2b45c6e44272827a10bea85e44 Mon Sep 17 00:00:00 2001 From: harisang Date: Fri, 4 Oct 2024 03:57:33 +0300 Subject: [PATCH 2/6] simplify code --- src/transaction_processor.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/transaction_processor.py b/src/transaction_processor.py index 5791343..89d74f6 100644 --- a/src/transaction_processor.py +++ b/src/transaction_processor.py @@ -201,10 +201,7 @@ def process_prices_for_tokens( set_params(token_address, block_number, tx_hash) ) if price_data: - res = [] - for data_point in price_data: - res.append(data_point) - prices[token_address] = res + prices[token_address] = price_data except Exception as e: logger.error(f"Failed to process prices for transaction {tx_hash}: {e}") From 08755a5b40fb65cfd269a481931f6dfcaf5c0f9b Mon Sep 17 00:00:00 2001 From: harisang Date: Fri, 4 Oct 2024 04:01:15 +0300 Subject: [PATCH 3/6] fix type annotation in function --- src/transaction_processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction_processor.py b/src/transaction_processor.py index 89d74f6..456a454 100644 --- a/src/transaction_processor.py +++ b/src/transaction_processor.py @@ -290,7 +290,7 @@ def handle_fees( ) def handle_prices( - self, prices: dict[str, tuple[float, str]], tx_hash: str, block_number: int + self, prices: dict[str, List[tuple[float, str]]], tx_hash: str, block_number: int ) -> None: """Function writes prices to table per token.""" try: From 0cbdc3aabba83918af4ef539eb97c2d2c0434a5b Mon Sep 17 00:00:00 2001 From: harisang Date: Fri, 4 Oct 2024 04:01:39 +0300 Subject: [PATCH 4/6] black fix --- src/transaction_processor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/transaction_processor.py b/src/transaction_processor.py index 456a454..7e8ac74 100644 --- a/src/transaction_processor.py +++ b/src/transaction_processor.py @@ -290,7 +290,10 @@ def handle_fees( ) def handle_prices( - self, prices: dict[str, List[tuple[float, str]]], tx_hash: str, block_number: int + self, + prices: dict[str, List[tuple[float, str]]], + tx_hash: str, + block_number: int, ) -> None: """Function writes prices to table per token.""" try: From b92c7b02d6f1cd289262d378246b03cf912737ab Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Fri, 4 Oct 2024 11:29:20 +0200 Subject: [PATCH 5/6] use standard list type --- src/price_providers/price_feed.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/price_providers/price_feed.py b/src/price_providers/price_feed.py index 4dfc103..fd86b96 100644 --- a/src/price_providers/price_feed.py +++ b/src/price_providers/price_feed.py @@ -1,4 +1,3 @@ -from typing import List from src.price_providers.coingecko_pricing import CoingeckoPriceProvider from src.price_providers.dune_pricing import DunePriceProvider from src.price_providers.moralis_pricing import MoralisPriceProvider @@ -19,7 +18,7 @@ def __init__(self, activate: bool): else: self.providers = [] - def get_price(self, price_params: dict) -> List[tuple[float, str]]: + def get_price(self, price_params: dict) -> list[tuple[float, str]]: """Function iterates over list of price provider objects and attempts to get a price.""" prices = [] for provider in self.providers: From 873af0687ddbce93236b4540947db4e62e1a2e38 Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Fri, 4 Oct 2024 11:37:53 +0200 Subject: [PATCH 6/6] one more list typing fix --- src/transaction_processor.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/transaction_processor.py b/src/transaction_processor.py index 7e8ac74..e427503 100644 --- a/src/transaction_processor.py +++ b/src/transaction_processor.py @@ -1,7 +1,6 @@ import time from hexbytes import HexBytes from web3 import Web3 -from typing import List from src.helpers.blockchain_data import BlockchainData from src.helpers.database import Database from src.imbalances_script import RawTokenImbalances @@ -192,7 +191,7 @@ def process_prices_for_tokens( token_imbalances: dict[str, int], block_number: int, tx_hash: str, - ) -> dict[str, List[tuple[float, str]]]: + ) -> dict[str, list[tuple[float, str]]]: """Compute prices for tokens with non-null imbalances.""" prices = {} try: @@ -291,7 +290,7 @@ def handle_fees( def handle_prices( self, - prices: dict[str, List[tuple[float, str]]], + prices: dict[str, list[tuple[float, str]]], tx_hash: str, block_number: int, ) -> None: