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

Get latest hourly tick to compute final tick for megavault PnL. #2454

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

vincentwschau
Copy link
Contributor

@vincentwschau vincentwschau commented Oct 3, 2024

Changelist

For all resolutions, get the latest hourly PnL tick to compute the final "current" tick. This ensures the final PnL tick when looking at daily resolution to have the most update to date PnL snapshot (computed hourly).

Test Plan

[Describe how this PR was tested (if applicable)]

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 configuration parameter for vault settings, enhancing customization options.
    • Added functionality to retrieve the latest PnL tick for vault subaccounts, improving data granularity.
  • Bug Fixes
    • Improved time and block height management in tests for the vault controller, ensuring accuracy.
  • Documentation
    • Updated test cases to reflect changes in time handling and block height calculations.

@vincentwschau vincentwschau requested a review from a team as a code owner October 3, 2024 17:18
Copy link
Contributor

coderabbitai bot commented Oct 3, 2024

Walkthrough

The pull request introduces modifications to the vault controller's test file, enhancing the handling of block heights and time representations. It adds new time-related variables and updates existing ones for clarity. Additionally, a new configuration parameter is introduced in the configuration schema. The VaultController class is updated with a new method to retrieve the latest PnL tick, which integrates with existing methods to improve data granularity. Overall, the changes aim to refine the management of time and block heights in the vault's functionality and testing.

Changes

File Change Summary
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts Renamed currentBlockHeight to currentHourBlockHeight, added currentDayBlockHeight, and updated time-related variables. Adjusted test cases and assertions for new structure.
indexer/services/comlink/src/config.ts Added new configuration parameter VAULT_LATEST_PNL_TICK_WINDOW_HOURS with a default value of 1.
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts Introduced getLatestPnlTick function and updated getPnlTicksWithCurrentTick to include an optional latestTick parameter. Enhanced logic to include the latest tick in responses.

Possibly related PRs

Suggested labels

indexer, backport/indexer/v7.x

🐇 In the vault where numbers play,
Time ticks on, both night and day.
New heights we chart, with care we seek,
Latest ticks, our data's peak!
With every change, our tests align,
In this rabbit hole, all's by design! 🕰️✨


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 (6)
indexer/services/comlink/src/config.ts (1)

65-65: New configuration parameter added for vault PnL tick window

The new configuration parameter VAULT_LATEST_PNL_TICK_WINDOW_HOURS has been added with a default value of 1 hour. This aligns with the PR objective of enhancing the computation of the final "current" tick by retrieving the latest hourly PnL tick.

A few points to consider:

  1. The default value of 1 hour seems reasonable for retrieving the latest tick, but it might be worth documenting why this specific default was chosen.
  2. It may be beneficial to add a comment explaining the purpose and impact of this configuration parameter for future maintainers.

Consider adding a brief comment above this line to explain the purpose of this configuration, for example:

// Window in hours for retrieving the latest PnL tick for vault calculations
VAULT_LATEST_PNL_TICK_WINDOW_HOURS: parseInteger({ default: 1 }),
indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts (4)

Line range hint 44-59: New configuration for latest PnL tick window

The introduction of VAULT_LATEST_PNL_TICK_WINDOW_HOURS set to 48 hours aligns with the PR objective of retrieving the latest hourly PnL tick. This change enhances the flexibility of the system.

Consider adding a comment explaining the purpose and significance of the VAULT_LATEST_PNL_TICK_WINDOW_HOURS configuration.


140-147: Updated test parameters for new PnL tick behavior

The addition of finalTickIndex and the update to expected ticks for hourly resolution reflect the new behavior of including the latest hourly PnL tick in the results. This change aligns well with the PR objective.

Consider adding a comment explaining the significance of finalTickIndex to improve test readability.


179-186: Updated test assertions for different resolutions

The changes to expected tick indices for different resolutions accurately reflect the new behavior of including or excluding certain ticks based on the resolution and the latest PnL tick window. The use of undefined values appropriately indicates scenarios where certain ticks should not be present.

Consider adding comments explaining the significance of undefined values in the expected tick indices to improve test readability.


227-228: Updated total PnL calculation

The comment explaining that total PnL should be fetched from the latest hourly PnL tick aligns with the PR objective. The update to multiply by 4 instead of 3 in the total PnL calculation likely accounts for an additional tick or data point.

Consider adding a brief explanation for why the multiplication factor is now 4 instead of 3 to improve code clarity.

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

489-505: Add unit tests and documentation for getLatestPnlTick function.

The new function getLatestPnlTick retrieves the latest hourly PnL tick. To ensure correctness and handle edge cases (e.g., no available ticks within the window), please add unit tests covering various scenarios. Additionally, include inline documentation to explain the function's purpose and logic for future maintainers.

Would you like assistance in writing unit tests or adding documentation for this function?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8fdf8c5 and b5ecf95.

📒 Files selected for processing (3)
  • indexer/services/comlink/tests/controllers/api/v4/vault-controller.test.ts (14 hunks)
  • indexer/services/comlink/src/config.ts (1 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (4 hunks)
🔇 Additional comments (10)
indexer/services/comlink/src/config.ts (1)

65-65: Verify usage of the new configuration parameter

The new configuration parameter VAULT_LATEST_PNL_TICK_WINDOW_HOURS has been added to the schema, but its usage is not visible in this file. To ensure it's properly integrated into the system:

Please run the following script to check where and how this new configuration is being used:

This will help confirm that the new configuration is being utilized correctly in the relevant parts of the codebase for computing the final "current" tick as mentioned in the PR objectives.

✅ Verification successful

Retry searching for the usage of the new configuration parameter with correct file types

The previous search command failed due to an unrecognized file type. Please run the following script to search for VAULT_LATEST_PNL_TICK_WINDOW_HOURS in .ts and .tsx files:


Retry searching for the usage of the new configuration parameter with correct file patterns

The previous search command failed due to unrecognized file types. Please run the following script to search for VAULT_LATEST_PNL_TICK_WINDOW_HOURS in .ts and .tsx files using glob patterns:


Usage of VAULT_LATEST_PNL_TICK_WINDOW_HOURS Verified

The new configuration parameter VAULT_LATEST_PNL_TICK_WINDOW_HOURS is properly utilized in the codebase:

  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts
  • indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for usage of VAULT_LATEST_PNL_TICK_WINDOW_HOURS in the codebase

# Test: Search for the usage of the new configuration parameter
rg --type typescript --type javascript 'VAULT_LATEST_PNL_TICK_WINDOW_HOURS'

Length of output: 110


Script:

#!/bin/bash
# Description: Search for usage of VAULT_LATEST_PNL_TICK_WINDOW_HOURS in TypeScript files

rg --type ts --type tsx 'VAULT_LATEST_PNL_TICK_WINDOW_HOURS'

Length of output: 88


Script:

#!/bin/bash
# Description: Search for usage of VAULT_LATEST_PNL_TICK_WINDOW_HOURS in TypeScript files

rg --glob '*.ts' --glob '*.tsx' 'VAULT_LATEST_PNL_TICK_WINDOW_HOURS'

Length of output: 755

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

28-38: Improved clarity in time and block height variables

The renaming of currentBlockHeight to currentHourBlockHeight and the addition of currentDayBlockHeight provide a clearer distinction between hourly and daily contexts. This change aligns well with the PR objective of enhancing the computation of the final "current" tick by retrieving the latest hourly PnL tick.


Line range hint 76-93: Additional block creation for current day and hour

The addition of block creation for currentDay and currentHour ensures that the test data includes blocks for both daily and hourly contexts. This change is necessary for thoroughly testing the new functionality related to retrieving the latest hourly PnL tick.


127-127: Proper reset of new configuration

Resetting the VAULT_LATEST_PNL_TICK_WINDOW_HOURS configuration after each test ensures proper test isolation. This is a good practice to prevent unintended side effects between tests.


Line range hint 156-160: Correct creation of final PnL tick

The creation of finalTick using finalTickIndex and setting its blockHeight, blockTime, and createdAt to the latest values ensures that it accurately represents the most recent data. This is crucial for computing the final "current" tick as per the PR objective.


542-551: Addition of current hour PnL tick

The creation of a new PnL tick for the current hour with a doubled total PnL value enhances the test coverage for the latest hourly PnL tick retrieval. This addition allows for more comprehensive testing of the new functionality, aligning well with the PR objective.


574-583: Additional PnL ticks for vault subaccount

The creation of new PnL ticks for the current day and current hour for the vault subaccount ensures comprehensive test data. This addition is crucial for thoroughly testing the new functionality related to retrieving the latest hourly PnL tick across all subaccounts.


610-619: Additional PnL ticks for main subaccount

The creation of new PnL ticks for the current day and current hour for the main subaccount completes the comprehensive test data set. This addition ensures that the new functionality for retrieving the latest hourly PnL tick is tested across all subaccount types, including the main subaccount.

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

83-95: Verify error handling and type safety for latestPnlTick in Promise.all.

The addition of latestPnlTick to the destructured array and the Promise.all call introduces a new asynchronous operation. Please ensure that getLatestPnlTick handles errors properly to prevent unhandled rejections, and that all promises in Promise.all are reliable. Also, verify that the destructured types align correctly with the resolved values, considering latestPnlTick can be undefined.


111-111: Confirm correct handling of optional latestTick in function call.

The getPnlTicksWithCurrentTick function now accepts an optional latestTick parameter. The updated function call correctly includes latestPnlTick. This change is appropriate and ensures the latest tick is considered in the PnL calculation.

@vincentwschau vincentwschau merged commit deff758 into main Oct 3, 2024
16 checks passed
@vincentwschau vincentwschau deleted the vincentc/get-latest-pnl-tick-for-megavault branch October 3, 2024 21:38
mergify bot pushed a commit that referenced this pull request Oct 3, 2024
vincentwschau added a commit that referenced this pull request Oct 3, 2024
@coderabbitai coderabbitai bot mentioned this pull request Oct 4, 2024
6 tasks
@coderabbitai coderabbitai bot mentioned this pull request Dec 17, 2024
6 tasks
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