Repository navigation
Encode_Wrap_Unwrap_Eth
Let's say we want to convert ETH to WETH (or the opposite) through the Universal Router (UR).
No rpc access is required to encode these commands:
from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()WRAP_ETH converts ETH into WETH. The ETH to wrap comes from the value field of the transaction:
from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec
codec = RouterCodec()
encoded_data = codec.encode.chain().wrap_eth(
FunctionRecipient.SENDER, # the WETH are sent to the transaction sender
amount, # the amount of ETH to wrap, in Wei
).build(codec.get_default_deadline()) # unix timestamp after which the trx is not valid any more
# then in your transaction dict:
transaction["data"] = encoded_data
# you can now sign and send the transaction to the URUNWRAP_WETH converts WETH held by the UR back into ETH:
encoded_data = codec.encode.chain().unwrap_weth(
FunctionRecipient.SENDER, # the ETH are sent to the transaction sender
amount_min, # the min amount of WETH to unwrap, in Wei (0 to unwrap everything)
).build(codec.get_default_deadline()) # unix timestamp after which the trx is not valid any moreFor both functions, FunctionRecipient can be:
-
FunctionRecipient.SENDER: the recipient is the transaction sender. -
FunctionRecipient.ROUTER: the recipient is the UR itself. This is useful to chain a wrap before a swap in the same transaction. -
FunctionRecipient.CUSTOM: the recipient is provided as an extra argument.
A common pattern is to chain WRAP_ETH, a swap and UNWRAP_WETH in the same transaction, to swap ETH for a token without having to hold WETH first. See How to chain several commands in the same transaction ?.
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!