Skip to content

chore: init activeAssetCtxPrice price resolution#9160

Draft
gambinish wants to merge 3 commits into
mainfrom
perps/tat-3333_l2Book-sdk-upgrade
Draft

chore: init activeAssetCtxPrice price resolution#9160
gambinish wants to merge 3 commits into
mainfrom
perps/tat-3333_l2Book-sdk-upgrade

Conversation

@gambinish

@gambinish gambinish commented Jun 16, 2026

Copy link
Copy Markdown
Member

Explanation

Two related improvements to HyperLiquidSubscriptionService:

TAT-3334 — Per-subscriber activeAssetCtx price projection

Hyperliquid throttles the main-DEX allMids stream to ~2 s per push. For list/overview screens that's fine, but a focused detail/ticket screen would show a noticeably stale price at that interval.
Previous approach (PR 9160 initial draft): #createPriceUpdate unconditionally baked the activeAssetCtx fast-stream price into #cachedPriceData, meaning all subscribers — including list/overview — received the fast price, contradicting the per-subscriber contract documented in the code.
New approach — per-subscriber projection:

  • #createPriceUpdate is reverted to a pure allMids-baseline builder; #cachedPriceData always holds the raw allMids price.
  • A new #projectPriceUpdate(symbol, base) helper shallow-clones the base with price/timestamp overridden by the fresh activeAssetCtx value (within a 10 s staleness window).
  • #notifyAllPriceSubscribers projects per callback: focused (includeMarketData: true) callbacks receive the fast-stream price; list/overview (includeMarketData: false) callbacks always receive the raw allMids baseline, guaranteed, even when both subscriber types share the same symbol.
  • The subscribeToPrices immediate emit projects for focused new subscribers, and can send a fast-stream-only update even before the first allMids tick so detail screens aren't blank on first render.
  • Startup zero-price guard: if activeAssetCtx fires before allMids with no midPx/markPx, no notification is sent — the old '0' fallback is removed entirely.
  • No new WebSocket subscriptions; activeAssetCtx was already reference-counted under includeMarketData: true.

TAT-3333 — Order book fast flag + SDK bump

Hyperliquid is changing the default l2Book cadence (20 levels @ ~2 s). A new fast subscription mode is available (5 levels @ ~0.5 s).

  • Bumps @nktkas/hyperliquid from ^0.32.2 to ^0.33.1 (adds fast to the l2Book request schema).
  • Adds fast?: boolean to SubscribeOrderBookParams with JSDoc explaining the 5-level / ~0.5 s tradeoff.
  • Threads fast through subscribeToOrderBook into the SDK l2Book call.
  • No change to #processOrderBookData or cumulative-total math.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

@gambinish

Copy link
Copy Markdown
Member Author

@metamaskbot publish-previews

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.2-preview-1968d6ea0
@metamask-previews/accounts-controller@39.0.1-preview-1968d6ea0
@metamask-previews/address-book-controller@7.1.2-preview-1968d6ea0
@metamask-previews/ai-controllers@0.7.0-preview-1968d6ea0
@metamask-previews/analytics-controller@1.1.1-preview-1968d6ea0
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-1968d6ea0
@metamask-previews/announcement-controller@8.1.0-preview-1968d6ea0
@metamask-previews/app-metadata-controller@2.0.1-preview-1968d6ea0
@metamask-previews/approval-controller@9.0.2-preview-1968d6ea0
@metamask-previews/assets-controller@9.0.1-preview-1968d6ea0
@metamask-previews/assets-controllers@109.1.0-preview-1968d6ea0
@metamask-previews/authenticated-user-storage@2.0.0-preview-1968d6ea0
@metamask-previews/base-controller@9.1.0-preview-1968d6ea0
@metamask-previews/base-data-service@0.1.3-preview-1968d6ea0
@metamask-previews/bridge-controller@75.1.1-preview-1968d6ea0
@metamask-previews/bridge-status-controller@72.1.0-preview-1968d6ea0
@metamask-previews/build-utils@3.0.4-preview-1968d6ea0
@metamask-previews/chain-agnostic-permission@1.6.2-preview-1968d6ea0
@metamask-previews/chomp-api-service@3.1.0-preview-1968d6ea0
@metamask-previews/claims-controller@0.5.3-preview-1968d6ea0
@metamask-previews/client-controller@1.0.1-preview-1968d6ea0
@metamask-previews/compliance-controller@2.1.0-preview-1968d6ea0
@metamask-previews/composable-controller@12.0.1-preview-1968d6ea0
@metamask-previews/config-registry-controller@0.4.1-preview-1968d6ea0
@metamask-previews/connectivity-controller@0.2.0-preview-1968d6ea0
@metamask-previews/controller-utils@12.2.0-preview-1968d6ea0
@metamask-previews/core-backend@6.3.3-preview-1968d6ea0
@metamask-previews/delegation-controller@3.0.2-preview-1968d6ea0
@metamask-previews/earn-controller@12.2.0-preview-1968d6ea0
@metamask-previews/eip-5792-middleware@3.0.4-preview-1968d6ea0
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-1968d6ea0
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-1968d6ea0
@metamask-previews/ens-controller@19.1.3-preview-1968d6ea0
@metamask-previews/eth-block-tracker@15.0.1-preview-1968d6ea0
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-1968d6ea0
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-1968d6ea0
@metamask-previews/foundryup@1.0.1-preview-1968d6ea0
@metamask-previews/gas-fee-controller@26.2.2-preview-1968d6ea0
@metamask-previews/gator-permissions-controller@4.2.0-preview-1968d6ea0
@metamask-previews/geolocation-controller@0.1.3-preview-1968d6ea0
@metamask-previews/json-rpc-engine@10.5.0-preview-1968d6ea0
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-1968d6ea0
@metamask-previews/keyring-controller@27.1.0-preview-1968d6ea0
@metamask-previews/logging-controller@8.0.2-preview-1968d6ea0
@metamask-previews/message-manager@14.1.2-preview-1968d6ea0
@metamask-previews/messenger@1.2.0-preview-1968d6ea0
@metamask-previews/messenger-cli@0.2.0-preview-1968d6ea0
@metamask-previews/money-account-balance-service@2.0.0-preview-1968d6ea0
@metamask-previews/money-account-controller@0.3.3-preview-1968d6ea0
@metamask-previews/money-account-upgrade-controller@2.0.5-preview-1968d6ea0
@metamask-previews/multichain-account-service@10.0.3-preview-1968d6ea0
@metamask-previews/multichain-api-middleware@3.1.4-preview-1968d6ea0
@metamask-previews/multichain-network-controller@3.1.3-preview-1968d6ea0
@metamask-previews/multichain-transactions-controller@7.1.1-preview-1968d6ea0
@metamask-previews/name-controller@9.1.2-preview-1968d6ea0
@metamask-previews/network-controller@32.0.0-preview-1968d6ea0
@metamask-previews/network-enablement-controller@5.3.0-preview-1968d6ea0
@metamask-previews/notification-services-controller@24.1.3-preview-1968d6ea0
@metamask-previews/passkey-controller@2.0.1-preview-1968d6ea0
@metamask-previews/permission-controller@13.1.1-preview-1968d6ea0
@metamask-previews/permission-log-controller@5.1.0-preview-1968d6ea0
@metamask-previews/perps-controller@8.1.0-preview-1968d6ea0
@metamask-previews/phishing-controller@17.2.0-preview-1968d6ea0
@metamask-previews/polling-controller@16.0.6-preview-1968d6ea0
@metamask-previews/preferences-controller@23.1.0-preview-1968d6ea0
@metamask-previews/profile-metrics-controller@3.2.0-preview-1968d6ea0
@metamask-previews/profile-sync-controller@28.2.0-preview-1968d6ea0
@metamask-previews/ramps-controller@14.2.0-preview-1968d6ea0
@metamask-previews/rate-limit-controller@7.0.1-preview-1968d6ea0
@metamask-previews/react-data-query@0.2.1-preview-1968d6ea0
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-1968d6ea0
@metamask-previews/sample-controllers@5.0.1-preview-1968d6ea0
@metamask-previews/seedless-onboarding-controller@10.0.2-preview-1968d6ea0
@metamask-previews/selected-network-controller@26.1.3-preview-1968d6ea0
@metamask-previews/shield-controller@5.1.2-preview-1968d6ea0
@metamask-previews/signature-controller@39.2.5-preview-1968d6ea0
@metamask-previews/smart-transactions-controller@24.2.1-preview-1968d6ea0
@metamask-previews/snap-account-service@0.3.1-preview-1968d6ea0
@metamask-previews/social-controllers@2.3.0-preview-1968d6ea0
@metamask-previews/storage-service@1.0.2-preview-1968d6ea0
@metamask-previews/subscription-controller@6.2.0-preview-1968d6ea0
@metamask-previews/transaction-controller@68.0.0-preview-1968d6ea0
@metamask-previews/transaction-pay-controller@23.8.0-preview-1968d6ea0
@metamask-previews/user-operation-controller@41.2.4-preview-1968d6ea0
@metamask-previews/wallet@3.0.0-preview-1968d6ea0
@metamask-previews/wallet-cli@0.0.0-preview-1968d6ea0

