[pull] main from MetaMask:main#877
Merged
Merged
Conversation
## 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. --- ### Version / consumption note Because this changes published behavior, it lands as a new release of `@metamask/perps-controller` (the entries are in `[Unreleased]`). Clients pick it up by bumping the dependency. The `@nktkas/hyperliquid` bump to `^0.33.1` is transitive — clients that don't import the SDK directly don't need to do anything, but anyone pinning it should be aware. ### Summary | | Price fast-stream | Order book `fast` | | --------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------ | | **Problem** | `allMids` lags ~5s on focused screens | default l2Book ~2s, 20 levels | | **Mechanism** | Route already-subscribed `activeAssetCtx` `midPx`/`markPx` per-subscriber, with 10s TTL fallback to `allMids` | Pass `fast` through to SDK `l2Book` | | **New connections** | None | None | | **Client integration**| None (automatic for `includeMarketData: true`) | Opt-in `fast: true`, accept ≤5 levels | | **Backward compatible**| Yes | Yes ## References - [TAT-3333](https://consensyssoftware.atlassian.net/browse/TAT-3333) - [TAT-3334](https://consensyssoftware.atlassian.net/browse/TAT-3334) <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them [TAT-3333]: https://consensyssoftware.atlassian.net/browse/TAT-3333?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes live price sourcing and WebSocket client wiring for trading UI; behavior is backward compatible but incorrect staleness or projection could show wrong prices on detail screens. > > **Overview** > Upgrades `@nktkas/hyperliquid` to **^0.33.1** and adjusts WebSocket transport setup (`close()` is synchronous; reconnect no longer injects `globalThis.WebSocket`). Perps package **tsconfig** now uses **ESNext** / **Bundler** module resolution for the SDK. > > **Price subscriptions:** For `subscribeToPrices` with **`includeMarketData: true`**, displayed **`price`** now comes from the existing per-symbol **`activeAssetCtx`** stream (`midPx` / `markPx`) when fresh (**10 s TTL**), with **`allMids`** still the baseline for derived fields and for **`includeMarketData: false`** list subscribers. **`#notifyAllPriceSubscribers`** projects per callback; startup avoids emitting **`'0'`** when context arrives before **`allMids`**. **`assetCtxs`** batch updates no longer wipe the fast-stream cache fields. > > **Order book:** **`SubscribeOrderBookParams`** gains optional **`fast?: boolean`**, passed through to SDK **`l2Book`** (faster ~0.5 s / 5-level mode vs default). > > Changelog also documents new **Perps Advanced Chart** analytics constants (separate from the Hyperliquid work). Minor typing cleanup for Hyperliquid child orders (no **`FrontendOrder`** cast). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit e12e4a3. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )