-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CL]: Increase min tick past canonical tick range #5551
Changes from 13 commits
4679405
f34cbaf
a9de3ef
4984fd4
8b3670d
3471bfb
1bea678
8675792
28409d2
b84aef2
bb6d2b9
aca6f22
a1b7c5c
749212c
bc6a4e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,12 +105,12 @@ func (suite *ConcentratedMathTestSuite) TestTickToSqrtPrice() { | |
expectedPrice: types.MaxSpotPrice, | ||
}, | ||
"Min tick and max k": { | ||
tickIndex: -162000000, | ||
tickIndex: types.MinTick, | ||
expectedPrice: types.MinSpotPrice, | ||
}, | ||
"error: tickIndex less than minimum": { | ||
tickIndex: -162000000 - 1, | ||
expectedError: types.TickIndexMinimumError{MinTick: -162000000}, | ||
tickIndex: types.MinTick - 1, | ||
expectedError: types.TickIndexMinimumError{MinTick: types.MinTick}, | ||
}, | ||
"error: tickIndex greater than maximum": { | ||
tickIndex: 342000000 + 1, | ||
|
@@ -234,25 +234,25 @@ func (suite *ConcentratedMathTestSuite) TestTicksToSqrtPrice() { | |
expectedUpperPrice: sdk.MustNewDecFromStr("10733"), | ||
}, | ||
"Max tick and min k": { | ||
lowerTickIndex: sdk.NewInt(-162000000), | ||
upperTickIndex: sdk.NewInt(342000000), | ||
lowerTickIndex: sdk.NewInt(types.MinTick), | ||
upperTickIndex: sdk.NewInt(types.MaxTick), | ||
expectedUpperPrice: types.MaxSpotPrice, | ||
expectedLowerPrice: types.MinSpotPrice, | ||
}, | ||
"error: lowerTickIndex less than minimum": { | ||
lowerTickIndex: sdk.NewInt(-162000000 - 1), | ||
lowerTickIndex: sdk.NewInt(types.MinTick - 1), | ||
upperTickIndex: sdk.NewInt(36073300), | ||
expectedError: types.TickIndexMinimumError{MinTick: -162000000}, | ||
expectedError: types.TickIndexMinimumError{MinTick: types.MinTick}, | ||
}, | ||
"error: upperTickIndex greater than maximum": { | ||
lowerTickIndex: sdk.NewInt(-162000000), | ||
upperTickIndex: sdk.NewInt(342000000 + 1), | ||
expectedError: types.TickIndexMaximumError{MaxTick: 342000000}, | ||
lowerTickIndex: sdk.NewInt(types.MinTick), | ||
upperTickIndex: sdk.NewInt(types.MaxTick + 1), | ||
expectedError: types.TickIndexMaximumError{MaxTick: types.MaxTick}, | ||
}, | ||
"error: provided lower tick and upper tick are same": { | ||
lowerTickIndex: sdk.NewInt(-162000000), | ||
upperTickIndex: sdk.NewInt(-162000000), | ||
expectedError: types.InvalidLowerUpperTickError{LowerTick: sdk.NewInt(-162000000).Int64(), UpperTick: sdk.NewInt(-162000000).Int64()}, | ||
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()}, | ||
}, | ||
} | ||
|
||
|
@@ -425,9 +425,11 @@ func (suite *ConcentratedMathTestSuite) TestPriceToTickRoundDown() { | |
tickExpected: types.MinTick, | ||
}, | ||
"tick spacing 100, Spot price one tick above min, one tick above min -> MinTick": { | ||
price: types.MinSpotPrice.Add(sdk.SmallestDec()), | ||
tickSpacing: defaultTickSpacing, | ||
tickExpected: closestTickAboveMinPriceDefaultTickSpacing.Int64(), | ||
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, | ||
}, | ||
"tick spacing 100, Spot price one tick below max, one tick below max -> MaxTick - 1": { | ||
price: closestPriceBelowMaxPriceDefaultTickSpacing, | ||
|
@@ -505,49 +507,41 @@ func (suite *ConcentratedMathTestSuite) TestTickToSqrtPricePriceToTick_InverseRe | |
}, | ||
"min spot price": { | ||
price: types.MinSpotPrice, | ||
tickExpected: -162000000, | ||
tickExpected: types.MinTick, | ||
}, | ||
"smallest + min price increment": { | ||
price: sdk.MustNewDecFromStr("0.000000000000000002"), | ||
tickExpected: -161000000, | ||
"smallest + min price + tick": { | ||
price: sdk.MustNewDecFromStr("0.000000000001000001"), | ||
tickExpected: types.MinTick + 1, | ||
}, | ||
"min price increment 10^1": { | ||
price: sdk.MustNewDecFromStr("0.000000000000000009"), | ||
tickExpected: -154000000, | ||
}, | ||
"smallest + min price increment 10^1": { | ||
price: sdk.MustNewDecFromStr("0.000000000000000010"), | ||
tickExpected: -153000000, | ||
price: sdk.MustNewDecFromStr("0.000000000010000000"), | ||
tickExpected: types.MinTick + (9 * 1e6), | ||
}, | ||
"smallest + min price increment * 10^2": { | ||
price: sdk.MustNewDecFromStr("0.000000000000000100"), | ||
tickExpected: -144000000, | ||
"min price increment 10^2": { | ||
price: sdk.MustNewDecFromStr("0.000000000100000000"), | ||
tickExpected: types.MinTick + (2 * 9 * 1e6), | ||
}, | ||
"smallest + min price increment * 10^3": { | ||
price: sdk.MustNewDecFromStr("0.000000000000001000"), | ||
tickExpected: -135000000, | ||
"min price increment 10^3": { | ||
price: sdk.MustNewDecFromStr("0.000000001000000000"), | ||
tickExpected: types.MinTick + (3 * 9 * 1e6), | ||
}, | ||
"smallest + min price increment * 10^4": { | ||
price: sdk.MustNewDecFromStr("0.000000000000010000"), | ||
tickExpected: -126000000, | ||
"min price increment 10^4": { | ||
price: sdk.MustNewDecFromStr("0.000000010000000000"), | ||
tickExpected: types.MinTick + (4 * 9 * 1e6), | ||
}, | ||
"smallest + min price * increment 10^5": { | ||
price: sdk.MustNewDecFromStr("0.000000000000100000"), | ||
tickExpected: -117000000, | ||
"min price increment 10^5": { | ||
price: sdk.MustNewDecFromStr("0.000000100000000000"), | ||
tickExpected: types.MinTick + (5 * 9 * 1e6), | ||
}, | ||
"smallest + min price * increment 10^6": { | ||
price: sdk.MustNewDecFromStr("0.000000000001000000"), | ||
tickExpected: -108000000, | ||
"min price increment 10^6": { | ||
price: sdk.MustNewDecFromStr("0.000001000000000000"), | ||
tickExpected: types.MinTick + (6 * 9 * 1e6), | ||
}, | ||
"smallest + min price * increment 10^6 + tick": { | ||
price: sdk.MustNewDecFromStr("0.000000000001000001"), | ||
tickExpected: -107999999, | ||
}, | ||
"smallest + min price * increment 10^17": { | ||
"smallest + min price * increment 10^11": { | ||
price: sdk.MustNewDecFromStr("0.100000000000000000"), | ||
tickExpected: -9000000, | ||
}, | ||
"smallest + min price * increment 10^18": { | ||
"smallest + min price * increment 10^12": { | ||
price: sdk.MustNewDecFromStr("1.000000000000000000"), | ||
tickExpected: 0, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this title reflect this test case? I think the previous one might have been incorrect as well right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for flagging this, fixed |
||
|
@@ -755,6 +749,10 @@ 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) | ||
s.Require().NoError(err) | ||
_, sqpMinTickPlusTwo, err := math.TickToSqrtPrice(types.MinTick + 2) | ||
s.Require().NoError(err) | ||
|
||
testCases := map[string]struct { | ||
sqrtPrice sdk.Dec | ||
|
@@ -836,6 +834,40 @@ func (s *ConcentratedMathTestSuite) TestSqrtPriceToTickRoundDownSpacing() { | |
tickSpacing: defaultTickSpacing, | ||
tickExpected: types.MaxTick - defaultTickSpacing, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 1 (tick spacing 1)": { | ||
sqrtPrice: sqpMinTickPlusOne, | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick + 1, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 1 minus 1 ULP (tick spacing 1)": { | ||
// Calculated using TickToSqrtPrice(types.MinTick + 1) - 1 ULP | ||
sqrtPrice: sqpMinTickPlusOne.Sub(sdk.SmallestDec()), | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 1 plus 1 ULP (tick spacing 1)": { | ||
// Calculated using TickToSqrtPrice(types.MinTick + 1) + 1 ULP | ||
sqrtPrice: sqpMinTickPlusOne.Add(sdk.SmallestDec()), | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick + 1, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 2 (tick spacing 1)": { | ||
sqrtPrice: sqpMinTickPlusTwo, | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick + 2, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 2 plus 1 ULP (tick spacing 1)": { | ||
// Calculated using TickToSqrtPrice(types.MinTick + 2) + 1 ULP | ||
sqrtPrice: sqpMinTickPlusTwo.Add(sdk.SmallestDec()), | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick + 2, | ||
}, | ||
"sqrt price corresponds exactly to min tick + 2 minus 1 ULP (tick spacing 1)": { | ||
// Calculated using TickToSqrtPrice(types.MinTick + 2) - 1 ULP | ||
sqrtPrice: sqpMinTickPlusTwo.Sub(sdk.SmallestDec()), | ||
tickSpacing: 1, | ||
tickExpected: types.MinTick + 1, | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
s.Run(name, func() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1405,89 +1405,3 @@ func (s *KeeperTestSuite) TestValidateTickRangeIsValid() { | |
}) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestRoundTickToCanonicalPriceTick() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this removed? Is there a replacement for these test vectors? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The notion of canonical price ticks no longer exist, so these vectors no longer make sense. All the test vectors added relating to the behavior in ticks surrounding the new MinTick ensure the appropriate properties hold. |
||
tests := []struct { | ||
name string | ||
lowerTick int64 | ||
upperTick int64 | ||
expectedNewLowerTick int64 | ||
expectedNewUpperTick int64 | ||
expectedError error | ||
}{ | ||
{ | ||
name: "exact upper tick for 0.000000000000000003 to exact lower tick for 0.000000000000000002", | ||
lowerTick: -161000000, | ||
expectedNewLowerTick: -161000000, | ||
upperTick: -160000000, | ||
expectedNewUpperTick: -160000000, | ||
}, | ||
{ | ||
name: "exact upper tick for 0.000000000000000003 to inexact lower tick for 0.000000000000000002", | ||
lowerTick: -161001234, | ||
expectedNewLowerTick: -161000000, | ||
upperTick: -160000000, | ||
expectedNewUpperTick: -160000000, | ||
}, | ||
{ | ||
name: "inexact upper tick for 0.000000000000000003 to exact lower tick for 0.000000000000000002", | ||
lowerTick: -161000000, | ||
expectedNewLowerTick: -161000000, | ||
upperTick: -160001234, | ||
expectedNewUpperTick: -160000000, | ||
}, | ||
{ | ||
name: "inexact upper tick for 0.000000000000000003 to inexact lower tick for 0.000000000000000002", | ||
lowerTick: -161001234, | ||
expectedNewLowerTick: -161000000, | ||
upperTick: -160001234, | ||
expectedNewUpperTick: -160000000, | ||
}, | ||
{ | ||
name: "upper tick one tick away from lower tick", | ||
lowerTick: -161001234, | ||
expectedNewLowerTick: -161000000, | ||
upperTick: -160999999, | ||
expectedNewUpperTick: -160000000, | ||
}, | ||
{ | ||
name: "error: new upper tick is lower than new lower tick", | ||
lowerTick: -160001234, | ||
expectedNewLowerTick: -160000000, | ||
upperTick: -161001234, | ||
expectedNewUpperTick: -161000000, | ||
expectedError: types.InvalidLowerUpperTickError{LowerTick: -160000000, UpperTick: -161000000}, | ||
}, | ||
{ | ||
name: "error: new upper tick is the same as new lower tick", | ||
lowerTick: -160001234, | ||
expectedNewLowerTick: -160000000, | ||
upperTick: -160000000, | ||
expectedNewUpperTick: -160000000, | ||
expectedError: types.InvalidLowerUpperTickError{LowerTick: -160000000, UpperTick: -160000000}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
s.Run(test.name, func() { | ||
s.SetupTest() | ||
|
||
_, sqrtPriceTickLower, err := math.TickToSqrtPrice(test.lowerTick) | ||
s.Require().NoError(err) | ||
_, sqrtPriceTickUpper, err := math.TickToSqrtPrice(test.upperTick) | ||
s.Require().NoError(err) | ||
|
||
// System Under Test | ||
newLowerTick, newUpperTick, err := cl.RoundTickToCanonicalPriceTick(test.lowerTick, test.upperTick, sqrtPriceTickLower, sqrtPriceTickUpper, DefaultTickSpacing) | ||
|
||
if test.expectedError != nil { | ||
s.Require().Error(err) | ||
s.Require().ErrorContains(err, test.expectedError.Error()) | ||
} else { | ||
s.Require().NoError(err) | ||
s.Require().Equal(test.expectedNewLowerTick, newLowerTick) | ||
s.Require().Equal(test.expectedNewUpperTick, newUpperTick) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you explaining being off by one compared to the Wolfram calculation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes sorry – the expected output is bankers rounded instead of truncated due to
GetLiquidityFromAmounts
being bankers rounded. This does not pose any security risks as the balance is still deducted from the caller.