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

Removes an unneeded sqrt call from CL logic #3978

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

ValarDragon
Copy link
Member

No idea if this is a bottleneck in TRPC/locally.

just noticed it as I was reading through code. Also deletes some duplicate logic

No idea if this is a bottleneck in TRPC/locally.

just noticed it as I was reading through code
Copy link

vercel bot commented Nov 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
osmosis-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 5:30pm
4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
osmosis-frontend-datadog ⬜️ Ignored (Inspect) Nov 28, 2024 5:30pm
osmosis-frontend-dev ⬜️ Ignored (Inspect) Nov 28, 2024 5:30pm
osmosis-frontend-edgenet ⬜️ Ignored (Inspect) Nov 28, 2024 5:30pm
osmosis-testnet ⬜️ Ignored (Inspect) Nov 28, 2024 5:30pm

Copy link
Contributor

coderabbitai bot commented Nov 28, 2024

Walkthrough

The changes involve a refactoring of the tickToSqrtPrice function in the packages/math/src/pool/concentrated/tick.ts file, replacing its internal logic with a call to the newly introduced tickToPrice function. This simplifies the calculation of square root prices. Additionally, in the packages/server/src/queries/complex/concentrated-liquidity/index.ts file, the getTickPrice function has been updated to use tickToPrice, and a new function, getDisplayPriceFromPrice, has been added for display price calculations, streamlining the overall price calculation process.

Changes

File Path Change Summary
packages/math/src/pool/concentrated/tick.ts Refactored tickToSqrtPrice to call tickToPrice, which retains original logic for price calculation. Added export for tickToPrice.
packages/server/src/queries/complex/concentrated-liquidity/index.ts Updated getTickPrice to use tickToPrice, added getDisplayPriceFromPrice, and adjusted import statements accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Math

    Client->>Server: Request price for tick
    Server->>Math: Call getTickPrice(tick)
    Math->>Math: Call tickToPrice(tick)
    Math-->>Server: Return price
    Server->>Math: Call getDisplayPriceFromPrice(price, baseCoin, quoteCoin)
    Math-->>Server: Return display price
    Server-->>Client: Return display price
Loading

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/math/src/pool/concentrated/tick.ts

Oops! Something went wrong! :(

ESLint: 8.50.0

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/packages/math/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ed6aafb and 495ebf8.

📒 Files selected for processing (2)
  • packages/math/src/pool/concentrated/tick.ts (1 hunks)
  • packages/server/src/queries/complex/concentrated-liquidity/index.ts (3 hunks)
🔇 Additional comments (3)
packages/math/src/pool/concentrated/tick.ts (1)

23-25: LGTM! Clean refactor that improves code organization

The refactoring of tickToSqrtPrice to use tickToPrice followed by approxSqrt is a good improvement that:

  1. Reduces code duplication
  2. Maintains the same mathematical correctness
  3. Has no performance regression (still one sqrt operation)
  4. Improves maintainability through better separation of concerns

Let's verify the usage of this function to ensure the refactor doesn't impact behavior:

✅ Verification successful

Based on the search results, I can now generate the final response since I have enough information to verify the usage and impact of the tickToSqrtPrice function refactoring:

✅ Verified: The refactoring is safe and maintains consistent behavior

The function is used consistently across the codebase in expected mathematical contexts:

  • Price calculations in position management (position-by-id.ts)
  • Swap calculations and testing (swap-exact-in-positions.spec.ts)
  • Core concentrated liquidity math operations (math.ts, quotes.ts)
  • All usages treat it as a pure mathematical transformation from tick to square root of price

The test file tick.spec.ts explicitly verifies the mathematical correctness by comparing against expected sqrt price values, providing additional confidence in the refactoring.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all usages of tickToSqrtPrice to verify impact
rg "tickToSqrtPrice" --type ts -A 2

Length of output: 8699

packages/server/src/queries/complex/concentrated-liquidity/index.ts (2)

157-171: LGTM! Well-structured price display calculation.

The function correctly extracts the common decimal adjustment logic, improving code maintainability by following the DRY principle.


182-186: Verify the tickToPrice implementation.

The simplified price calculation looks good, but we should verify the implementation of the imported tickToPrice function to ensure it maintains the same precision and correctness.

✅ Verification successful

Implementation of tickToPrice is verified and maintains precision

The implementation of tickToPrice has been verified and is well-tested. Key observations:

  • The function is a direct port from Osmosis core implementation
  • It handles edge cases (zero tick, min/max bounds)
  • Comprehensive test suite covers various scenarios including:
    • Different price ranges (from very small to very large numbers)
    • Boundary conditions
    • Common trading pairs (BTC/USD, ETH/BTC, etc.)
  • Maintains precision through proper decimal handling using Dec and BigDec types
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of tickToPrice function
# Expected: Should find the implementation and confirm it maintains precision

# Search for tickToPrice implementation
ast-grep --pattern 'export function tickToPrice($_) {
  $$$
}'

# Search for any tests related to tickToPrice
rg -l "tickToPrice.*test"

Length of output: 92


Script:

#!/bin/bash
# Let's try different approaches to locate the tickToPrice implementation

# Search for tickToPrice function with more flexible pattern
ast-grep --pattern 'tickToPrice'

# Search for files containing tickToPrice using ripgrep with context
rg -A 5 "tickToPrice"

# Find files with 'tick' and 'price' in their names
fd -t f "tick|price"

Length of output: 9707


Script:

#!/bin/bash
# Let's examine the implementation and tests of tickToPrice

# Get the implementation
cat packages/math/src/pool/concentrated/tick.ts

# Get the test file
cat packages/math/src/pool/concentrated/__tests__/tick.spec.ts

Length of output: 24082


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@JoseRFelix JoseRFelix merged commit 076f5db into stage Nov 28, 2024
35 checks passed
@JoseRFelix JoseRFelix deleted the dev/remove_unneeded_sqrt_call branch November 28, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants