Skip to content

Commit

Permalink
fix/test: separate min initialized and min current ticks; swap tests …
Browse files Browse the repository at this point in the history
…at min and max tick (osmosis-labs#5581)
  • Loading branch information
p0mvn authored Jun 21, 2023
1 parent 90ca18a commit 8f20c3c
Show file tree
Hide file tree
Showing 22 changed files with 373 additions and 136 deletions.
2 changes: 1 addition & 1 deletion tests/cl-go-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func main() {
}

func createManyRandomPositions(igniteClient cosmosclient.Client, poolId uint64, numPositions int) {
minTick, maxTick := cltypes.MinTick, cltypes.MaxTick
minTick, maxTick := cltypes.MinInitializedTick, cltypes.MaxTick
log.Println(minTick, " ", maxTick)
for i := 0; i < numPositions; i++ {
var (
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {

// Create 2 positions for address3: overlap together, overlap with 2 address1 positions, one position starts from minimum
chainANode.CreateConcentratedPosition(address3, "[-160000]", "[-20000]", fmt.Sprintf("10000000%s,10000000%s", denom0, denom1), 0, 0, poolID)
chainANode.CreateConcentratedPosition(address3, fmt.Sprintf("[%d]", cltypes.MinTick), "140000", fmt.Sprintf("10000000%s,10000000%s", denom0, denom1), 0, 0, poolID)
chainANode.CreateConcentratedPosition(address3, fmt.Sprintf("[%d]", cltypes.MinInitializedTick), "140000", fmt.Sprintf("10000000%s,10000000%s", denom0, denom1), 0, 0, poolID)

// Get newly created positions
positionsAddress1 := chainANode.QueryConcentratedPositions(address1)
Expand Down Expand Up @@ -278,7 +278,7 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
// First position third address
s.validateCLPosition(addr3position1, poolID, -160000, -20000)
// Second position third address
s.validateCLPosition(addr3position2, poolID, cltypes.MinTick, 140000)
s.validateCLPosition(addr3position2, poolID, cltypes.MinInitializedTick, 140000)

// Collect SpreadRewards

Expand Down
6 changes: 3 additions & 3 deletions x/concentrated-liquidity/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func runBenchmark(b *testing.B, testFunc func(b *testing.B, s *BenchTestSuite, p
tokenDesired0 := sdk.NewCoin(denom0, sdk.NewInt(100))
tokenDesired1 := sdk.NewCoin(denom1, sdk.NewInt(100))
tokensDesired := sdk.NewCoins(tokenDesired0, tokenDesired1)
_, _, _, _, _, _, err = clKeeper.CreatePosition(s.Ctx, clPoolId, s.TestAccs[0], tokensDesired, sdk.ZeroInt(), sdk.ZeroInt(), types.MinTick, types.MaxTick)
_, _, _, _, _, _, err = clKeeper.CreatePosition(s.Ctx, clPoolId, s.TestAccs[0], tokensDesired, sdk.ZeroInt(), sdk.ZeroInt(), types.MinInitializedTick, types.MaxTick)
noError(b, err)

pool, err := clKeeper.GetPoolById(s.Ctx, clPoolId)
Expand All @@ -133,7 +133,7 @@ func runBenchmark(b *testing.B, testFunc func(b *testing.B, s *BenchTestSuite, p
if denomIn == denom0 {
// Decreasing price so want to be below current tick
// minTick <= lowerTick <= currentTick
lowerTick = rand.Int63n(currentTick-types.MinTick+1) + types.MinTick
lowerTick = rand.Int63n(currentTick-types.MinInitializedTick+1) + types.MinInitializedTick
// lowerTick <= upperTick <= currentTick
upperTick = currentTick - rand.Int63n(int64(math.Abs(float64(currentTick-lowerTick))))
} else {
Expand Down Expand Up @@ -180,7 +180,7 @@ func runBenchmark(b *testing.B, testFunc func(b *testing.B, s *BenchTestSuite, p
// Setup numberOfPositions full range positions for deeper liquidity.
setupFullRangePositions := func() {
for i := 0; i < numberOfPositions; i++ {
lowerTick := types.MinTick
lowerTick := types.MinInitializedTick
upperTick := types.MaxTick
createPosition(lowerTick, upperTick)
}
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func (s *KeeperTestSuite) TestUpdateUptimeAccumulatorsToNow() {
qualifyingBalancerLiquidity = (sdk.OneDec().Sub(types.DefaultBalancerSharesDiscount)).Mul(qualifyingBalancerLiquidityPreDiscount)
qualifyingLiquidity = qualifyingLiquidity.Add(qualifyingBalancerLiquidity)

actualLiquidityAdded0, actualLiquidityAdded1, err := clPool.CalcActualAmounts(s.Ctx, types.MinTick, types.MaxTick, qualifyingBalancerLiquidity)
actualLiquidityAdded0, actualLiquidityAdded1, err := clPool.CalcActualAmounts(s.Ctx, types.MinInitializedTick, types.MaxTick, qualifyingBalancerLiquidity)
s.Require().NoError(err)
s.FundAcc(clPool.GetIncentivesAddress(), sdk.NewCoins(sdk.NewCoin(clPool.GetToken0(), actualLiquidityAdded0.TruncateInt()), sdk.NewCoin(clPool.GetToken1(), actualLiquidityAdded1.TruncateInt())))
}
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

var (
DefaultMinTick, DefaultMaxTick = types.MinTick, types.MaxTick
DefaultMinTick, DefaultMaxTick = types.MinInitializedTick, types.MaxTick
DefaultLowerPrice = sdk.NewDec(4545)
DefaultLowerTick = int64(30545000)
DefaultUpperPrice = sdk.NewDec(5500)
Expand Down
12 changes: 6 additions & 6 deletions x/concentrated-liquidity/math/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TickToPrice(tickIndex int64) (price sdk.Dec, err error) {
geometricExponentIncrementDistanceInTicks := sdkNineDec.Mul(PowTenInternal(-exponentAtPriceOne)).TruncateInt64()

// Check that the tick index is between min and max value
if tickIndex < types.MinTick {
return sdk.Dec{}, types.TickIndexMinimumError{MinTick: types.MinTick}
if tickIndex < types.MinInitializedTick {
return sdk.Dec{}, types.TickIndexMinimumError{MinTick: types.MinInitializedTick}
}
if tickIndex > types.MaxTick {
return sdk.Dec{}, types.TickIndexMaximumError{MaxTick: types.MaxTick}
Expand Down Expand Up @@ -114,8 +114,8 @@ func RoundDownTickToSpacing(tickIndex int64, tickSpacing int64) (int64, error) {

// Defense-in-depth check to ensure that the tick index is within the authorized range
// Should never get here.
if tickIndex > types.MaxTick || tickIndex < types.MinTick {
return 0, types.TickIndexNotWithinBoundariesError{ActualTick: tickIndex, MinTick: types.MinTick, MaxTick: types.MaxTick}
if tickIndex > types.MaxTick || tickIndex < types.MinInitializedTick {
return 0, types.TickIndexNotWithinBoundariesError{ActualTick: tickIndex, MinTick: types.MinInitializedTick, MaxTick: types.MaxTick}
}

return tickIndex, nil
Expand Down Expand Up @@ -218,8 +218,8 @@ func CalculateSqrtPriceToTick(sqrtPrice sdk.Dec) (tickIndex int64, err error) {
// We check this at max tick - 1 instead of max tick, since we expect the output to
// have some error that can push us over the tick boundary.
outOfBounds := false
if truncatedTick <= types.MinTick {
truncatedTick = types.MinTick + 1
if truncatedTick <= types.MinInitializedTick {
truncatedTick = types.MinInitializedTick + 1
outOfBounds = true
} else if truncatedTick >= types.MaxTick-1 {
truncatedTick = types.MaxTick - 2
Expand Down
70 changes: 35 additions & 35 deletions x/concentrated-liquidity/math/tick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// Note we get spot price exponent by counting the number of digits in the max spot price and subtracting 1.
closestPriceBelowMaxPriceDefaultTickSpacing = types.MaxSpotPrice.Sub(sdk.NewDec(10).PowerMut(uint64(len(types.MaxSpotPrice.TruncateInt().String()) - 1 - int(-types.ExponentAtPriceOne) - 1)))
// min tick + 10 ^ -expoentAtPriceOne
closestTickAboveMinPriceDefaultTickSpacing = sdk.NewInt(types.MinTick).Add(sdk.NewInt(10).ToDec().Power(uint64(types.ExponentAtPriceOne * -1)).TruncateInt())
closestTickAboveMinPriceDefaultTickSpacing = sdk.NewInt(types.MinInitializedTick).Add(sdk.NewInt(10).ToDec().Power(uint64(types.ExponentAtPriceOne * -1)).TruncateInt())
)

// use following equations to test testing vectors using sage
Expand Down Expand Up @@ -105,12 +105,12 @@ func (suite *ConcentratedMathTestSuite) TestTickToSqrtPrice() {
expectedPrice: types.MaxSpotPrice,
},
"Min tick and max k": {
tickIndex: types.MinTick,
tickIndex: types.MinInitializedTick,
expectedPrice: types.MinSpotPrice,
},
"error: tickIndex less than minimum": {
tickIndex: types.MinTick - 1,
expectedError: types.TickIndexMinimumError{MinTick: types.MinTick},
tickIndex: types.MinInitializedTick - 1,
expectedError: types.TickIndexMinimumError{MinTick: types.MinInitializedTick},
},
"error: tickIndex greater than maximum": {
tickIndex: 342000000 + 1,
Expand Down Expand Up @@ -234,25 +234,25 @@ func (suite *ConcentratedMathTestSuite) TestTicksToSqrtPrice() {
expectedUpperPrice: sdk.MustNewDecFromStr("10733"),
},
"Max tick and min k": {
lowerTickIndex: sdk.NewInt(types.MinTick),
lowerTickIndex: sdk.NewInt(types.MinInitializedTick),
upperTickIndex: sdk.NewInt(types.MaxTick),
expectedUpperPrice: types.MaxSpotPrice,
expectedLowerPrice: types.MinSpotPrice,
},
"error: lowerTickIndex less than minimum": {
lowerTickIndex: sdk.NewInt(types.MinTick - 1),
lowerTickIndex: sdk.NewInt(types.MinInitializedTick - 1),
upperTickIndex: sdk.NewInt(36073300),
expectedError: types.TickIndexMinimumError{MinTick: types.MinTick},
expectedError: types.TickIndexMinimumError{MinTick: types.MinInitializedTick},
},
"error: upperTickIndex greater than maximum": {
lowerTickIndex: sdk.NewInt(types.MinTick),
lowerTickIndex: sdk.NewInt(types.MinInitializedTick),
upperTickIndex: sdk.NewInt(types.MaxTick + 1),
expectedError: types.TickIndexMaximumError{MaxTick: types.MaxTick},
},
"error: provided lower tick and upper tick are same": {
lowerTickIndex: sdk.NewInt(types.MinTick),
upperTickIndex: sdk.NewInt(types.MinTick),
expectedError: types.InvalidLowerUpperTickError{LowerTick: sdk.NewInt(types.MinTick).Int64(), UpperTick: sdk.NewInt(types.MinTick).Int64()},
lowerTickIndex: sdk.NewInt(types.MinInitializedTick),
upperTickIndex: sdk.NewInt(types.MinInitializedTick),
expectedError: types.InvalidLowerUpperTickError{LowerTick: sdk.NewInt(types.MinInitializedTick).Int64(), UpperTick: sdk.NewInt(types.MinInitializedTick).Int64()},
},
}

Expand Down Expand Up @@ -422,14 +422,14 @@ func (suite *ConcentratedMathTestSuite) TestPriceToTickRoundDown() {
"tick spacing 100, MinSpotPrice, MinTick": {
price: types.MinSpotPrice,
tickSpacing: defaultTickSpacing,
tickExpected: types.MinTick,
tickExpected: types.MinInitializedTick,
},
"tick spacing 100, Spot price one tick above min, one tick above min -> MinTick": {
price: types.MinSpotPrice.Add(sdk.SmallestDec()),
tickSpacing: defaultTickSpacing,
// Since the tick should always be the closest tick below (and `smallestDec` isn't sufficient
// to push us into the next tick), we expect MinTick to be returned here.
tickExpected: types.MinTick,
tickExpected: types.MinInitializedTick,
},
"tick spacing 100, Spot price one tick below max, one tick below max -> MaxTick - 1": {
price: closestPriceBelowMaxPriceDefaultTickSpacing,
Expand Down Expand Up @@ -507,35 +507,35 @@ func (suite *ConcentratedMathTestSuite) TestTickToSqrtPricePriceToTick_InverseRe
},
"min spot price": {
price: types.MinSpotPrice,
tickExpected: types.MinTick,
tickExpected: types.MinInitializedTick,
},
"smallest + min price + tick": {
price: sdk.MustNewDecFromStr("0.000000000001000001"),
tickExpected: types.MinTick + 1,
tickExpected: types.MinInitializedTick + 1,
},
"min price increment 10^1": {
price: sdk.MustNewDecFromStr("0.000000000010000000"),
tickExpected: types.MinTick + (9 * 1e6),
tickExpected: types.MinInitializedTick + (9 * 1e6),
},
"min price increment 10^2": {
price: sdk.MustNewDecFromStr("0.000000000100000000"),
tickExpected: types.MinTick + (2 * 9 * 1e6),
tickExpected: types.MinInitializedTick + (2 * 9 * 1e6),
},
"min price increment 10^3": {
price: sdk.MustNewDecFromStr("0.000000001000000000"),
tickExpected: types.MinTick + (3 * 9 * 1e6),
tickExpected: types.MinInitializedTick + (3 * 9 * 1e6),
},
"min price increment 10^4": {
price: sdk.MustNewDecFromStr("0.000000010000000000"),
tickExpected: types.MinTick + (4 * 9 * 1e6),
tickExpected: types.MinInitializedTick + (4 * 9 * 1e6),
},
"min price increment 10^5": {
price: sdk.MustNewDecFromStr("0.000000100000000000"),
tickExpected: types.MinTick + (5 * 9 * 1e6),
tickExpected: types.MinInitializedTick + (5 * 9 * 1e6),
},
"min price increment 10^6": {
price: sdk.MustNewDecFromStr("0.000001000000000000"),
tickExpected: types.MinTick + (6 * 9 * 1e6),
tickExpected: types.MinInitializedTick + (6 * 9 * 1e6),
},
"min price * increment 10^11": {
price: sdk.MustNewDecFromStr("0.100000000000000000"),
Expand Down Expand Up @@ -659,7 +659,7 @@ func (suite *ConcentratedMathTestSuite) TestTickToPrice_ErrorCases() {
tickIndex: types.MaxTick + 1,
},
"tick index is less than min tick": {
tickIndex: types.MinTick - 1,
tickIndex: types.MinInitializedTick - 1,
},
}
for name, tc := range testCases {
Expand Down Expand Up @@ -749,9 +749,9 @@ func (s *ConcentratedMathTestSuite) TestSqrtPriceToTickRoundDownSpacing() {
s.Require().NoError(err)
_, sqpMaxTickSubOne, err := math.TickToSqrtPrice(types.MaxTick - 1)
s.Require().NoError(err)
_, sqpMinTickPlusOne, err := math.TickToSqrtPrice(types.MinTick + 1)
_, sqpMinTickPlusOne, err := math.TickToSqrtPrice(types.MinInitializedTick + 1)
s.Require().NoError(err)
_, sqpMinTickPlusTwo, err := math.TickToSqrtPrice(types.MinTick + 2)
_, sqpMinTickPlusTwo, err := math.TickToSqrtPrice(types.MinInitializedTick + 2)
s.Require().NoError(err)

testCases := map[string]struct {
Expand Down Expand Up @@ -812,7 +812,7 @@ func (s *ConcentratedMathTestSuite) TestSqrtPriceToTickRoundDownSpacing() {
"sqrt price exactly equal to min sqrt price": {
sqrtPrice: types.MinSqrtPrice,
tickSpacing: defaultTickSpacing,
tickExpected: types.MinTick,
tickExpected: types.MinInitializedTick,
},
"sqrt price equal to max sqrt price minus one ULP": {
sqrtPrice: types.MaxSqrtPrice.Sub(sdk.SmallestDec()),
Expand All @@ -837,36 +837,36 @@ func (s *ConcentratedMathTestSuite) TestSqrtPriceToTickRoundDownSpacing() {
"sqrt price corresponds exactly to min tick + 1 (tick spacing 1)": {
sqrtPrice: sqpMinTickPlusOne,
tickSpacing: 1,
tickExpected: types.MinTick + 1,
tickExpected: types.MinInitializedTick + 1,
},
"sqrt price corresponds exactly to min tick + 1 minus 1 ULP (tick spacing 1)": {
// Calculated using TickToSqrtPrice(types.MinTick + 1) - 1 ULP
// Calculated using TickToSqrtPrice(types.MinInitializedTick + 1) - 1 ULP
sqrtPrice: sqpMinTickPlusOne.Sub(sdk.SmallestDec()),
tickSpacing: 1,
tickExpected: types.MinTick,
tickExpected: types.MinInitializedTick,
},
"sqrt price corresponds exactly to min tick + 1 plus 1 ULP (tick spacing 1)": {
// Calculated using TickToSqrtPrice(types.MinTick + 1) + 1 ULP
// Calculated using TickToSqrtPrice(types.MinInitializedTick + 1) + 1 ULP
sqrtPrice: sqpMinTickPlusOne.Add(sdk.SmallestDec()),
tickSpacing: 1,
tickExpected: types.MinTick + 1,
tickExpected: types.MinInitializedTick + 1,
},
"sqrt price corresponds exactly to min tick + 2 (tick spacing 1)": {
sqrtPrice: sqpMinTickPlusTwo,
tickSpacing: 1,
tickExpected: types.MinTick + 2,
tickExpected: types.MinInitializedTick + 2,
},
"sqrt price corresponds exactly to min tick + 2 plus 1 ULP (tick spacing 1)": {
// Calculated using TickToSqrtPrice(types.MinTick + 2) + 1 ULP
// Calculated using TickToSqrtPrice(types.MinInitializedTick + 2) + 1 ULP
sqrtPrice: sqpMinTickPlusTwo.Add(sdk.SmallestDec()),
tickSpacing: 1,
tickExpected: types.MinTick + 2,
tickExpected: types.MinInitializedTick + 2,
},
"sqrt price corresponds exactly to min tick + 2 minus 1 ULP (tick spacing 1)": {
// Calculated using TickToSqrtPrice(types.MinTick + 2) - 1 ULP
// Calculated using TickToSqrtPrice(types.MinInitializedTick + 2) - 1 ULP
sqrtPrice: sqpMinTickPlusTwo.Sub(sdk.SmallestDec()),
tickSpacing: 1,
tickExpected: types.MinTick + 1,
tickExpected: types.MinInitializedTick + 1,
},
}
for name, tc := range testCases {
Expand Down
4 changes: 2 additions & 2 deletions x/concentrated-liquidity/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ func (p *Pool) ApplySwap(newLiquidity sdk.Dec, newCurrentTick int64, newCurrentS
}

// Check if the new tick provided is within boundaries of the pool's precision factor.
if newCurrentTick < types.MinTick || newCurrentTick > types.MaxTick {
if newCurrentTick < types.MinCurrentTick || newCurrentTick > types.MaxTick {
return types.TickIndexNotWithinBoundariesError{
MaxTick: types.MaxTick,
MinTick: types.MinTick,
MinTick: types.MinCurrentTick,
ActualTick: newCurrentTick,
}
}
Expand Down
18 changes: 14 additions & 4 deletions x/concentrated-liquidity/model/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,22 @@ func (s *ConcentratedPoolTestSuite) TestApplySwap() {
expectErr: nil,
},
{
name: "new tick is equal to min tick",
name: "new tick is equal to min initialized tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: types.MinTick,
newTick: types.MinInitializedTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: nil,
},
{
name: "new tick is equal to min current tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: types.MinCurrentTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: nil,
},
Expand All @@ -387,7 +397,7 @@ func (s *ConcentratedPoolTestSuite) TestApplySwap() {
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: types.TickIndexNotWithinBoundariesError{
MaxTick: types.MaxTick,
MinTick: types.MinTick,
MinTick: types.MinCurrentTick,
ActualTick: math.MaxInt64,
},
},
Expand All @@ -401,7 +411,7 @@ func (s *ConcentratedPoolTestSuite) TestApplySwap() {
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: types.TickIndexNotWithinBoundariesError{
MaxTick: types.MaxTick,
MinTick: types.MinTick,
MinTick: types.MinCurrentTick,
ActualTick: math.MinInt64,
},
},
Expand Down
6 changes: 3 additions & 3 deletions x/concentrated-liquidity/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (k Keeper) SetPosition(ctx sdk.Context,
}

// If position is full range, update the pool ID to total full range liquidity mapping.
if lowerTick == types.MinTick && upperTick == types.MaxTick {
if lowerTick == types.MinInitializedTick && upperTick == types.MaxTick {
err := k.updateFullRangeLiquidityInPool(ctx, poolId, liquidity)
if err != nil {
return err
Expand Down Expand Up @@ -413,7 +413,7 @@ func (k Keeper) CreateFullRangePosition(ctx sdk.Context, poolId uint64, owner sd
}

// Create a full range (min to max tick) concentrated liquidity position.
positionId, amount0, amount1, liquidity, _, _, err = k.createPosition(ctx, concentratedPool.GetId(), owner, coins, sdk.ZeroInt(), sdk.ZeroInt(), types.MinTick, types.MaxTick)
positionId, amount0, amount1, liquidity, _, _, err = k.createPosition(ctx, concentratedPool.GetId(), owner, coins, sdk.ZeroInt(), sdk.ZeroInt(), types.MinInitializedTick, types.MaxTick)
if err != nil {
return 0, sdk.Int{}, sdk.Int{}, sdk.Dec{}, err
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func (k Keeper) mintSharesAndLock(ctx sdk.Context, concentratedPoolId, positionI
if err != nil {
return 0, sdk.Coins{}, err
}
if position.LowerTick != types.MinTick || position.UpperTick != types.MaxTick {
if position.LowerTick != types.MinInitializedTick || position.UpperTick != types.MaxTick {
return 0, sdk.Coins{}, types.PositionNotFullRangeError{PositionId: positionId, LowerTick: position.LowerTick, UpperTick: position.UpperTick}
}

Expand Down
Loading

0 comments on commit 8f20c3c

Please sign in to comment.