Skip to content

Commit 0228e5d

Browse files
committed
v4 test prototype
1 parent 149aba1 commit 0228e5d

File tree

2 files changed

+358
-1
lines changed

2 files changed

+358
-1
lines changed

tests/v4tests.py

Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
from colorama import Fore, Style
2+
from web3 import Web3
3+
4+
from uniswap import Uniswap4, V4pools
5+
6+
7+
def pool_tests():
8+
# pool list tests
9+
first_block = 21688329
10+
print("Testing fetch_poolkey_data()...")
11+
try:
12+
v4pools_test.fetch_poolkey_data(first_block, chunk_size=500, clear_list=False)
13+
print("Test passed.")
14+
except Exception:
15+
print("Test failed.")
16+
17+
print("Testing save_poolkeys_list() absolute path")
18+
try:
19+
v4pools_test.save_poolkeys_list(
20+
"/home/yohan/src/Uniswap/uniswap-python/tests/pools/pool_list_mainnet.tst1"
21+
)
22+
print("Test passed.")
23+
except Exception:
24+
print("Test failed.")
25+
26+
print("Testing save_poolkeys_list() relative path")
27+
try:
28+
v4pools_test.save_poolkeys_list("./pools/pool_list_mainnet.tst2")
29+
print("Test passed.")
30+
except Exception:
31+
print("Test failed.")
32+
33+
print("Testing load_poolkeys_list() absolute path")
34+
try:
35+
v4pools_test.load_poolkeys_list(
36+
"/home/yohan/src/Uniswap/uniswap-python/tests/pools/pool_list_mainnet.tst1"
37+
)
38+
print("Test passed.")
39+
except Exception:
40+
print("Test failed.")
41+
print("Testing load_poolkeys_list() relative path")
42+
try:
43+
v4pools_test.load_poolkeys_list("./pools/pool_list_mainnet.tst2")
44+
print("Test passed.")
45+
except Exception:
46+
print("Test failed.")
47+
48+
print("Testing get_pool_key() for ETH-USDC (correct entry)...")
49+
test_result = v4pools_test.get_poolkeys_sublist(
50+
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
51+
"0x0000000000000000000000000000000000000000",
52+
)
53+
print(test_result)
54+
if len(test_result) > 0:
55+
print("Test passed.")
56+
else:
57+
print("Test failed.")
58+
59+
print("Testing get_pool_key() for 0x1-WETH (incorrect entry)")
60+
test_result = v4pools_test.get_poolkeys_sublist(
61+
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
62+
"0x0000000000000000000000000000000000000001",
63+
)
64+
print(test_result)
65+
if len(test_result) == 0:
66+
print("Test passed.")
67+
else:
68+
print("Test failed.")
69+
70+
print("")
71+
print("")
72+
73+
74+
def quoter_tests():
75+
# get_token_token_spot_price() tests
76+
print(f"Testing getSlot0() for {Fore.GREEN}ETH-USDC{Style.RESET_ALL}")
77+
test_result = str(uniV4_test.get_token_token_spot_price(test_token0, test_token1))
78+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL}")
79+
80+
print(f"Testing getSlot0() for {Fore.GREEN}USDC-ETH{Style.RESET_ALL}")
81+
test_result = str(uniV4_test.get_token_token_spot_price(test_token1, test_token0))
82+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL}")
83+
print("")
84+
print("")
85+
86+
##get_quote_exact_input_single() and get_quote_exact_output_single() tests
87+
test_volume = 1
88+
print(
89+
"Testing exactInputSingle() for "
90+
+ str(test_volume)
91+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
92+
)
93+
test_result = str(
94+
uniV4_test.get_quote_exact_input_single(
95+
test_token0, test_token1, test_volume * test_d0
96+
)
97+
/ test_d1
98+
)
99+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
100+
test_volume = 3000
101+
print(
102+
"Testing exactInputSingle() for "
103+
+ str(test_volume)
104+
+ f" {Fore.GREEN}USDC{Style.RESET_ALL} to {Fore.GREEN}ETH{Style.RESET_ALL}"
105+
)
106+
test_result = str(
107+
uniV4_test.get_quote_exact_input_single(
108+
test_token1, test_token0, test_volume * test_d1
109+
)
110+
/ test_d0
111+
)
112+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
113+
print("")
114+
print("")
115+
116+
test_volume = 3000
117+
print(
118+
"Testing exactOutputSingle() for "
119+
+ str(test_volume)
120+
+ f" {Fore.GREEN}USDC{Style.RESET_ALL} to {Fore.GREEN}ETH{Style.RESET_ALL}"
121+
)
122+
test_result = str(
123+
uniV4_test.get_quote_exact_output_single(
124+
test_token0, test_token1, test_volume * test_d1
125+
)
126+
/ test_d0
127+
)
128+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
129+
test_volume = 1
130+
print(
131+
"Testing exactOutputSingle() for "
132+
+ str(test_volume)
133+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
134+
)
135+
test_result = str(
136+
uniV4_test.get_quote_exact_output_single(
137+
test_token1, test_token0, test_volume * test_d0
138+
)
139+
/ test_d1
140+
)
141+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
142+
143+
print("")
144+
print("")
145+
146+
147+
def price_impact_tests():
148+
##estimate_price_impact() tests
149+
test_volume = 1
150+
print(
151+
"Testing estimate_price_impact() for "
152+
+ str(test_volume)
153+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
154+
)
155+
test_result = str(
156+
uniV4_test.estimate_price_impact(
157+
test_token0, test_token1, test_volume * test_d0
158+
)
159+
)
160+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
161+
test_volume = 10
162+
print(
163+
"Testing estimate_price_impact() for "
164+
+ str(test_volume)
165+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
166+
)
167+
test_result = str(
168+
uniV4_test.estimate_price_impact(
169+
test_token0, test_token1, test_volume * test_d0
170+
)
171+
)
172+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
173+
test_volume = 100
174+
print(
175+
"Testing estimate_price_impact() for "
176+
+ str(test_volume)
177+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
178+
)
179+
test_result = str(
180+
uniV4_test.estimate_price_impact(
181+
test_token0, test_token1, test_volume * test_d0
182+
)
183+
)
184+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
185+
test_volume = 1000
186+
print(
187+
"Testing estimate_price_impact() for "
188+
+ str(test_volume)
189+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
190+
)
191+
test_result = str(
192+
uniV4_test.estimate_price_impact(
193+
test_token0, test_token1, test_volume * test_d0
194+
)
195+
)
196+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
197+
198+
print("")
199+
print("")
200+
201+
202+
def state_view_tests():
203+
##StateView tests
204+
print(
205+
f"Testing get_liquidity() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
206+
)
207+
test_result = uniV4_test.get_liquidity_stateview(
208+
test_token0,
209+
test_token1,
210+
default_test_fee,
211+
default_test_tick_spacing,
212+
default_test_hooks,
213+
)
214+
print(f"Result: {Fore.GREEN}" + str(test_result) + f"{Style.RESET_ALL}")
215+
#
216+
print(
217+
f"Testing get_slot0() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
218+
)
219+
test_result1 = uniV4_test.get_slot0_stateview(
220+
test_token0,
221+
test_token1,
222+
default_test_fee,
223+
default_test_tick_spacing,
224+
default_test_hooks,
225+
)
226+
test_tick: int = int(test_result1["tick"])
227+
print(f"Result: {Fore.GREEN}" + str(test_result) + f"{Style.RESET_ALL}")
228+
#
229+
print(
230+
f"Testing get_fee_growth_globals() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
231+
)
232+
test_result2 = uniV4_test.get_fee_growth_globals_stateview(
233+
test_token0,
234+
test_token1,
235+
default_test_fee,
236+
default_test_tick_spacing,
237+
default_test_hooks,
238+
)
239+
print(f"Result: {Fore.GREEN}" + str(test_result2) + f"{Style.RESET_ALL}")
240+
#
241+
print(
242+
f"Testing get_fee_growth_inside() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
243+
)
244+
test_result3 = uniV4_test.get_fee_growth_inside_stateview(
245+
test_token0,
246+
test_token1,
247+
default_test_fee,
248+
default_test_tick_spacing,
249+
default_test_hooks,
250+
test_tick - default_test_tick_spacing * 3,
251+
test_tick + default_test_tick_spacing * 3,
252+
)
253+
print(f"Result: {Fore.GREEN}" + str(test_result3) + f"{Style.RESET_ALL}")
254+
#
255+
# get_position_info !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
256+
print(
257+
f"Testing get_position_info() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
258+
)
259+
# taken from 0xe529044f9cb8526c9b4d635f81889d991d217a6fb859b3e4f446cbe0ba988e31 as sample data
260+
pos_inf_token0 = "0x6c76de483f1752ac8473e2b4983a873991e70da7"
261+
pos_inf_token1 = "0xdac17f958d2ee523a2206206994597c13d831ec7"
262+
pos_inf_fee = 13686
263+
pos_inf_tick_spacing = 10
264+
pos_inf_hooks = default_test_hooks
265+
pos_inf_owner = "0x42562F062D618EfeB53cAB346E3b6E2EaB2e5BCB"
266+
pos_inf_tick_lower = -301070
267+
pos_inf_tick_upper = -292859
268+
pos_inf_token_id = 156881
269+
test_result4 = uniV4_test.get_position_info_stateview(
270+
pos_inf_token0,
271+
pos_inf_token1,
272+
pos_inf_fee,
273+
pos_inf_tick_spacing,
274+
pos_inf_hooks,
275+
pos_inf_owner,
276+
pos_inf_tick_lower,
277+
pos_inf_tick_upper,
278+
pos_inf_token_id,
279+
)
280+
print(f"Result: {Fore.GREEN}" + str(test_result4) + f"{Style.RESET_ALL}")
281+
#
282+
print(
283+
f"Testing get_tick_bitmap() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
284+
)
285+
test_result5 = uniV4_test.get_tick_bitmap_stateview(
286+
test_token0,
287+
test_token1,
288+
default_test_fee,
289+
default_test_tick_spacing,
290+
default_test_hooks,
291+
-30000, # must be int16
292+
)
293+
print(f"Result: {Fore.GREEN}" + str(test_result5) + f"{Style.RESET_ALL}")
294+
#
295+
print(
296+
f"Testing get_tick_fee_growth_outside() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
297+
)
298+
test_result6 = uniV4_test.get_tick_fee_growth_outside_stateview(
299+
test_token0,
300+
test_token1,
301+
default_test_fee,
302+
default_test_tick_spacing,
303+
default_test_hooks,
304+
test_tick,
305+
)
306+
print(f"Result: {Fore.GREEN}" + str(test_result6) + f"{Style.RESET_ALL}")
307+
#
308+
print(
309+
f"Testing get_tick_pool_info() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
310+
)
311+
test_result7 = uniV4_test.get_tick_pool_info_stateview(
312+
test_token0,
313+
test_token1,
314+
default_test_fee,
315+
default_test_tick_spacing,
316+
default_test_hooks,
317+
test_tick,
318+
)
319+
print(f"Result: {Fore.GREEN}" + str(test_result7) + f"{Style.RESET_ALL}")
320+
#
321+
print("")
322+
print("")
323+
324+
325+
if __name__ == "__main__":
326+
test_token0 = "0x0000000000000000000000000000000000000000"
327+
test_token1 = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
328+
test_zero_hook = "0x0000000000000000000000000000000000000000"
329+
test_d0 = 10**18
330+
test_d1 = 10**6
331+
test_fee = 500
332+
default_test_fee = 500
333+
default_test_tick_spacing = 10
334+
default_test_hooks = test_zero_hook
335+
336+
rpc_endpoint = "https://eth.drpc.org" # "https://go.getblock.us/27eb23f40b964c9bb71b62f721e594e7"
337+
address = "0x94e3361495bD110114ac0b6e35Ed75E77E6a6cFA"
338+
w3_test = Web3(Web3.HTTPProvider(rpc_endpoint, request_kwargs={"timeout": 60}))
339+
340+
uniV4_test = Uniswap4(
341+
address,
342+
None,
343+
provider=rpc_endpoint,
344+
)
345+
v4pools_test = V4pools(w3_test)
346+
347+
##TESTS
348+
print("Started.")
349+
print("")
350+
print("")
351+
352+
# pool_tests()
353+
quoter_tests()
354+
price_impact_tests()
355+
state_view_tests()
356+
357+
print("Done.")

uniswap/uniswap4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ def estimate_price_impact(
14841484
# `(token0, token1, fee)` hasn't been deployed.
14851485
return 1
14861486
price = (
1487-
quote_amount / (qty / (10 ** self.get_token(_str_to_addr(token0)).decimals))
1487+
quote_amount / (qty / (10 ** self.get_token(token0).decimals))
14881488
) / 10 ** self.get_token(_str_to_addr(token1)).decimals
14891489

14901490
# calculate and subtract the realised fees from the price impact. See:

0 commit comments

Comments
 (0)