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

allow operator and governance to retrieve from vaults #2290

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Sep 18, 2024

Changelist

add MsgRetrieveFromVault and cli for it

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 the ability to retrieve funds from a vault through the new RetrieveFromVault command and associated message types.
    • Enhanced CLI functionality for vault interactions, allowing users to specify parameters for fund retrieval.
  • Bug Fixes

    • Improved error handling for vault retrieval operations, ensuring proper validation of authorities and vault existence.
  • Tests

    • Added comprehensive unit tests for the new retrieval functionality, covering various scenarios to ensure robustness.

@tqin7 tqin7 requested a review from a team as a code owner September 18, 2024 20:57
Copy link

linear bot commented Sep 18, 2024

Copy link
Contributor

coderabbitai bot commented Sep 18, 2024

Walkthrough

This pull request introduces a new functionality to the vault management system by adding the ability to retrieve funds from a vault. It includes the implementation of the retrieveFromVault method in the Msg interface and MsgClientImpl class, along with the definition of new message types MsgRetrieveFromVault and MsgRetrieveFromVaultResponse. Additionally, it updates the CLI commands and the message server to handle retrieval requests, enhancing the overall vault interaction capabilities.

Changes

File Path Change Summary
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.rpc.msg.ts Added retrieveFromVault method in Msg interface and MsgClientImpl class.
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts Introduced MsgRetrieveFromVault and MsgRetrieveFromVaultResponse interfaces, including encoding/decoding methods.
proto/dydxprotocol/vault/tx.proto Added RetrieveFromVault RPC method and associated message types.
protocol/app/msgs/all_msgs.go Added entries for MsgRetrieveFromVault and MsgRetrieveFromVaultResponse.
protocol/app/msgs/normal_msgs.go Added message types for vault retrieval operations.
protocol/app/msgs/normal_msgs_test.go Updated tests to include new message types for vault retrieval.
protocol/x/vault/client/cli/tx.go Added CmdRetrieveFromVault command for CLI interaction.
protocol/x/vault/keeper/msg_server_retrieve_from_vault.go Implemented RetrieveFromVault function in message server for fund retrieval.
protocol/x/vault/keeper/msg_server_retrieve_from_vault_test.go Created tests for RetrieveFromVault functionality, covering various scenarios.

Possibly related PRs

Suggested reviewers

  • vincentwschau

🐇 In the vaults where treasures hide,
A new path opens, side by side.
With funds to gather, swift and bright,
We hop along, in pure delight!
Retrieve with ease, oh what a thrill,
A rabbit's joy, our hearts to fill! 🌟


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>, please review it.
    -- 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 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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Caution

Inline review comments failed to post

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 e0b5afa and d016291.

Files ignored due to path filters (1)
  • protocol/x/vault/types/tx.pb.go is excluded by !**/*.pb.go
Files selected for processing (9)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.rpc.msg.ts (4 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts (2 hunks)
  • proto/dydxprotocol/vault/tx.proto (2 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/normal_msgs.go (1 hunks)
  • protocol/app/msgs/normal_msgs_test.go (1 hunks)
  • protocol/x/vault/client/cli/tx.go (2 hunks)
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault.go (1 hunks)
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault_test.go (1 hunks)
Additional context used
Biome
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts

[error] 233-233: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)


[error] 236-236: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

Additional comments not posted (30)
protocol/x/vault/keeper/msg_server_retrieve_from_vault.go (5)

14-17: LGTM!

The function signature and documentation are clear and accurately describe the purpose of the function.


21-28: LGTM!

The authority validation logic is correct. It checks if the authority is either a module authority or the operator, and returns an appropriate error if the authority is invalid.


30-33: LGTM!

The vault existence check is correct. It retrieves the vault parameters using the provided VaultId and returns an appropriate error if the vault doesn't exist.


35-44: LGTM!

The fund transfer logic is implemented correctly:

  • It uses the correct method TransferFundsFromSubaccountToSubaccount to transfer funds.
  • It correctly specifies the source subaccount using the provided VaultId and the destination subaccount as the main vault.
  • It uses the correct asset type (USDC) for the transfer.
  • It correctly retrieves the transfer amount from the QuoteQuantums field of the message.
  • It propagates any errors returned by the transfer method.

46-47: LGTM!

The function correctly returns an empty response on successful execution and propagates any errors encountered during the process.

indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.rpc.msg.ts (4)

3-3: LGTM!

The import statement correctly includes the new message types MsgRetrieveFromVault and MsgRetrieveFromVaultResponse related to the retrieval functionality.


27-29: LGTM!

The retrieveFromVault method has been correctly added to the Msg interface with the appropriate request and response types. The JSDoc comment provides a clear description of the method's purpose to retrieve funds from a vault to the main vault.


42-42: LGTM!

The retrieveFromVault method is correctly bound in the constructor of the MsgClientImpl class, ensuring it has the right this context when called.


81-85: LGTM!

The retrieveFromVault method has been implemented correctly in the MsgClientImpl class. It follows the same pattern as the other methods:

  1. Encodes the request using MsgRetrieveFromVault.encode(request).finish().
  2. Sends an RPC request using this.rpc.request("dydxprotocol.vault.Msg", "RetrieveFromVault", data).
  3. Decodes the response using MsgRetrieveFromVaultResponse.decode(new _m0.Reader(data)).
  4. Returns a Promise that resolves to the decoded response.

The implementation is consistent and handles the request and response correctly.

protocol/x/vault/client/cli/tx.go (1)

197-245: LGTM!

The CmdRetrieveFromVault function introduces a new CLI command that enables retrieving funds from a vault. The implementation follows the existing patterns and conventions used in the codebase, ensuring consistency and maintainability. Here are the key aspects of the implementation:

  • The function correctly parses the vault type, vault number, and quantums from the command arguments using appropriate utility functions.
  • It constructs a MsgRetrieveFromVault message with the parsed parameters, adhering to the expected message structure.
  • The existing transaction broadcasting mechanism is utilized to handle the transaction, either generating or broadcasting it based on the provided flags.
  • The necessary transaction flags are added to the command, ensuring proper CLI functionality and user interaction.

Overall, the implementation enhances the functionality of the vault management system by providing a convenient way to interact with vaults through the CLI.

proto/dydxprotocol/vault/tx.proto (3)

37-39: LGTM!

The new RetrieveFromVault RPC method is properly defined in the Msg service, following the existing naming conventions and syntax.


159-174: Looks good!

The MsgRetrieveFromVault message is well-defined with appropriate fields and options:

  • The authority field with (cosmos.msg.v1.signer) option ensures that only authorized addresses can perform the retrieval.
  • The vault_id field correctly specifies the source vault for the retrieval.
  • The quote_quantums field uses the SerializableInt custom type for proper serialization and is marked as non-nullable to enforce a valid value.

176-177: Approved.

The MsgRetrieveFromVaultResponse message is defined correctly as an empty message, which is valid for the response type of the RetrieveFromVault RPC method if no specific response data is needed.

protocol/app/msgs/normal_msgs_test.go (1)

154-155: LGTM! The test coverage has been expanded to include the new vault retrieval functionality.

The addition of the new message types MsgRetrieveFromVault and MsgRetrieveFromVaultResponse to the expectedMsgs slice ensures that these messages are properly registered and can be processed by the system. This change enhances the test coverage for the vault-related features.

protocol/x/vault/keeper/msg_server_retrieve_from_vault_test.go (8)

19-253: Comprehensive test suite for RetrieveFromVault functionality.

The TestMsgRetrieveFromVault function provides a thorough set of test cases to validate the behavior of the RetrieveFromVault message under various scenarios. The test cases cover successful retrievals, failures due to insufficient funds, invalid authorities, and attempts to access non-existent vaults.

The use of table-driven testing allows for easy maintenance and extension of the test suite. The test function sets up the mock application environment based on the test case parameters, executes the message, and verifies the outcomes against the expected results.

Overall, this test function ensures the robustness and correctness of the RetrieveFromVault functionality.


36-48: LGTM!

This test case correctly verifies a successful retrieval of 50 quote quantums from Vault Clob 0 by the governance authority. The setup ensures that the retrieval amount is within the available balance of the sub vault, and the vault is in the appropriate status for retrieval.


49-61: Looks good!

This test case correctly verifies a successful retrieval of all quote quantums (3,500,000) from Vault Clob 1 by the operator authority. The setup ensures that the retrieval amount matches the entire balance of the sub vault, and the vault is in the "close_only" status, allowing for a complete withdrawal.


62-75: Test case looks good!

This test case correctly verifies a failure scenario where the requested retrieval amount (27 quote quantums) exceeds the available balance (26 quote quantums) in the sub vault of Vault Clob 0. The setup ensures that the retrieval fails with the expected error message "failed to apply subaccount updates" and that the vault balances remain unchanged.


76-93: Well-crafted test case!

This test case correctly verifies a successful retrieval scenario where the sub vault has an open position and the retrieval amount (1,925,000 quote quantums) exactly meets the initial margin requirement. The setup accurately calculates the open notional, equity, and initial margin requirement based on the position size and the initial margin factor (IMF).

The test case ensures that the retrieval succeeds without any errors and that the vault balances are updated accordingly.


94-112: Excellent test case!

This test case correctly verifies a failure scenario where the sub vault has an open position and the retrieval amount (1,925,001 quote quantums) would result in undercollateralization. The setup accurately calculates the open notional, equity, and initial margin requirement based on the position size and the initial margin factor (IMF).

The test case ensures that the retrieval fails with the expected error message "NewlyUndercollateralized" and that the vault balances remain unchanged.


113-123: Test case looks good!

This test case correctly verifies a failure scenario where the retrieval is attempted from a non-existent vault (Vault Clob 0). The setup ensures that the retrieval fails with the expected error message "ErrVaultParamsNotFound" and that the vault balances remain unchanged.


124-145: Test cases for invalid and empty authority look good!

The test cases "Failure - Invalid Authority" and "Failure - Empty Authority" correctly verify failure scenarios where the retrieval is attempted with an invalid authority or an empty authority.

In the "Failure - Invalid Authority" test case, the operator is set to Bob's address, but the retrieval message specifies Alice's address as the authority. This mismatch should result in an "ErrInvalidAuthority" error.

In the "Failure - Empty Authority" test case, the authority field in the retrieval message is left empty, which should also result in an "ErrInvalidAuthority" error.

Both test cases ensure that the retrieval fails with the expected error message and that the vault balances remain unchanged.

protocol/app/msgs/normal_msgs.go (2)

253-253: LGTM!

The addition of the MsgRetrieveFromVault message type to the NormalMsgsDydxCustom map is correct and consistent with the existing code.


254-254: Looks good!

The addition of the MsgRetrieveFromVaultResponse key-value pair with a nil value is consistent with the existing code and follows the common practice for response message types.

protocol/app/msgs/all_msgs.go (1)

256-257: LGTM!

The addition of the new message types MsgRetrieveFromVault and MsgRetrieveFromVaultResponse aligns with the PR objective of enabling operators and governance to retrieve from vaults. The entries follow the existing naming conventions in the AllTypeMessages map.

indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts (5)

211-219: LGTM!

The MsgRetrieveFromVault interface is well-defined and captures the necessary request parameters for a vault retrieval operation.


869-875: LGTM!

The createBaseMsgRetrieveFromVault function correctly initializes a MsgRetrieveFromVault object with default values.


878-892: LGTM!

The MsgRetrieveFromVault.encode function correctly encodes a MsgRetrieveFromVault message to a binary format using a Writer object, following the protobuf encoding conventions.


894-922: LGTM!

The MsgRetrieveFromVault.decode function correctly decodes a binary-encoded MsgRetrieveFromVault message using a Reader object, following the protobuf decoding conventions.


924-930: LGTM!

The MsgRetrieveFromVault.fromPartial function correctly creates a MsgRetrieveFromVault message from a partial object, handling the field assignments and default values properly.

Comments failed to post (1)
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts (1)

233-233: Use a type alias instead of an empty interface.

An empty interface is equivalent to {}. Consider using a type alias instead for better readability and to avoid the empty interface lint error.

-export interface MsgRetrieveFromVaultResponse {}
+export type MsgRetrieveFromVaultResponse = Record<string, never>;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

export type MsgRetrieveFromVaultResponse = Record<string, never>;
Tools
Biome

[error] 233-233: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d016291 and 51f754f.

Files ignored due to path filters (1)
  • protocol/x/vault/types/tx.pb.go is excluded by !**/*.pb.go
Files selected for processing (9)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.rpc.msg.ts (4 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts (2 hunks)
  • proto/dydxprotocol/vault/tx.proto (2 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/normal_msgs.go (1 hunks)
  • protocol/app/msgs/normal_msgs_test.go (1 hunks)
  • protocol/x/vault/client/cli/tx.go (2 hunks)
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault.go (1 hunks)
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (8)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.rpc.msg.ts
  • proto/dydxprotocol/vault/tx.proto
  • protocol/app/msgs/all_msgs.go
  • protocol/app/msgs/normal_msgs.go
  • protocol/app/msgs/normal_msgs_test.go
  • protocol/x/vault/client/cli/tx.go
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault.go
  • protocol/x/vault/keeper/msg_server_retrieve_from_vault_test.go
Additional context used
Biome
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts

[error] 233-233: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)


[error] 236-236: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

Additional comments not posted (1)
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts (1)

211-219: LGTM!

The MsgRetrieveFromVault interface looks good. The properties are correctly typed and match the naming conventions.

export interface MsgRetrieveFromVaultResponse {}
/** MsgRetrieveFromVaultResponse is the Msg/RetrieveFromVault response type. */

export interface MsgRetrieveFromVaultResponseSDKType {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a type alias instead of an empty interface.

The MsgRetrieveFromVaultResponseSDKType interface is currently empty, which is equivalent to {}. Using an empty interface is not recommended as it can lead to confusion and maintenance issues.

Consider using a type alias instead:

-export interface MsgRetrieveFromVaultResponseSDKType {}
+export type MsgRetrieveFromVaultResponseSDKType = Record<string, never>;

This provides a cleaner and more explicit way to represent an empty type.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export interface MsgRetrieveFromVaultResponseSDKType {}
export type MsgRetrieveFromVaultResponseSDKType = Record<string, never>;
Tools
Biome

[error] 236-236: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

}
/** MsgRetrieveFromVaultResponse is the Msg/RetrieveFromVault response type. */

export interface MsgRetrieveFromVaultResponse {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a type alias instead of an empty interface.

The MsgRetrieveFromVaultResponse interface is currently empty, which is equivalent to {}. Using an empty interface is not recommended as it can lead to confusion and maintenance issues.

Consider using a type alias instead:

-export interface MsgRetrieveFromVaultResponse {}
+export type MsgRetrieveFromVaultResponse = Record<string, never>;

This provides a cleaner and more explicit way to represent an empty type.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export interface MsgRetrieveFromVaultResponse {}
export type MsgRetrieveFromVaultResponse = Record<string, never>;
Tools
Biome

[error] 233-233: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

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.

2 participants