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

Remove duplicated one minus spread factor calculations #8040

Merged
merged 1 commit into from
Apr 17, 2024

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Apr 13, 2024

Remove duplicated one minus spread factor calculations.

Since there are only a small number of spread factors, we may consider just putting this in RAM eventually. In a subsequent (state breaking PR) I also want to cache spreadfactor / (1 - spreadfactor) and use that directly in computations.

I'm interested in feedback if this is worth the code complexity. This PR: cosmos/cosmos-sdk#20034 will also help speedup the quo logic in this code. (It will 50% reduce the QuoRoundUpMut time seen in prod)

Summary by CodeRabbit

  • New Features
    • Improved consistency in spread factor calculations across various trading strategies.
  • Refactor
    • Updated functions related to spread reward charges to include new parameters and logic enhancements, enhancing accuracy and performance in trading calculations.

@ValarDragon ValarDragon added V:state/compatible/backport State machine compatible PR, should be backported A:no-changelog A:backport/v24.x backport patches to v24.x branch labels Apr 13, 2024
Copy link
Contributor

coderabbitai bot commented Apr 13, 2024

Walkthrough

The recent updates in the swapstrategy package introduce a new field oneMinusSpreadFactor and a method for its calculation in swap strategy structures. This enhancement is used across various functions to ensure consistent calculations of fees and spread rewards. The modifications streamline the process, improving accuracy in financial computations during swaps.

Changes

Files Change Summary
.../swapstrategy/one_for_zero.go, .../zero_for_one.go Added oneMinusSpreadFactor field and method, updated fee and reward calculations.
.../swapstrategy/export_test.go, .../spread_rewards.go Updated functions to include oneMinusSpreadFactorGetter parameter, modifying spread reward logic.

🐰✨
In the land of code, where logic threads weave,
A rabbit hopped in, new tricks up its sleeve.
With a flick and a twist of its tiny paw,
It refined the swaps, without a flaw.
Now each calculation, precise and clear,
Thanks to the rabbit, with its code so dear. 🌟🐇


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e98d023 and 4c8353d.
Files selected for processing (4)
  • x/concentrated-liquidity/swapstrategy/export_test.go (1 hunks)
  • x/concentrated-liquidity/swapstrategy/one_for_zero.go (5 hunks)
  • x/concentrated-liquidity/swapstrategy/spread_rewards.go (4 hunks)
  • x/concentrated-liquidity/swapstrategy/zero_for_one.go (5 hunks)
Additional Context Used
Learnings (1)
x/concentrated-liquidity/swapstrategy/spread_rewards.go (1)
User: ValarDragon"
URL: https://github.com/osmosis-labs/osmosis/pull/8014

Timestamp: 2024-04-11T14:25:11.315Z
Learning: Refactoring the function `computeSpreadRewardChargeFromAmountIn` to take `1 - spreadFactor` as an argument is planned to be handled in a separate PR due to the need for extensive changes across multiple callers.
Additional comments not posted (10)
x/concentrated-liquidity/swapstrategy/export_test.go (2)

6-9: LGTM! The addition of oneMinusSpreadFactorGetter optimizes the calculation of "one minus spread factor" by ensuring it's computed only once.


13-13: Consider refactoring to use a getter function for oneMinusSpreadFactor to avoid redundant calculations and maintain consistency with other updates.

Skipped due to learnings
User: ValarDragon"
URL: https://github.com/osmosis-labs/osmosis/pull/8014

Timestamp: 2024-04-11T14:25:11.315Z
Learning: Refactoring the function `computeSpreadRewardChargeFromAmountIn` to take `1 - spreadFactor` as an argument is planned to be handled in a separate PR due to the need for extensive changes across multiple callers.
x/concentrated-liquidity/swapstrategy/spread_rewards.go (2)

24-30: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [27-42]

LGTM! The use of oneMinusSpreadFactorGetter in computeSpreadRewardChargePerSwapStepOutGivenIn aligns with the PR's objective to optimize performance by reducing redundant calculations.


64-65: LGTM! The refactoring of computeSpreadRewardChargeFromAmountIn to take oneMinusSpreadFactor as a parameter aligns with the PR's objective and previous discussions.

x/concentrated-liquidity/swapstrategy/zero_for_one.go (3)

27-29: LGTM! The addition of the oneMinusSpreadFactor field in zeroForOneStrategy is a good optimization to pre-compute and reuse the "one minus spread factor".


69-75: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [72-102]

LGTM! The use of getOneMinusSpreadFactor in ComputeSwapWithinBucketOutGivenIn ensures that the "one minus spread factor" is computed once and reused, aligning with the PR's optimization goals.


185-190: LGTM! The implementation of getOneMinusSpreadFactor in zeroForOneStrategy efficiently ensures the "one minus spread factor" is computed only once, optimizing performance.

x/concentrated-liquidity/swapstrategy/one_for_zero.go (3)

27-29: LGTM! The addition of the oneMinusSpreadFactor field in oneForZeroStrategy is a good optimization to pre-compute and reuse the "one minus spread factor".


67-73: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [70-100]

LGTM! The use of getOneMinusSpreadFactor in ComputeSwapWithinBucketOutGivenIn ensures that the "one minus spread factor" is computed once and reused, aligning with the PR's optimization goals.


186-190: LGTM! The implementation of getOneMinusSpreadFactor in oneForZeroStrategy efficiently ensures the "one minus spread factor" is computed only once, optimizing performance.


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
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ValarDragon ValarDragon merged commit 670efe0 into main Apr 17, 2024
1 check passed
@ValarDragon ValarDragon deleted the dev/remove_duplicated_one_minus_spreadfactor branch April 17, 2024 03:20
mergify bot pushed a commit that referenced this pull request Apr 17, 2024
ValarDragon added a commit that referenced this pull request Apr 17, 2024
(cherry picked from commit 670efe0)

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v24.x backport patches to v24.x branch A:no-changelog C:x/concentrated-liquidity V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants