-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
186 lines (148 loc) · 4.96 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import typing
import web3
import dotenv
import os
import functools
from web3.middleware import geth_poa_middleware
from eth_utils import keccak
import constants
def connect_web3(host: str = None) -> web3.Web3:
if host is None:
host = os.environ.get("WEB3_HOST", "ws://localhost:8546")
return web3.Web3(
web3.WebsocketProvider(
host, websocket_kwargs={"timeout": 5 * 60, "max_size": 1024 * 1024 * 1024}
)
)
def connect_web3_goerli() -> web3.Web3:
host = os.environ.get("WEB3_HOST_GOERLI")
w3 = web3.Web3(
web3.HTTPProvider(
host,
)
)
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
return w3
@functools.lru_cache(maxsize=10_000)
def uniswap_v2_pool_address(token0, token1) -> bytes:
"""
Compute the uniswap v2 pool address for a given pair of tokens
"""
if isinstance(token0, str):
if token0.startswith("0x"):
token0 = token0[2:]
token0 = bytes.fromhex(token0)
if isinstance(token1, str):
if token1.startswith("0x"):
token1 = token1[2:]
token1 = bytes.fromhex(token1)
token0, token1 = sorted([token0, token1])
salt = keccak(token0 + token1)
h = keccak(
b"\xff"
+ bytes.fromhex("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"[2:])
+ salt
+ bytes.fromhex(
"96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f"
)
)
return h[-20:]
@functools.lru_cache(maxsize=10_000)
def uniswap_v3_pool_address(token0, token1, fee) -> bytes:
"""
Compute the uniswap v3 pool address for a given pair of tokens
"""
if isinstance(token0, str):
if token0.startswith("0x"):
token0 = token0[2:]
token0 = bytes.fromhex(token0)
if isinstance(token1, str):
if token1.startswith("0x"):
token1 = token1[2:]
token1 = bytes.fromhex(token1)
token0, token1 = sorted([token0, token1])
token0 = token0.rjust(32, b"\x00")
token1 = token1.rjust(32, b"\x00")
fee = int.to_bytes(fee, length=32, byteorder="big", signed=False)
salt = keccak(token0 + token1 + fee)
h = keccak(
b"\xff"
+ bytes.fromhex("0x1F98431c8aD98523631AE4a59f267346ea31F984"[2:])
+ salt
+ bytes.fromhex(
"e34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54"
)
)
return h[-20:]
def sushiswap_pool_address(token0, token1) -> bytes:
"""
Compute the sushiswap pool address for a given pair of tokens
"""
if isinstance(token0, str):
if token0.startswith("0x"):
token0 = token0[2:]
token0 = bytes.fromhex(token0)
if isinstance(token1, str):
if token1.startswith("0x"):
token1 = token1[2:]
token1 = bytes.fromhex(token1)
token0, token1 = sorted([token0, token1])
salt = keccak(token0 + token1)
h = keccak(
b"\xff"
+ bytes.fromhex("0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac"[2:])
+ salt
+ bytes.fromhex(
"e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303"
)
)
return h[-20:]
@functools.lru_cache(maxsize=1_000)
def infer_uniswap_v3_pool_fee(
address: str, token0: str, token1: str
) -> typing.Optional[int]:
# infer the fee
for fee in [100, 500, 3_000, 10_000]:
if address.endswith(uniswap_v3_pool_address(token0, token1, fee).hex()):
# found it
return fee
return None
@functools.lru_cache(maxsize=10_000)
def is_uniswap_v2(token0, token1, address) -> bool:
if isinstance(token0, str):
if token0.startswith("0x"):
token0 = token0[2:]
token0 = bytes.fromhex(token0)
if isinstance(token1, str):
if token1.startswith("0x"):
token1 = token1[2:]
token1 = bytes.fromhex(token1)
if isinstance(address, str):
if address.startswith("0x"):
address = address[2:]
address = bytes.fromhex(address)
return address == uniswap_v2_pool_address(token0, token1)
@functools.lru_cache(maxsize=10_000)
def is_uniswap_v3(token0, token1, address) -> bool:
return get_uniswap_v3_fee(token0, token1, address) is not None
@functools.lru_cache(maxsize=10_000)
def get_uniswap_v3_fee(token0, token1, address) -> typing.Optional[int]:
"""
Returns the fee if the address is a uniswap v3 pool, otherwise None
"""
if isinstance(token0, str):
if token0.startswith("0x"):
token0 = token0[2:]
token0 = bytes.fromhex(token0)
if isinstance(token1, str):
if token1.startswith("0x"):
token1 = token1[2:]
token1 = bytes.fromhex(token1)
if isinstance(address, str):
if address.startswith("0x"):
address = address[2:]
address = bytes.fromhex(address)
for fee in constants.UNISWAP_V3_FEES_BIPS:
if address == uniswap_v3_pool_address(token0, token1, fee):
return fee
return None