Uniswap / v3-sdk

🛠 An SDK for building applications on top of Uniswap V3
MIT License
544 stars 416 forks source link

Pool getOutputAmount calculation incorrect for some cases #159

Open z-hao-wang opened 1 year ago

z-hao-wang commented 1 year ago

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 commented 1 year ago

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