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-220] add activation threshold on vaults #1344

Merged
merged 4 commits into from
Apr 11, 2024
Merged

[TRA-220] add activation threshold on vaults #1344

merged 4 commits into from
Apr 11, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Apr 5, 2024

Changelist

do not activate a vault if it has no perp position and has less than some threshold of quote quantums

default value is 1000 USDC

Test Plan

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.

Summary by CodeRabbit

  • New Features

    • Introduced a new parameter to specify the minimum quote asset amount required for vault activation without perpetual positions.
    • Added a new reason for order removal related to isolated subaccount constraints in the API documentation.
  • Bug Fixes

    • Implemented checks to skip processing for vaults not meeting the new activation threshold.
  • Documentation

    • Updated Swagger UI documentation to reflect new enum values and descriptions.
  • Tests

    • Enhanced test coverage to include scenarios with the new activation threshold parameter.
  • Refactor

    • Updated various parts of the code to integrate the new activation threshold logic.
  • Chores

    • Added necessary imports and updated default parameters to support the new features.

Copy link

linear bot commented Apr 5, 2024

Copy link
Contributor

coderabbitai bot commented Apr 5, 2024

Walkthrough

The recent updates aim to improve vault order processing by introducing a new parameter, activationThresholdQuoteQuantums, ensuring vaults meet a minimum USDC threshold before processing orders. These changes impact interfaces, testing environments, and validation checks, providing a comprehensive solution for managing asset quantums effectively.

Changes

File(s) Change Summary
params.ts
params.proto
Added activationThresholdQuoteQuantums field in Params interfaces and message.
msg_server_update_params_test.go
orders.go
sample_pregenesis.json
Included activation_threshold_quote_quantums in test and genesis configurations.
orders.go
keeper/orders.go
Implemented a new check using activationThresholdQuoteQuantums to skip processing for specific vaults.
protocol/client/docs/swagger-ui/swagger.yaml Added a new enum value and descriptions related to order removal reasons.
protocol/scripts/genesis/sample_pregenesis.json Introduced activation_threshold_quote_quantums parameter in the genesis file under the vault section.

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0a3068c and a2b6427.
Files selected for processing (1)
  • protocol/scripts/genesis/sample_pregenesis.json (1 hunks)
Additional Context Used
Path-based Instructions (1)
protocol/scripts/genesis/sample_pregenesis.json (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.

Additional comments not posted (1)
protocol/scripts/genesis/sample_pregenesis.json (1)

1811-1811: The addition of activation_threshold_quote_quantums with a value of "1000000000" correctly implements the intended feature as described in the PR. The JSON structure is maintained, and the value format is consistent with other parameters in this section.


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.

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

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c995787 and dd8cba3.
Files ignored due to path filters (3)
  • protocol/app/testdata/default_genesis_state.json is excluded by !**/*.json
  • protocol/scripts/genesis/sample_pregenesis.json is excluded by !**/*.json
  • protocol/x/vault/types/params.pb.go is excluded by !**/*.pb.go, !**/*.pb.go
Files selected for processing (10)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/params.ts (6 hunks)
  • proto/dydxprotocol/vault/params.proto (2 hunks)
  • protocol/testutil/constants/genesis.go (1 hunks)
  • protocol/x/vault/keeper/msg_server_update_params_test.go (2 hunks)
  • protocol/x/vault/keeper/orders.go (2 hunks)
  • protocol/x/vault/keeper/orders_test.go (10 hunks)
  • protocol/x/vault/keeper/params_test.go (2 hunks)
  • protocol/x/vault/types/errors.go (1 hunks)
  • protocol/x/vault/types/params.go (2 hunks)
  • protocol/x/vault/types/params_test.go (2 hunks)
Additional Context Used
Path-based Instructions (10)
proto/dydxprotocol/vault/params.proto (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.

protocol/x/vault/types/errors.go (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.

protocol/x/vault/types/params.go (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.

protocol/x/vault/keeper/params_test.go (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.

protocol/x/vault/keeper/msg_server_update_params_test.go (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.

protocol/x/vault/types/params_test.go (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/packages/v4-protos/src/codegen/dydxprotocol/vault/params.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.

protocol/x/vault/keeper/orders.go (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.

protocol/x/vault/keeper/orders_test.go (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.

protocol/testutil/constants/genesis.go (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.

Additional comments not posted (22)
proto/dydxprotocol/vault/params.proto (2)

4-4: LGTM!


31-38: LGTM!

protocol/x/vault/types/errors.go (1)

68-72: LGTM!

protocol/x/vault/types/params.go (2)

12-18: LGTM!


40-43: LGTM!

protocol/x/vault/keeper/params_test.go (2)

6-6: LGTM!


23-43: LGTM!

protocol/x/vault/keeper/msg_server_update_params_test.go (2)

6-6: LGTM!


41-47: LGTM!

protocol/x/vault/types/params_test.go (2)

6-6: LGTM!


24-80: LGTM!

indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/params.ts (2)

30-36: LGTM!


81-82: LGTM!

protocol/x/vault/keeper/orders.go (2)

9-13: LGTM!


39-53: LGTM!

protocol/x/vault/keeper/orders_test.go (6)

68-108: > 📝 NOTE

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

The changes from lines 27 to 105 introduce the activationThresholdQuoteQuantums field in test cases, which is crucial for testing the new activation threshold feature for vaults. Ensure that the test cases cover a comprehensive range of scenarios, including edge cases where the asset quantums are exactly equal to the activation threshold. This will help in thoroughly validating the feature's behavior under various conditions.


125-142: The setup for initializing the vault's parameters in the genesis state, specifically the activationThresholdQuoteQuantums, is correctly implemented. This setup is essential for ensuring that the test environment accurately reflects the intended system state with the new activation threshold feature. It's good practice to ensure that all relevant parameters are initialized to their expected values before running the tests.


164-164: The conditional logic from lines 164 onwards, which determines whether to place vault CLOB orders based on the activation threshold, is a critical part of the test. It accurately reflects the intended behavior of the system with the new feature. However, consider adding explicit test assertions to verify that vaults below the activation threshold do not have their orders placed. This would make the tests more robust by explicitly checking for the absence of orders in scenarios where the activation threshold is not met.


187-187: The verification logic for expected orders, starting from line 187, is well-structured and ensures that only orders from vaults meeting the activation threshold are processed. This aligns with the feature's goal of optimizing system efficiency by preventing unnecessary activation of vaults. To further enhance the test's clarity and maintainability, consider abstracting some of the repetitive logic into helper functions or using table-driven tests to manage the various scenarios more concisely.


365-377: > 📝 NOTE

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

The parameter setup for the TestGetVaultClobOrders function, specifically the vaultParams struct, is comprehensive and covers a wide range of scenarios to ensure thorough testing of the new activation threshold feature. It's important to maintain this level of detail in test setups to capture the full spectrum of system behavior under different configurations. Additionally, consider including comments or documentation that explain the rationale behind the chosen parameter values, especially for complex calculations or non-obvious choices. This will aid in understanding the test's intent and make it easier for future maintainers to modify or extend the tests as needed.


442-448: In the test case "Success - Get orders from Vault for Clob Pair 1, No Orders due to Zero Order Size", the scenario where the order size is effectively zero due to a very small vault asset quote quantum is well thought out. It's a good practice to include such edge cases in tests to ensure the system behaves as expected under extreme conditions. However, ensure that similar edge cases are consistently tested across all relevant functions to maintain comprehensive test coverage.

protocol/testutil/constants/genesis.go (1)

1502-1503: The addition of the activation_threshold_quote_quantums field with the value "1000000000" correctly implements the PR's objective.

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 Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dd8cba3 and 0003e3a.
Files ignored due to path filters (1)
  • protocol/x/vault/types/params.pb.go is excluded by !**/*.pb.go
Files selected for processing (3)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/params.ts (6 hunks)
  • proto/dydxprotocol/vault/params.proto (2 hunks)
  • protocol/client/docs/swagger-ui/swagger.yaml (8 hunks)
Files skipped from review as they are similar to previous changes (2)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/params.ts
  • proto/dydxprotocol/vault/params.proto
Additional Context Used
Path-based Instructions (1)
protocol/client/docs/swagger-ui/swagger.yaml (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.

// positions must have to activate, i.e. if a vault has no perpetual positions
// and has strictly less than this amount of quote asset, it will not
// activate.
bytes activation_threshold_quote_quantums = 7 [
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: consider using uint64 why is sufficient for all realistic threshold here (uint64 max = 18 trillion USDC)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i agreed that uint64 is probably sufficient but a deposit to vault is represented as a serializable int already and wanted to keep it consistent

// positions must have to activate, i.e. if a vault has no perpetual positions
// and has strictly less than this amount of quote asset, it will not
// activate.
bytes activation_threshold_quote_quantums = 7 [
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we ever create upgrade handler for x/vault? If so do we need to initialize this value as well?

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 upgrade handler is created for x/vault. this value should be automatically initialized as the upgrade handler calls DefaultParams() to get params to set

@@ -34,6 +36,22 @@ func (k Keeper) RefreshAllVaultOrders(ctx sdk.Context) {
continue
}

// Skip if vault has no perpetual positions and strictly less than `activation_threshold_quote_quantums` USDC.
params := k.GetParams(ctx)
Copy link
Contributor

@teddyding teddyding Apr 9, 2024

Choose a reason for hiding this comment

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

We don't need to query this params for each vaults. Should just query once at the top

params := k.GetParams(ctx)
vault := k.subaccountsKeeper.GetSubaccount(ctx, *vaultId.ToSubaccountId())
if vault.PerpetualPositions == nil || len(vault.PerpetualPositions) == 0 {
usdcQuoteQuantums := dtypes.NewInt(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use subaccount.GetUsdcPosition() instead

@tqin7 tqin7 merged commit f652898 into main Apr 11, 2024
33 checks passed
@tqin7 tqin7 deleted the tq/tra-220 branch April 11, 2024 03:33
@tqin7
Copy link
Contributor Author

tqin7 commented Apr 11, 2024

@Mergifyio backport release/protocol/v5.x

Copy link
Contributor

mergify bot commented Apr 11, 2024

backport release/protocol/v5.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Apr 11, 2024
* add activation threshold on vaults

* fix proto format

* address comments

(cherry picked from commit f652898)

# Conflicts:
#	protocol/client/docs/statik/statik.go
tqin7 added a commit that referenced this pull request Apr 11, 2024
* [TRA-220] add activation threshold on vaults (#1344)

* add activation threshold on vaults

* fix proto format

* address comments

(cherry picked from commit f652898)

# Conflicts:
#	protocol/client/docs/statik/statik.go

* fix statik merge conflict

---------

Co-authored-by: Tian <tian@dydx.exchange>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants