5

I am trying to create an instance of a v3 uniswap Pool. I am using hardhat and a fork of mumbai testnet.

When I try to create the pool instance :

const poolExample = new Pool(
  TokenA,
  TokenB,
  immutables.fee,
  state.sqrtPriceX96.toString(),
  state.liquidity.toString(),
  state.tick
);

I get the following error :

Error: Invariant failed: PRICE_BOUNDS

The stack is :

Error: Invariant failed: PRICE_BOUNDS
  at invariant (node_modules/tiny-invariant/dist/tiny-invariant.cjs.js:14:11)
  at new Pool (node_modules/@uniswap/v3-sdk/src/entities/pool.ts:71:5)
  at Context.<anonymous> (test/Uniswap-test.js:134:25)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)

Any hints on tracing the origin of the error ?

The paramters of the pool have the following values :

fee : 3000
state.sqrtPriceX96 : 0
state.liquidity: 0
state.tick: 0
gpasse
  • 4,380
  • 7
  • 44
  • 75
  • the origin is here but i don't know how to fix? https://github.com/Uniswap/v3-sdk/blob/4459663920b558cc100239081a1d3782ee512264/src/entities/pool.ts#L71 – Rasoul Salehi May 31 '22 at 07:25

1 Answers1

0

I think the problem is where you create the state (PoolState), and not the Pool.

sqrtPriceX96 can't be 0, those parameters need to be set correctly

If you followed the uniswap tutorial, you are probably calling the getPoolState correctly, but you may be using a Testnet as the rpc provider, that may be returning incorrect data about the contract. Try the same code using ethereum mainnet and see if it works.

Just in case, this is how you should get the pool state:


async function getPoolState() {
  const liquidity = await poolContract.liquidity();
  const slot = await poolContract.slot0();

  const PoolState: State = {
    liquidity,
    sqrtPriceX96: slot[0],
    tick: slot[1],
    observationIndex: slot[2],
    observationCardinality: slot[3],
    observationCardinalityNext: slot[4],
    feeProtocol: slot[5],
    unlocked: slot[6],
  };

  return PoolState;
}

Then you use it like this:

  const immutables = await getPoolImmutables();
  const state = await getPoolState();

  const TokenA = new Token(3, immutables.token0, 18, "DAI", "Dai Stablecoin");
  const TokenB = new Token(3, immutables.token1, 18, "WETH", "Wrapped Ether");

  const poolExample = new Pool(
    TokenA,
    TokenB,
    immutables.fee,
    state.sqrtPriceX96.toString(),
    state.liquidity.toString(),
    state.tick
  );
femanzo
  • 179
  • 1
  • 7