-
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
refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level #6369
Merged
Merged
refactor/test(CL): Stricter rounding behavior in CL math methods; unit tests at low price level #6369
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad759f8
refactor/test(CL): Stricter rounding behavior in CL math methods; uni…
p0mvn 3cc21a8
comment updates
p0mvn 41e436c
clarifying comment
p0mvn 1562a84
remove comment
p0mvn 9f538c3
Merge branch 'main' into roman/clmath-rounding-tests
p0mvn c8bd721
Merge branch 'main' into roman/clmath-rounding-tests
p0mvn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm | |
|
||
// We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, | ||
// our liquidity calculations will be off from our theoretical calculations within our tests. | ||
// TODO (perf): consider better conversion helpers to minimize reallocations. | ||
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) | ||
|
||
product := sqrtPriceA.Mul(sqrtPriceB) | ||
|
@@ -26,6 +27,7 @@ func Liquidity0(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm | |
panic(fmt.Sprintf("liquidity0 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB)) | ||
} | ||
|
||
// TODO (perf): consider Dec() function that does not reallocate | ||
return amountBigDec.MulMut(product).QuoMut(diff).Dec() | ||
Comment on lines
+30
to
31
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. |
||
} | ||
|
||
|
@@ -40,12 +42,14 @@ func Liquidity1(amount osmomath.Int, sqrtPriceA, sqrtPriceB osmomath.BigDec) osm | |
|
||
// We convert to BigDec to avoid precision loss when calculating liquidity. Without doing this, | ||
// our liquidity calculations will be off from our theoretical calculations within our tests. | ||
// TODO (perf): consider better conversion helpers to minimize reallocations. | ||
amountBigDec := osmomath.BigDecFromDec(amount.ToLegacyDec()) | ||
diff := sqrtPriceB.Sub(sqrtPriceA) | ||
if diff.IsZero() { | ||
panic(fmt.Sprintf("liquidity1 diff is zero: sqrtPriceA %s sqrtPriceB %s", sqrtPriceA, sqrtPriceB)) | ||
} | ||
|
||
// TODO (perf): consider Dec() function that does not reallocate | ||
return amountBigDec.QuoMut(diff).Dec() | ||
Comment on lines
+52
to
53
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. |
||
} | ||
|
||
|
@@ -73,13 +77,19 @@ func CalcAmount0Delta(liq, sqrtPriceA, sqrtPriceB osmomath.BigDec, roundUp bool) | |
// - calculating amountIn during swap | ||
// - adding liquidity (request user to provide more tokens in in favor of the pool) | ||
// The denominator is truncated to get a higher final amount. | ||
return liq.MulRoundUp(diff).QuoRoundUp(sqrtPriceA).QuoRoundUp(sqrtPriceB).Ceil() | ||
// Note that the order of divisions is important here. First, we divide by a larger number (sqrtPriceB) and then by a smaller number (sqrtPriceA). | ||
// This leads to a smaller error amplification. This only matters in cases where at least one of the sqrt prices is below 1. | ||
// TODO (perf): QuoRoundUpMut with no reallocation. | ||
return liq.MulRoundUp(diff).QuoRoundUp(sqrtPriceB).QuoRoundUp(sqrtPriceA).Ceil() | ||
p0mvn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// These are truncated at precision end to round in favor of the pool when: | ||
// - calculating amount out during swap | ||
// - withdrawing liquidity | ||
// Each intermediary step is truncated at precision end to get a smaller final amount. | ||
return liq.MulTruncate(diff).QuoTruncate(sqrtPriceA).QuoTruncate(sqrtPriceB) | ||
// Note that the order of divisions is important here. First, we divide by a larger number (sqrtPriceB) and then by a smaller number (sqrtPriceA). | ||
// This leads to a smaller error amplification. | ||
// TODO (perf): QuoTruncate with no reallocation. | ||
return liq.MulTruncate(diff).QuoTruncate(sqrtPriceB).QuoTruncate(sqrtPriceA) | ||
} | ||
|
||
// CalcAmount1Delta takes the asset with the smaller liquidity in the pool as well as the sqrtpCur and the nextPrice and calculates the amount of asset 1 | ||
|
@@ -124,11 +134,13 @@ func GetNextSqrtPriceFromAmount0InRoundingUp(sqrtPriceCurrent, liquidity, amount | |
return sqrtPriceCurrent | ||
} | ||
|
||
product := amountZeroRemainingIn.Mul(sqrtPriceCurrent) | ||
// Truncate at precision end to make denominator smaller so that the final result is larger. | ||
product := amountZeroRemainingIn.MulTruncate(sqrtPriceCurrent) | ||
// denominator = product + liquidity | ||
denominator := product | ||
denominator.AddMut(liquidity) | ||
return liquidity.Mul(sqrtPriceCurrent).QuoRoundUp(denominator) | ||
// MulRoundUp and QuoRoundUp to make the final result larger by rounding up at precision end. | ||
return liquidity.MulRoundUp(sqrtPriceCurrent).QuoRoundUp(denominator) | ||
} | ||
|
||
// GetNextSqrtPriceFromAmount0OutRoundingUp utilizes sqrtPriceCurrent, liquidity, and amount of denom0 that still needs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note to reviewer:
tracking these here: #6370
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.
I think this pr can resolve this TODO #6409