From 36387e882df4d4ed8faf19f07bb38c75429208d6 Mon Sep 17 00:00:00 2001 From: alpo Date: Sun, 18 Jun 2023 10:57:18 -0700 Subject: [PATCH 1/2] repro panic trigger and fix bound check --- x/concentrated-liquidity/math/math.go | 2 +- x/concentrated-liquidity/math/math_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index 48c020da9e3..fec8724ebe9 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -181,7 +181,7 @@ 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) { + } else if sqrtPrice.LT(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. liquidity0 := Liquidity0(amount0, sqrtPrice, sqrtPriceB) 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 { From 2945e889865dbe993ff061099a5cc3dbc41f0fec Mon Sep 17 00:00:00 2001 From: alpo Date: Sun, 18 Jun 2023 21:27:04 -0700 Subject: [PATCH 2/2] fix comments --- x/concentrated-liquidity/math/math.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/concentrated-liquidity/math/math.go b/x/concentrated-liquidity/math/math.go index fec8724ebe9..b5f9801fad9 100644 --- a/x/concentrated-liquidity/math/math.go +++ b/x/concentrated-liquidity/math/math.go @@ -182,8 +182,8 @@ func GetLiquidityFromAmounts(sqrtPrice, sqrtPriceA, sqrtPriceB sdk.Dec, amount0, // 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.LT(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. + // 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)