Skip to content

Conversation

@nickewansmith
Copy link
Contributor

@nickewansmith nickewansmith commented Nov 7, 2025

Description

Summary

Fixes incorrect open interest calculation in the market list. Open interest was showing values in thousands instead of billions because the calculation wasn't converting raw contract units to USD by multiplying by the current price.

Root Cause

The transformMarketData() function used by the market list was parsing raw open interest values directly:
// BEFORE (incorrect)const openInterest = assetCtx?.openInterest ? parseFloat(assetCtx.openInterest) : NaN;
This treated open interest as USD when it's actually in contract units. For example, BTC with 1M open interest contracts at $50K should be $50B, but was showing as $1M.

Solution

Created a centralized calculateOpenInterestUSD() utility function that properly converts contract units to USD by multiplying by the current price:
// AFTER (correct)const openInterest = calculateOpenInterestUSD( assetCtx?.openInterest, effectiveCurrentPrice,);
This utility is now used consistently in:
transformMarketData() - Initial REST API data transformation (market list)
HyperLiquidSubscriptionService - WebSocket real-time updates (2 locations)

Known Discrepancy (Not Addressed in This PR)

There are two separate data sources for market data:

Market List: Uses REST API snapshot via getMarketDataWithPrices()
Data source: allMids prices from REST API
Update frequency: Cached, refreshes periodically or on pull-to-refresh
Use case: Browsing markets

Market Statistics Card: Uses real-time WebSocket via subscribeToPrices()
Data source: midPx || markPx from WebSocket
Update frequency: Real-time, updates every ~1-2 seconds
Use case: Viewing specific market details

Impact: While both now use the same calculation logic (calculateOpenInterestUSD), the market list shows a snapshot while the statistics card shows live real-time values. This is by design to prevent scroll jumping and excessive re-renders in the list view. Users can pull-to-refresh to update the market list.

Changes Made

Created utility function: calculateOpenInterestUSD() in marketDataTransform.ts
Updated market list: Uses utility in transformMarketData()
Updated WebSocket service: Uses utility in HyperLiquidSubscriptionService (2 locations)

Changelog

CHANGELOG entry: null

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TAT-1972

Manual testing steps

Scenario: Sorting preserves correct open interest calculation
    Given BTC has 1,000,000 contracts at $52,000 per contract
    And ETH has 5,000,000 contracts at $3,100 per contract
    When I sort markets by open interest
    Then BTC displays "$52.00B" and appears before ETH
    And ETH displays "$15.50B" and appears after BTC
    And the sorting uses the calculated USD values (contracts × price)
    And not the raw contract numbers

  Scenario: Switching from volume sort to open interest sort
    Given markets are currently sorted by "Volume"
    And the order is: SOL ($500M), BTC ($450M), ETH ($300M)
    When I change the sort to "Open Interest"
    Then markets reorder immediately
    And the new order is: BTC ($52.00B), ETH ($15.50B), SOL ($1.50B)
    And the sort dropdown shows "Open Interest" as selected

Screenshots/Recordings

Before

After

image

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Centralizes open interest USD calculation and uses it in REST transform and WebSocket subscriptions, updating displays and adding unit tests.

  • Utils:
    • Add calculateOpenInterestUSD in app/components/UI/Perps/utils/marketDataTransform.ts and use it in transformMarketData() to convert contract units × price to USD.
  • Services:
    • Update app/components/UI/Perps/services/HyperLiquidSubscriptionService.ts to compute openInterest via calculateOpenInterestUSD in both activeAssetCtx and assetCtxs handlers.
  • Tests:
    • Expand app/components/UI/Perps/utils/marketDataTransform.test.ts with comprehensive tests for calculateOpenInterestUSD and adjust expectations (e.g., BTC openInterest to $52.00B).

Written by Cursor Bugbot for commit f48648e. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2025

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@github-actions github-actions bot added the size-M label Nov 7, 2025
@nickewansmith nickewansmith marked this pull request as ready for review November 7, 2025 04:10
@nickewansmith nickewansmith requested a review from a team as a code owner November 7, 2025 04:10
@nickewansmith nickewansmith added team-perps Perps team and removed team-earn labels Nov 7, 2025
@nickewansmith nickewansmith force-pushed the TAT-1972-incorrect-oi-data-when-sorting-market-by-oi branch from 6209fa3 to 8389d73 Compare November 7, 2025 05:53
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 7, 2025

@gambinish gambinish added this pull request to the merge queue Nov 7, 2025
Merged via the queue into main with commit aa15c73 Nov 7, 2025
85 checks passed
@gambinish gambinish deleted the TAT-1972-incorrect-oi-data-when-sorting-market-by-oi branch November 7, 2025 18:02
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2025
@metamaskbot metamaskbot added the release-7.60.0 Issue or pull request that will be included in release 7.60.0 label Nov 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.60.0 Issue or pull request that will be included in release 7.60.0 size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants