0

In Uniswap v3 you could get the pool data by calling the read-only functions (public variables) on a pool (fee, tickSpacing, token0, token1).

Since Uniswap v4 implements a singleton design where all pools are managed by one contract, how can I get the same data?

1 Answer 1

2

In Uniswap v4 you have to call the poolKeys function on the positionManager contract, which takes only one argument (poolId of type bytes25) and returns an array which contains currency0, currency, fee, tickSpacing, and hooks.

Here is a function in JavaScript:

import { slice } from "viem"; // Get the keys of a pool (currency0, currency1, fee, tickSpacing, hooks) export const getPoolKeys = async (positionManager, poolId) => { try { const poolIdBytes25 = slice(poolId, 0, 25); const [currency0, currency1, fee, tickSpacing, hooks] = await positionManager.read.poolKeys([poolIdBytes25]); return { currency0, currency1, fee, tickSpacing, hooks, }; } catch (error) { console.error("Error fetching pool keys:", error); return null; } }; 

In the function above poolId is taken as bytes32 and sliced using the slice function from viem.

You can find the addresses of all positionManager contracts in Uniswap v4 docs.

7
  • Excuse me, could you please clarify, how you're getting poolId, knowing only pool address? Also, is positionManager you pass to getPoolKeys actually result of calling new ethers.Contract() or something else? Commented Apr 8 at 5:49
  • 1
    Uniswap v4 implements a singleton design, which means that every pool is stored as a struct at the same address, which is the address of the PoolManager contract. PositionManager is a unique contract on every chain. You can find the list of addresses in the Uniswap docs docs.uniswap.org/contracts/v4/deployments. In the case of Ethereum, positionManager is deployed to 0xbD216513d74C8cf14cf4747E6AaA6420FF64ee9e. I explained how to get the poolId in this post (also created a sample repo): ethereum.stackexchange.com/questions/168549/…. Commented Apr 8 at 18:04
  • Thank you for an example! However, that example is related to smart contract code. I am a bit confused because docs and examples are jumping from solidity to js, and it's hard for newbie to understand full picture. Let's consider any existing V4 pool, for example this one: app.uniswap.org/explore/pools/ethereum/… And I would like to check, what liquidity and prices are available there. Examples are referring to poolId: docs.uniswap.org/contracts/v4/guides/read-pool-state Commented Apr 10 at 12:07
  • To build poolId, I need to apply the same function as in your *.sol example, just in JS. However, PoolKey is made of currency0, currency1, fee, tickSpacing and hooks. And it's really confusing, because there is no place to define V4 pool I am interested in. Moreover, I have no idea what fee and tickSpacing is relevant. I am not going to create new position, I just want to read what is available. Commented Apr 10 at 12:07
  • 1
    I edited the linked post with a JS implementation. To clarify your dilemma, you use Solidity for onchain implementations and JS for offchain interactions (such as a frontend). To read the state of these contracts, you have to provide the fee and tickSpacing even though you don't intend to create/manage any positions. It is like providing a street name and a number instead of a number only (there are multiple houses in a single street). Commented Apr 11 at 17:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.