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 logic of FundingRateToIndex #1528

Merged
merged 2 commits into from
May 22, 2024
Merged

Remove logic of FundingRateToIndex #1528

merged 2 commits into from
May 22, 2024

Conversation

BrendanChou
Copy link
Contributor

@BrendanChou BrendanChou commented May 15, 2024

Changelist

  • Change keeper.getFundingIndexDelta() to a stateless helper function, GetFundingIndexDelta(), which replicates the old behavior of the function except for:
    • Requires the price value to be passed into the function instead of getting it from state
    • Therefore, is stateless and does not return an error in any case
  • Add unit tests for this function (previously only tested indirectly via testing of the stateful calling function, MaybeProcessNewFundingTickEpoch()
  • This fully removes use of multiplyByPrice() and FundingRateToIndex() in lib/quantums.go and isolates the code to the x/perpetuals module
  • This change is intended to be non-breaking, but could technically error in a different way (with a different message, at a different point in the code flow) if the price for a perpetual is not found

Test Plan

  • New unit tests
  • Replicating old behavior is covered indirectly by tests for MaybeProcessNewFundingTickEpoch()

Summary by CodeRabbit

  • New Features

    • Introduced a new function to set the AtomicResolution parameter in the Perpetual struct.
    • Added functionality to calculate the change in the funding index based on various parameters for more precise funding calculations.
  • Bug Fixes

    • Improved precision in multiplication and division operations by refining the implementation of the MultiplyByPrice function.
  • Tests

    • Added comprehensive tests for the new funding index delta calculations to ensure accuracy and reliability.

@BrendanChou BrendanChou requested a review from roy-dydx May 15, 2024 22:51
Copy link
Contributor

coderabbitai bot commented May 15, 2024

Walkthrough

The recent updates involve enhancements across various packages in the codebase. Changes include refining arithmetic functions with big integers for precision, introducing new functions for calculations, and improving testing coverage for mathematical operations.

Changes

File Change Summary
protocol/lib/quantums.go Refactored multiplyByPrice to MultiplyByPrice with explicit big integer handling for multiplication and division.
protocol/testutil/perpetuals/perpetuals.go Added WithAtomicResolution function to set AtomicResolution in Perpetual struct.
protocol/x/perpetuals/funding/funding.go Introduced GetFundingIndexDelta function for precise funding index calculations using big integers.
protocol/x/perpetuals/funding/funding_test.go Added tests for GetFundingIndexDelta function with various scenarios for accurate funding index delta calculation.
protocol/x/perpetuals/keeper/perpetual.go Refactored funding index calculation, added import for funding package, and updated funding index processing logic.

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: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f3e9ac5 and 1a867f3.
Files selected for processing (3)
  • protocol/Makefile (2 hunks)
  • protocol/lib/quantums.go (4 hunks)
  • protocol/lib/quantums_test.go (1 hunks)
Additional Context Used
Path-based Instructions (3)
protocol/lib/quantums.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/lib/quantums_test.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/Makefile (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments not posted (7)
protocol/lib/quantums.go (3)

32-32: No issues found in BaseToQuoteQuantums.


Line range hint 55-55: No issues found in QuoteToBaseQuantums.


Line range hint 87-113: Performance improvements in MultiplyByPrice look good. Ensure all usages are updated to the new public function.

Verification successful

All usages of the MultiplyByPrice function have been updated to match the new public function signature.

  • protocol/lib/quantums_test.go
  • protocol/lib/quantums.go
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `MultiplyByPrice` match the new public function.

# Test: Search for the function usage. Expect: Only occurrences of the new public function.
rg --type go $'MultiplyByPrice'

Length of output: 409



Script:

#!/bin/bash
# Extract lines around the function calls in the test file and the main file to verify the usage of MultiplyByPrice.

# Extract lines around the function call in the test file
rg --type go --context 5 'MultiplyByPrice' protocol/lib/quantums_test.go

# Extract lines around the function call in the main file
rg --type go --context 5 'MultiplyByPrice' protocol/lib/quantums.go

Length of output: 1819

protocol/lib/quantums_test.go (3)

Line range hint 14-14: No issues found in TestBaseToQuoteQuantums.


Line range hint 123-123: No issues found in TestQuoteToBaseQuantums.


224-238: No issues found in BenchmarkMultiplyByPrice.

protocol/Makefile (1)

234-234: No issues found in the benchmark target.

@BrendanChou BrendanChou changed the title Improve benchmark capability. Use it to improve performance of MultiplyByPrice by over 2x Improve benchmark capability. Improve MultiplyByPrice 80% and BigRatMulPpm 20%. May 15, 2024
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: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1a867f3 and 8f98b1d.
Files selected for processing (5)
  • protocol/Makefile (2 hunks)
  • protocol/lib/big_math.go (1 hunks)
  • protocol/lib/big_math_test.go (1 hunks)
  • protocol/lib/quantums.go (4 hunks)
  • protocol/lib/quantums_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/Makefile
  • protocol/lib/quantums.go
  • protocol/lib/quantums_test.go
Additional Context Used
Path-based Instructions (2)
protocol/lib/big_math.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/lib/big_math_test.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/lib/big_math_test.go Outdated Show resolved Hide resolved
protocol/lib/big_math.go Outdated Show resolved Hide resolved
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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 8f98b1d and b6893f9.
Files selected for processing (5)
  • protocol/Makefile (2 hunks)
  • protocol/lib/big_math.go (1 hunks)
  • protocol/lib/big_math_test.go (1 hunks)
  • protocol/lib/quantums.go (4 hunks)
  • protocol/lib/quantums_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • protocol/Makefile
  • protocol/lib/big_math.go
  • protocol/lib/big_math_test.go
  • protocol/lib/quantums.go
  • protocol/lib/quantums_test.go

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between b6893f9 and 201a23b.
Files selected for processing (2)
  • protocol/lib/big_math.go (1 hunks)
  • protocol/lib/big_math_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/lib/big_math.go
  • protocol/lib/big_math_test.go

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 201a23b and fe372d7.
Files selected for processing (3)
  • protocol/lib/big_math.go (1 hunks)
  • protocol/lib/quantums.go (4 hunks)
  • protocol/lib/quantums_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/lib/big_math.go
  • protocol/lib/quantums.go
  • protocol/lib/quantums_test.go

@BrendanChou BrendanChou changed the title Improve benchmark capability. Improve MultiplyByPrice 80% and BigRatMulPpm 20%. Improve performance of MultiplyByPrice and BigRatMulPpm May 17, 2024
@BrendanChou BrendanChou removed the chore label May 17, 2024
@BrendanChou BrendanChou marked this pull request as draft May 20, 2024 03:50
@BrendanChou BrendanChou changed the base branch from main to bc/base-to-quote-quantums May 20, 2024 14:29
@BrendanChou BrendanChou changed the title Improve performance of MultiplyByPrice and BigRatMulPpm Remove logic of FundingRateToIndex May 20, 2024
Base automatically changed from bc/base-to-quote-quantums to main May 20, 2024 14:59
@BrendanChou BrendanChou changed the base branch from main to bc/quote-to-base-quantums May 21, 2024 01:33
@BrendanChou BrendanChou force-pushed the bc/bench branch 3 times, most recently from aa19716 to a10ee8d Compare May 21, 2024 15:47
@BrendanChou BrendanChou marked this pull request as ready for review May 21, 2024 16:50
Base automatically changed from bc/quote-to-base-quantums to main May 21, 2024 19:10
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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between fe372d7 and c5011e1.
Files selected for processing (5)
  • protocol/lib/quantums.go (1 hunks)
  • protocol/testutil/perpetuals/perpetuals.go (1 hunks)
  • protocol/x/perpetuals/funding/funding.go (1 hunks)
  • protocol/x/perpetuals/funding/funding_test.go (1 hunks)
  • protocol/x/perpetuals/keeper/perpetual.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/lib/quantums.go
Additional Context Used
Path-based Instructions (4)
protocol/x/perpetuals/funding/funding.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/perpetuals/funding/funding_test.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/testutil/perpetuals/perpetuals.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/perpetuals/keeper/perpetual.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments not posted (5)
protocol/x/perpetuals/funding/funding.go (1)

11-46: The implementation of GetFundingIndexDelta appears correct and efficient. Consider addressing the TODO regarding the 8-hour funding rate period configurability in future iterations.

protocol/x/perpetuals/funding/funding_test.go (1)

15-88: The unit tests for GetFundingIndexDelta are comprehensive and well-structured. Good use of table-driven tests to cover various scenarios.

protocol/testutil/perpetuals/perpetuals.go (1)

47-50: The addition of WithAtomicResolution follows the established pattern of perpetual modifier options and is implemented correctly.

protocol/x/perpetuals/keeper/perpetual.go (2)

734-753: The changes from lines 734 to 753 introduce a new method for updating the funding index based on the new funding rate. This method retrieves the market price, calculates the funding index delta, and updates the funding index in the state. Ensure that the error handling and the logic for updating the funding index are thoroughly tested, especially since this involves financial calculations which are critical to the system's integrity.

Verification successful

The integration of the new funding index update logic appears to be well-covered in both the main code and the test cases. The test cases include scenarios for success, non-existent perpetual, and integer overflow/underflow, indicating thorough testing.

  • perpetual.go references GetFundingIndexDelta and ModifyFundingIndex with proper error handling.
  • perpetual_test.go includes multiple test cases for ModifyFundingIndex.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the integration of the new funding index update logic with the rest of the system.

# Test: Search for the usage of `ModifyFundingIndex` and `GetFundingIndexDelta`. Expect: Proper integration and error handling.
rg --type go $'ModifyFundingIndex|GetFundingIndexDelta' ./protocol/x/perpetuals/keeper/

Length of output: 1268


1065-1065: Ensure that the negation of bigNetSettlementPpm is always correct, as this affects financial calculations directly. Consider adding a unit test specifically for this logic to verify its correctness under various scenarios.

Verification successful

The negation logic for bigNetSettlementPpm in the GetSettlementPpmWithPerpetual function is correctly implemented. The function ensures that bigNetSettlementPpm is always negated, as required by the financial calculations.

  • The function GetSettlementPpmWithPerpetual is located in protocol/x/perpetuals/keeper/perpetual.go.
  • The negation logic is explicitly handled with the line bigNetSettlementPpm = bigNetSettlementPpm.Neg(bigNetSettlementPpm).
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of the negation logic in `GetSettlementPpmWithPerpetual`.

# Test: Search for the usage of `GetSettlementPpmWithPerpetual`. Expect: Correct implementation of negation logic.
rg --type go $'GetSettlementPpmWithPerpetual' ./protocol/x/perpetuals/keeper/

Length of output: 271



Script:

#!/bin/bash
# Extract the implementation of the function `GetSettlementPpmWithPerpetual` for review.
ast-grep --lang go --pattern $'func GetSettlementPpmWithPerpetual($_, $_, $_, $_) ($_, $_) {
  $$$
}' ./protocol/x/perpetuals/keeper/

Length of output: 134



Script:

#!/bin/bash
# Extract the implementation of the function `GetSettlementPpmWithPerpetual` for review with a more flexible pattern.
ast-grep --lang go --pattern $'func GetSettlementPpmWithPerpetual($$$) {
  $$$
}' ./protocol/x/perpetuals/keeper/

Length of output: 114



Script:

#!/bin/bash
# Locate the function definition of `GetSettlementPpmWithPerpetual` and extract the surrounding lines for review.
rg --type go --context 20 'func GetSettlementPpmWithPerpetual' ./protocol/x/perpetuals/keeper/

Length of output: 3204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants