Commit 9a7858c
authored
chore: add OHLCVService for real-time candlestick WebSocket streaming (#8695)
## Explanation
### Architecture Overview
```
┌─────────────────┐ messenger ┌──────────────────────────┐
│ OHLCVService │ ─── calls actions ────► │ BackendWebSocketService │
│ (domain logic) │ │ (raw WS connection) │
│ │ ◄── listens to events ── │ │
└────────┬────────┘ └──────────┬───────────────┘
│ │
publishes events actual WebSocket
to UI consumers (connect, auth, reconnect,
heartbeat, JSON framing)
│
▼
┌──────────────────┐
│ Mobile UI │
│ (React hooks) │
│ useOHLCVRealtime │
└──────────────────┘
```
### What
- Add `OHLCVService` for real-time OHLCV (candlestick) data streaming
via the backend WebSocket gateway
- Move all WebSocket-related files (`BackendWebSocketService`,
`AccountActivityService`) into a new `src/ws/` directory per code review
feedback
### Why
- Enable real-time chart updates on the Token Details screen without
polling
- Reduce API load by replacing periodic HTTP calls with persistent
WebSocket subscriptions
- Organize WebSocket code into a dedicated `ws/` folder for better
discoverability
### New files
- `src/ws/ohlcv/OHLCVService.ts` — main service with
subscribe/unsubscribe semantics, reference counting, grace-period
unsubscribe, idempotency checks, chain-status forwarding, and automatic
resubscription on reconnect
- `src/ws/ohlcv/OHLCVService.test.ts` — 22 unit tests covering all paths
(100% branch coverage)
- `src/ws/ohlcv/OHLCVService-method-action-types.ts` — auto-generated
messenger action types
- `src/ws/ohlcv/types.ts` — `OHLCVBar` and `OHLCVSubscriptionOptions`
types
- `src/ws/ohlcv/index.ts` — barrel exports
### Modified files
- `src/index.ts` — added exports for `OHLCVService`, its types, and
allowed actions/events; updated import paths to `./ws/`
- `eslint-suppressions.json` — updated paths for moved files, added
suppressions for new test file
- `CHANGELOG.md` — documented new service and exports
### Moved files (no logic changes)
- `src/BackendWebSocketService.ts` → `src/ws/BackendWebSocketService.ts`
- `src/BackendWebSocketService.test.ts` →
`src/ws/BackendWebSocketService.test.ts`
- `src/BackendWebSocketService-method-action-types.ts` →
`src/ws/BackendWebSocketService-method-action-types.ts`
- `src/AccountActivityService.ts` → `src/ws/AccountActivityService.ts`
- `src/AccountActivityService.test.ts` →
`src/ws/AccountActivityService.test.ts`
- `src/AccountActivityService-method-action-types.ts` →
`src/ws/AccountActivityService-method-action-types.ts`
- Only import path updates (`./logger` → `../logger`, `./types` →
`../types`, test helper paths)
### Key design decisions
- **UI-driven lifecycle** — unlike `AccountActivityService`
(auto-subscribes on account change), `OHLCVService` exposes
`subscribe()`/`unsubscribe()` called by the UI when the chart
mounts/unmounts
- **Reference counting** — multiple UI consumers subscribing to the same
assetId/interval/currency share one WebSocket subscription
- **Grace period (3s)** — when all consumers unsubscribe, actual WS
unsubscribe is delayed 3 seconds to absorb rapid navigation (Token A →
Token B → Token A)
- **Idempotency** — uses `channelHasSubscription` before subscribing;
duplicate calls are no-ops (React Strict Mode safe)
- **Chain status** — listens to `system-notifications.v1.market-data.v1`
(auto-subscribed by server) and publishes
`OHLCVService:chainStatusChanged`
- **Disconnect handling** — on WebSocket disconnect, publishes
`chainStatusChanged { status: 'down' }` for all tracked chains,
triggering UI polling fallback
- **Reconnect** — resubscribes all active channels when WebSocket
reconnects (no `sessionId` needed for OHLCV; UI polling fallback covers
the gap)
- **`init()` method** — system notification callback registered in
`init()` (not constructor) to comply with messenger-in-constructor lint
rule
### Events published
- `OHLCVService:barUpdated` — `{ channel, bar: OHLCVBar }` — new candle
data from WebSocket
- `OHLCVService:chainStatusChanged` — `{ chainIds, status, timestamp? }`
— chain up/down (server notification or WS disconnect)
- `OHLCVService:subscriptionError` — `{ channel, error, operation }` —
subscribe or unsubscribe failure
## References
* Related to
https://www.notion.so/metamask-consensys/OHLCV-WebSocket-Integration-UI-Implementation-Guide-346f86d67d6880b6a70fc3be0f0c34b9
* Related to MetaMask/metamask-mobile#29739
* Fixes https://consensyssoftware.atlassian.net/browse/ASSETS-3195
## 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](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
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Adds a new WebSocket-driven market-data service with reference
counting, timers, and reconnect resubscription logic, which can affect
subscription lifecycles and event delivery. Also moves existing
WebSocket services into `src/ws/`, so consumers relying on internal
paths (vs package exports) could break if any remain.
>
> **Overview**
> Adds a new `OHLCVService` to stream real-time OHLCV bars over
WebSocket, exposing `subscribe`/`unsubscribe` via messenger actions,
publishing `barUpdated`/`chainStatusChanged`/`subscriptionError` events,
and handling reconnect resubscription with ref-counting plus a
grace-period unsubscribe (mutex-protected).
>
> Refactors `core-backend` by moving `BackendWebSocketService` and
`AccountActivityService` (and their tests/action-type files) into
`src/ws/`, updating imports/exports (`src/index.ts`), and updating lint
suppressions; also adds `async-mutex` plus comprehensive unit tests for
the new service and documents the addition in the changelog.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
730af62. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 057b1d1 commit 9a7858c
16 files changed
Lines changed: 1835 additions & 25 deletions
File tree
- packages/core-backend
- src
- ws
- ohlcv
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
862 | 862 | | |
863 | 863 | | |
864 | 864 | | |
865 | | - | |
| 865 | + | |
866 | 866 | | |
867 | | - | |
| 867 | + | |
868 | 868 | | |
869 | 869 | | |
870 | | - | |
| 870 | + | |
871 | 871 | | |
872 | | - | |
| 872 | + | |
873 | 873 | | |
874 | 874 | | |
875 | | - | |
| 875 | + | |
876 | 876 | | |
877 | | - | |
| 877 | + | |
878 | 878 | | |
879 | 879 | | |
880 | | - | |
| 880 | + | |
881 | 881 | | |
882 | | - | |
| 882 | + | |
883 | 883 | | |
884 | 884 | | |
885 | | - | |
| 885 | + | |
886 | 886 | | |
887 | 887 | | |
888 | 888 | | |
889 | 889 | | |
890 | | - | |
| 890 | + | |
891 | 891 | | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
892 | 897 | | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
893 | 904 | | |
894 | 905 | | |
895 | 906 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
10 | 18 | | |
11 | 19 | | |
12 | 20 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
83 | 108 | | |
84 | 109 | | |
85 | 110 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | | - | |
19 | | - | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
0 commit comments