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

Refactor out looking up clob pair into SQL function. #784

Merged
merged 1 commit into from
Nov 9, 2023
Merged

Conversation

lcwik
Copy link
Contributor

@lcwik lcwik commented Nov 9, 2023

Changelist

Refactor out looking up clob pair into SQL function.

Test Plan

No change.

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 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 Nov 9, 2023

Walkthrough

The changes primarily involve the introduction of a new PostgreSQL function dydx_get_perpetual_market_for_clob_pair that retrieves the perpetual market record for a given clob pair ID. This function is then used to replace repetitive blocks of code in several scripts, improving code readability and maintainability. Additionally, the type of perpetual_position_record variable is modified in the dydx_update_perpetual_position_aggregate_fields function.

Changes

File Path Change Summary
.../postgres/postgres-functions.ts Added a new SQL script file 'dydx_get_perpetual_market_for_clob_pair.sql' to the list of scripts.
.../dydx_get_perpetual_market_for_clob_pair.sql Introduced a new PostgreSQL function dydx_get_perpetual_market_for_clob_pair to retrieve the perpetual market record for a given clob pair ID.
.../dydx_liquidation_fill_handler_per_order.sql
.../dydx_order_fill_handler_per_order.sql
.../dydx_stateful_order_handler.sql
Replaced repetitive blocks of code for fetching perpetual market data with a call to the new function dydx_get_perpetual_market_for_clob_pair.
.../dydx_update_perpetual_position_aggregate_fields.sql Modified the type of perpetual_position_record variable to match the row type of the perpetual_positions table.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

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

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3cd0a64 and 8666dda.
Files selected for processing (6)
  • indexer/services/ender/src/helpers/postgres/postgres-functions.ts (1 hunks)
  • indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql (1 hunks)
  • indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql (1 hunks)
  • indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql (1 hunks)
  • indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql (2 hunks)
  • indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql (1 hunks)
Files skipped from review due to trivial changes (2)
  • indexer/services/ender/src/helpers/postgres/postgres-functions.ts
  • indexer/services/ender/src/scripts/dydx_stateful_order_handler.sql
Additional comments: 4
indexer/services/ender/src/scripts/dydx_get_perpetual_market_for_clob_pair.sql (1)
  • 1-23: The new function dydx_get_perpetual_market_for_clob_pair is well defined and handles exceptions for no data found and too many rows. It returns a perpetual_markets type. Ensure that the function is being used correctly in the rest of the codebase.
indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql (1)
  • 15-21: The change in the declaration of perpetual_position_record to match the row type of the perpetual_positions table is a good practice. It ensures that the variable will have the same structure as the table, which can prevent potential issues if the table structure changes in the future.
indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql (1)
  • 52-55: The retrieval of the perpetual_market_record has been abstracted into the dydx_get_perpetual_market_for_clob_pair function. This improves code readability and maintainability. Ensure that the new function handles all edge cases and exceptions that were previously handled in this script.
indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql (1)
  • 46-49: The new function dydx_get_perpetual_market_for_clob_pair is used here to retrieve the perpetual market data. Ensure that this function is correctly implemented and tested, as it is now a critical part of the dydx_order_fill_handler_per_order function.

Comment on lines 51 to 52
BEGIN
SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
Copy link
Contributor

Choose a reason for hiding this comment

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

The query to retrieve the asset record is not wrapped in a try-catch block. If the query fails for any reason (e.g., the asset with the given ID does not exist), it will cause the entire function to fail. Consider adding error handling here.

+    BEGIN
        SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
+    EXCEPTION
+        WHEN NO_DATA_FOUND THEN
+            RAISE EXCEPTION 'Unable to find asset with id %', usdc_asset_id;
+    END;

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
BEGIN
SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
BEGIN
SELECT * INTO STRICT asset_record FROM assets WHERE "id" = usdc_asset_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION 'Unable to find asset with id %', usdc_asset_id;
END;

@lcwik lcwik merged commit cfd2be7 into main Nov 9, 2023
11 checks passed
@lcwik lcwik deleted the lukeindrefactor branch November 9, 2023 23:01
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