Skip to content

Encode_Uniswap_V4_Pool_Id

Elnaril edited this page Apr 25, 2025 · 1 revision

How to encode a Uniswap V4 pool id?

Let's say we want to encode the id of the V4 WBTC/USDC pool with a 0.3% fee (= 3000), tick space of 60:

On Ethereum we have the following addresses:

  • WBTC: 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599
  • USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

Pool Key

First, we need to build the pool key.

With no hook:

from uniswap_universal_router_decoder import RouterCodec
codec = RouterCodec()
pool_key = codec.encode.v4_pool_key('0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 3000, 60)

The Uniswap protocol needs the token addresses to be ordered, but no worries, the codec.encode.v4_pool_key() method takes care of it.

With a hook:

The general call to codec.encode.v4_pool_key() is done as follow:

pool_key = codec.encode.v4_pool_key(
    currency_0_address,
    currency_1_address,
    fee,
    tick_spacing,
    hook_address,  # or 0x0000000000000000000000000000000000000000 if no hook
)

Pool Id

And now, we can encode the pool id as follow:

pool_id = codec.encode.v4_pool_id(pool_key)

And print it as an hexadecimal string:

print(pool_id.hex())

b98437c7ba28c6590dd4e1cc46aa89eed181f97108e5b6221730d41347bc817f
Clone this wiki locally