Skip to content

chore(perps): Refactor HL subscription service (#24015)#24274

Merged
joaoloureirop merged 1 commit into
release/7.61.6from
release/7.61.6_cp-c77dd1a6ec08308a80eca140e4f6b4040661625c
Jan 6, 2026
Merged

chore(perps): Refactor HL subscription service (#24015)#24274
joaoloureirop merged 1 commit into
release/7.61.6from
release/7.61.6_cp-c77dd1a6ec08308a80eca140e4f6b4040661625c

Conversation

@gambinish
Copy link
Copy Markdown
Member

@gambinish gambinish commented Jan 6, 2026

Description

This PR refactors the HyperLiquid subscription service to drop subscription fallbacks.

This refactor was raised during the offsite, as it is needed as a result of the hotfix we had to deploy to address Perps loading issue during the weekend. The initial PR
#23753 introduced a fallback mechanism that would create new subscriptions when the properties in the data coming from the old subscriptions were missing. Given that these properties will be always missing, this PR uses the new subscriptions as a default.

Source PR: #24015

Changelog

CHANGELOG entry: null

Related issues

Fixes:

Manual testing steps

Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]

Screenshots/Recordings

Before

No visible change

After

No visible change

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

Refactors HyperLiquid live data flow and improves TP/SL processing.

  • Switches HIP-3 to individual per-DEX subscriptions: clearinghouseState (positions/account) and openOrders (orders); uses webData3 only for OI cap updates; removes fallback caches/subscriptions and updates cleanup/restore logic
  • Enhances extractTPSLFromOrders to accept cached orders and classify ambiguous Trigger orders via price vs entry; re-extracts TP/SL on clearinghouseState updates to preserve UI state
  • Adds triggerPrice to Order and sets it in adaptOrderFromSDK; price now falls back to triggerPx when limitPx is absent
  • Test suite updates: adopt Jest fake timers (runAllTimersAsync), add mocks for new per-DEX subscriptions, and extend cases for TP/SL counts, cached rehydration, L2 book ref counting, reconnection, and error handling

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


Description

Changelog

CHANGELOG entry:

Related issues

Fixes:

Manual testing steps

Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]

Screenshots/Recordings

Before

After

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.

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

This PR refactors the HyperLiquid subscription service to drop
subscription fallbacks.

This refactor was raised during the offsite, as it is needed as a result
of the hotfix we had to deploy to address Perps loading issue during the
weekend. The initial PR
#23753 introduced a
fallback mechanism that would create new subscriptions when the
properties in the data coming from the old subscriptions were missing.
Given that these properties will be always missing, this PR uses the new
subscriptions as a default.



## **Changelog**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

CHANGELOG entry: null

## **Related issues**

Fixes:

## **Manual testing steps**

