|
| 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() |