Skip to content

chore: init activeAssetCtxPrice price resolution#9160

Draft
gambinish wants to merge 1 commit into
mainfrom
perps/tat-3333_l2Book-sdk-upgrade
Draft

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

Conversation

@gambinish

@gambinish gambinish commented Jun 16, 2026

Copy link
Copy Markdown
Member

Explanation

Prefer activeAssetCtx price on detail/ticket screens to absorb 5s allMids cadence

Hyperliquid is throttling the allMids WebSocket stream to push every ~5 seconds. For list/overview screens this is fine, but on a focused single-symbol view (trade detail, order ticket) the displayed price would noticeably lag at that interval.

This PR fixes that by making subscribeToPrices calls with includeMarketData: true use the per-symbol activeAssetCtx stream as the source of truth for the displayed price. That stream already ticks at a faster cadence and was already established for market-data-enabled subscriptions — this change just wires its midPx/markPx through to PriceUpdate.price instead of waiting for the next allMids snapshot.

A 10-second staleness gate ensures that if the fast stream goes quiet, allMids takes back over automatically.

What changed:

  • #marketDataCache now stores the last activeAssetCtx price and its timestamp per symbol

  • #createPriceUpdate prefers the fresh fast-stream price over the passed-in allMids price (with TTL fallback)

  • The activeAssetCtx handler drives price updates directly from the incoming midPx/markPx rather than replaying the last cached allMids value

  • A comment on #ensureGlobalAllMidsSubscription documents the 5s cadence and the mitigation strategy
    What didn't change:

  • List/overview screens (includeMarketData: false) are completely unaffected — they still run on allMids only

  • No new WebSocket subscriptions are created; activeAssetCtx was already reference-counted under includeMarketData

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.

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