Skip to content

Chain_Multiple_Commands

Elnaril edited this page Oct 13, 2026 · 1 revision

How to chain several commands in the same transaction ?

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_data

Starting with encode()

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

Ending words

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.

Clone this wiki locally