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

OIMF: Simplify interface for GetDeltaOpenInterestFromUpdates #1227

Merged
merged 2 commits into from
Mar 22, 2024

Conversation

teddyding
Copy link
Contributor

@teddyding teddyding commented Mar 22, 2024

Changelist

Simply GetDeltaOpenInterestFromUpdates interface as well as upstream usage

Test Plan

unit tests

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Copy link
Contributor

coderabbitai bot commented Mar 22, 2024

Walkthrough

The update involves refactoring the functionality related to calculating the delta of open interest from perpetual match updates into a more generic approach that can handle various update types. This is achieved by renaming and repurposing GetDeltaOpenInterestFromPerpMatchUpdates to GetDeltaOpenInterestFromUpdates and introducing a structured return type OpenInterestDelta. Additionally, the logic for calculating the perpetual open interest delta within the subaccount update process has been modularized into this new function for clearer code structure. Test cases have also been updated to align with these changes.

Changes

Files Change Summary
.../keeper/oimf.go, .../keeper/oimf_test.go Refactored GetDeltaOpenInterestFromPerpMatchUpdates to GetDeltaOpenInterestFromUpdates for handling multiple update types. Introduced OpenInterestDelta return type. Updated test cases.
.../keeper/subaccount.go Extracted perpOpenInterestDelta calculation into the new GetDeltaOpenInterestFromUpdates function for better readability.

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-tests 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 tests 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 tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@teddyding teddyding marked this pull request as ready for review March 22, 2024 21:03
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 58a0f11 and 67b7fa5.
Files selected for processing (3)
  • protocol/x/subaccounts/keeper/oimf.go (4 hunks)
  • protocol/x/subaccounts/keeper/oimf_test.go (13 hunks)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)
Additional comments: 8
protocol/x/subaccounts/keeper/oimf.go (4)
  • 47-52: The function GetDeltaOpenInterestFromUpdates now handles various update types, specifically returning nil for non-Match updates. This change simplifies the handling of updates by centralizing the logic for determining the delta open interest based on the update type. However, it's important to ensure that all callers of this function are aware of this behavior and handle the nil return value appropriately to avoid potential nil dereferences.
  • 56-58: The check for updateType != types.Match returning nil directly is a clear and concise way to filter out non-relevant update types for this function. This approach keeps the function focused on its intended purpose and simplifies the logic for handling different types of updates.
  • 85-94: > 📝 NOTE

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

The validation logic within GetDeltaOpenInterestFromUpdates for ensuring the correct number of updates and the consistency of perpetual IDs across updates is robust and thorough. It correctly panics on invalid input, which is appropriate given that these conditions represent programming errors that should be caught during development rather than at runtime in a production environment. However, it's crucial to ensure that these panic conditions are well-documented in the function's comments so that callers are aware of the potential for panics and can design their code accordingly to avoid these scenarios.

  • 102-118: The logic for calculating the base quantums delta and constructing the OpenInterestDelta return value is clear and logically sound. It correctly aggregates the delta long from each settled update and constructs the return value based on the aggregated result. This approach is efficient and minimizes the complexity of the calculation. However, it's important to ensure that the function's documentation clearly explains the expected format and contents of the settledUpdates parameter, especially regarding the structure and meaning of the PerpetualUpdates within each SettledUpdate, to aid future maintainers or users of this function.
protocol/x/subaccounts/keeper/oimf_test.go (3)
  • 24-29: The renaming of the test function to TestGetDeltaOpenInterestFromUpdates and the inclusion of an updateType field in the test cases are appropriate changes that align with the modifications made to the GetDeltaOpenInterestFromUpdates function. This ensures that the tests accurately reflect the new functionality and are capable of covering the different scenarios that the function is expected to handle. It's important to ensure comprehensive test coverage, especially for the new logic paths introduced by handling different update types.
  • 175-187: > 📝 NOTE

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

The test cases have been well-structured to cover a variety of scenarios, including invalid configurations and valid updates with different outcomes. This thorough testing approach is commendable as it ensures that the GetDeltaOpenInterestFromUpdates function behaves as expected across a wide range of inputs. However, it's crucial to also include test cases that specifically target the edge cases introduced by the new logic, such as handling updates of types other than Match and ensuring that the function returns nil as expected in those cases. Adding these test cases will further strengthen the test suite and ensure that all aspects of the function's behavior are adequately covered.

  • 367-383: The use of require.PanicsWithValue and require.Equal to validate the behavior of GetDeltaOpenInterestFromUpdates in both panic and non-panic scenarios is a good practice. It ensures that the function's error handling and return values are as expected. However, it's important to ensure that the error messages and return values used in these assertions accurately reflect the function's implementation to avoid false negatives in the test results. Additionally, consider adding comments to the test cases to explain the rationale behind each scenario, especially for the more complex or less intuitive cases, to aid future maintainers.
protocol/x/subaccounts/keeper/subaccount.go (1)
  • 645-648: Extracting the logic for calculating perpOpenInterestDelta to the GetDeltaOpenInterestFromUpdates function is a significant improvement in terms of code modularity and readability. This change not only simplifies the internalCanUpdateSubaccounts function but also centralizes the logic related to open interest delta calculation, making it easier to maintain and understand. It's important to ensure that the integration between these functions is thoroughly tested, especially to verify that the GetDeltaOpenInterestFromUpdates function is correctly handling all update types and scenarios as expected.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 67b7fa5 and 4615c82.
Files selected for processing (1)
  • protocol/x/subaccounts/keeper/oimf.go (4 hunks)
Additional comments: 3
protocol/x/subaccounts/keeper/oimf.go (3)
  • 47-53: The changes from lines 47 to 53 introduce a new function signature for GetDeltaOpenInterestFromUpdates, which now returns a *perptypes.OpenInterestDelta and takes settledUpdates and updateType as parameters. This refactoring aims to simplify the interface and improve the function's capability to handle various types of updates.

While the function signature and the initial checks (lines 57-59 and 60-92) are clear and align with the PR objectives, it's important to ensure that the documentation comments (lines 47-53) accurately describe the behavior of the function, especially regarding how it handles different update types. The comment mentions that for Match updates, it returns a struct OpenInterestDelta or nil if the OI delta is zero, and panics if the update format is invalid. For other update types, it returns nil. This is a significant change from the previous implementation and should be clearly documented to avoid confusion.

Consider expanding the documentation to include examples of different update types and how they are handled by the function. This will provide clearer guidance for future developers and maintainers.

  • 86-95: > 📝 NOTE

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

The changes from lines 60 to 92 implement the logic to handle Match updates by checking the number of settledUpdates and their PerpetualUpdates. The function panics with specific error messages if the updates do not meet the expected criteria (e.g., must have two updates, must update one perpetual, must be the same perpetual ID).

This approach ensures that the function operates on well-defined inputs, reducing the risk of unexpected behavior. However, the use of panic for error handling in this context might be too aggressive, especially if this function is called frequently or in scenarios where a non-terminating error handling strategy would be preferable.

Evaluate the impact of using panic for error handling in this context. Consider whether returning an error would be more appropriate, allowing the caller to decide how to handle these conditions. This could improve the robustness and flexibility of the error handling strategy.

  • 103-119: The changes from lines 103 to 119 implement the core logic of the GetDeltaOpenInterestFromUpdates function, calculating the baseQuantumsDelta from the settledUpdates and returning an OpenInterestDelta struct if the delta is non-zero.

This implementation is concise and leverages the getDeltaLongFromSettledUpdate helper function to compute the delta long for each update. The use of big.Int for arithmetic operations ensures accuracy for large numbers, which is crucial in financial calculations.

However, there's an opportunity to improve the readability and efficiency of this code segment. Specifically, the loop (lines 103-109) iterates over settledUpdates to accumulate baseQuantumsDelta, which is straightforward but could benefit from a more descriptive comment explaining the rationale behind this calculation, especially for developers unfamiliar with the domain.

Consider adding a comment above the loop (lines 103-109) explaining the purpose of accumulating baseQuantumsDelta from the settledUpdates. This will enhance the readability and maintainability of the code.

@teddyding teddyding merged commit 8629884 into main Mar 22, 2024
17 checks passed
@teddyding teddyding deleted the td/update-oi branch March 22, 2024 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants