Skip to content

Decode_Transaction_Input

Elnaril edited this page Nov 2, 2023 · 3 revisions

How to decode a transaction input data?

Let's say we already have the input data of a transaction and we want to decode it. For example:

input_data = "0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000065250c4700000000000000000000000000000000000000000000000000000000000000020b080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000997a2bce4c00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000997a2bce4c0000000000000000000000000000000000000000000000000000da929373f5cb95100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000001f9840a85d5af5bf1d1762f925bdaddc4201f984"

Codec creation

In this case we don't need to instantiate the codec with a rpc endpoint or a Web3 instance. We can create it as simply as:

from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()

Codec usage

Now that the codec is instantiated, we can call its method decode.function_input() like that:

decoded_input_data = codec.decode.function_input(input_data)

Result

Here is the value of decoded_input_data:

(
    <Function execute(bytes,bytes[],uint256)>,
    {
        'commands': b'\x0b\x08',  # the list of commands sent to the UR
        'inputs': [  # the inputs used for each command
            (
                <Function WRAP_ETH(address,uint256)>,  # the function corresponding to the first command
                {                                      # and its parameters
                    'recipient': '0x0000000000000000000000000000000000000002',  # code indicating the recipient of this command is the router
                    'amountMin': 2700000000000000  # the amount in WEI to wrap
                }
            ),
            (
                <Function V2_SWAP_EXACT_IN(address,uint256,uint256,address[],bool)>,  # the function corresponding to the second command
                {                                                                     # and its parameters
                    'recipient': '0x0000000000000000000000000000000000000001',  # code indicating the sender will receive the output of this command
                    'amountIn': 2700000000000000,  # the exact amount to send
                    'amountOutMin': 984363310820145489,  # the min amount expected to receive
                    'path': [  # The V2 path
                                '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
                                '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'
                    ],
                    'payerIsSender': False  # a bool indicating if the input tokens come from the sender or are already in the UR
                }
            )
        ],
        'deadline': 1696926791  # The deadline after which the transaction is not valid any more.
    }
)

To get the function names:

decoded_input_data[0].fn_name  # "execute"
decoded_input_data[1]['inputs'][0][0].fn_name  # "WRAP_ETH"
decoded_input_data[1]['inputs'][1][0].fn_name  # "V2_SWAP_EXACT_IN"
Clone this wiki locally