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

[CT-629] Fix entryPrice calc #2455

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Oct 3, 2024

Summary by CodeRabbit

  • Bug Fixes

    • Adjusted test cases for LiquidationHandler and OrderHandler to reflect changes in the defaultPerpetualPosition size, ensuring accurate validation of liquidation orders and order fills.
    • Updated calculations in tests to align with the new expected values for sumOpen and position size.
  • Chores

    • Refined SQL functions for handling entry price calculations, improving accuracy in order fill processing and perpetual position updates.

@dydxwill dydxwill requested a review from a team as a code owner October 3, 2024 17:22
Copy link

linear bot commented Oct 3, 2024

Copy link
Contributor

coderabbitai bot commented Oct 3, 2024

Walkthrough

The pull request introduces modifications to the test suites for the LiquidationHandler and OrderHandler, specifically adjusting the defaultPerpetualPosition size from '10' to '5'. This change impacts various assertions within the tests to ensure they align with the updated size. Additionally, the SQL functions dydx_liquidation_fill_handler_per_order and dydx_update_perpetual_position_aggregate_fields have been updated to use the new size parameter in calculations for entryPrice. The overall structure of the tests and functions remains intact, focusing on refining the handling of liquidation orders.

Changes

File Change Summary
indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts Adjusted defaultPerpetualPosition.size from '10' to '5'; updated assertions and added new test cases for LiquidationHandler.
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts Modified defaultPerpetualPosition.size from '10' to '5'; updated expected values in assertions for OrderHandler.
indexer/services/ender/src/scripts/helpers/dydx_liquidation_fill_handler_per_order.sql Changed calculation of entryPrice to use ABS(perpetual_position_record."size") instead of perpetual_position_record."sumOpen".
indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql Updated calculation of entryPrice to use ABS(perpetual_position_record."size") instead of perpetual_position_record."sumOpen".

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • vincentwschau

Poem

In the fields where rabbits play,
Changes hop, they come to stay.
Liquidations now refined,
Order fills, perfectly aligned.
With a size that's just right,
We celebrate this coding delight! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6349294 and 02bcc8c.

📒 Files selected for processing (2)
  • indexer/services/ender/src/scripts/helpers/dydx_liquidation_fill_handler_per_order.sql (1 hunks)
  • indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • indexer/services/ender/src/scripts/helpers/dydx_liquidation_fill_handler_per_order.sql
  • indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql

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.

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

🧹 Outside diff range and nitpick comments (1)
indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql (1)

Line range hint 1-78: Consider additional improvements and clarifications.

While the main change has been addressed, here are some suggestions to enhance the overall function:

  1. Error Handling: The function raises an exception when no existing perpetual position is found. Consider adding a more graceful error handling mechanism or documenting this behavior clearly in the function description.

  2. Documentation: The function's description could be expanded to include information about the dydx_get_weighted_average, dydx_trim_scale, and dydx_perpetual_position_and_order_side_matching functions. This would provide better context for future maintainers.

  3. Performance: For frequently called functions, consider adding an index on (subaccountId, perpetualId, openEventId) if not already present, to optimize the initial SELECT query.

Could you provide more context on:

  1. The frequency of calls to this function?
  2. The typical volume of data it processes?
  3. Any performance benchmarks or requirements for this function?

This information would help in determining if any further optimizations are necessary.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cbf7574 and 6349294.

📒 Files selected for processing (4)
  • indexer/services/ender/tests/handlers/order-fills/liquidation-handler.test.ts (2 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (2 hunks)
  • indexer/services/ender/src/scripts/helpers/dydx_liquidation_fill_handler_per_order.sql (1 hunks)
  • indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql (1 hunks)
🔇 Additional comments (7)
indexer/services/ender/src/scripts/helpers/dydx_update_perpetual_position_aggregate_fields.sql (1)

47-47: Approved: Entry price calculation reverted as intended.

The change from perpetual_position_record."sumOpen" to perpetual_position_record."size" in the dydx_get_weighted_average function call aligns with the PR objective to revert the previous change in entry price calculation.

To ensure the change doesn't introduce unintended consequences, please verify:

  1. The accuracy of entry price calculations, especially for positions that have been partially closed and reopened.
  2. The consistency of this change with other parts of the codebase that may rely on the entry price calculation.

Consider adding a comment explaining the rationale behind using size instead of sumOpen for future reference. For example:

-- Using 'size' instead of 'sumOpen' for entry price calculation to accurately reflect the current position
entry_price := dydx_get_weighted_average(
    perpetual_position_record."entryPrice", perpetual_position_record."size", price, size
);
indexer/services/ender/src/scripts/helpers/dydx_liquidation_fill_handler_per_order.sql (1)

203-203: Approved: Correction to entry price calculation

This change reverts the entry price calculation to use perpetual_position_record."size" instead of perpetual_position_record."sumOpen" when calculating the weighted average. This modification aligns with the PR objective and likely provides a more accurate representation of the current position size for the entry price calculation.

The impact of this change:

  1. It corrects the entry price calculation, addressing the issue introduced in PR Revert "[CT-629] Fix entryPrice calc" #2425.
  2. The resulting entry_price will now be based on the actual size of the position rather than the sum of open amounts.
  3. This change ensures that the entry price more accurately reflects the current state of the perpetual position.

To ensure this change doesn't introduce any unintended side effects, please verify that:

  1. All other occurrences of dydx_get_weighted_average in the codebase use the correct size parameter.
  2. The perpetual_position_record."size" is always up-to-date and accurately represents the current position size.

You can use the following script to check for other occurrences:

indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts (3)

140-141: Update in default perpetual position size

The size property of defaultPerpetualPosition has been changed from '10' to '5'. This change affects the initial state of the perpetual position used in tests.

This change is likely intentional and may be part of adjusting test scenarios to match updated business logic or requirements.


395-397: Consistent updates to test expectations

The test expectations for sumOpen and other calculations have been updated to reflect the change in the initial position size. This ensures consistency with the modified defaultPerpetualPosition.

These changes maintain the integrity of the test cases and accurately reflect the new initial conditions.


Line range hint 1-1180: Summary of changes

The primary change in this file is the update to the initial perpetual position size from '10' to '5'. This change has been consistently applied throughout the test cases, with appropriate adjustments to calculations and expectations. The core functionality and structure of the tests remain unchanged.

These changes appear to be intentional and well-implemented, maintaining the integrity and purpose of the test suite while adjusting for new initial conditions.

indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (2)

442-442: Approved: Consistent use of sumOpen for perpetual position calculations

This change correctly updates the expectPerpetualPosition function to use defaultPerpetualPosition.sumOpen instead of defaultPerpetualPosition.size when calculating the expected sumOpen value. This modification ensures consistency with the changes made to the defaultPerpetualPosition object and more accurately reflects the intended behavior of the perpetual position calculations.


141-142: Verify the relationship between size and sumOpen

The size property has been reduced from '10' to '5', but the sumOpen property remains at '10'. This results in sumOpen being larger than size, which might be intentional but could also be an oversight. Please verify if this is the desired behavior for the defaultPerpetualPosition object.

✅ Verification successful

No inconsistency found between size and sumOpen for defaultPerpetualPosition.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for other occurrences of defaultPerpetualPosition to ensure consistency
grep -rn "defaultPerpetualPosition" . | grep -v "__tests__"

Length of output: 57

@@ -200,7 +200,7 @@ BEGIN
perpetual_position_record."side", order_side) THEN
sum_open = dydx_trim_scale(perpetual_position_record."sumOpen" + fill_amount);
entry_price = dydx_get_weighted_average(
perpetual_position_record."entryPrice", perpetual_position_record."sumOpen",
perpetual_position_record."entryPrice", ABS(perpetual_position_record."size"),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Possible to add a test-case where perpetual position size is = negative size of fill to test that the previous error case couldn't happen again?

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