Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit944a391

Browse files
committed
feat: added estimate_price_impact helper function and example
1 parentd95a0b2 commit944a391

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

‎examples/price_impact.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
fromtypingimportList
2+
3+
fromweb3importWeb3
4+
5+
fromuniswapimportUniswap
6+
fromuniswap.typesimportAddressLike
7+
8+
eth=Web3.toChecksumAddress("0x0000000000000000000000000000000000000000")
9+
weth=Web3.toChecksumAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
10+
usdt=Web3.toChecksumAddress("0xdac17f958d2ee523a2206206994597c13d831ec7")
11+
vxv=Web3.toChecksumAddress("0x7d29a64504629172a429e64183d6673b9dacbfce")
12+
13+
14+
defusdt_to_vxv_v2():
15+
"""
16+
Checks impact for a pool with very little liquidity.
17+
18+
This particular route caused a $14k loss for one user: https://github.com/uniswap-python/uniswap-python/discussions/198
19+
"""
20+
uniswap=Uniswap(address=None,private_key=None,version=2)
21+
22+
route:List[AddressLike]= [usdt,weth,vxv]
23+
24+
# Compare the results with the output of:
25+
# https://app.uniswap.org/#/swap?use=v2&inputCurrency=0xdac17f958d2ee523a2206206994597c13d831ec7&outputCurrency=0x7d29a64504629172a429e64183d6673b9dacbfce
26+
qty=10*10**8
27+
28+
# price = uniswap.get_price_input(usdt, vxv, qty, route=route) / 10 ** 18
29+
# print(price)
30+
31+
impact=uniswap.estimate_price_impact(usdt,vxv,qty,route=route)
32+
# NOTE: Not sure why this differs from the quote in the UI?
33+
# Getting -27% in the UI for 10 USDT, but this returns >95%
34+
# The slippage for v3 (in example below) returns correct results.
35+
print(f"Impact for{qty/10**8} USDT:{round(impact*100,3)}%")
36+
37+
qty=13900*10**8
38+
impact=uniswap.estimate_price_impact(usdt,vxv,qty,route=route)
39+
print(f"Impact for{qty/10**8} USDT:{round(impact*100,3)}%")
40+
41+
42+
defusdt_to_vxv_v3():
43+
"""Checks price impact for a pool with liquidity."""
44+
uniswap=Uniswap(address=None,private_key=None,version=3)
45+
46+
# Compare the results with the output of:
47+
# https://app.uniswap.org/#/swap?use=v3&inputCurrency=ETH&outputCurrency=0x7d29a64504629172a429e64183d6673b9dacbfce
48+
qty=1*10**18
49+
impact=uniswap.estimate_price_impact(eth,vxv,qty,fee=10000)
50+
print(f"Impact for{qty/10**18} ETH:{round(impact*100,3)}%")
51+
52+
qty=100*10**18
53+
impact=uniswap.estimate_price_impact(eth,vxv,qty,fee=10000)
54+
print(f"Impact for{qty/10**18} ETH:{round(impact*100,3)}%")
55+
56+
57+
if__name__=="__main__":
58+
usdt_to_vxv_v2()
59+
usdt_to_vxv_v3()

‎uniswap/uniswap.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ def _calculate_max_output_token(
11721172

11731173
# ------ Helpers ------------------------------------------------------------
11741174

1175-
defget_token(self,address:AddressLike,abi_name:str="erc20")->ERC20Token:
1175+
defget_token(self,address:AddressLike,abi_name:str="erc20")->ERC20Token:
11761176
"""
11771177
Retrieves metadata from the ERC20 contract of a given token, like its name, symbol, and decimals.
11781178
"""
@@ -1209,6 +1209,34 @@ def get_weth_address(self) -> ChecksumAddress:
12091209
address=self.router.functions.WETH9().call()
12101210
returnaddress
12111211

1212+
defestimate_price_impact(
1213+
self,
1214+
token_in:AddressLike,
1215+
token_out:AddressLike,
1216+
amount_in:int,
1217+
fee:int=None,
1218+
route:Optional[List[AddressLike]]=None,
1219+
)->float:
1220+
"""
1221+
Returns the estimated price impact as a positive float (0.01 = 1%).
1222+
1223+
NOTE: Work-in-progress.
1224+
1225+
See ``examples/price_impact.py`` for an example which uses this.
1226+
"""
1227+
amount_small=10**2
1228+
cost_small=self.get_price_input(
1229+
token_in,token_out,amount_small,fee=fee,route=route
1230+
)
1231+
cost_amount=self.get_price_input(
1232+
token_in,token_out,amount_in,fee=fee,route=route
1233+
)
1234+
1235+
price_small=cost_small/amount_small
1236+
price_amount=cost_amount/amount_in
1237+
1238+
return (price_small-price_amount)/price_small
1239+
12121240
# ------ Exchange ------------------------------------------------------------------
12131241
@supports([1,2])
12141242
defget_fee_maker(self)->float:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp