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

Use vault table rather than placeholder config flags to fetch vaults. #2364

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

vincentwschau
Copy link
Contributor

@vincentwschau vincentwschau commented Sep 26, 2024

Changelist

Fetch vaults from the database rather than using the placeholder flags.

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 asynchronous function for retrieving vault subaccount mappings directly from the database, improving data accuracy and reliability.
  • Bug Fixes
    • Removed outdated configuration properties related to vaults, streamlining the setup process and enhancing test clarity.

@vincentwschau vincentwschau requested a review from a team as a code owner September 26, 2024 16:12
Copy link
Contributor

coderabbitai bot commented Sep 26, 2024

Walkthrough

The changes involve the removal of configuration properties related to vaults and their markets, transitioning from a configuration-based approach to a database-driven method for managing vault subaccount mappings. Tests have been updated to create necessary vault entries directly in the database instead of relying on configurations. Additionally, several methods in the vault controller have been modified to utilize the new asynchronous function for retrieving vault mappings.

Changes

File Path Change Summary
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts Removed configuration settings for EXPERIMENT_VAULTS and EXPERIMENT_VAULT_MARKETS; updated tests to create vault entries directly in the database.
indexer/services/comlink/src/config.ts Removed EXPERIMENT_VAULTS and EXPERIMENT_VAULT_MARKETS properties from configSchema.
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts Introduced getVaultMapping function for database-driven retrieval of vault mappings; updated several methods to use this new function.

Possibly related PRs

  • [TRA-571] Add resolution argument to vault PnL endpoint. #2282: The changes in this PR enhance the vault controller's historical PnL endpoints by introducing a resolution parameter, which is relevant to the modifications in the vault-controller.test.ts file of the main PR that also focuses on improving test setups related to vaults.
  • Add current equity as a pnl tick. #2335: This PR adds functionality to retrieve the current equity of vaults for PnL calculations, which aligns with the main PR's focus on improving the clarity and accuracy of tests related to vaults and their states.

Suggested labels

protocol, proto

🐰 In the land of code, where vaults do play,
Configuration's gone, now we seed the way.
With tests that create, and mappings anew,
Our rabbit hearts leap, for the changes are true!
Hopping through functions, async and bright,
We celebrate progress, in the coding delight! 🎉


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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (5)
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts (5)

117-121: LGTM: Test setup now uses VaultTable.create() instead of config flags.

This change aligns with the PR objective of using the vault table rather than placeholder config flags. It provides a more accurate representation of the database state for testing.

Consider extracting the vault creation into a separate helper function to improve test readability and reusability, especially if this pattern is repeated in other tests.


155-166: LGTM: Test setup for multiple vaults uses VaultTable.create().

The change to use VaultTable.create() for setting up multiple vaults is consistent with the PR objective and previous modifications. The use of Promise.all() for creating multiple vaults concurrently is a good practice for efficiency.

Consider extracting the vault creation logic into a helper function to reduce duplication and improve maintainability, especially if this pattern is used in multiple tests.


261-272: LGTM: Consistent use of VaultTable.create() for multiple vaults.

The change to use VaultTable.create() for setting up multiple vaults is consistent with previous modifications and the PR objective. The continued use of Promise.all() for creating multiple vaults concurrently is good for efficiency.

To improve consistency and reduce code duplication across all test cases, consider creating a helper function for vault creation. This function could accept an array of vault configurations and use Promise.all() to create them. This would make the test setup more concise and easier to maintain.


332-336: LGTM: Consistent use of VaultTable.create() for test setup.

The modification to use VaultTable.create() for setting up the test data is consistent with previous changes and the PR objective. This approach provides a more accurate representation of the database state for testing.

For consistency with the previous test cases and to improve code reusability, consider using the helper function suggested earlier for vault creation. This would make the test setup more uniform across all test cases.


Line range hint 1-500: Overall LGTM with suggestions for improvement

The changes in this file consistently implement the PR objective of using the vault table instead of placeholder config flags. All test cases have been updated to use VaultTable.create() for setting up test data, which provides a more accurate representation of the database state for testing.

To further improve the code:

  1. Create a helper function for vault creation that can be reused across all test cases. This function could accept an array of vault configurations and use Promise.all() to create them concurrently.
  2. Update all test cases to use this helper function, which will make the test setup more concise, consistent, and easier to maintain.

These refactoring steps will enhance code reusability and maintainability without changing the functionality or intent of the tests.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3019562 and 2af0f51.

