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-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg sent from fills/liquidations/deleveraging Ender handlers #1603

Merged
merged 4 commits into from
May 31, 2024

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented May 30, 2024

Changelist

Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg sent from fills/liquidations/deleveraging Ender handlers.

Test Plan

Unit tested.

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.

Summary by CodeRabbit

  • New Features

    • Enhanced market data handling, including updates to perpetual positions.
  • Improvements

    • Improved PnL (Profit and Loss) calculations for perpetual positions.
  • Bug Fixes

    • Addressed issues related to market data updates and perpetual position handling.

These changes aim to provide more accurate market data and better performance in updating perpetual positions, enhancing overall user experience in market-related functionalities.

Copy link

linear bot commented May 30, 2024

Copy link
Contributor

coderabbitai bot commented May 30, 2024

Warning

Rate limit exceeded

@dydxwill has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 0 minutes and 51 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 00dee64 and b03bc59.

Walkthrough

Recent updates in the indexer service involve enhancements to market data handling and perpetual position updates. New data structures and logic were added to manage market information and annotate positions with profit and loss (PnL) calculations. Changes span across test helpers, order handling, and liquidation handling, ensuring consistent updates and improved functionality.

Changes

File(s) Change Summary
indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts Added declarations for various market-related entities and logic for handling market data and updating perpetual positions.
indexer/services/ender/src/handlers/order-fills/order-handler.ts Imported new entities and modified logic for market handling and perpetual position updates with PnL annotations.
indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts Added imports and logic for market data handling, perpetual position updates with PnL annotations, and lodash integration.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant OrderHandler
    participant MarketDatabase
    participant PnLAnnotator
    participant PositionUpdater

    OrderHandler->>MarketDatabase: Fetch Market Data
    MarketDatabase-->>OrderHandler: Market Data
    OrderHandler->>PnLAnnotator: Annotate with PnL
    PnLAnnotator-->>OrderHandler: Annotated Data
    OrderHandler->>PositionUpdater: Update Perpetual Position
    PositionUpdater-->>OrderHandler: Confirmation
Loading
sequenceDiagram
    participant LiquidationHandler
    participant MarketDatabase
    participant PnLAnnotator
    participant PositionUpdater

    LiquidationHandler->>MarketDatabase: Fetch Market Data
    MarketDatabase-->>LiquidationHandler: Market Data
    LiquidationHandler->>PnLAnnotator: Annotate with PnL
    PnLAnnotator-->>LiquidationHandler: Annotated Data
    LiquidationHandler->>PositionUpdater: Update Perpetual Position
    PositionUpdater-->>LiquidationHandler: Confirmation
Loading

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.
    • @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 as 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.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4eb219b and aeb1da6.

Files selected for processing (2)
  • indexer/services/ender/tests/helpers/indexer-proto-helpers.ts (3 hunks)
  • indexer/services/ender/src/handlers/order-fills/order-handler.ts (2 hunks)
