Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get topics from correct event contract abi #677

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions fastlane_bot/events/event_gatherer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(
config: Config,
w3: AsyncWeb3,
exchanges: Dict[str, Exchange],
event_contracts: Dict[str, Contract],
):
""" Initializes the EventManager.
Args:
Expand All @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions fastlane_bot/events/exchanges/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/bancor_pol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/bancor_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/bancor_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions fastlane_bot/events/exchanges/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
barakman marked this conversation as resolved.
Show resolved Hide resolved

@abstractmethod
def add_pool(self, pool: Pool):
"""
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/carbon_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/solidly_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/uniswap_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
6 changes: 4 additions & 2 deletions fastlane_bot/events/exchanges/uniswap_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
3 changes: 1 addition & 2 deletions fastlane_bot/tests/test_event_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == {}
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading