不变失败:PRICE_BOUNDS 是什么意思?

gpa*_*sse 6 hardhat uniswap

我正在尝试创建 v3 uniswap 池的实例。我正在使用安全帽和孟买测试网的分支。

当我尝试创建池实例时:

const poolExample = new Pool(
  TokenA,
  TokenB,
  immutables.fee,
  state.sqrtPriceX96.toString(),
  state.liquidity.toString(),
  state.tick
);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Error: Invariant failed: PRICE_BOUNDS
Run Code Online (Sandbox Code Playgroud)

堆栈是:

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)
Run Code Online (Sandbox Code Playgroud)

有关追踪错误根源的任何提示吗?

池的参数具有以下值:

fee : 3000
state.sqrtPriceX96 : 0
state.liquidity: 0
state.tick: 0
Run Code Online (Sandbox Code Playgroud)

fem*_*nzo 0

我认为问题在于您创建状态(PoolState)的位置,而不是池。

sqrtPriceX96不能为0,这些参数需要正确设置

如果您遵循了 uniswap 教程,您可能会正确调用 getPoolState,但您可能会使用 Testnet 作为 rpc 提供程序,这可能会返回有关合约的错误数据。使用以太坊主网尝试相同的代码,看看它是否有效。

为了以防万一,您应该通过以下方式获取池状态:


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;
}
Run Code Online (Sandbox Code Playgroud)

然后你像这样使用它:

  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
  );
Run Code Online (Sandbox Code Playgroud)