Additional context used
Path-based instructions (2)
indexer/services/ender/src/handlers/order-fills/order-handler.ts (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.

indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts (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.

Biome
indexer/services/ender/src/handlers/order-fills/order-handler.ts

[error] 39-39: This type annotation is trivially inferred from its initialization.


[error] 49-49: Forbidden non-null assertion.


[error] 49-49: Forbidden non-null assertion.


[error] 50-50: Forbidden non-null assertion.


[error] 50-50: Forbidden non-null assertion.


[error] 52-52: Forbidden non-null assertion.


[error] 54-54: Forbidden non-null assertion.


[error] 85-85: Forbidden non-null assertion.


[error] 85-85: Forbidden non-null assertion.


[error] 87-87: Forbidden non-null assertion.


[error] 87-87: Forbidden non-null assertion.


[error] 119-119: Forbidden non-null assertion.


[error] 135-135: Forbidden non-null assertion.


[error] 1-20: Some named imports are only used as types.


[error] 22-25: All these imports are only used as types.


[error] 26-27: All these imports are only used as types.


[error] 27-28: All these imports are only used as types.


[error] 33-34: All these imports are only used as types.


[error] 34-35: All these imports are only used as types.

indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts

[error] 1-1: A Node.js builtin module should be imported with the node: protocol.


[error] 96-96: This type annotation is trivially inferred from its initialization.


[error] 186-186: Unexpected any. Specify a different type.


[error] 201-201: Unexpected any. Specify a different type.


[error] 251-251: Unexpected any. Specify a different type.


[error] 262-262: Unexpected any. Specify a different type.


[error] 306-306: Unexpected any. Specify a different type.


[error] 319-319: Unexpected any. Specify a different type.


[error] 424-424: Unexpected any. Specify a different type.


[error] 666-666: This type annotation is trivially inferred from its initialization.


[error] 667-667: This type annotation is trivially inferred from its initialization.


[error] 668-668: This type annotation is trivially inferred from its initialization.


[error] 686-686: Forbidden non-null assertion.


[error] 690-690: Forbidden non-null assertion.


[error] 714-714: This type annotation is trivially inferred from its initialization.


[error] 715-715: This type annotation is trivially inferred from its initialization.


[error] 716-716: This type annotation is trivially inferred from its initialization.


[error] 722-722: Forbidden non-null assertion.


[error] 727-727: Forbidden non-null assertion.


[error] 728-728: Forbidden non-null assertion.

Additional comments not posted (6)
indexer/services/ender/src/handlers/order-fills/order-handler.ts (3)

14-19: The imports for MarketFromDatabase, MarketTable, MarketsMap, MarketColumns, perpetualMarketRefresher, and UpdatedPerpetualPositionSubaccountKafkaObject are correctly added to support the new functionality related to market and position updates.


31-31: The import of annotateWithPnl and convertPerpetualPosition is essential for the new functionality that calculates and annotates profit and loss on perpetual positions. Ensure that these functions are thoroughly unit tested given their critical role in financial calculations.

Verification successful

The functions annotateWithPnl and convertPerpetualPosition are indeed referenced in test files, specifically in indexer/services/ender/__tests__/handlers/subaccount-update-handler.test.ts and indexer/services/ender/__tests__/helpers/kafka-helper.test.ts. These references indicate that the functions are being tested.

  • indexer/services/ender/__tests__/handlers/subaccount-update-handler.test.ts
  • indexer/services/ender/__tests__/helpers/kafka-helper.test.ts
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify unit tests for `annotateWithPnl` and `convertPerpetualPosition`.

# Test: Search for unit tests related to the functions.
rg --type python "test_annotateWithPnl|test_convertPerpetualPosition"

Length of output: 102



Script:

#!/bin/bash
# Description: Verify unit tests for `annotateWithPnl` and `convertPerpetualPosition`.

# Test: Search for the function definitions in the codebase.
rg --type ts "function annotateWithPnl|function convertPerpetualPosition"

# Test: Search for unit tests related to the functions.
rg --type ts "annotateWithPnl|convertPerpetualPosition" --files-with-matches | xargs -I {} rg --type ts "test" {}

Length of output: 3571



Script:

#!/bin/bash
# Description: Verify unit tests for `annotateWithPnl` and `convertPerpetualPosition`.

# Test: Search for test files in the repository.
test_files=$(fd --type f --extension ts --glob '*test*')

# Test: Check if any of these test files contain references to `annotateWithPnl` or `convertPerpetualPosition`.
for file in $test_files; do
  if rg -q 'annotateWithPnl|convertPerpetualPosition' "$file"; then
    echo "Found references in: $file"
    rg 'annotateWithPnl|convertPerpetualPosition' "$file"
  fi
done

Length of output: 36234


110-110: The method generateConsolidatedKafkaEvent is used here to push updates. It's crucial that this method handles the data correctly and ensures data integrity, especially since it deals with financial information.

indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts (3)

32-38: The addition of MarketFromDatabase, MarketTable, MarketsMap, MarketColumns, and UpdatedPerpetualPositionSubaccountKafkaObject in the imports section supports the new functionality for handling market and position data in unit tests. This is a good practice to ensure that the new data structures are covered by tests.


67-67: The import of annotateWithPnl and convertPerpetualPosition here is crucial for testing the new PnL functionality. It's important to ensure that these helper functions are used correctly in the tests to prevent any inaccuracies in PnL calculations.

Verification successful

The search results indicate that both annotateWithPnl and convertPerpetualPosition are indeed used in various test files, confirming their integration into the testing framework for the new PnL functionality.

  • indexer/services/ender/__tests__/helpers/kafka-helper.test.ts
  • indexer/services/ender/__tests__/handlers/subaccount-update-handler.test.ts

These files contain test cases that utilize the annotateWithPnl and convertPerpetualPosition functions, ensuring that the PnL calculations are being tested.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify correct usage of `annotateWithPnl` and `convertPerpetualPosition` in tests.

# Test: Search for their usage in test scenarios.
rg --type python "test_annotateWithPnl|test_convertPerpetualPosition"

Length of output: 102



Script:

#!/bin/bash
# Description: Verify correct usage of `annotateWithPnl` and `convertPerpetualPosition` in tests.

# Test: Search for their usage in test scenarios.
rg --type ts "annotateWithPnl|convertPerpetualPosition"

Length of output: 4339


812-827: This block of code is responsible for fetching market data and updating perpetual positions with PnL information in a test environment. It's critical to ensure that the test mimics the production behavior accurately, especially in handling edge cases and error scenarios.

Comment on lines 89 to 105
const markets: MarketFromDatabase[] = await MarketTable.findAll(
{},
[],
{ txId: this.txId },
);
const marketIdToMarket: MarketsMap = _.keyBy(
markets,
MarketColumns.id,
);
let perpUpdate: UpdatedPerpetualPositionSubaccountKafkaObject = convertPerpetualPosition(
position,
);
perpUpdate = annotateWithPnl(
perpUpdate,
perpetualMarketRefresher.getPerpetualMarketsMap(),
marketIdToMarket,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The implementations of MarketTable.findAll and annotateWithPnl do not show explicit handling for edge cases such as empty market data or incorrect market IDs. This is critical as it involves financial data processing.

  • MarketTable.findAll does not include checks for empty results or invalid IDs.
  • annotateWithPnl assumes valid data is passed without additional validation.

Please review and ensure these methods handle edge cases appropriately.

  • indexer/packages/postgres/src/stores/market-table.ts
  • indexer/services/ender/src/helpers/kafka-helper.ts
Analysis chain

The changes from lines 89 to 105 introduce a new logic for fetching market data and updating perpetual positions with PnL calculations. This is a critical section as it involves financial data processing. Ensure that the logic correctly handles edge cases such as empty market data or incorrect market IDs.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify handling of edge cases in market data processing.

# Test: Check for error handling related to empty market data or incorrect market IDs.
rg --type python "if not markets|if 'id' not in market"

Length of output: 94



Script:

#!/bin/bash
# Description: Verify handling of edge cases in market data processing.

# Test: Check for error handling related to empty market data or incorrect market IDs in TypeScript files.
rg --type ts "if (!markets|if (!marketIdToMarket\[)"

Length of output: 146



Script:

#!/bin/bash
# Description: Verify handling of edge cases in market data processing.

# Test: Check for error handling related to empty market data or incorrect market IDs in TypeScript files.
rg --type ts "if \(!markets\)|if \(!marketIdToMarket\["

Length of output: 57



Script:

#!/bin/bash
# Description: Verify handling of edge cases in market data processing.

# Test: Locate the implementation of the MarketTable.findAll method.
rg "MarketTable\.findAll"

# Test: Locate the implementation of the annotateWithPnl method.
rg "annotateWithPnl"

Length of output: 7630



Script:

#!/bin/bash
# Description: Verify handling of edge cases in market data processing.

# Test: Locate the definition of the MarketTable.findAll method.
rg "findAll" --type ts

# Test: Locate the definition of the annotateWithPnl method.
rg "function annotateWithPnl" --type ts

Length of output: 42530



Script:

#!/bin/bash
# Description: Retrieve the implementation details of the MarketTable.findAll method.

# Display the content of the market-table.ts file to inspect the findAll method.
rg "export async function findAll" -A 20 indexer/packages/postgres/src/stores/market-table.ts

# Display the content of the kafka-helper.ts file to inspect the annotateWithPnl method.
rg "export function annotateWithPnl" -A 20 indexer/services/ender/src/helpers/kafka-helper.ts

Length of output: 1254

@dydxwill dydxwill changed the title [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg send from fills/liquidations Ender handler May 30, 2024
@dydxwill dydxwill changed the title [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg send from fills/liquidations Ender handler [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg sent from fills/liquidations Ender handler May 30, 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aeb1da6 and 00dee64.

Files selected for processing (2)
  • indexer/services/ender/tests/helpers/indexer-proto-helpers.ts (4 hunks)
  • indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts (4 hunks)
Files not reviewed due to errors (1)
  • indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts (no review received)
Additional context used
Path-based instructions (2)
indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts (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.

indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts (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.

Biome
indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts

[error] 43-43: This type annotation is trivially inferred from its initialization.


[error] 55-55: Forbidden non-null assertion.


[error] 55-55: Forbidden non-null assertion.


[error] 57-57: Forbidden non-null assertion.


[error] 59-59: Forbidden non-null assertion.


[error] 67-80: This else clause can be omitted because previous branches break early.


[error] 68-68: Forbidden non-null assertion.


[error] 70-70: Forbidden non-null assertion.


[error] 131-131: Forbidden non-null assertion.


[error] 137-137: Forbidden non-null assertion.


[error] 137-137: Forbidden non-null assertion.


[error] 145-145: Forbidden non-null assertion.


[error] 145-145: Forbidden non-null assertion.


[error] 155-155: Forbidden non-null assertion.


[error] 155-155: Forbidden non-null assertion.


[error] 159-172: This else clause can be omitted because previous branches break early.


[error] 162-162: Forbidden non-null assertion.


[error] 1-19: Some named imports are only used as types.


[error] 21-24: All these imports are only used as types.


[error] 25-26: All these imports are only used as types.

indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts

[error] 1-1: A Node.js builtin module should be imported with the node: protocol.


[error] 96-96: This type annotation is trivially inferred from its initialization.


[error] 186-186: Unexpected any. Specify a different type.


[error] 201-201: Unexpected any. Specify a different type.


[error] 251-251: Unexpected any. Specify a different type.


[error] 262-262: Unexpected any. Specify a different type.


[error] 306-306: Unexpected any. Specify a different type.


[error] 319-319: Unexpected any. Specify a different type.


[error] 424-424: Unexpected any. Specify a different type.


[error] 666-666: This type annotation is trivially inferred from its initialization.


[error] 667-667: This type annotation is trivially inferred from its initialization.


[error] 668-668: This type annotation is trivially inferred from its initialization.


[error] 685-685: Forbidden non-null assertion.


[error] 703-703: Forbidden non-null assertion.


[error] 731-731: This type annotation is trivially inferred from its initialization.


[error] 732-732: This type annotation is trivially inferred from its initialization.


[error] 733-733: This type annotation is trivially inferred from its initialization.


[error] 739-739: Forbidden non-null assertion.


[error] 744-744: Forbidden non-null assertion.


[error] 745-745: Forbidden non-null assertion.

Additional comments not posted (3)
indexer/services/ender/__tests__/helpers/indexer-proto-helpers.ts (3)

32-38: The addition of new type declarations such as MarketFromDatabase, MarketTable, MarketsMap, MarketColumns, and UpdatedPerpetualPositionSubaccountKafkaObject supports the enhanced handling of market data and perpetual positions. These types are essential for the new functionality introduced in the PR.


67-67: The import of annotateWithPnl and convertPerpetualPosition from ../../src/helpers/kafka-helper is crucial for the new functionality to annotate perpetual positions with profit and loss data. This aligns with the changes in the liquidation-handler.ts file.


684-700: This block introduces logic similar to that in liquidation-handler.ts, where perpetual positions are converted and annotated with PnL data. The use of MarketTable.findAll and _.keyBy for handling market data is consistent with the other file. Ensure that the logic is correctly handling edge cases and that the data integrity is maintained throughout the process.

@dydxwill dydxwill changed the title [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg sent from fills/liquidations Ender handler [CT-874] Add realizedPnl/unrealizedPnl to consolidated subaccount websocket msg sent from fills/liquidations/deleveraging Ender handlers May 30, 2024
@dydxwill dydxwill merged commit e61b387 into main May 31, 2024
11 checks passed
@dydxwill dydxwill deleted the wl/add_pnl branch May 31, 2024 17:19
@dydxwill
Copy link
Contributor Author

https://github.com/Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented May 31, 2024

backport release/indexer/v5.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request May 31, 2024
…socket msg sent from fills/liquidations/deleveraging Ender handlers (#1603)

(cherry picked from commit e61b387)
dydxwill added a commit that referenced this pull request May 31, 2024
…socket msg sent from fills/liquidations/deleveraging Ender handlers (backport #1603) (#1609)

Co-authored-by: dydxwill <119354122+dydxwill@users.noreply.github.com>
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.

3 participants