Skip to content

Commit 19ecf0c

Browse files
committed
Generate pool address deterministically instead of calling factory
1 parent 32b0c65 commit 19ecf0c

File tree

1 file changed

+19
-48
lines changed

1 file changed

+19
-48
lines changed

src/degenbot/uniswap/managers.py

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import contextlib
22
from threading import Lock
3-
from typing import TYPE_CHECKING, Any, cast
3+
from typing import Any
44

5-
from eth_typing import BlockIdentifier, ChecksumAddress
5+
from eth_typing import ChecksumAddress
66
from eth_utils.address import to_checksum_address
77
from typing_extensions import Self
8-
from web3 import Web3
98

109
from ..config import web3_connection_manager
1110
from ..exceptions import (
@@ -15,11 +14,11 @@
1514
ManagerError,
1615
PoolNotAssociated,
1716
)
18-
from ..functions import encode_function_calldata, get_number_for_block_identifier, raw_call
1917
from ..logging import logger
2018
from ..registry.all_pools import pool_registry
2119
from ..types import AbstractLiquidityPool, AbstractPoolManager
2220
from ..uniswap.deployments import UniswapV2ExchangeDeployment, UniswapV3ExchangeDeployment
21+
from ..uniswap.v2_functions import generate_v2_pool_address
2322
from .deployments import FACTORY_DEPLOYMENTS
2423
from .v3_functions import generate_v3_pool_address
2524
from .v3_snapshot import UniswapV3LiquiditySnapshot
@@ -106,33 +105,14 @@ def __delitem__(self, pool: AbstractLiquidityPool | ChecksumAddress | str) -> No
106105
def __repr__(self) -> str: # pragma: no cover
107106
return f"UniswapV2PoolManager(factory={self._factory_address})"
108107

109-
def _add_tracked_pool(self, pool_helper: AbstractLiquidityPool) -> None:
108+
def _add_tracked_pool(self, pool_helper: Pool) -> None:
110109
with self._lock:
111110
self._tracked_pools[pool_helper.address] = pool_helper
112111

113112
@property
114113
def chain_id(self) -> int:
115114
return self._chain_id
116115

117-
def get_pair_from_factory(
118-
self,
119-
w3: Web3,
120-
token0: ChecksumAddress,
121-
token1: ChecksumAddress,
122-
block_identifier: BlockIdentifier | None = None,
123-
) -> str:
124-
pool_address, *_ = raw_call(
125-
w3=w3,
126-
address=self._factory_address,
127-
calldata=encode_function_calldata(
128-
function_prototype="getPair(address,address)",
129-
function_arguments=[token0, token1],
130-
),
131-
return_types=["address"],
132-
block_identifier=get_number_for_block_identifier(block_identifier, w3),
133-
)
134-
return cast(str, pool_address)
135-
136116
def _build_pool(
137117
self,
138118
pool_address: ChecksumAddress,
@@ -157,7 +137,7 @@ def get_pool(
157137
silent: bool = False,
158138
state_block: int | None = None,
159139
pool_class_kwargs: dict[str, Any] | None = None,
160-
) -> AbstractLiquidityPool:
140+
) -> Pool:
161141
"""
162142
Get a pool from its address
163143
"""
@@ -166,8 +146,7 @@ def get_pool(
166146

167147
with contextlib.suppress(KeyError):
168148
result = self._tracked_pools[pool_address]
169-
if TYPE_CHECKING:
170-
assert isinstance(result, self.Pool)
149+
assert isinstance(result, self.Pool)
171150
return result
172151

173152
if pool_address in self._untracked_pools:
@@ -182,8 +161,7 @@ def get_pool(
182161
chain_id=self._chain_id,
183162
)
184163
) is not None:
185-
if TYPE_CHECKING:
186-
assert isinstance(pool_from_registry, self.Pool)
164+
assert isinstance(pool_from_registry, self.Pool)
187165
if pool_from_registry.factory == self._factory_address:
188166
self._add_tracked_pool(pool_from_registry)
189167
return pool_from_registry
@@ -211,21 +189,19 @@ def get_pool(
211189
def get_pool_from_tokens(
212190
self,
213191
token_addresses: tuple[str, str],
192+
*,
214193
silent: bool = False,
215194
state_block: int | None = None,
216195
pool_class_kwargs: dict[str, Any] | None = None,
217-
) -> AbstractLiquidityPool:
196+
) -> Pool:
218197
"""
219198
Get a pool by its token addresses
220199
"""
221200

222-
pool_address = to_checksum_address(
223-
self.get_pair_from_factory(
224-
w3=web3_connection_manager.get_web3(self.chain_id),
225-
token0=to_checksum_address(token_addresses[0]),
226-
token1=to_checksum_address(token_addresses[1]),
227-
block_identifier=None,
228-
)
201+
pool_address = generate_v2_pool_address(
202+
deployer_address=self._deployer_address,
203+
token_addresses=token_addresses,
204+
init_hash=self._pool_init_hash,
229205
)
230206

231207
return self.get_pool(
@@ -304,13 +280,8 @@ def __init__(
304280
self._tracked_pools: dict[ChecksumAddress, AbstractLiquidityPool] = dict()
305281
self._untracked_pools: set[ChecksumAddress] = set()
306282

307-
def __delitem__(self, pool: AbstractLiquidityPool | ChecksumAddress | str) -> None:
308-
pool_address: ChecksumAddress
309-
310-
if isinstance(pool, AbstractLiquidityPool):
311-
pool_address = pool.address
312-
else:
313-
pool_address = to_checksum_address(pool)
283+
def __delitem__(self, pool: Pool | ChecksumAddress | str) -> None:
284+
pool_address = pool.address if isinstance(pool, self.Pool) else to_checksum_address(pool)
314285

315286
with contextlib.suppress(KeyError):
316287
del self._tracked_pools[pool_address]
@@ -376,6 +347,7 @@ def _build_pool(
376347
def get_pool(
377348
self,
378349
pool_address: ChecksumAddress | str,
350+
*,
379351
silent: bool = False,
380352
state_block: int | None = None,
381353
# keyword arguments passed to the pool class constructor
@@ -389,8 +361,7 @@ def get_pool(
389361

390362
with contextlib.suppress(KeyError):
391363
result = self._tracked_pools[pool_address]
392-
if TYPE_CHECKING:
393-
assert isinstance(result, self.Pool)
364+
assert isinstance(result, self.Pool)
394365
return result
395366

396367
if pool_address in self._untracked_pools:
@@ -405,8 +376,7 @@ def get_pool(
405376
chain_id=self._chain_id,
406377
)
407378
) is not None:
408-
if TYPE_CHECKING:
409-
assert isinstance(pool_from_registry, self.Pool)
379+
assert isinstance(pool_from_registry, self.Pool)
410380
if pool_from_registry.factory == self._factory_address:
411381
self._add_tracked_pool(pool_from_registry)
412382
return pool_from_registry
@@ -439,6 +409,7 @@ def get_pool_from_tokens_and_fee(
439409
ChecksumAddress | str,
440410
],
441411
pool_fee: int,
412+
*,
442413
silent: bool = False,
443414
state_block: int | None = None,
444415
# keyword arguments passed to the pool class constructor

0 commit comments

Comments
 (0)