From 22a41f2e1962dfcab3ba78a879af88b1b37c42ad Mon Sep 17 00:00:00 2001 From: alpo <62043214+AlpinYukseloglu@users.noreply.github.com> Date: Mon, 19 Jun 2023 06:01:01 -0700 Subject: [PATCH] [CL]: Fix incorrect bound check/chain halt vector (#5557) * repro panic trigger and fix bound check * fix comments --- x/concentrated-liquidity/math/math.go | 6 +++--- x/concentrated-liquidity/math/math_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index 48c020da9e3..b5f9801fad9 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -181,9 +181,9 @@ func GetLiquidityFromAmounts(sqrtPrice, sqrtPriceA, sqrtPriceB sdk.Dec, amount0, if sqrtPrice.LTE(sqrtPriceA) { // If the current price is less than or equal to the lower tick, then we use the liquidity0 formula. liquidity = Liquidity0(amount0, sqrtPriceA, sqrtPriceB) - } else if sqrtPrice.LTE(sqrtPriceB) { - // If the current price is between the lower and upper ticks (non-inclusive of the lower tick but inclusive of the upper tick), - // then we use the minimum of the liquidity0 and liquidity1 formulas. + } else if sqrtPrice.LT(sqrtPriceB) { + // If the current price is between the lower and upper ticks (exclusive of both the lower and upper ticks, + // as both would trigger a division by zero), then we use the minimum of the liquidity0 and liquidity1 formulas. liquidity0 := Liquidity0(amount0, sqrtPrice, sqrtPriceB) liquidity1 := Liquidity1(amount1, sqrtPrice, sqrtPriceA) liquidity = sdk.MinDec(liquidity0, liquidity1) diff --git a/x/concentrated-liquidity/math/math_test.go b/x/concentrated-liquidity/math/math_test.go index 30da7a6dbfc..5fbd329cd75 100644 --- a/x/concentrated-liquidity/math/math_test.go +++ b/x/concentrated-liquidity/math/math_test.go @@ -413,6 +413,16 @@ func (suite *ConcentratedMathTestSuite) TestGetLiquidityFromAmounts() { expectedLiquidity0: sdk.MustNewDecFromStr("7.706742302257039729"), expectedLiquidity1: sdk.MustNewDecFromStr("4.828427124746190095"), }, + "current sqrt price on upper bound": { + currentSqrtP: sqrt5500, + sqrtPHigh: sqrt5500, + sqrtPLow: sqrt4545, + amount0Desired: sdk.ZeroInt(), + amount1Desired: sdk.NewInt(1000000), + // Liquidity1 = amount1 / (sqrtPriceB - sqrtPriceA) + // https://www.wolframalpha.com/input?i=1000000%2F%2874.161984870956629487-67.416615162732695594%29 + expectedLiquidity: "148249.842967213952971325", + }, } for name, tc := range testCases {