Skip to content

Decode_Uniswap_V3_Path

Elnaril edited this page Nov 2, 2023 · 1 revision

How to decode a Uniswap V3 path?

Let's say we want to decode this Uniswap V3 path:

uniswap_v3_path = b"\x1f\x98@\xa8]Z\xf5\xbf\x1d\x17b\xf9%\xbd\xad\xdcB\x01\xf9\x84\x00'\x10\xc0*\xaa9\xb2#\xfe\x8d\n\x0e\\O'\xea\xd9\x08<ul\xc2"

which is used to call the V3_SWAP_EXACT_OUT function (but that could also be V3_SWAP_EXACT_IN).

The RouterCodec class exposes the method decode.v3_path() which can be used to decode a given Uniswap V3 path:

from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()
decoded_path = codec.decode.v3_path("V3_SWAP_EXACT_OUT", uniswap_v3_path)

The result is a tuple, starting with the "in-token" and ending with the "out-token", with the pool fees between each pair:

(
    '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',  # in token
    10000,  # 1% fee
    '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'  # out token
)

That would also work if your path is a str instead of bytes, for ex:

uniswap_v3_path = "1f9840a85d5af5bf1d1762f925bdaddc4201f984002710c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
Clone this wiki locally