-
-
Notifications
You must be signed in to change notification settings - Fork 58
Encode_Uniswap_V3_Path
Elnaril edited this page Apr 1, 2024
·
1 revision
Let's say we want to encode the Uniswap V3 path to swap WETH for UNI on a 1% pool:
path = (
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', # in token
10000, # 1% pool fee
'0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984' # out token
)
If we want to swap with an exact in_amount
, we just need to write:
from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()
encoded_path = codec.encode.v3_path("V3_SWAP_EXACT_IN", path)
The result is the following bytes:
b"\xc0*\xaa9\xb2#\xfe\x8d\n\x0e\\O'\xea\xd9\x08<ul\xc2\x00'\x10\x1f\x98@\xa8]Z\xf5\xbf\x1d\x17b\xf9%\xbd\xad\xdcB\x01\xf9\x84"
Now, if we want to swap with an exact out_amount
, we just need to write:
from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()
encoded_path = codec.encode.v3_path("V3_SWAP_EXACT_OUT", path)
The result is the following bytes:
b"\x1f\x98@\xa8]Z\xf5\xbf\x1d\x17b\xf9%\xbd\xad\xdcB\x01\xf9\x84\x00'\x10\xc0*\xaa9\xb2#\xfe\x8d\n\x0e\\O'\xea\xd9\x08<ul\xc2"
As you can see, V3 paths are super easy to build with this SDK. Despite that, it's not even a necessary step as the SDK handles it for you when you build the transaction data!
If you find this library useful, consider a donation and be part of the Supporter Community! :)
- ☕️☕️☕️ https://www.buymeacoffee.com/elnaril ☕️☕️☕️
- Ethereum compatible: 0xe32A121C89121bF8B829D6d88BBEA89e5d88f002
- Sponsoring: https://github.com/sponsors/Elnaril
Thank you to all supporters!! 🙏
Also, consider ⭐ starring ⭐ the repository and following the GitHub account and/or 𝕏 to get the next lib updates!