📒 Files selected for processing (3)
  • indexer/services/comlink/tests/controllers/api/v4/vault-controller.test.ts (7 hunks)
  • indexer/services/comlink/src/config.ts (0 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (8 hunks)
💤 Files not reviewed due to no reviewable changes (1)
  • indexer/services/comlink/src/config.ts
🔇 Additional comments (2)
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts (2)

15-15: LGTM: Import of VaultTable is appropriate for the changes.

The addition of VaultTable to the imports is consistent with the new approach of directly creating vault entries in the database for test setup, aligning with the PR's objective of using the vault table instead of placeholder config flags.


220-224: LGTM: Consistent use of VaultTable.create() for test setup.

The modification to use VaultTable.create() for setting up the test data is consistent with previous changes and the PR objective. This approach provides a more accurate representation of the database state for testing.

Comment on lines 458 to 470
async function getVaultMapping(): Promise<VaultMapping> {
const vaults: VaultFromDatabase[] = await VaultTable.findAll(
{},
[],
{}
);
return _.zipObject(
vaults.map((vault: VaultFromDatabase): string => {
return SubaccountTable.uuid(vault.address, 0);
}),
vaults.map((vault: VaultFromDatabase): string => {
return vault.clobPairId;
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize database query in getVaultMapping()

In the getVaultMapping() function, consider specifying the fields to retrieve in VaultTable.findAll() to limit the data fetched from the database to only the necessary fields (address, clobPairId). This can improve performance by reducing unnecessary data transfer.

Apply this diff to select only the required fields:

 async function getVaultMapping(): Promise<VaultMapping> {
   const vaults: VaultFromDatabase[] = await VaultTable.findAll(
     {},
-    [],
+    ['address', 'clobPairId'],
     {}
   );
   return  _.zipObject(
     vaults.map((vault: VaultFromDatabase): string => {
       return SubaccountTable.uuid(vault.address, 0);
     }),
     vaults.map((vault: VaultFromDatabase): string => {
       return vault.clobPairId;
     }),
   );
 }
📝 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
async function getVaultMapping(): Promise<VaultMapping> {
const vaults: VaultFromDatabase[] = await VaultTable.findAll(
{},
[],
{}
);
return _.zipObject(
vaults.map((vault: VaultFromDatabase): string => {
return SubaccountTable.uuid(vault.address, 0);
}),
vaults.map((vault: VaultFromDatabase): string => {
return vault.clobPairId;
}),
async function getVaultMapping(): Promise<VaultMapping> {
const vaults: VaultFromDatabase[] = await VaultTable.findAll(
{},
['address', 'clobPairId'],
{}
);
return _.zipObject(
vaults.map((vault: VaultFromDatabase): string => {
return SubaccountTable.uuid(vault.address, 0);
}),
vaults.map((vault: VaultFromDatabase): string => {
return vault.clobPairId;
}),

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

🧹 Outside diff range and nitpick comments (2)
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (2)

Line range hint 72-82: LGTM: Updated to use database-driven vault mapping

The changes align with the PR objective of fetching vaults from the database instead of using placeholder config flags. The modification to getVaultSubaccountPnlTicks call is consistent with its updated function signature.

Consider caching the result of getVaultMapping() to improve performance, as it's called multiple times in different methods (e.g., lines 72, 114, 166). If the vault mappings don't change frequently during the lifecycle of a request, you could cache the result at the class level or use a memoization technique.


458-471: LGTM with suggestions: New function to fetch vault mapping from database

The new getVaultMapping function correctly implements fetching vault data from the database and creating the required mapping. This aligns with the PR objective.

  1. Consider optimizing the database query by specifying only the required fields:
 const vaults: VaultFromDatabase[] = await VaultTable.findAll(
   {},
-  [],
+  ['address', 'clobPairId'],
   {},
 );
  1. Add error handling for the database query:
try {
  const vaults: VaultFromDatabase[] = await VaultTable.findAll(...);
  // ... rest of the function
} catch (error) {
  console.error('Error fetching vault data:', error);
  throw new Error('Failed to fetch vault mapping from database');
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2af0f51 and 1f31f38.

📒 Files selected for processing (2)
  • indexer/services/comlink/tests/controllers/api/v4/vault-controller.test.ts (7 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (8 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • indexer/services/comlink/tests/controllers/api/v4/vault-controller.test.ts
🔇 Additional comments (3)
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (3)

25-26: LGTM: New imports for database interaction

The addition of VaultTable and VaultFromDatabase imports aligns with the PR objective of fetching vaults from the database instead of using placeholder config flags.


Line range hint 114-124: LGTM: Consistent update to use database-driven vault mapping

The changes in this method are consistent with those in getMegavaultHistoricalPnl, correctly implementing the transition to database-driven vault mapping.


166-166: LGTM: Consistent update to use database-driven vault mapping

The change in this method is consistent with the updates in other methods, correctly implementing the transition to database-driven vault mapping.

@vincentwschau vincentwschau merged commit 35a70aa into main Sep 26, 2024
16 checks passed
@vincentwschau vincentwschau deleted the vincentc/use-vaults-table branch September 26, 2024 17:32
mergify bot pushed a commit that referenced this pull request Sep 26, 2024
vincentwschau added a commit that referenced this pull request Sep 26, 2024
… (backport #2364) (#2367)

Co-authored-by: vincentwschau <99756290+vincentwschau@users.noreply.github.com>
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