Skip to content

Commit

Permalink
[CL]: Fix incorrect bound check/chain halt vector (#5557)
Browse files Browse the repository at this point in the history
* repro panic trigger and fix bound check

* fix comments
  • Loading branch information
AlpinYukseloglu authored Jun 19, 2023
1 parent de18b81 commit 22a41f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions x/concentrated-liquidity/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions x/concentrated-liquidity/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 22a41f2

Please sign in to comment.