Skip to content

Build_Transaction

Elnaril edited this page Oct 13, 2026 · 1 revision

How to build a full transaction to the Universal Router ?

Instead of only building the input data with the build() method, the SDK can also build the full transaction with the build_transaction() method. It returns a transaction dictionary (TxParams) ready to be signed.

build_transaction() computes most of the transaction parameters (gas fees, chain id, nonce, deadline, ...), but they can also be provided. It requires the codec to have been created with a rpc endpoint or a Web3 instance.

Codec creation

from web3 import Web3
from uniswap_universal_router_decoder import RouterCodec
w3 = Web3(...)  # your web3 instance
codec = RouterCodec(w3=w3)

Usage

Let's say we want to build the full transaction to swap tokens on a V3 pool:

from uniswap_universal_router_decoder import FunctionRecipient, RouterCodec

codec = RouterCodec(w3)
trx_params = (
    codec.encode.chain().v3_swap_exact_in(
        FunctionRecipient.SENDER,  # the output tokens are sent to the transaction sender
        amount_in,  # in Wei
        amount_out_min,  # in Wei
        [
            in_token_address,
            pool_fee,
            out_token_address,
        ],
    ).build_transaction(
        sender_address,  # the 'from' address
        deadline=codec.get_default_deadline(),  # the swap deadline
    )
)

# then sign and send it with web3
raw_transaction = w3.eth.account.sign_transaction(trx_params, account.key).raw_transaction
trx_hash = w3.eth.send_raw_transaction(raw_transaction)

Parameters

  • sender (mandatory): the from address.
  • value: the quantity of ETH in Wei sent to the UR, for example when wrapping ETH before a swap.
  • trx_speed: a TransactionSpeed enum influencing the transaction rank in the block. Values are SLOW, AVERAGE, FAST (default) and FASTER.
  • max_fee_per_gas_limit: if the computed max_fee_per_gas is greater than this limit, a ValueError is raised, allowing you to stay in control. Default is 100 gwei.

How to use the trx_speed parameter:

from uniswap_universal_router_decoder import TransactionSpeed

.build_transaction(sender_address, trx_speed=TransactionSpeed.FASTER)

Custom gas fees

Instead of trx_speed, custom gas fees can be provided through the priority_fee and max_fee_per_gas parameters. In that case, trx_speed must be set to None, otherwise a ValueError is raised.

Other parameters

Other parameters can also be provided: gas_limit, chain_id, nonce, ur_address and block_identifier.

Asynchronous

With an AsyncRouterCodec, build_transaction() becomes asynchronous and must be awaited:

from uniswap_universal_router_decoder import AsyncRouterCodec

codec = AsyncRouterCodec(async_w3=async_w3)  # your AsyncWeb3 instance
trx_params = await (
    codec
        .encode
        .chain()
        .v3_swap_exact_in(
            FunctionRecipient.SENDER,
            amount_in,
            amount_out_min,
            [in_token_address, pool_fee, out_token_address],
        )
        .build_transaction(sender_address, deadline=codec.get_default_deadline())
)

Clone this wiki locally