```gherkin
Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

No visible change

### **After**

No visible change

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **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.


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Replaces HIP‑3 webData3 fallback with per‑DEX
clearinghouseState/openOrders subscriptions, adds `triggerPrice` to
orders, and hardens TP/SL extraction with price-based fallback and cache
re-use; tests updated accordingly.
> 
> - **Perps Subscription Service (HIP‑3)**:
> - Switch to individual subscriptions: `clearinghouseState`
(positions/account) and `openOrders` (orders); use `webData3` only for
OI caps.
> - Remove fallback caches/handlers; centralize aggregation and cache
updates; update cleanup/restore logic to handle new subs.
> - Re-extract TP/SL from cached orders on `clearinghouseState` updates
to prevent flicker.
> - Update hashing/change-detection and notify paths; broaden
reconnection restoration across subtypes.
> - **TP/SL Processing**:
> - New shared helper to extract TP/SL from orders; adds price-based
fallback for ambiguous `Trigger` types (long/short aware) and counts
TP/SL.
> - **Order Model & Adapter**:
>   - Extend `Order` with `triggerPrice`.
> - `adaptOrderFromSDK` sets `triggerPrice` (uses `triggerPx`) and
prioritizes `limitPx` for price; child TP/SL preserved.
> - **Tests**:
> - Overhaul to reflect new HIP‑3 flow: assert
`clearinghouseState`/`openOrders` usage; OI caps via `webData3`.
> - Replace timeouts with `jest.runAllTimersAsync()`; add coverage for
TP/SL classification, cache re-use, short/long logic, L2 book handling,
and feature-flag updates.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
cd9bbb0. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: abretonc7s <107169956+abretonc7s@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 6, 2026

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.

@metamaskbot metamaskbot added the team-perps Perps team label Jan 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 6, 2026

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps
  • Risk Level: medium
  • AI Confidence: 85%
click to see 🤖 AI reasoning details

The changes are entirely isolated to the Perps (Perpetuals) trading feature, specifically:

  1. Scope of Changes: All 5 modified files are within app/components/UI/Perps/ directory:

    • Type definitions for order callbacks and order data structures
    • Major refactoring of WebSocket subscription service for multi-DEX support
    • Adapter improvements for trigger price handling
    • Comprehensive test updates
  2. Nature of Changes:

    • Refactoring from monolithic webData3 subscription to individual clearinghouseState and openOrders subscriptions
    • Enhanced TP/SL (Take Profit/Stop Loss) data processing
    • Added isSnapshot parameter for order fill callbacks
    • Added triggerPrice field to orders for better UI display
  3. Risk Level: Medium because:

    • This is a significant architectural change to the WebSocket subscription layer
    • Real-time data updates are critical for trading features
    • Changes affect position tracking, order updates, and fill notifications
    • However, changes are well-isolated within the Perps module
  4. Test Tag Selection:

    • SmokePerps: This is the only relevant tag as all changes are Perps-specific
    • No other features are impacted (no core wallet, network, confirmation, or other trading changes)
    • The tag exists and has a clear description: "Perpetuals trading"
  5. Important Notes:

    • Currently, no E2E tests are actually tagged with SmokePerps (checked e2e/specs/perps/*.ts files which use RegressionTrade tag)
    • The perps E2E specs exist (6 files in e2e/specs/perps/) but aren't using the SmokePerps tag yet
    • If SmokePerps tag has no associated tests, this selection will effectively skip E2E testing
    • However, the changes include comprehensive unit test updates that provide coverage

The changes don't affect any other wallet features, so only SmokePerps should be run if such tests exist.

View GitHub Actions results

@gambinish gambinish marked this pull request as ready for review January 6, 2026 22:30
@gambinish gambinish requested a review from a team as a code owner January 6, 2026 22:30
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jan 6, 2026

@joaoloureirop joaoloureirop added the skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. label Jan 6, 2026
@joaoloureirop joaoloureirop enabled auto-merge (squash) January 6, 2026 22:38
isTrigger: false,
reduceOnly: false,
isTrigger: order.isTrigger ?? false,
reduceOnly: order.reduceOnly ?? false,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mock missing triggerPrice masks TP/SL test coverage (Bugbot Rules)

Low Severity

Violates the testing rule "Use realistic mock data that reflects real usage." The adaptOrderFromSDK mock doesn't include triggerPrice, but the real implementation sets order.triggerPrice = rawOrder.triggerPx when triggerPx exists. The production code at line 390 uses order.triggerPrice || order.price for TP/SL extraction from cached orders. Tests pass coincidentally because test data uses identical limitPx and triggerPx values, making the fallback to price produce correct results. If test data had limitPx != triggerPx (as real Take Profit Limit orders do), the mock would return wrong TP/SL prices. The mock should include triggerPrice: order.triggerPx || undefined to match the real adapter behavior.

Fix in Cursor Fix in Web

@joaoloureirop joaoloureirop merged commit d400d2e into release/7.61.6 Jan 6, 2026
173 of 178 checks passed
@joaoloureirop joaoloureirop deleted the release/7.61.6_cp-c77dd1a6ec08308a80eca140e4f6b4040661625c branch January 6, 2026 23:24
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 6, 2026
@metamaskbot metamaskbot added the release-7.61.6 Issue or pull request that will be included in release 7.61.6 label Jan 14, 2026
@metamaskbot
Copy link
Copy Markdown
Collaborator

No release label on PR. Adding release label release-7.61.6 on PR, as PR was added to branch 7.61.6 when release was cut.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.61.6 Issue or pull request that will be included in release 7.61.6 size-XL skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants