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-571] Add resolution argument to vault PnL endpoint. #2282

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

vincentwschau
Copy link
Contributor

@vincentwschau vincentwschau commented Sep 17, 2024

Changelist

Allow fetching for hourly or daily PnL points for vault endpoints.

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

Release Notes

  • New Features

    • Introduced a resolution parameter for historical PnL API endpoints, allowing users to specify data intervals ("hour" or "day").
    • Enhanced vault controller functionality to support the new resolution parameter for more flexible PnL data retrieval.
  • Bug Fixes

    • Improved test suite for vault controller's historical PnL endpoints, enhancing clarity and reliability.
  • Documentation

    • Updated API documentation to include new parameters and their definitions for better user guidance.
  • Configuration

    • Added a new configuration parameter, VAULT_PNL_HISTORY_DAYS, to manage PnL history duration.

@vincentwschau vincentwschau requested a review from a team as a code owner September 17, 2024 21:03
Copy link

linear bot commented Sep 17, 2024

Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Walkthrough

The changes involve enhancements to the vault controller's handling of historical Profit and Loss (PnL) data and the associated API endpoints. Key modifications include the introduction of a resolution query parameter for two API endpoints, updates to testing structures for PnL tick data, and the addition of new configuration and type definitions to support these features. Overall, the updates aim to improve the functionality and maintainability of the vault-related components.

Changes

File Path Change Summary
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts Enhanced testing for historical PnL endpoints, added createPnlTicks helper function, refactored test cases for clarity and maintainability.
indexer/services/comlink/public/api-documentation.md Added resolution query parameter to two API endpoints and documented the new PnlTickInterval schema.
indexer/services/comlink/public/swagger.json Introduced PnlTickInterval schema and updated API endpoints to include the resolution parameter.
indexer/services/comlink/src/config.ts Added VAULT_PNL_HISTORY_DAYS configuration parameter to manage PnL history duration.
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts Modified vault controller methods to accept resolution parameter for PnL data retrieval.
indexer/services/comlink/src/types.ts Added MegavaultHistoricalPnlRequest and VaultsHistoricalPnlRequest interfaces for PnL requests.

Possibly related PRs

Suggested labels

indexer

Poem

🐇 In the vaults where profits grow,
A new resolution starts to flow.
With ticks of time, we’ll see the gain,
In every test, our efforts reign.
So hop along, let’s cheer and play,
For clearer paths in PnL today! 🌟


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.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
indexer/services/comlink/public/api-documentation.md (1)

5972-5973: Typo: Use "an" instead of "a" before words starting with a vowel sound.

Please fix the typos in the schema definition comments:

  • Line 5972: Change "a id" to "an id".
  • Line 5973: Change "a id" to "an id".
Tools
LanguageTool

[misspelling] ~5972-~5972: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: ...val <a id=...

(EN_A_VS_AN)


[misspelling] ~5973-~5973: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: .../a> <a id="to...

(EN_A_VS_AN)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4fc6109 and f636b7b.

Files selected for processing (6)
  • indexer/services/comlink/tests/controllers/api/v4/vault-controller.test.ts (9 hunks)
  • indexer/services/comlink/public/api-documentation.md (3 hunks)
  • indexer/services/comlink/public/swagger.json (3 hunks)
  • indexer/services/comlink/src/config.ts (1 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (8 hunks)
  • indexer/services/comlink/src/types.ts (2 hunks)
Additional context used
LanguageTool
indexer/services/comlink/public/api-documentation.md

[misspelling] ~5972-~5972: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: ...val <a id=...

(EN_A_VS_AN)


[misspelling] ~5973-~5973: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: .../a> <a id="to...

(EN_A_VS_AN)

Additional comments not posted (21)
indexer/services/comlink/src/config.ts (1)

66-68: Approve the addition of the VAULT_PNL_HISTORY_DAYS configuration parameter.

The new configuration parameter VAULT_PNL_HISTORY_DAYS with a default value of 90 days is a useful addition for controlling the duration of historical PnL data maintenance for vaults.

Verify that this parameter is used consistently across the codebase wherever historical PnL data handling is involved. Run the following script to check for usage:

Verification successful

Approve the addition and usage of the VAULT_PNL_HISTORY_DAYS configuration parameter

The VAULT_PNL_HISTORY_DAYS parameter has been correctly implemented in the config file and is being used appropriately in the vault-controller.ts file. The parameter is converted to seconds for practical application, which is a logical approach.

While the usage is limited to a single controller file, this appears to be intentional and focused on its specific purpose related to vault PnL history. The config object is widely imported across the codebase, ensuring that the parameter can be easily accessed by other parts of the system if needed in the future.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of VAULT_PNL_HISTORY_DAYS parameter in the codebase.

# Test: Search for the parameter usage. Expect: Relevant occurrences across the codebase.
rg --type typescript $'VAULT_PNL_HISTORY_DAYS' -g '!config.ts'

Length of output: 96


Script:

#!/bin/bash
# Description: Verify usage of VAULT_PNL_HISTORY_DAYS parameter in the codebase.

# Search for VAULT_PNL_HISTORY_DAYS in .ts and .tsx files, excluding config.ts
echo "Searching for VAULT_PNL_HISTORY_DAYS in .ts and .tsx files:"
rg 'VAULT_PNL_HISTORY_DAYS' --type-add 'ts:*.{ts,tsx}' -g '!config.ts' -g '*.{ts,tsx}'

# Search for files that import or use the config object
echo -e "\nSearching for files that import or use the config object:"
rg 'import.*from.*config' --type-add 'ts:*.{ts,tsx}' -g '*.{ts,tsx}'

Length of output: 16900

indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (3)

Line range hint 68-83: LGTM!

The changes to introduce the optional resolution parameter and update the getVaultSubaccountPnlTicks function to handle it look good. The aggregation logic and response mapping remain unchanged.


Line range hint 85-117: LGTM!

The changes to introduce the optional resolution parameter and update the getVaultSubaccountPnlTicks function to handle it look good. The grouping and mapping logic remain unchanged.


351-372: LGTM!

The changes to introduce the optional resolution parameter and use the new PnlTicksTable.getPnlTicksAtIntervals method to fetch PnL ticks at the specified interval look good. The function determines the PnL tick interval based on the provided resolution or defaults to a daily interval.

indexer/services/comlink/src/types.ts (2)

21-21: LGTM!

The import statement for PnlTickInterval type looks good.


683-687: Looks good!

The new interfaces MegavaultHistoricalPnlRequest and VaultsHistoricalPnlRequest are well-defined and will enable fetching historical PnL data at different resolutions. The usage of PnlTickInterval type ensures type safety for the resolution property.

This change enhances the functionality and flexibility of the vault-related components.

indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts (10)

414-461: Excellent refactoring!

The createPnlTicks function consolidates the creation of PnL tick entries into a single asynchronous operation, improving the clarity and maintainability of the test setup. It generates multiple PnL ticks with varying timestamps and block heights, which enhances the flexibility of the tests.


Line range hint 77-85: LGTM!

The test case correctly verifies the behavior of the /megavault/historicalPnl endpoint when there are no vault subaccounts. It sets the relevant config values to empty strings and expects an empty array in the response.


86-109: Great use of parameterized tests!

The test case effectively verifies the behavior of the /megavault/historicalPnl endpoint with a single vault subaccount and different resolution query parameters. It uses a parameterized test to cover different scenarios and expects the response to contain the correct PnL ticks based on the resolution.


Line range hint 111-153: Excellent test coverage!

The test case thoroughly verifies the behavior of the /megavault/historicalPnl endpoint with two vault subaccounts and different resolution query parameters. It sets up the necessary config values, creates PnL ticks, and expects the response to contain the correct PnL ticks based on the resolution, with the values doubled due to having two vault subaccounts.


Line range hint 155-166: LGTM!

The test case correctly verifies the behavior of the /vaults/historicalPnl endpoint when there are no vault subaccounts. It sets the relevant config values to empty strings and expects an empty array in the response.


168-194: Great use of parameterized tests!

The test case effectively verifies the behavior of the /vaults/historicalPnl endpoint with a single vault subaccount and different resolution query parameters. It uses a parameterized test to cover different scenarios and expects the response to contain an array of length 1 with the correct PnL ticks based on the resolution.


Line range hint 196-245: Excellent test coverage!

The test case thoroughly verifies the behavior of the /vaults/historicalPnl endpoint with two vault subaccounts and different resolution query parameters. It sets up the necessary config values, creates PnL ticks, and expects the response to contain an array with the correct PnL ticks for each vault subaccount based on the resolution.


Line range hint 247-258: LGTM!

The test case correctly verifies the behavior of the /megavault/positions endpoint when there are no vault subaccounts. It sets the relevant config values to empty strings and expects an empty array for the positions property in the response.


Line range hint 260-326: Comprehensive test case!

The test case thoroughly verifies the behavior of the /megavault/positions endpoint with a single vault subaccount. It sets up the necessary data in the database, including a perpetual position, an asset position, and funding index updates. It then expects the response to contain the correct perpetual and asset positions with calculated values.


Line range hint 328-411: Excellent test coverage!

The test case thoroughly verifies the behavior of the /megavault/positions endpoint with two vault subaccounts, one of which has no perpetual position. It sets up the necessary config values and data in the database, including a perpetual position, asset positions for both subaccounts, and funding index updates. It then expects the response to contain the correct perpetual and asset positions for the first subaccount and only the asset position for the second subaccount.

indexer/services/comlink/public/swagger.json (2)

1499-1505: LGTM!

The PnlTickInterval schema is correctly defined with appropriate enum values for specifying the resolution of PnL tick data.


3434-3443: Looks good!

The resolution query parameter is correctly added to the /vault/v1/megavault/historicalPnl and /vault/v1/vaults/historicalPnl endpoints. It references the PnlTickInterval schema to specify the allowed values for retrieving PnL data at different resolutions. This enhances the API's functionality and flexibility.

Also applies to: 3462-3471

indexer/services/comlink/public/api-documentation.md (3)

3444-3456: LGTM!

The addition of the optional resolution query parameter to the GET /vault/v1/megavault/historicalPnl endpoint looks good. It aligns with the PR objective and maintains backward compatibility by being optional.


3536-3548: Looks good!

The resolution query parameter is added consistently to the GET /vault/v1/vaults/historicalPnl endpoint. The type and optionality match the other endpoint.


5970-5994: The new PnlTickInterval schema definition looks good!

The enumerated values cover the required resolutions of "hour" and "day". The naming is clear and it is used consistently in the resolution query parameters added to the endpoints.

Tools
LanguageTool

[misspelling] ~5972-~5972: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: ...val <a id=...

(EN_A_VS_AN)


[misspelling] ~5973-~5973: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’.
Context: .../a> <a id="to...

(EN_A_VS_AN)

@vincentwschau vincentwschau merged commit e0b5afa into main Sep 18, 2024
16 checks passed
@vincentwschau vincentwschau deleted the vincentc/tra-571-add-resolution-arg branch September 18, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants