Skip to content

Commit 9de6a7e

Browse files
committed
Checks improvements, small logic fix, multi-hop tests added
1 parent d32b5dd commit 9de6a7e

File tree

2 files changed

+168
-68
lines changed

2 files changed

+168
-68
lines changed

tests/v4tests.py

Lines changed: 148 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from web3 import Web3
55

66
from uniswap import Uniswap4, V4pools
7+
from uniswap.types import PoolKey
78

89

910
def pool_tests():
@@ -63,16 +64,36 @@ def pool_tests():
6364
def quoter_tests():
6465
# get_token_token_spot_price() tests
6566
print(f"Testing getSlot0() for {Fore.GREEN}ETH-USDC{Style.RESET_ALL}")
66-
test_result = str(uniV4_test.get_token_token_spot_price(test_token0, test_token1))
67+
test_result = str(uniV4_test.get_token_token_spot_price(test_ETH, test_USDC))
6768
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL}")
6869

6970
print(f"Testing getSlot0() for {Fore.GREEN}USDC-ETH{Style.RESET_ALL}")
70-
test_result = str(uniV4_test.get_token_token_spot_price(test_token1, test_token0))
71+
test_result = str(uniV4_test.get_token_token_spot_price(test_USDC, test_ETH))
7172
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL}")
7273
print("")
7374
print("")
7475

75-
##get_quote_exact_input_single() and get_quote_exact_output_single() tests
76+
test_pool_key1 = PoolKey(
77+
test_ETH,
78+
test_USDC,
79+
default_test_fee,
80+
default_test_tick_spacing,
81+
default_test_hooks,
82+
)
83+
test_pool_key2 = PoolKey(
84+
test_USDT,
85+
test_USDC,
86+
test_pool2_fee,
87+
test_pool2_tick_spacing,
88+
default_test_hooks,
89+
)
90+
test_path_1hop = list()
91+
test_path_1hop.append(test_pool_key1)
92+
test_path_2hop = list()
93+
test_path_2hop.append(test_pool_key1)
94+
test_path_2hop.append(test_pool_key2)
95+
96+
# Testing get_quote_exact_input_single()
7697
test_volume = 1
7798
print(
7899
"Testing exactInputSingle() for "
@@ -81,11 +102,12 @@ def quoter_tests():
81102
)
82103
test_result = str(
83104
uniV4_test.get_quote_exact_input_single(
84-
test_token0, test_token1, test_volume * test_d0
105+
test_ETH, test_USDC, test_volume * test_d0
85106
)
86107
/ test_d1
87108
)
88109
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
110+
89111
test_volume = 3000
90112
print(
91113
"Testing exactInputSingle() for "
@@ -94,14 +116,62 @@ def quoter_tests():
94116
)
95117
test_result = str(
96118
uniV4_test.get_quote_exact_input_single(
97-
test_token1, test_token0, test_volume * test_d1
119+
test_USDC, test_ETH, test_volume * test_d1
120+
)
121+
/ test_d0
122+
)
123+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
124+
print("")
125+
print("")
126+
127+
# Testing get_quote_exact_input()
128+
test_volume = 1
129+
print(
130+
"Testing exactInput() for "
131+
+ str(test_volume)
132+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
133+
)
134+
test_result = str(
135+
uniV4_test.get_quote_exact_input(
136+
test_ETH, test_volume * test_d0, test_path_1hop
137+
)
138+
/ test_d1
139+
)
140+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
141+
142+
test_volume = 3000
143+
print(
144+
"Testing exactInput() for "
145+
+ str(test_volume)
146+
+ f" {Fore.GREEN}USDC{Style.RESET_ALL} to {Fore.GREEN}ETH{Style.RESET_ALL}"
147+
)
148+
test_result = str(
149+
uniV4_test.get_quote_exact_input(
150+
test_USDC, test_volume * test_d1, test_path_1hop
98151
)
99152
/ test_d0
100153
)
101154
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
155+
156+
# 2-hop test
157+
test_volume = 1
158+
print(
159+
"Testing 2-hop exactInput() for "
160+
+ str(test_volume)
161+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDT{Style.RESET_ALL}"
162+
)
163+
test_result = str(
164+
uniV4_test.get_quote_exact_input(
165+
test_ETH, test_volume * test_d0, test_path_2hop
166+
)
167+
/ test_d2
168+
)
169+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDT")
170+
102171
print("")
103172
print("")
104173

174+
# Testing get_quote_exact_output_single()
105175
test_volume = 3000
106176
print(
107177
"Testing exactOutputSingle() for "
@@ -110,11 +180,12 @@ def quoter_tests():
110180
)
111181
test_result = str(
112182
uniV4_test.get_quote_exact_output_single(
113-
test_token0, test_token1, test_volume * test_d1
183+
test_ETH, test_USDC, test_volume * test_d1
114184
)
115185
/ test_d0
116186
)
117187
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
188+
118189
test_volume = 1
119190
print(
120191
"Testing exactOutputSingle() for "
@@ -123,12 +194,57 @@ def quoter_tests():
123194
)
124195
test_result = str(
125196
uniV4_test.get_quote_exact_output_single(
126-
test_token1, test_token0, test_volume * test_d0
197+
test_USDC, test_ETH, test_volume * test_d0
198+
)
199+
/ test_d1
200+
)
201+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
202+
203+
# Testing get_quote_exact_output()
204+
test_volume = 3000
205+
print(
206+
"Testing exactOutput() one hop for "
207+
+ str(test_volume)
208+
+ f" {Fore.GREEN}USDC{Style.RESET_ALL} to {Fore.GREEN}ETH{Style.RESET_ALL}"
209+
)
210+
test_result = str(
211+
uniV4_test.get_quote_exact_output(
212+
test_USDC, test_volume * test_d1, test_path_1hop
213+
)
214+
/ test_d0
215+
)
216+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} ETH")
217+
218+
test_volume = 1
219+
print(
220+
"Testing exactOutput() for "
221+
+ str(test_volume)
222+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
223+
)
224+
test_result = str(
225+
uniV4_test.get_quote_exact_output(
226+
test_ETH, test_volume * test_d0, test_path_1hop
127227
)
128228
/ test_d1
129229
)
130230
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDC")
131231

232+
# 2-hop test
233+
test_volume = 1
234+
print(
235+
"Testing 2-hop exactOutput() for "
236+
+ str(test_volume)
237+
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDT{Style.RESET_ALL}"
238+
)
239+
reversed_test_path_2hop = list(reversed(test_path_2hop))
240+
test_result = str(
241+
uniV4_test.get_quote_exact_output(
242+
test_ETH, test_volume * test_d0, reversed_test_path_2hop
243+
)
244+
/ test_d2
245+
)
246+
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} USDT")
247+
132248
print("")
133249
print("")
134250

@@ -142,9 +258,7 @@ def price_impact_tests():
142258
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
143259
)
144260
test_result = str(
145-
uniV4_test.estimate_price_impact(
146-
test_token0, test_token1, test_volume * test_d0
147-
)
261+
uniV4_test.estimate_price_impact(test_ETH, test_USDC, test_volume * test_d0)
148262
)
149263
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
150264
test_volume = 10
@@ -154,9 +268,7 @@ def price_impact_tests():
154268
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
155269
)
156270
test_result = str(
157-
uniV4_test.estimate_price_impact(
158-
test_token0, test_token1, test_volume * test_d0
159-
)
271+
uniV4_test.estimate_price_impact(test_ETH, test_USDC, test_volume * test_d0)
160272
)
161273
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
162274
test_volume = 100
@@ -166,9 +278,7 @@ def price_impact_tests():
166278
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
167279
)
168280
test_result = str(
169-
uniV4_test.estimate_price_impact(
170-
test_token0, test_token1, test_volume * test_d0
171-
)
281+
uniV4_test.estimate_price_impact(test_ETH, test_USDC, test_volume * test_d0)
172282
)
173283
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
174284
test_volume = 1000
@@ -178,9 +288,7 @@ def price_impact_tests():
178288
+ f" {Fore.GREEN}ETH{Style.RESET_ALL} to {Fore.GREEN}USDC{Style.RESET_ALL}"
179289
)
180290
test_result = str(
181-
uniV4_test.estimate_price_impact(
182-
test_token0, test_token1, test_volume * test_d0
183-
)
291+
uniV4_test.estimate_price_impact(test_ETH, test_USDC, test_volume * test_d0)
184292
)
185293
print(f"Result: {Fore.GREEN}" + test_result + f"{Style.RESET_ALL} %")
186294

@@ -194,8 +302,8 @@ def state_view_tests():
194302
f"Testing get_liquidity() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
195303
)
196304
test_result = uniV4_test.get_liquidity_stateview(
197-
test_token0,
198-
test_token1,
305+
test_ETH,
306+
test_USDC,
199307
default_test_fee,
200308
default_test_tick_spacing,
201309
default_test_hooks,
@@ -206,8 +314,8 @@ def state_view_tests():
206314
f"Testing get_slot0() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
207315
)
208316
test_result1 = uniV4_test.get_slot0_stateview(
209-
test_token0,
210-
test_token1,
317+
test_ETH,
318+
test_USDC,
211319
default_test_fee,
212320
default_test_tick_spacing,
213321
default_test_hooks,
@@ -219,8 +327,8 @@ def state_view_tests():
219327
f"Testing get_fee_growth_globals() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
220328
)
221329
test_result2 = uniV4_test.get_fee_growth_globals_stateview(
222-
test_token0,
223-
test_token1,
330+
test_ETH,
331+
test_USDC,
224332
default_test_fee,
225333
default_test_tick_spacing,
226334
default_test_hooks,
@@ -231,8 +339,8 @@ def state_view_tests():
231339
f"Testing get_fee_growth_inside() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
232340
)
233341
test_result3 = uniV4_test.get_fee_growth_inside_stateview(
234-
test_token0,
235-
test_token1,
342+
test_ETH,
343+
test_USDC,
236344
default_test_fee,
237345
default_test_tick_spacing,
238346
default_test_hooks,
@@ -272,8 +380,8 @@ def state_view_tests():
272380
f"Testing get_tick_bitmap() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
273381
)
274382
test_result5 = uniV4_test.get_tick_bitmap_stateview(
275-
test_token0,
276-
test_token1,
383+
test_ETH,
384+
test_USDC,
277385
default_test_fee,
278386
default_test_tick_spacing,
279387
default_test_hooks,
@@ -285,8 +393,8 @@ def state_view_tests():
285393
f"Testing get_tick_fee_growth_outside() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
286394
)
287395
test_result6 = uniV4_test.get_tick_fee_growth_outside_stateview(
288-
test_token0,
289-
test_token1,
396+
test_ETH,
397+
test_USDC,
290398
default_test_fee,
291399
default_test_tick_spacing,
292400
default_test_hooks,
@@ -298,8 +406,8 @@ def state_view_tests():
298406
f"Testing get_tick_pool_info() for ({Fore.GREEN}ETH{Style.RESET_ALL}, {Fore.GREEN}USDC{Style.RESET_ALL}) liquidity pool"
299407
)
300408
test_result7 = uniV4_test.get_tick_pool_info_stateview(
301-
test_token0,
302-
test_token1,
409+
test_ETH,
410+
test_USDC,
303411
default_test_fee,
304412
default_test_tick_spacing,
305413
default_test_hooks,
@@ -312,18 +420,22 @@ def state_view_tests():
312420

313421

314422
if __name__ == "__main__":
315-
test_token0 = "0x0000000000000000000000000000000000000000"
316-
test_token1 = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
423+
test_ETH = "0x0000000000000000000000000000000000000000"
424+
test_USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
425+
test_USDT = Web3.to_checksum_address("0xdac17f958d2ee523a2206206994597c13d831ec7")
317426
test_zero_hook = "0x0000000000000000000000000000000000000000"
318427
test_d0 = 10**18
319428
test_d1 = 10**6
429+
test_d2 = 10**6
320430
test_fee = 500
321431
default_test_fee = 500
322432
default_test_tick_spacing = 10
433+
test_pool2_fee = 10
434+
test_pool2_tick_spacing = 1
323435
default_test_hooks = test_zero_hook
324436
_TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
325437

326-
rpc_endpoint = "https://eth.drpc.org" # "https://go.getblock.us/27eb23f40b964c9bb71b62f721e594e7"
438+
rpc_endpoint = "https://eth.drpc.org"
327439
address = "0x94e3361495bD110114ac0b6e35Ed75E77E6a6cFA"
328440
w3_test = Web3(Web3.HTTPProvider(rpc_endpoint, request_kwargs={"timeout": 60}))
329441

0 commit comments

Comments
 (0)