Skip to content

Commit 16a605f

Browse files
feat: add chart-type preference, OHLCV service wiring (#47749)
## **Description** Foundation PR (1/5) for the Token Details Page (TDP) advanced charts work. This splits the original #47737 into five reviewable, stacked PRs: 1. **#47749 (this PR)** — background/state foundation 2. #47751 — chart UI 3. #47753 — host-backed OHLCV pagination 4. #47771 — chart theming (line/candle colors driven by design tokens) 5. #47772 — chart analytics/instrumentation (interaction + empty-state events) This PR contains **no user-facing UI on its own**; the advanced chart is rendered in the UI PR (#47751) and stays behind the `AdvancedCharts` remote feature flag. Landing it first keeps the CODEOWNER-reviewed surface (background controller wiring, fixtures, privacy/state-logs) small and isolated. What it includes: - **Chart-type preference** on `PreferencesController` (plus `shared/types/preferences.ts` and `shared/lib/selectors/preferences.ts` selector) so the TDP can persist the user's candle/line choice. - **OHLCV service wiring** into the background: the `@metamask/core-backend` OHLCV service and its messenger are registered via `app/scripts/messenger-client-init/*` and `metamask-controller.js`. - **Fixtures & state snapshots** (`default-fixture.json`, `onboarding-fixture.json`, metrics state snapshots, `state-logs.json`) updated to reflect the new controller state. Base branch: `main`. ## **Changelog** CHANGELOG entry: null <!-- ## **Related issues** Fixes: --> ## **Manual testing steps** Since this PR adds no visible UI, testing is a no-regression smoke check: 1. Build and run the extension. 2. Complete onboarding / unlock an existing wallet. 3. Confirm the wallet loads normally (home, asset list, settings) with no errors in the background or UI console. 4. Open a token details page and confirm the existing (legacy) price chart still renders as before. <!-- ## **Screenshots/Recordings** ### **Before** ### **After** --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I've included tests if applicable - [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/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] > **Medium Risk** > Touches persisted preferences, background service initialization, and LavaMoat policies; new OHLCV service may open backend connectivity when later UI/flags enable it, but this PR alone adds no visible surface. > > **Overview** > Adds **background/state foundation** for Token Details Page advanced charts (no user-facing UI in this PR). > > **Chart preferences** — `PreferencesController` and `Preferences` types now persist `tdpChartType`, `tdpChartInterval`, and `tdpChartIndicators`, with shared defaults in `shared/constants/preferences` and new selectors (`getTdpChartType`, `getTdpChartInterval`, `getTdpChartIndicators`) so controller defaults and UI fallbacks stay aligned. Controller tests, fixtures, metrics snapshots, and state-logs schemas are updated for the new fields. > > **OHLCV streaming** — Registers `@metamask/core-backend` `OHLCVService` in the background via `OHLCVServiceInit`, a restricted `getOHLCVServiceMessenger` (core allow-lists), and `metamask-controller` / messenger factory wiring. LavaMoat webpack policies grant `AbortController` and `async-mutex` to `core-backend` for this dependency path. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 7f6d7d4. 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: MetaMask Bot <metamaskbot@users.noreply.github.com>
1 parent 6df047e commit 16a605f

29 files changed

Lines changed: 318 additions & 1 deletion

‎app/scripts/controllers/preferences-controller.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,9 @@ describe('preferences controller', () => {
605605
sortCallback: 'stringNumeric',
606606
},
607607
tokenNetworkFilter: {},
608+
tdpChartType: 2,
609+
tdpChartInterval: '15m',
610+
tdpChartIndicators: [],
608611
gasSponsorshipOptOutByChainId: {},
609612
});
610613
});
@@ -643,6 +646,9 @@ describe('preferences controller', () => {
643646
sortCallback: 'stringNumeric',
644647
},
645648
tokenNetworkFilter: {},
649+
tdpChartType: 2,
650+
tdpChartInterval: '15m',
651+
tdpChartIndicators: [],
646652
gasSponsorshipOptOutByChainId: {},
647653
});
648654
});
@@ -1059,6 +1065,9 @@ describe('preferences controller', () => {
10591065
"skipDeepLinkInterstitial": false,
10601066
"smartTransactionsMigrationApplied": false,
10611067
"smartTransactionsOptInStatus": true,
1068+
"tdpChartIndicators": [],
1069+
"tdpChartInterval": "15m",
1070+
"tdpChartType": 2,
10621071
"tokenNetworkFilter": {},
10631072
"tokenSortConfig": {
10641073
"key": "tokenFiatAmount",
@@ -1133,6 +1142,9 @@ describe('preferences controller', () => {
11331142
"skipDeepLinkInterstitial": false,
11341143
"smartTransactionsMigrationApplied": false,
11351144
"smartTransactionsOptInStatus": true,
1145+
"tdpChartIndicators": [],
1146+
"tdpChartInterval": "15m",
1147+
"tdpChartType": 2,
11361148
"tokenNetworkFilter": {},
11371149
"tokenSortConfig": {
11381150
"key": "tokenFiatAmount",
@@ -1221,6 +1233,9 @@ describe('preferences controller', () => {
12211233
"skipDeepLinkInterstitial": false,
12221234
"smartTransactionsMigrationApplied": false,
12231235
"smartTransactionsOptInStatus": true,
1236+
"tdpChartIndicators": [],
1237+
"tdpChartInterval": "15m",
1238+
"tdpChartType": 2,
12241239
"tokenNetworkFilter": {},
12251240
"tokenSortConfig": {
12261241
"key": "tokenFiatAmount",
@@ -1310,6 +1325,9 @@ describe('preferences controller', () => {
13101325
"skipDeepLinkInterstitial": false,
13111326
"smartTransactionsMigrationApplied": false,
13121327
"smartTransactionsOptInStatus": true,
1328+
"tdpChartIndicators": [],
1329+
"tdpChartInterval": "15m",
1330+
"tdpChartType": 2,
13131331
"tokenNetworkFilter": {},
13141332
"tokenSortConfig": {
13151333
"key": "tokenFiatAmount",

‎app/scripts/controllers/preferences-controller.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { IPFS_DEFAULT_GATEWAY_URL } from '../../../shared/constants/network';
1616
import { LedgerTransportTypes } from '../../../shared/constants/hardware-wallets';
1717
import {
1818
DEFAULT_AUTO_LOCK_TIME_LIMIT,
19+
DEFAULT_TDP_CHART_INDICATORS,
20+
DEFAULT_TDP_CHART_INTERVAL,
21+
DEFAULT_TDP_CHART_TYPE,
1922
ThemeType,
2023
} from '../../../shared/constants/preferences';
2124
import type {
@@ -202,6 +205,9 @@ export const getDefaultPreferencesControllerState =
202205
},
203206
useNativeCurrencyAsPrimaryCurrency: true,
204207
useSidePanelAsDefault: true,
208+
tdpChartType: DEFAULT_TDP_CHART_TYPE,
209+
tdpChartInterval: DEFAULT_TDP_CHART_INTERVAL,
210+
tdpChartIndicators: DEFAULT_TDP_CHART_INDICATORS,
205211
gasSponsorshipOptOutByChainId: {},
206212
},
207213
securityAlertsEnabled: true,

‎app/scripts/messenger-client-init/controller-list.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import { UserOperationController } from '@metamask/user-operation-controller';
7878
import {
7979
AccountActivityService,
8080
BackendWebSocketService,
81+
OHLCVService,
8182
} from '@metamask/core-backend';
8283
import { AuthenticatedUserStorageService } from '@metamask/authenticated-user-storage';
8384
import { ChompApiService } from '@metamask/chomp-api-service';
@@ -244,6 +245,7 @@ export type MessengerClient =
244245
| WebSocketService
245246
| BackendWebSocketService
246247
| AccountActivityService
248+
| OHLCVService
247249
| SnapAccountService
248250
| MultichainAccountService
249251
| NetworkEnablementController

‎app/scripts/messenger-client-init/core-backend/backend-websocket-service-init.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export const BackendWebSocketServiceInit: MessengerClientInitFunction<
119119
'RemoteFeatureFlagController:getState',
120120
)?.remoteFeatureFlags?.backendWebSocketConnection;
121121

122-
return resolveFlag(manifestFlag ?? remoteFlag);
122+
const isEnabled = resolveFlag(manifestFlag ?? remoteFlag);
123+
return isEnabled;
123124
} catch (error) {
124125
// If feature flag check fails, default to NOT connecting for safer startup
125126
console.warn(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { BackendWebSocketServiceInit } from './backend-websocket-service-init';
22
export { AccountActivityServiceInit } from './account-activity-service-init';
3+
export { OHLCVServiceInit } from './ohlcv-service-init';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { OHLCVService, OHLCVServiceMessenger } from '@metamask/core-backend';
2+
import { trace } from '../../../../shared/lib/trace';
3+
import { MessengerClientInitFunction } from '../types';
4+
5+
/**
6+
* Initialize the OHLCV service for real-time candlestick streaming.
7+
*
8+
* @param request - The request object.
9+
* @param request.controllerMessenger - The messenger to use for the service.
10+
* @returns The initialized service.
11+
*/
12+
export const OHLCVServiceInit: MessengerClientInitFunction<
13+
OHLCVService,
14+
OHLCVServiceMessenger
15+
> = ({ controllerMessenger }) => {
16+
const messengerClient = new OHLCVService({
17+
messenger: controllerMessenger,
18+
// @ts-expect-error: Types of `TraceRequest` are not the same.
19+
traceFn: trace,
20+
});
21+
22+
messengerClient.init();
23+
24+
return {
25+
memStateKey: null,
26+
persistedStateKey: null,
27+
messengerClient,
28+
};
29+
};

‎app/scripts/messenger-client-init/messengers/core-backend/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export type {
77
BackendWebSocketServiceInitMessenger,
88
} from './backend-websocket-service-messenger';
99
export { getAccountActivityServiceMessenger } from './account-activity-service-messenger';
10+
export { getOHLCVServiceMessenger } from './ohlcv-service-messenger';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
OHLCVServiceMessenger,
3+
OHLCV_SERVICE_ALLOWED_ACTIONS,
4+
OHLCV_SERVICE_ALLOWED_EVENTS,
5+
} from '@metamask/core-backend';
6+
import {
7+
Messenger,
8+
MessengerActions,
9+
MessengerEvents,
10+
} from '@metamask/messenger';
11+
import { RootMessenger } from '../../../lib/messenger';
12+
13+
/**
14+
* Get a restricted messenger for the OHLCV service. This is scoped to the
15+
* actions and events that the OHLCV service is allowed to handle.
16+
*
17+
* @param messenger - The main controller messenger.
18+
* @returns The restricted messenger.
19+
*/
20+
export function getOHLCVServiceMessenger(
21+
messenger: RootMessenger<
22+
MessengerActions<OHLCVServiceMessenger>,
23+
MessengerEvents<OHLCVServiceMessenger>
24+
>,
25+
): OHLCVServiceMessenger {
26+
const serviceMessenger: OHLCVServiceMessenger = new Messenger({
27+
namespace: 'OHLCVService',
28+
parent: messenger,
29+
});
30+
// Delegate exactly the actions/events the OHLCV service needs, sourced from
31+
// the allow-lists `@metamask/core-backend` exports. This grants minimal
32+
// permissions and stays in sync with core instead of a hand-maintained list.
33+
messenger.delegate({
34+
messenger: serviceMessenger,
35+
actions: [...OHLCV_SERVICE_ALLOWED_ACTIONS],
36+
events: [...OHLCV_SERVICE_ALLOWED_EVENTS],
37+
});
38+
return serviceMessenger;
39+
}

‎app/scripts/messenger-client-init/messengers/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getBackendWebSocketServiceMessenger,
2727
getBackendWebSocketServiceInitMessenger,
2828
getAccountActivityServiceMessenger,
29+
getOHLCVServiceMessenger,
2930
} from './core-backend';
3031
import {
3132
getMultichainTransactionsControllerMessenger,
@@ -626,6 +627,10 @@ export const MESSENGER_FACTORIES = {
626627
getMessenger: getAccountActivityServiceMessenger,
627628
getInitMessenger: noop,
628629
},
630+
OHLCVService: {
631+
getMessenger: getOHLCVServiceMessenger,
632+
getInitMessenger: noop,
633+
},
629634
SmartTransactionsController: {
630635
getMessenger: getSmartTransactionsControllerMessenger,
631636
getInitMessenger: getSmartTransactionsControllerInitMessenger,

‎app/scripts/metamask-controller.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ import {
320320
import {
321321
BackendWebSocketServiceInit,
322322
AccountActivityServiceInit,
323+
OHLCVServiceInit,
323324
} from './messenger-client-init/core-backend';
324325
import { AuthenticationControllerInit } from './messenger-client-init/identity/authentication-controller-init';
325326
import { UserStorageControllerInit } from './messenger-client-init/identity/user-storage-controller-init';
@@ -570,6 +571,7 @@ export default class MetamaskController extends EventEmitter {
570571
WebSocketService: WebSocketServiceInit,
571572
BackendWebSocketService: BackendWebSocketServiceInit,
572573
AccountActivityService: AccountActivityServiceInit,
574+
OHLCVService: OHLCVServiceInit,
573575
SentinelApiService: SentinelApiServiceInit,
574576
ComplianceService: ComplianceServiceInit,
575577
ComplianceController: ComplianceControllerInit,

0 commit comments

Comments
 (0)