Skip to content

Encode_Wrap_Unwrap_Eth

Elnaril edited this page Oct 13, 2026 · 1 revision

How to encode an ETH wrap or unwrap ?

Let's say we want to convert ETH to WETH (or the opposite) through the Universal Router (UR).

Codec creation

No rpc access is required to encode these commands:

from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()

WRAP_ETH

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 UR

UNWRAP_WETH

UNWRAP_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 more

Recipients

For 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.

Ending words

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 ?.

Clone this wiki locally