@geositta geositta left a comment

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.

This is a good direction. The main thing I think we should tighten is the subscriber contract. The PR currently says includeMarketData: false subscribers remain allMids only, but the implementation stores the fast selected price in shared cached price data and fans it out to every subscriber for the symbol. If the intended behavior is per-subscriber price source selection, the cache/notify path needs to project PriceUpdate per subscriber. If the intended behavior is shared fast price per symbol, the changelog/comments should say that clearly and include mixed subscriber coverage.

There is also one startup edge case where activeAssetCtx can publish '0' before either activeAssetCtx or allMids has a real price. That should be skipped until a usable price exists, since focused screens may feed this value into display and calculations.

// fallback string if ctxPrice is absent so #createPriceUpdate still
// has something to work with. The preference logic inside
// #createPriceUpdate will pick up the cached activeAssetCtxPrice.
const fallbackPrice =

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.

  1. activeAssetCtx can send a 0 price before a real price exists.

When activeAssetCtx has neither midPx nor markPx, and no allMids price is cached yet, the new path falls back to '0', writes that as the cached price, and notifies subscribers immediately.

Changed hunk: packages/perps-controller/src/services/HyperLiquidSubscriptionService.ts:3099

Proof this PR introduces/regresses it: before this PR, activeAssetCtx only refreshed an existing cached price; this PR makes activeAssetCtx drive a first price update and uses '0' when neither source has a real price. That can briefly surface 0 on focused screens and feed 0 into calculations.

Suggested fix: skip the price update until either activeAssetCtx has midPx ?? markPx or a real cached allMids fallback exists. Please add coverage for activeAssetCtx first without price.

now - marketData.priceLastUpdated <=
HyperLiquidSubscriptionService.#activeAssetCtxPriceTtlMs;

const effectivePrice = hasFreshActiveAssetCtxPrice

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.

The PR description/changelog says list and overview screens with includeMarketData: false are unaffected and continue to use allMids exclusively, but the changed implementation stores the activeAssetCtx-selected price in shared #cachedPriceData and then fans that same PriceUpdate out to every subscriber for the symbol.

Fanout path: packages/perps-controller/src/services/HyperLiquidSubscriptionService.ts:3850

Contract text: packages/perps-controller/CHANGELOG.md:13

Proof this PR introduces/regresses it: this PR adds activeAssetCtxPrice preference inside #createPriceUpdate, writes the selected fast price into shared #cachedPriceData, and leaves #notifyAllPriceSubscribers subscriber agnostic. If a focused subscriber and a list row subscribe to the same symbol, the list subscriber can receive the fast activeAssetCtx price without opting in.

Suggested fix: keep raw price sources separate and project PriceUpdate per subscriber at notify time. A pragmatic version would be a price source aware #createPriceUpdate(symbol, { priceSource }) path or separate cached allMids and activeAssetCtx prices.

Please add a mixed subscriber test where allMids emits 50000, activeAssetCtx emits 50500, the focused subscriber receives 50500, and the list
subscriber remains at 50000.

gambinish and others added 2 commits June 25, 2026 16:27
…grade

Resolves conflict in HyperLiquidSubscriptionService.market-data.test.ts
by keeping both the activeAssetCtx price preference tests (HEAD) and
the new Market tradability (isTradable) tests (main).

Co-authored-by: Cursor <cursoragent@cursor.com>
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updated@​nktkas/​hyperliquid@​0.32.2 ⏵ 0.33.110010010094 +7100

View full report

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants