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

[TRA-152] refresh all vault orders #1213

Merged
merged 2 commits into from
Mar 21, 2024
Merged

[TRA-152] refresh all vault orders #1213

merged 2 commits into from
Mar 21, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Mar 20, 2024

Changelist

refresh all vault orders

Test Plan

added 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

linear bot commented Mar 20, 2024

Copy link

coderabbitai bot commented Mar 20, 2024

Walkthrough

The updates involve enhancing the vault functionality within a blockchain protocol, introducing new constants, and refining the management of vault orders. A focus on testing ensures the reliability of these features, particularly through the addition of tests for endblocker functionality and order refresh mechanisms. Modifications in the keeper logic cater to different vault types and improve the handling of vault orders, showcasing a concerted effort to bolster the protocol's vault operations.

Changes

File(s) Change Summary
protocol/lib/metrics/constants.go Added constants for Vault functionality including order actions, VaultType, and VaultId.
protocol/x/vault/abci_test.go Introduced TestEndBlocker to test endblocker functionality related to deposits.
protocol/x/vault/keeper/orders.go Added imports for dtypes, log, and metrics. Removed import for satypes. Added RefreshAllVaultOrders method to refresh orders for vaults. Added RefreshVaultClobOrders method to refresh CLOB vault orders. Updated logic within RefreshAllVaultOrders to handle different vault types. Updated GetVaultClobOrders method to use vaultId.ToSubaccountId() instead of satypes.SubaccountId.
protocol/x/vault/keeper/orders_test.go Added import for dtypes from github.com/dydxprotocol/v4-chain/protocol/dtypes. Added TestRefreshAllVaultOrders function to refresh vault orders. Added TestRefreshVaultClobOrders function to refresh specific vault orders. Minor adjustments in TestGetVaultClobOrders.
protocol/x/vault/keeper/shares.go Added a function to get an iterator over all TotalShares.
protocol/x/vault/types/vault_id.go, .../vault_id_test.go Enhanced vault ID functionality with ToSubaccountId and metrics, and added tests for these.

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.

Copy link

@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 42cbee6 and c400abd.
Files selected for processing (7)
  • protocol/lib/metrics/constants.go (1 hunks)
  • protocol/x/vault/abci_test.go (1 hunks)
  • protocol/x/vault/keeper/orders.go (5 hunks)
  • protocol/x/vault/keeper/orders_test.go (3 hunks)
  • protocol/x/vault/keeper/shares.go (2 hunks)
  • protocol/x/vault/types/vault_id.go (2 hunks)
  • protocol/x/vault/types/vault_id_test.go (2 hunks)
Additional comments: 10
protocol/x/vault/types/vault_id_test.go (1)
  • 33-50: The addition of TestToSubaccountId effectively tests the conversion of vault IDs to subaccount IDs using constants and assertions. This is a good practice for ensuring the correctness of the ToSubaccountId method.
protocol/x/vault/keeper/shares.go (1)
  • 44-49: The addition of the getTotalSharesIterator function follows best practices for iterating over key-value pairs in Cosmos SDK applications, including the correct use of a prefix store and the deferment of iterator closure. This is a solid implementation for retrieving an iterator over all TotalShares.
protocol/x/vault/types/vault_id.go (1)
  • 28-57: The additions in vault_id.go, including the ToSubaccountId and IncrCounterWithLabels methods, are well-implemented. They follow best practices for constructing subaccount IDs from vault IDs and for enhancing monitoring capabilities with vault-specific metrics labels.
protocol/x/vault/keeper/orders.go (3)
  • 38-61: The introduction of the RefreshAllVaultOrders method is a significant addition that enhances the system's ability to maintain accurate and up-to-date vault orders. This method, along with its implementation details, follows best practices and adheres to the project's architectural patterns.
  • 63-107: The RefreshVaultClobOrders method adds specific functionality for refreshing CLOB vault orders. Its implementation, including error handling and metrics incrementing, is well-structured and adheres to best practices.
  • 151-157: > 📝 NOTE

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

The update to the GetVaultClobOrders method to use vaultId.ToSubaccountId() simplifies the code and improves maintainability. This change, along with the detailed logic for placing new CLOB orders, is well-implemented.

protocol/x/vault/keeper/orders_test.go (3)
  • 27-139: The tests for RefreshAllVaultOrders are comprehensive, covering various scenarios and ensuring the functionality works as expected across different vault states. These tests are well-structured and follow best practices for unit testing.
  • 141-215: The tests for RefreshVaultClobOrders effectively validate the functionality for refreshing specific vault orders, including handling success cases and expected errors. The structure and assertions used in these tests adhere to best practices.
  • 24-219: > 📝 NOTE

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

The adjustments in TestGetVaultClobOrders align with the changes in the main codebase and ensure the tests remain relevant and accurate. The detailed scenarios tested here contribute to the robustness of the system's testing strategy.

protocol/lib/metrics/constants.go (1)
  • 246-250: The newly added constants related to vault functionality (VaultCancelOrder, VaultPlaceOrder, VaultType, and VaultId) are correctly named and follow the existing naming convention. They are also appropriately placed in alphabetical order within their category.

However, it's advisable to periodically review the entire list of constants for potential duplication or unused constants, ensuring the codebase remains clean and maintainable.


import "testing"

// TODO (TRA-168): add endblocker test once deposit is implemented.
Copy link

Choose a reason for hiding this comment

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

The TODO comment indicates that an endblocker test will be added once the deposit functionality is implemented. It's important to ensure that this TODO is tracked in a project management tool or issue tracker to ensure it's addressed in a timely manner.

Would you like me to open a GitHub issue to track this task?


// Refresh orders depending on vault type.
switch vaultId.Type {
case types.VaultType_VAULT_TYPE_CLOB:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the only vault type we support currently right?

nit: add comment that no other vault types exist

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Add a default case and error there.

@@ -30,10 +32,79 @@ const (
)

// RefreshAllVaultOrders refreshes all orders for all vaults by
// TODO(TRA-134)
// 1. Cancelling all existing orders.
// 2. Placing new orders.
func (k Keeper) RefreshAllVaultOrders(ctx sdk.Context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

will it be a final PR at the end that ties this to app.go?

Where is this called, would this be in CommitTx or in PrepareProposal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already tied (see here). At the end of every block, this will be called

if _, exists := k.clobKeeper.GetLongTermOrderPlacement(ctx, order.OrderId); exists {
err := k.clobKeeper.HandleMsgCancelOrder(ctx, clobtypes.NewMsgCancelOrderStateful(
order.OrderId,
uint32(ctx.BlockTime().Unix())+ORDER_EXPIRATION_SECONDS,
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we want to wait 5 seconds instead of just cancelling this now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this cancels the order now (doesn't wait for 5 seconds). have to set this to be currentBlockTime + 5 for this cancellation message to be valid

return err
}
for _, order := range ordersToPlace {
err := k.clobKeeper.HandleMsgPlaceOrder(ctx, clobtypes.NewMsgPlaceOrder(*order))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make the optimization if the order already exists, do not remove it? Would save us two operations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

every block, we want to refresh these orders so that their sizes and prices are updated

Copy link
Contributor

Choose a reason for hiding this comment

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

if the oracle price doesn't update, and nobody has filled us, in the current strategy, all order prices and sizes should be the same.
I don't know how often the situation above happens, or how much faster we would run if we made this optimization, but there is definitely a scenario where our orders don't change between blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep there can be. created a ticket here to work on optimization later


// Check that there's no stateful orders yet.
allStatefulOrders := tApp.App.ClobKeeper.GetAllStatefulOrders(ctx)
require.Len(t, allStatefulOrders, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Should we test that this is cancelling existing orders?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call!

Copy link

@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 c400abd and 8a2b8c5.
Files selected for processing (2)
  • protocol/x/vault/keeper/orders.go (5 hunks)
  • protocol/x/vault/keeper/orders_test.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/x/vault/keeper/orders.go
  • protocol/x/vault/keeper/orders_test.go

return err
}
for _, order := range ordersToCancel {
if _, exists := k.clobKeeper.GetLongTermOrderPlacement(ctx, order.OrderId); exists {
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 method able to find orders that are partially filled?
For example, if the vault had half of its order filled, it would still get canceled in RefreshVaultClobOrders, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct

@tqin7 tqin7 merged commit 1788384 into main Mar 21, 2024
17 checks passed
@tqin7 tqin7 deleted the tq/tra-152 branch March 21, 2024 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants