-
Notifications
You must be signed in to change notification settings - Fork 568
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
perf!: Simplify the spread factor calculation #8073
Conversation
WalkthroughThe concentrated liquidity module changes revolve around refining spread reward calculations in swap strategies. These updates streamline variable naming, introduce new fields, and adjust function implementations to enhance accuracy and efficiency in reward computations during swaps. Changes
Poem
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Additional comments not posted (3)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 1
Actionable comments outside the diff hunks (9)
CHANGELOG.md (9)
Line range hint
984-984
: Ensure consistent heading levels.- #### Features + ### Features
Line range hint
996-996
: Ensure consistent heading levels.- #### Minor improvements & Bug Fixes + ### Minor improvements & Bug Fixes
Line range hint
1005-1005
: Ensure consistent heading levels.- #### SDK fork updates + ### SDK fork updates
Line range hint
1010-1010
: Ensure consistent heading levels.- #### IAVL fork updates + ### IAVL fork updates
Line range hint
1062-1062
: Ensure consistent heading levels.- #### Features + ### Features
Line range hint
323-323
: Remove trailing punctuation in heading.- ## [v6.4.0] + ## v6.4.0
Line range hint
586-586
: Convert bare URL to a markdown link for better readability.- See: [SDK v0.43.0 Release Notes](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0) For more details + See: [SDK v0.43.0 Release Notes](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0) for more details.
Line range hint
644-644
: Convert bare URL to a markdown link for better readability.- [wasmd-v.022.0-osmo-v7.2](https://github.com/osmosis-labs/wasmd/releases/tag/v0.22.0-osmo-v7.2) Upgrade SDK and IAVL dependencies to use fast storage + [wasmd-v.022.0-osmo-v7.2](https://github.com/osmosis-labs/wasmd/releases/tag/v0.22.0-osmo-v7.2) Upgrade SDK and IAVL dependencies to use fast storage.
Line range hint
1225-1225
: Ensure there are no empty links in the document.- [v6.4.0](https://github.com/osmosis-labs/osmosis/releases/tag/v6.4.0) + [v6.4.0](https://github.com/osmosis-labs/osmosis/releases/tag/v6.4.0) - Description of the release.
CHANGELOG.md
Outdated
* [#8073](https://github.com/osmosis-labs/osmosis/pull/8073) Speedup CL spread factor calculations, but mildly changes rounding behavior in the final decimal place. | ||
|
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.
Ensure consistent heading levels.
- ### State Breaking
+ ## State Breaking
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
* [#8073](https://github.com/osmosis-labs/osmosis/pull/8073) Speedup CL spread factor calculations, but mildly changes rounding behavior in the final decimal place. | |
* [#8073](https://github.com/osmosis-labs/osmosis/pull/8073) Speedup CL spread factor calculations, but mildly changes rounding behavior in the final decimal place. | |
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.
LGTM! The rounding diff seems safe
@@ -189,6 +191,13 @@ func (s zeroForOneStrategy) getOneMinusSpreadFactor() osmomath.Dec { | |||
return s.oneMinusSpreadFactor | |||
} | |||
|
|||
func (s zeroForOneStrategy) getSpfOverOneMinusSpf() osmomath.Dec { | |||
if s.spfOverOneMinusSpf.IsNil() { | |||
s.spfOverOneMinusSpf = s.spreadFactor.QuoRoundUp(s.getOneMinusSpreadFactor()) |
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.
just curious, whats the reason we do round up?
@@ -121,7 +121,7 @@ func (suite *StrategyTestSuite) TestComputeSwapStepOutGivenIn_ZeroForOne() { | |||
amountZeroInConsumed: defaultAmountZero.Ceil(), | |||
// liquidity * (sqrtPriceNext - sqrtPriceCurrent) | |||
expectedAmountOneOut: defaultAmountOne, | |||
expectedSpreadRewardChargeTotal: defaultAmountZero.Ceil().Quo(oneMinusDefaultSpreadFactor).Mul(defaultSpreadReward), | |||
expectedSpreadRewardChargeTotal: defaultAmountZero.Ceil().Mul(defaultSpreadReward.Quo(oneMinusDefaultSpreadFactor)), |
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.
Just making sure, these two should be the same value right
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.
They weren't due to rounding differences 😅
@@ -1630,7 +1630,7 @@ var ( | |||
ExpectedSecondUpperTickSpreadRewardGrowth: secondPosition{TickIndex: 322500, ExpectedSpreadRewardGrowth: cl.EmptyCoins}, | |||
NewLowerPrice: osmomath.NewDec(5500), | |||
NewUpperPrice: osmomath.NewDec(6250), | |||
ExpectedSpreadRewardGrowthAccumulatorValue: osmomath.MustNewDecFromStr("0.007433904623597252"), | |||
ExpectedSpreadRewardGrowthAccumulatorValue: osmomath.MustNewDecFromStr("0.007433904623597258"), |
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.
What's the reason these changed?
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.
This PR changes rounding behavior is why
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.
Hm, this may be an invalid concern but I am worried since the rounding behavior changed in favor of the user.
Imagine a user's current expected spread rewards is 0.007433904623597252, and after the upgrade it is now 0.007433904623597258. Where do these additional funds come from?
I guess the concern is, we calculated accum values with one rounding behavior, and now claim with a rounding behavior more in favor of the user, so what effect does this have (if any)?
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.
So remember this is only affecting the swap amount for what gets added to the spread factor accumulator. The reward calculation is based off the difference of two accumulator values.
So how we computed past accumulator values doesn't matter. It just matters that we are consistent with new additions
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.
Actionable comments posted: 0
Actionable comments outside the diff hunks (9)
CHANGELOG.md (9)
Line range hint
326-326
: Correct the heading punctuation.-## v6.4.0 +## v6.4.0.
Line range hint
589-589
: Replace the bare URL with a markdown link for better readability.- https://github.com/osmosis-labs/osmosis/releases/tag/v6.4.0 + [v6.4.0](https://github.com/osmosis-labs/osmosis/releases/tag/v6.4.0)
Line range hint
647-647
: Replace the bare URL with a markdown link for better readability.- https://github.com/osmosis-labs/osmosis/releases/tag/v6.3.1 + [v6.3.1](https://github.com/osmosis-labs/osmosis/releases/tag/v6.3.1)
Line range hint
987-987
: Ensure heading levels increment by one level at a time.- #### Golang API breaks + ### Golang API breaks
Line range hint
999-999
: Ensure heading levels increment by one level at a time.- #### Bug Fixes + ### Bug Fixes
Line range hint
1008-1008
: Ensure heading levels increment by one level at a time.- #### Features + ### Features
Line range hint
1013-1013
: Ensure heading levels increment by one level at a time.- #### Minor improvements & Bug Fixes + ### Minor improvements & Bug Fixes
Line range hint
1065-1065
: Ensure heading levels increment by one level at a time.- #### SDK fork updates + ### SDK fork updates
Line range hint
1228-1228
: Ensure there are no empty links in the document.- [v6.3.1](https://github.com/osmosis-labs/osmosis/releases/tag/v6.3.1) + [v6.3.1](https://github.com/osmosis-labs/osmosis/releases/tag/v6.3.1) - Description or title of the link
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.
Actionable comments posted: 0
Out of diff range and nitpick comments (1)
CHANGELOG.md (1)
47-54
: Check for consistency in formatting across all entries in the changelog. Ensure that each entry is clearly separated and follows a uniform style.
What is the purpose of the change
Simplify the spread factor calculation in CL. We compute a coefficient
spread factor / (1 - spread factor)
in each swap and then do a single multiplication by that in swaps. Eventually we can change this to be a map in memory, for all authorized spread factors so this coefficient no longer even needs to be computed.This does change rounding in the final decimal place. So we can decide to remove this if we want due to concern of it breaking spread factor calculations.
Testing and Verifying
Passes all but 5 existing tests, those tests are fixed by changing the final digit.
Documentation and Release Note
Unreleased
section ofCHANGELOG.md
?Where is the change documented?
x/{module}/README.md
)Summary by CodeRabbit
Refactor
Bug Fixes