diff --git a/fastlane_bot/events/event_gatherer.py b/fastlane_bot/events/event_gatherer.py index fa6b9bb4b..93156cab8 100644 --- a/fastlane_bot/events/event_gatherer.py +++ b/fastlane_bot/events/event_gatherer.py @@ -27,7 +27,6 @@ def __init__( config: Config, w3: AsyncWeb3, exchanges: Dict[str, Exchange], - event_contracts: Dict[str, Contract], ): """ Initializes the EventManager. Args: @@ -37,13 +36,11 @@ def __init__( self._config = config self._w3 = w3 self._subscriptions = [] - unique_topics = set() - for exchange_name, exchange in exchanges.items(): - subscriptions = exchange.get_subscriptions(event_contracts[exchange_name]) + for exchange in exchanges.values(): + subscriptions = exchange.get_subscriptions(w3) for sub in subscriptions: - if sub.topic not in unique_topics: - unique_topics.add(sub.topic) + if sub.topic not in [s.topic for s in self._subscriptions]: self._subscriptions.append(sub) def get_all_events(self, from_block: int, to_block: int): diff --git a/fastlane_bot/events/exchanges/balancer.py b/fastlane_bot/events/exchanges/balancer.py index 384ee5fda..efba312e1 100644 --- a/fastlane_bot/events/exchanges/balancer.py +++ b/fastlane_bot/events/exchanges/balancer.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple, Any +from typing import List, Type, Tuple, Any, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract from fastlane_bot.data.abi import BALANCER_VAULT_ABI, BALANCER_POOL_ABI_V1 @@ -48,7 +49,7 @@ def get_pool_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.AuthorizerChanged] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: return [] async def get_fee(self, pool_id: str, contract: Contract) -> Tuple[str, float]: diff --git a/fastlane_bot/events/exchanges/bancor_pol.py b/fastlane_bot/events/exchanges/bancor_pol.py index 25ba9be5f..2c63dda6d 100644 --- a/fastlane_bot/events/exchanges/bancor_pol.py +++ b/fastlane_bot/events/exchanges/bancor_pol.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple, Any, Dict, Callable +from typing import List, Type, Tuple, Any, Dict, Callable, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract from fastlane_bot.data.abi import BANCOR_POL_ABI @@ -48,7 +49,8 @@ def factory_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.TokenTraded, contract.events.TradingEnabled] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [ Subscription(contract.events.TokenTraded, collect_all=True), Subscription(contract.events.TradingEnabled, "0xae3f48c001771f8e9868e24d47b9e4295b06b1d78072acf96f167074aa3fab64", collect_all=True), diff --git a/fastlane_bot/events/exchanges/bancor_v2.py b/fastlane_bot/events/exchanges/bancor_v2.py index a4ab5db4a..efa54f433 100644 --- a/fastlane_bot/events/exchanges/bancor_v2.py +++ b/fastlane_bot/events/exchanges/bancor_v2.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple +from typing import List, Type, Tuple, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract, AsyncContract from fastlane_bot.data.abi import BANCOR_V2_CONVERTER_ABI @@ -46,7 +47,8 @@ def factory_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.TokenRateUpdate] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [Subscription(contract.events.TokenRateUpdate)] # def async convert_address(self, address: str, contract: Contract) -> str: diff --git a/fastlane_bot/events/exchanges/bancor_v3.py b/fastlane_bot/events/exchanges/bancor_v3.py index b9ea312b7..ed5ba0720 100644 --- a/fastlane_bot/events/exchanges/bancor_v3.py +++ b/fastlane_bot/events/exchanges/bancor_v3.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple +from typing import List, Type, Tuple, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract from fastlane_bot.data.abi import BANCOR_V3_POOL_COLLECTION_ABI @@ -50,7 +51,8 @@ def factory_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.TradingLiquidityUpdated] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [ Subscription(contract.events.TradingLiquidityUpdated, TRADING_LIQUIDITY_UPDATED_TOPIC), # Subscription(contract.events.TotalLiquidityUpdated, TOTAL_LIQUIDITY_UPDATED_TOPIC), # Unused diff --git a/fastlane_bot/events/exchanges/base.py b/fastlane_bot/events/exchanges/base.py index 8ff438a1b..62bfe7996 100644 --- a/fastlane_bot/events/exchanges/base.py +++ b/fastlane_bot/events/exchanges/base.py @@ -12,8 +12,9 @@ """ from abc import ABC, abstractmethod from dataclasses import dataclass, field -from typing import Dict, List, Type, Any +from typing import Dict, List, Type, Any, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract, AsyncContract from fastlane_bot.config.constants import CARBON_V1_NAME @@ -51,6 +52,9 @@ def get_pools(self) -> List[Pool]: """ return list(self.pools.values()) + def get_event_contract(self, w3: Union[Web3, AsyncWeb3]) -> Union[Contract, AsyncContract]: + return w3.eth.contract(abi=self.get_abi()) + @abstractmethod def add_pool(self, pool: Pool): """ @@ -99,7 +103,7 @@ def get_events(self, contract: Contract) -> List[Type[Contract]]: pass @abstractmethod - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: ... @staticmethod diff --git a/fastlane_bot/events/exchanges/carbon_v1.py b/fastlane_bot/events/exchanges/carbon_v1.py index fa4a40107..125dadb26 100644 --- a/fastlane_bot/events/exchanges/carbon_v1.py +++ b/fastlane_bot/events/exchanges/carbon_v1.py @@ -12,9 +12,10 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple, Any, Dict, Callable +from typing import List, Type, Tuple, Any, Dict, Callable, Union from fastlane_bot import Config +from web3 import Web3, AsyncWeb3 from web3.contract import Contract from fastlane_bot.data.abi import CARBON_CONTROLLER_ABI @@ -80,7 +81,8 @@ def get_events(self, contract: Contract) -> List[Type[Contract]]: contract.events.PairCreated, ] if self.exchange_initialized else [] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [ Subscription(contract.events.StrategyCreated, STRATEGY_CREATED_TOPIC), Subscription(contract.events.StrategyUpdated, STRATEGY_UPDATED_TOPIC), diff --git a/fastlane_bot/events/exchanges/solidly_v2/base.py b/fastlane_bot/events/exchanges/solidly_v2/base.py index 7bffef541..693c82197 100644 --- a/fastlane_bot/events/exchanges/solidly_v2/base.py +++ b/fastlane_bot/events/exchanges/solidly_v2/base.py @@ -13,8 +13,9 @@ """ from abc import abstractmethod from dataclasses import dataclass -from typing import List, Type, Any +from typing import List, Type, Any, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract, AsyncContract from fastlane_bot.data.abi import SOLIDLY_V2_POOL_ABI @@ -50,7 +51,8 @@ def add_pool(self, pool: Pool): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.Sync] if self.exchange_initialized else [] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [Subscription(contract.events.Sync)] def get_abi(self): diff --git a/fastlane_bot/events/exchanges/uniswap_v2.py b/fastlane_bot/events/exchanges/uniswap_v2.py index ce1e209cc..3fb5b902d 100644 --- a/fastlane_bot/events/exchanges/uniswap_v2.py +++ b/fastlane_bot/events/exchanges/uniswap_v2.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple, Any +from typing import List, Type, Tuple, Any, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract, AsyncContract from fastlane_bot.data.abi import UNISWAP_V2_POOL_ABI, UNISWAP_V2_FACTORY_ABI @@ -49,7 +50,8 @@ def get_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.Sync] if self.exchange_initialized else [] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [Subscription(contract.events.Sync)] async def get_fee(self, address: str, contract: AsyncContract) -> Tuple[str, float]: diff --git a/fastlane_bot/events/exchanges/uniswap_v3.py b/fastlane_bot/events/exchanges/uniswap_v3.py index 76d8a93d5..6a478faf6 100644 --- a/fastlane_bot/events/exchanges/uniswap_v3.py +++ b/fastlane_bot/events/exchanges/uniswap_v3.py @@ -12,8 +12,9 @@ Licensed under MIT. """ from dataclasses import dataclass -from typing import List, Type, Tuple, Any +from typing import List, Type, Tuple, Any, Union +from web3 import Web3, AsyncWeb3 from web3.contract import Contract from fastlane_bot.config.constants import AGNI_V3_NAME, PANCAKESWAP_V3_NAME, FUSIONX_V3_NAME, ECHODEX_V3_NAME, SECTA_V3_NAME @@ -46,7 +47,8 @@ def factory_abi(self): def get_events(self, contract: Contract) -> List[Type[Contract]]: return [contract.events.Swap] if self.exchange_initialized else [] - def get_subscriptions(self, contract: Contract) -> List[Subscription]: + def get_subscriptions(self, w3: Union[Web3, AsyncWeb3]) -> List[Subscription]: + contract = self.get_event_contract(w3) return [Subscription(contract.events.Swap)] async def get_fee(self, address: str, contract: Contract) -> Tuple[str, float]: diff --git a/fastlane_bot/tests/test_event_topics.py b/fastlane_bot/tests/test_event_topics.py index 67e456f71..506f9b280 100644 --- a/fastlane_bot/tests/test_event_topics.py +++ b/fastlane_bot/tests/test_event_topics.py @@ -46,7 +46,6 @@ ]) def test_event_topic(config, cls, exchange_name, event_topics): exchange = cls(exchange_name=exchange_name) - contract = config.w3.eth.contract(abi=exchange.get_abi()) - for subscription in exchange.get_subscriptions(contract): + for subscription in exchange.get_subscriptions(config.w3): assert event_topics.pop(subscription._event.event_name) == subscription.topic assert event_topics == {} diff --git a/main.py b/main.py index 15f4b3d05..36633d9d9 100644 --- a/main.py +++ b/main.py @@ -310,7 +310,6 @@ def run(mgr, args, tenderly_uri=None) -> None: config=mgr.cfg, w3=mgr.w3_async, exchanges=mgr.exchanges, - event_contracts=mgr.event_contracts ) pool_finder = PoolFinder(