Skip to content
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

Merged
merged 5 commits into from
Apr 25, 2024

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Apr 17, 2024

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

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

Summary by CodeRabbit

  • Refactor

    • Enhanced precision in reward calculations and price computations across multiple swapping functionalities.
    • Improved calculation methods for spread rewards in swap strategies, enhancing accuracy and efficiency.
    • Renamed type and function parameters related to spread factors for better clarity and consistency in swap strategy implementation.
  • Bug Fixes

    • Adjusted expected values in test scenarios to align with updated calculation methods, ensuring consistency and reliability in test results.

Copy link
Contributor

coderabbitai bot commented Apr 17, 2024

Walkthrough

The 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

Files Changes
.../swaps_test.go Adjusted expected values for ExpectedSpreadRewardGrowthAccumulatorValue in test cases.
.../swapstrategy/export_test.go, .../swapstrategy/spread_rewards.go Renamed oneMinusSpreadFactorGetter to spreadFactorOverOneMinusSpreadFactorGetter for consistency. Updated related functions accordingly.
.../swapstrategy/one_for_zero.go, .../swapstrategy/zero_for_one.go Added spfOverOneMinusSpf field, updated methods to use getSpfOverOneMinusSpf, and introduced new calculation methods.
CHANGELOG.md Enhanced CL spread factor calculations with slight rounding behavior adjustments for improved performance.

Poem

🐇🌟
In the realm of code, where logic threads tight,
A rabbit hopped through, enhancing the light.
With each tiny tweak, precision took flight,
Numbers now dance, in the moon's gentle light.
Celebrate the change, for it crafts the night! 🌙✨


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f9e67d8 and 9e793c0.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments not posted (3)
CHANGELOG.md (3)

47-47: Ensure the entry for PR #7005 is accurately described and linked.


48-48: Verify that the entries for PR #8053, #8106, and #8030 are state-compatible and correctly linked.


51-51: Confirm that the description for PR #8073 accurately reflects the changes made in the PR, especially regarding the speedup of CL spread factor calculations and the rounding behavior.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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
Comment on lines 48 to 49
* [#8073](https://github.com/osmosis-labs/osmosis/pull/8073) Speedup CL spread factor calculations, but mildly changes rounding behavior in the final decimal place.

Copy link
Contributor

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.

Suggested change
* [#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.

Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a 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())
Copy link
Member

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)),
Copy link
Member

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

Copy link
Member Author

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"),
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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)?

Copy link
Member Author

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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.

@ValarDragon ValarDragon merged commit 00a8f37 into main Apr 25, 2024
1 check passed
@ValarDragon ValarDragon deleted the dev/simplify_spread_factor_calculation branch April 25, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants