Repository navigation
Chain_Multiple_Commands
The Universal Router allows several functions to be executed in the same transaction, and the SDK supports it for all the supported functions: the encoding methods can simply be chained one after the other.
The chaining starts with the encode.chain() method and ends with the build() one, which returns the encoded data to be included in the transaction. The commands are executed in the order they are chained.
Let's say we want to give a Permit2 allowance and swap tokens in the same transaction:
from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec
codec = RouterCodec()
# Permit signature
data, signable_message = codec.create_permit2_signable_message(
token_address,
amount,
expiration,
nonce,
ur_address,
deadline,
chain_id,
)
signed_message = account.sign_message(signable_message) # where account is your LocalAccount
# Permit + swap encoding
path = [in_token_address, out_token_address]
encoded_data = (
codec
.encode
.chain()
.permit2_permit(data, signed_message)
.v2_swap_exact_in(FunctionRecipient.SENDER, amount_in, amount_out_min, path)
.build(deadline)
)
# then in your transaction dict:
transaction["data"] = encoded_dataSince v3.0.0, the chaining can also be started with encode() instead of encode.chain(), making the code a bit more concise:
encoded_data = (
codec
.encode()
.permit2_permit(data, signed_message)
.v2_swap_exact_in(FunctionRecipient.SENDER, amount_in, amount_out_min, path)
.build(deadline)
)Any number of supported functions can be chained this way. For example, WRAP_ETH, a swap and UNWRAP_WETH can be chained to swap ETH for a token in a single 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!