v3-sdk icon indicating copy to clipboard operation
v3-sdk copied to clipboard

Pool getOutputAmount calculation incorrect for some cases

Open z-hao-wang opened this issue 3 years ago • 1 comments

I've noticed for some pools, the getOutputAmount produces different results than quoter v2

https://github.com/z-hao-wang/v3-core-debug/pull/1 this branch can reproduce the error I found The quote results are same for config like this

{
    description: 'high fee, 1:1 price, default config',
    feeAmount: FeeAmount.HIGH,
    tickSpacing: TICK_SPACINGS[FeeAmount.HIGH],
    startingPrice: encodePriceSqrt(1, 1),
    positions: [
      {
        tickLower: getMinTick(TICK_SPACINGS[FeeAmount.HIGH]),
        tickUpper: getMaxTick(TICK_SPACINGS[FeeAmount.HIGH]),
        liquidity: expandTo18Decimals(2),
      },
    ],
    swapTests: [{
      zeroForOne: true,
      exactOut: false,
      amount0: expandTo18Decimals(1),
    }],
  }

but it doesn't work for config like this

{
    description: 'high fee, 1:1 price, buggy swap calculation',
    feeAmount: FeeAmount.HIGH,
    tickSpacing: TICK_SPACINGS[FeeAmount.HIGH],
    startingPrice: BigNumber.from('3905891053926514387903925684'), // tick at -60200
    positions: [
      {
        tickLower: -60200,
        tickUpper: 138200,
        liquidity: '1505426792435356595487',
      },
    ],
    swapTests: [{
      zeroForOne: true,
      exactOut: false,
      amount0: expandTo18Decimals(1),
    }],
  }

z-hao-wang avatar Dec 05 '22 02:12 z-hao-wang

I found this is due to amount remaining isn't returned. The pool doesn't get enough liquidity. see below pr that fixes it.

https://github.com/QuantSatoshi/v3-sdk/pull/1/files

z-hao-wang avatar Jan 20 '23 02:01 z-hao-wang