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

[IND-482]: Send timing fields for orders when possible #768

Merged
merged 2 commits into from
Nov 8, 2023

Conversation

Christopher-Li
Copy link
Contributor

Changelist

Sends createdAtHeight, updatedAtHeight, and updatedAt for stateful order placements and cancellations. When an order is removed, send updatedAtHeight and updatedAt for all orders and createdAtHeight for stateful orders

Test Plan

Tested with 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 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.

@Christopher-Li Christopher-Li added the bug Something isn't working label Nov 7, 2023
Copy link

linear bot commented Nov 7, 2023

IND-482 Stateful orders are not sending timing fields

When stateful orders are placed and canceled, they send the event to vulcan without any of the timing fields such as updatedAt, updatedAtHeight, and createdAtHeight. These fields should be added to the OffchainUpdateV1, only when the kafka message is sent from Ender

Copy link
Contributor

coderabbitai bot commented Nov 7, 2023

Walkthrough

The changes primarily revolve around the addition of a new optional field createdAtHeight to the OrderCreateObject and OrderFromDatabase interfaces, allowing short-term orders to not have this field. This change is reflected in the test suites and handlers for order placement and removal. Additionally, the OrderPlaceHandler and OrderRemoveHandler classes have been updated to handle new timestamp fields (updatedAt and updatedAtHeight), and the OrderRemoveHandler now handles canceledOrder information.

Changes

File Path Change Summary
indexer/packages/postgres/.../constants.ts
indexer/packages/postgres/src/types/...
Added createdAtHeight property to defaultOrderGoodTilBlockTime object in OrderCreateObject export.
Introduced optional createdAtHeight field to OrderFromDatabase and OrderCreateObject interfaces.
indexer/services/vulcan/.../order-place-handler.test.ts
indexer/services/vulcan/.../order-remove-handler.test.ts
Imported isStatefulOrder function, added isStateful variable, and conditionally included new timestamp fields based on statefulness of the order.
Added new timestamp fields to OrderRemoveHandler test suite and modified removalReason and clientMetadata fields.
indexer/services/vulcan/src/handlers/... Imported IsoString, added updatedAt and updatedAtHeight variables, and handled canceledOrder information in OrderPlaceHandler and OrderRemoveHandler classes.

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 32e07b2 and 44026c4.
Files selected for processing (7)
  • indexer/packages/postgres/tests/helpers/constants.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/order-types.ts (1 hunks)
  • indexer/services/vulcan/tests/handlers/order-place-handler.test.ts (3 hunks)
  • indexer/services/vulcan/tests/handlers/order-remove-handler.test.ts (9 hunks)
  • indexer/services/vulcan/src/handlers/order-place-handler.ts (3 hunks)
  • indexer/services/vulcan/src/handlers/order-remove-handler.ts (5 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/packages/postgres/src/types/db-model-types.ts
Additional comments: 20
indexer/packages/postgres/__tests__/helpers/constants.ts (1)
  • 226-231: The addition of createdAtHeight field to defaultOrderGoodTilBlockTime object is consistent with the PR description. Ensure that this field is being used correctly in the rest of the codebase.
indexer/packages/postgres/src/types/order-types.ts (1)
  • 62-68: The addition of the optional createdAtHeight field to the OrderCreateObject interface is consistent with the PR description. Ensure that all uses of this interface throughout the codebase have been updated to handle this new optional field.
indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts (3)
  • 64-64: The import of isStatefulOrder function is correct and necessary for the changes in this PR.

  • 1080-1080: The isStatefulOrder function is correctly used to determine if an order is stateful.

  • 1107-1109: The conditional inclusion of createdAtHeight, updatedAt, and updatedAtHeight based on the isStateful flag is a good practice to avoid unnecessary data in non-stateful orders.

indexer/services/vulcan/__tests__/handlers/order-remove-handler.test.ts (9)
  • 343-345: The addition of createdAtHeight, updatedAt, and updatedAtHeight fields in the test suite is consistent with the changes described in the pull request summary. Ensure that these fields are being correctly populated and used in the OrderRemoveHandler class.

  • 479-481: Same as above.

  • 616-618: Same as above.

  • 754-756: Same as above.

  • 1036-1038: Same as above.

  • 1150-1152: Same as above.

  • 1271-1273: Same as above.

  • 1408-1410: Same as above.

  • 1540-1542: Same as above.

indexer/services/vulcan/src/handlers/order-remove-handler.ts (4)
  • 13-17: The import of IsoString has been added. Ensure that it is used correctly in the code.

  • 272-286: The createdAtHeight, updatedAt, and updatedAtHeight fields have been added to the canceledOrder object. Ensure that these fields are being populated correctly and that they are being used as intended in the rest of the code.

  • 486-502: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [486-534]

The createdAtHeight, updatedAt, and updatedAtHeight fields have been added to the contents object. Ensure that these fields are being populated correctly and that they are being used as intended in the rest of the code.

  • 572-580: The createdAtHeight, updatedAt, and updatedAtHeight fields have been added to the contents object. Ensure that these fields are being populated correctly and that they are being used as intended in the rest of the code.
indexer/services/vulcan/src/handlers/order-place-handler.ts (2)
  • 16-20: The import of IsoString is new and is used later in the code for type annotation. Ensure that this import is necessary and correctly used.

  • 281-288: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [281-315]

The createdAtHeight, updatedAt, and updatedAtHeight fields are conditionally added to the contents object. This is done correctly, but ensure that the rest of the codebase can handle these fields being undefined.

+ const createdAtHeight: string | undefined = order?.createdAtHeight;
+ const updatedAt: IsoString | undefined = order?.updatedAt;
+ const updatedAtHeight: string | undefined = order?.updatedAtHeight;
+ const contents: SubaccountMessageContents = {
+   orders: [
+     {
+       id: OrderTable.orderIdToUuid(redisOrder.order!.orderId!),
+       subaccountId: SubaccountTable.subaccountIdToUuid(
+         redisOrder.order!.orderId!.subaccountId!,
+       ),
+       clientId: redisOrder.order!.orderId!.clientId.toString(),
+       clobPairId: perpetualMarket.clobPairId,
+       side: protocolTranslations.protocolOrderSideToOrderSide(redisOrder.order!.side),
+       size: redisOrder.size,
+       price: redisOrder.price,
+       status,
+       type: protocolTranslations.protocolConditionTypeToOrderType(
+         redisOrder.order!.conditionType,
+       ),
+       timeInForce: apiTranslations.orderTIFToAPITIF(orderTIF),
+       postOnly: apiTranslations.isOrderTIFPostOnly(orderTIF),
+       reduceOnly: redisOrder.order!.reduceOnly,
+       orderFlags: redisOrder.order!.orderId!.orderFlags.toString(),
+       goodTilBlock: protocolTranslations.getGoodTilBlock(redisOrder.order!)
+         ?.toString(),
+       goodTilBlockTime: protocolTranslations.getGoodTilBlockTime(redisOrder.order!),
+       ticker: redisOrder.ticker,
+       ...(createdAtHeight && { createdAtHeight }),
+       ...(updatedAt && { updatedAt }),
+       ...(updatedAtHeight && { updatedAtHeight }),
+       clientMetadata: redisOrder.order!.clientMetadata.toString(),
+       triggerPrice: getTriggerPrice(redisOrder.order!, perpetualMarket),
+     },
+   ],
+ };

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 32e07b2 and 8720e04.
Files selected for processing (7)
  • indexer/packages/postgres/tests/helpers/constants.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/order-types.ts (1 hunks)
  • indexer/services/vulcan/tests/handlers/order-place-handler.test.ts (3 hunks)
  • indexer/services/vulcan/tests/handlers/order-remove-handler.test.ts (9 hunks)
  • indexer/services/vulcan/src/handlers/order-place-handler.ts (3 hunks)
  • indexer/services/vulcan/src/handlers/order-remove-handler.ts (5 hunks)
Files skipped from review due to trivial changes (3)
  • indexer/packages/postgres/src/types/db-model-types.ts
  • indexer/packages/postgres/src/types/order-types.ts
  • indexer/services/vulcan/tests/handlers/order-remove-handler.test.ts
Additional comments: 8
indexer/services/vulcan/src/handlers/order-place-handler.ts (1)
  • 281-288: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [281-315]

The code correctly handles the optional fields createdAtHeight, updatedAt, and updatedAtHeight. It only includes these fields in the contents object if they are defined.

indexer/packages/postgres/__tests__/helpers/constants.ts (1)
  • 226-231: The addition of createdAtHeight to defaultOrderGoodTilBlockTime is consistent with the pull request summary. Ensure that this new field is handled correctly in all places where this object is used.
indexer/services/vulcan/src/handlers/order-remove-handler.ts (3)
  • 13-16: The addition of IsoString import is fine as it is used later in the code.

  • 277-291: The introduction of canceledOrder variable to store the result of a database query is a good practice. It helps in reducing the number of database calls if the same data is required multiple times.

  • 513-529: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [513-561]

The utilization of canceledOrder in the createSubaccountWebsocketMessageFromRemoveOrderResult method to extract createdAtHeight, updatedAt, and updatedAtHeight values is correct. It helps in providing more detailed information in the websocket message.

indexer/services/vulcan/__tests__/handlers/order-place-handler.test.ts (3)
  • 65-65: The import of isStatefulOrder function is correct and necessary for the changes in this pull request.

  • 1081-1081: The isStateful variable is correctly defined using the isStatefulOrder function.

  • 1108-1110: The conditional inclusion of createdAtHeight, updatedAt, and updatedAtHeight is correctly implemented. Ensure that dbOrder has these properties and they are of the correct type.

Comment on lines 286 to 290
} else {
await runFuncWithTimingStat(
OrderTable.findById(OrderTable.orderIdToUuid(orderRemove.removedOrderId!)),
this.generateTimingStatsOptions('find_order'),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this call needed? I don't see the results being assigned to a variable.

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 8720e04 and eb72a79.
Files selected for processing (2)
  • indexer/services/vulcan/tests/handlers/order-remove-handler.test.ts (9 hunks)
  • indexer/services/vulcan/src/handlers/order-remove-handler.ts (5 hunks)
Additional comments: 13
indexer/services/vulcan/__tests__/handlers/order-remove-handler.test.ts (9)
  • 344-346: The addition of createdAtHeight, updatedAt, and updatedAtHeight fields to the object is consistent with the pull request summary. Ensure that these fields are being correctly populated in the removedOrder object.

  • 481-483: Same as above.

  • 619-621: Same as above.

  • 758-760: Same as above.

  • 1056-1058: Same as above.

  • 1170-1172: Same as above.

  • 1292-1294: Same as above.

  • 1430-1432: Same as above.

  • 1564-1566: Same as above.

indexer/services/vulcan/src/handlers/order-remove-handler.ts (4)
  • 13-17: The import of IsoString is new and is used later in the code. Ensure that it is correctly imported and used.

  • 277-291: The logic to retrieve canceledOrder based on the remaining amount of the order is added. Ensure that the logic is correct and that the canceledOrder is correctly used later in the code.

  • 513-529: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [513-561]

The canceledOrder is now passed as a parameter in createSubaccountWebsocketMessageFromRemoveOrderResult method. Ensure that the method correctly handles the new parameter.

  • 553-559: The createdAtHeight, updatedAt, and updatedAtHeight fields are added to the SubaccountMessageContents object. Ensure that these fields are correctly populated and used.

Comment on lines +286 to +290
} else {
canceledOrder = await runFuncWithTimingStat(
OrderTable.findById(OrderTable.orderIdToUuid(orderRemove.removedOrderId!)),
this.generateTimingStatsOptions('find_order'),
);
Copy link
Contributor

@vincentwschau vincentwschau Nov 8, 2023

Choose a reason for hiding this comment

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

Suggestion: This could probably be optimized by not querying for the order if there isn't an entry in the state filled quantums cache for the order. That would mean no fills for the order, and as it would be a short-term order that means it won't be in the DB. Up to you to add it as a TODO or fix now.

@Christopher-Li Christopher-Li merged commit 16d738f into main Nov 8, 2023
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_order_timing branch November 8, 2023 18:07
vincentwschau pushed a commit that referenced this pull request Nov 9, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
vincentwschau pushed a commit that referenced this pull request Nov 9, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
* [IND-482]: Send timing fields for orders when possible

* nit and fix test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indexer
Development

Successfully merging this pull request may close these issues.

3 participants