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

Adam/ct 1059 address issue with orderbookmidprice calculation #2012

Merged

Conversation

adamfraser
Copy link
Contributor

@adamfraser adamfraser commented Aug 2, 2024

Changelist

Fixes an issue when calculating orderbookMidPrice that would occur if the prices were numbers with lots of decimal places.

Test Plan

  • Added test cases
  • Tested manually in Dev

Summary by CodeRabbit

  • New Features

    • Enhanced precision in financial calculations for order book management.
    • Updated handling of very small numerical values and decimal precision in price calculations.
  • Bug Fixes

    • Refined data type management for mid-price calculations, now returning strings for better format handling.
  • Documentation

    • Improved test coverage for order book pricing calculations.

@adamfraser adamfraser added the bug Something isn't working label Aug 2, 2024
Copy link

linear bot commented Aug 2, 2024

Copy link
Contributor

coderabbitai bot commented Aug 2, 2024

Walkthrough

The recent changes enhance the precision and formatting of financial calculations within the order book management system. Notably, the return type of the getOrderBookMidPrice function has shifted from numeric to string, reflecting a focus on accuracy and representation. Additional test cases have been added to verify the handling of small values and maintain decimal precision, ensuring the system can manage a wider range of price inputs effectively.

Changes

File Path Change Summary
indexer/packages/redis/__tests__/caches/...cache.test.ts Adjusted expected value type for mid price in tests, added new cases for small values and decimal precision.
indexer/packages/redis/src/caches/...levels-cache.ts Changed return type of getOrderBookMidPrice to string, updated internal price calculations using the Big library, removed error logging statements.
indexer/services/ender/src/lib/...candles-generator.ts Modified getOrderbookMidPriceMap to return price without converting it to a string, affecting downstream handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant OrderBook
    participant RedisClient
    
    User->>OrderBook: Request Mid Price
    OrderBook->>RedisClient: Fetch Prices
    RedisClient-->>OrderBook: Return Prices
    OrderBook->>OrderBook: Calculate Mid Price
    OrderBook-->>User: Return Mid Price as String
Loading

🐇 In a world of numbers, where prices play,
A rabbit hopped in with a cheer today!
"Precision is key," it gleefully said,
"With strings now in place, let no price dread!"
"For every small value, we'll find our way,
To balance the books in a bright, bold array!" 🎉


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

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@adamfraser adamfraser marked this pull request as ready for review August 2, 2024 01:08
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, codebase verification and nitpick comments (1)
indexer/services/ender/src/lib/candles-generator.ts (1)

544-544: Ensure downstream processing handles the new format.

The return statement now returns the price directly as a number instead of a string. Verify that all downstream processing, especially the test cases in indexer/services/ender/__tests__/lib/candles-generator.test.ts, can handle this change.

  • indexer/services/ender/src/lib/candles-generator.ts: Ensure that the downstream processing of orderbookMidPriceMap correctly handles prices as numbers.
  • indexer/services/ender/__tests__/lib/candles-generator.test.ts: Update the test cases to expect prices as numbers instead of strings.
Analysis chain

Ensure downstream processing handles the new format.

The return statement now returns the price directly without converting it to a string. Verify that all downstream processing can handle this change.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all downstream processing of `getOrderbookMidPriceMap` handles the new format.

# Test: Search for the function usage. Expect: Ensure all occurrences handle the new format.
rg --type javascript -A 5 $'getOrderbookMidPriceMap'

Length of output: 86


Script:

#!/bin/bash
# Description: Verify all downstream processing of `getOrderbookMidPriceMap` handles the new format.

# Test: Search for the function usage. Expect: Ensure all occurrences handle the new format.
rg -A 5 'getOrderbookMidPriceMap'

Length of output: 2813


Script:

#!/bin/bash
# Description: Extract and review the relevant code sections from `candles-generator.ts` and `candles-generator.test.ts` to verify downstream processing.

# Extract the relevant code section from `candles-generator.ts`
rg -A 10 'const orderbookMidPriceMap = await getOrderbookMidPriceMap();' indexer/services/ender/src/lib/candles-generator.ts

# Extract the relevant test cases from `candles-generator.test.ts`
rg -A 10 'const map = await getOrderbookMidPriceMap();' indexer/services/ender/__tests__/lib/candles-generator.test.ts

Length of output: 247


Script:

#!/bin/bash
# Description: Extract and review the relevant code sections from `candles-generator.ts` and `candles-generator.test.ts` to verify downstream processing.

# Extract the relevant code section from `candles-generator.ts`
rg 'const orderbookMidPriceMap = await getOrderbookMidPriceMap();' -A 20 indexer/services/ender/src/lib/candles-generator.ts

# Extract the relevant test cases from `candles-generator.test.ts`
rg 'const map = await getOrderbookMidPriceMap();' -A 20 indexer/services/ender/__tests__/lib/candles-generator.test.ts

Length of output: 247


Script:

#!/bin/bash
# Description: Verify the presence and usage of `getOrderbookMidPriceMap` function across the entire repository.

# Search for the function definition with increased context
rg 'async function getOrderbookMidPriceMap' -A 20

# Search for the function usage with increased context
rg 'getOrderbookMidPriceMap()' -A 20

Length of output: 9367

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1c7f0a2 and c407064.

Files selected for processing (3)
  • indexer/packages/redis/tests/caches/orderbook-levels-cache.test.ts (1 hunks)
  • indexer/packages/redis/src/caches/orderbook-levels-cache.ts (2 hunks)
  • indexer/services/ender/src/lib/candles-generator.ts (1 hunks)
Additional comments not posted (7)
indexer/packages/redis/src/caches/orderbook-levels-cache.ts (3)

536-536: Update the function signature in documentation.

The function signature has changed to return a Promise<string | undefined>. Ensure that all relevant documentation reflects this change.


548-549: Use the Big library for precise arithmetic operations.

The use of the Big library for handling humanPrice values enhances precision in arithmetic operations.


554-554: Return the mid-price as a string.

The final return statement now returns a string formatted result using toFixed(), reflecting a focus on precision and format.

indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts (4)

747-747: Update test case to expect a string mid price.

The expected value has been adjusted to a string type, reflecting the updated return type of the getOrderBookMidPrice function.


751-771: Add test case for very small numerical values.

The new test case verifies the correct calculation of the mid price for very small numerical values, ensuring accurate computation.


773-792: Add test case for decimal precision.

The new test case checks that the mid price calculation maintains appropriate decimal precision, ensuring precision in financial calculations.


793-795: Add test case for undefined mid price.

The test case verifies that the function returns undefined if there are no bids or asks, ensuring correct handling of such cases.

@adamfraser adamfraser merged commit 9385c5a into main Aug 2, 2024
11 checks passed
@adamfraser adamfraser deleted the adam/ct-1059-address-issue-with-orderbookmidprice-calculation branch August 2, 2024 16:52
@adamfraser
Copy link
Contributor Author

@Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Aug 2, 2024

backport release/indexer/v5.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Aug 2, 2024
adamfraser added a commit that referenced this pull request Aug 2, 2024
adamfraser added a commit that referenced this pull request Aug 6, 2024
adamfraser added a commit that referenced this pull request Aug 6, 2024
adamfraser added a commit that referenced this pull request Aug 6, 2024
Revert "Revert "Candles HLOC Data (#1887)""

This reverts commit 79345d6.

Revert "Revert "Adam/ct 1059 address issue with orderbookmidprice calculation (#2012)""

This reverts commit d10317a.

Explicity create nullable columns
adamfraser added a commit that referenced this pull request Aug 6, 2024
Revert "Revert "Candles HLOC Data (#1887)""

This reverts commit 79345d6.

Revert "Revert "Adam/ct 1059 address issue with orderbookmidprice calculation (#2012)""

This reverts commit d10317a.

Explicity create nullable columns
adamfraser added a commit that referenced this pull request Aug 6, 2024
Revert "Revert "Candles HLOC Data (#1887)""

This reverts commit 79345d6.

Revert "Revert "Adam/ct 1059 address issue with orderbookmidprice calculation (#2012)""

This reverts commit d10317a.

Explicity create nullable columns

Alter migration name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indexer
Development

Successfully merging this pull request may close these issues.

2 participants