Skip to content

Encode_V2_Swap

Elnaril edited this page Oct 13, 2026 · 1 revision

How to encode a Uniswap V2 swap ?

Let's say we want to swap tokens on a Uniswap V2 pool through the Universal Router (UR).

⚠ A correct token allowance must have been set before sending such a transaction, either directly to the UR or through the Permit2 flow.

Codec creation

No rpc access is required to encode a swap:

from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()

V2_SWAP_EXACT_IN

To swap tokens with an exact in_amount:

from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec

codec = RouterCodec()
encoded_data = codec.encode.chain().v2_swap_exact_in(
        FunctionRecipient.SENDER,  # the output tokens are sent to the transaction sender
        amount_in,  # the exact amount of tokens to send, in Wei
        amount_out_min,  # the minimum amount of tokens to receive, in Wei
        [
            in_token_address,  # checksum address of the token sent to the UR
            out_token_address,  # checksum address of the token received from the UR
        ],
    ).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 UR

V2_SWAP_EXACT_IN_FROM_BALANCE

When the exact in_amount is not known when the transaction is built, typically because it comes from a previous swap output, the v2_swap_exact_in_from_balance() method can be used. The input tokens are then taken from the UR balance, so no amount_in is needed:

encoded_data = codec.encode.chain().v2_swap_exact_in_from_balance(
        FunctionRecipient.SENDER,  # the output tokens are sent to the transaction sender
        amount_out_min,  # the minimum amount of tokens to receive, in Wei
        [
            in_token_address,  # checksum address of the token sent to the UR
            out_token_address,  # checksum address of the token received from the UR
        ],
    ).build(codec.get_default_deadline())  # unix timestamp after which the trx is not valid any more

This is typically chained after a V2_SWAP_EXACT_IN or V3_SWAP_EXACT_IN command, so that the second swap uses the tokens bought by the first one. See How to chain several commands in the same transaction ?.

V2_SWAP_EXACT_OUT

To swap tokens with an exact out_amount:

encoded_data = codec.encode.chain().v2_swap_exact_out(
        FunctionRecipient.SENDER,  # the input tokens are taken from the transaction sender
        amount_out,  # the exact amount of tokens to receive, in Wei
        amount_in_max,  # the maximum amount of tokens to send, in Wei
        [
            in_token_address,  # checksum address of the token sent to the UR
            out_token_address,  # checksum address of the token received from the UR
        ],
    ).build(codec.get_default_deadline())  # unix timestamp after which the trx is not valid any more

Multi-hop swaps

To swap through several pools, see How to encode a multi-hop swap ?.

Ending words

Only the input data is built here. See How to build a full transaction to the Universal Router ? to build the whole transaction.

A full working example is available in this tutorial.

Clone this wiki locally