@metamask-previews/core-backend 6.5.0-preview-e856aaf70 → 6.5.0-preview-b867f452d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -14
- package/dist/ws/AccountActivityService-method-action-types.cjs.map +1 -1
- package/dist/ws/AccountActivityService-method-action-types.d.cts +5 -8
- package/dist/ws/AccountActivityService-method-action-types.d.cts.map +1 -1
- package/dist/ws/AccountActivityService-method-action-types.d.mts +5 -8
- package/dist/ws/AccountActivityService-method-action-types.d.mts.map +1 -1
- package/dist/ws/AccountActivityService-method-action-types.mjs.map +1 -1
- package/dist/ws/AccountActivityService.cjs +51 -99
- package/dist/ws/AccountActivityService.cjs.map +1 -1
- package/dist/ws/AccountActivityService.d.cts +13 -20
- package/dist/ws/AccountActivityService.d.cts.map +1 -1
- package/dist/ws/AccountActivityService.d.mts +13 -20
- package/dist/ws/AccountActivityService.d.mts.map +1 -1
- package/dist/ws/AccountActivityService.mjs +51 -99
- package/dist/ws/AccountActivityService.mjs.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
|
@@ -9,22 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- **BREAKING:** `AccountActivityService` now determines which non-EVM chains to subscribe to from remote feature flags instead of a bundled list ([#9379](https://github.com/MetaMask/core/pull/9379))
|
|
13
|
-
- The `AccountActivityServiceMessenger` now requires the following delegate actions and events:
|
|
14
|
-
- `RemoteFeatureFlagController:getState` action and `RemoteFeatureFlagController:stateChange` event
|
|
15
|
-
- `AccountTreeController:getAccountsFromSelectedAccountGroup` action and `AccountTreeController:selectedAccountGroupChange` event
|
|
16
|
-
- EVM (`eip155`) subscriptions are always enabled. Solana, Tron, and Stellar subscriptions are enabled when the corresponding `networkAssetsSnapsMigrationSolana` / `networkAssetsSnapsMigrationTron` / `networkAssetsSnapsMigrationStellar` feature flag has `stage >= 1`, and disabled otherwise.
|
|
17
|
-
- Accounts whose scopes do not match an enabled chain are no longer subscribed at all.
|
|
18
|
-
- When the enabled chains change via a feature flag update while the websocket is connected, the service automatically resubscribes the selected account.
|
|
19
|
-
- **BREAKING:** `AccountActivityService.subscribe` and `AccountActivityService.unsubscribe` now require a `{ addresses: string[] }` argument ([#9531](https://github.com/MetaMask/core/pull/9531))
|
|
20
|
-
- Previously, the service only allowed a single address to be subscribed and unsubscribed at a time.
|
|
21
|
-
- Add `@metamask/remote-feature-flag-controller` `^4.2.2` as a dependency ([#9379](https://github.com/MetaMask/core/pull/9379))
|
|
22
12
|
- Bump `@metamask/messenger` from `^1.2.0` to `^2.0.0` ([#9392](https://github.com/MetaMask/core/pull/9392))
|
|
23
13
|
- Bump `@metamask/profile-sync-controller` from `^28.2.0` to `^28.3.0` ([#9463](https://github.com/MetaMask/core/pull/9463))
|
|
24
|
-
|
|
25
|
-
### Fixed
|
|
26
|
-
|
|
27
|
-
- `AccountActivityService` subscribes to all supported scopes for a given account ([#9379](https://github.com/MetaMask/core/pull/9379))
|
|
14
|
+
- Bump `@metamask/accounts-controller` from `^39.0.4` to `^39.0.5` ([#9470](https://github.com/MetaMask/core/pull/9470))
|
|
28
15
|
|
|
29
16
|
## [6.5.0]
|
|
30
17
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService-method-action-types.cjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { AccountActivityService } from './AccountActivityService';\n\n/**\n * Subscribe to account activity (transactions and balance updates)\n *
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.cjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { AccountActivityService } from './AccountActivityService';\n\n/**\n * Subscribe to account activity (transactions and balance updates)\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address\n */\nexport type AccountActivityServiceSubscribeAction = {\n type: `AccountActivityService:subscribe`;\n handler: AccountActivityService['subscribe'];\n};\n\n/**\n * Unsubscribe from account activity for specified address\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address to unsubscribe\n */\nexport type AccountActivityServiceUnsubscribeAction = {\n type: `AccountActivityService:unsubscribe`;\n handler: AccountActivityService['unsubscribe'];\n};\n\n/**\n * Union of all AccountActivityService action types.\n */\nexport type AccountActivityServiceMethodActions =\n | AccountActivityServiceSubscribeAction\n | AccountActivityServiceUnsubscribeAction;\n"]}
|
|
@@ -5,22 +5,19 @@
|
|
|
5
5
|
import type { AccountActivityService } from "./AccountActivityService.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* Subscribe to account activity (transactions and balance updates)
|
|
8
|
-
*
|
|
8
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
9
9
|
*
|
|
10
|
-
* @param subscription -
|
|
11
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
12
|
-
* or an `addresses` array for batch subscription
|
|
10
|
+
* @param subscription - Account subscription configuration with address
|
|
13
11
|
*/
|
|
14
12
|
export type AccountActivityServiceSubscribeAction = {
|
|
15
13
|
type: `AccountActivityService:subscribe`;
|
|
16
14
|
handler: AccountActivityService['subscribe'];
|
|
17
15
|
};
|
|
18
16
|
/**
|
|
19
|
-
* Unsubscribe from account activity for specified
|
|
20
|
-
*
|
|
17
|
+
* Unsubscribe from account activity for specified address
|
|
18
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
21
19
|
*
|
|
22
|
-
* @param subscription -
|
|
23
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
20
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
24
21
|
*/
|
|
25
22
|
export type AccountActivityServiceUnsubscribeAction = {
|
|
26
23
|
type: `AccountActivityService:unsubscribe`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService-method-action-types.d.cts","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.d.cts","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;;GAKG;AACH,MAAM,MAAM,qCAAqC,GAAG;IAClD,IAAI,EAAE,kCAAkC,CAAC;IACzC,OAAO,EAAE,sBAAsB,CAAC,WAAW,CAAC,CAAC;CAC9C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,uCAAuC,GAAG;IACpD,IAAI,EAAE,oCAAoC,CAAC;IAC3C,OAAO,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC3C,qCAAqC,GACrC,uCAAuC,CAAC"}
|
|
@@ -5,22 +5,19 @@
|
|
|
5
5
|
import type { AccountActivityService } from "./AccountActivityService.mjs";
|
|
6
6
|
/**
|
|
7
7
|
* Subscribe to account activity (transactions and balance updates)
|
|
8
|
-
*
|
|
8
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
9
9
|
*
|
|
10
|
-
* @param subscription -
|
|
11
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
12
|
-
* or an `addresses` array for batch subscription
|
|
10
|
+
* @param subscription - Account subscription configuration with address
|
|
13
11
|
*/
|
|
14
12
|
export type AccountActivityServiceSubscribeAction = {
|
|
15
13
|
type: `AccountActivityService:subscribe`;
|
|
16
14
|
handler: AccountActivityService['subscribe'];
|
|
17
15
|
};
|
|
18
16
|
/**
|
|
19
|
-
* Unsubscribe from account activity for specified
|
|
20
|
-
*
|
|
17
|
+
* Unsubscribe from account activity for specified address
|
|
18
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
21
19
|
*
|
|
22
|
-
* @param subscription -
|
|
23
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
20
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
24
21
|
*/
|
|
25
22
|
export type AccountActivityServiceUnsubscribeAction = {
|
|
26
23
|
type: `AccountActivityService:unsubscribe`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService-method-action-types.d.mts","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.d.mts","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;;GAKG;AACH,MAAM,MAAM,qCAAqC,GAAG;IAClD,IAAI,EAAE,kCAAkC,CAAC;IACzC,OAAO,EAAE,sBAAsB,CAAC,WAAW,CAAC,CAAC;CAC9C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,uCAAuC,GAAG;IACpD,IAAI,EAAE,oCAAoC,CAAC;IAC3C,OAAO,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC3C,qCAAqC,GACrC,uCAAuC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService-method-action-types.mjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { AccountActivityService } from './AccountActivityService';\n\n/**\n * Subscribe to account activity (transactions and balance updates)\n *
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.mjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { AccountActivityService } from './AccountActivityService';\n\n/**\n * Subscribe to account activity (transactions and balance updates)\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address\n */\nexport type AccountActivityServiceSubscribeAction = {\n type: `AccountActivityService:subscribe`;\n handler: AccountActivityService['subscribe'];\n};\n\n/**\n * Unsubscribe from account activity for specified address\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address to unsubscribe\n */\nexport type AccountActivityServiceUnsubscribeAction = {\n type: `AccountActivityService:unsubscribe`;\n handler: AccountActivityService['unsubscribe'];\n};\n\n/**\n * Union of all AccountActivityService action types.\n */\nexport type AccountActivityServiceMethodActions =\n | AccountActivityServiceSubscribeAction\n | AccountActivityServiceUnsubscribeAction;\n"]}
|
|
@@ -16,29 +16,18 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
16
16
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
17
17
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
18
18
|
};
|
|
19
|
-
var _AccountActivityService_instances, _AccountActivityService_messenger, _AccountActivityService_options, _AccountActivityService_trace, _AccountActivityService_chainsUp, _AccountActivityService_handleAccountActivityUpdate, _AccountActivityService_handleSelectedAccountChange, _AccountActivityService_handleSystemNotification, _AccountActivityService_handleWebSocketStateChange, _AccountActivityService_subscribeToSelectedAccount, _AccountActivityService_unsubscribeFromAllAccountActivity,
|
|
19
|
+
var _AccountActivityService_instances, _AccountActivityService_messenger, _AccountActivityService_options, _AccountActivityService_trace, _AccountActivityService_chainsUp, _AccountActivityService_handleAccountActivityUpdate, _AccountActivityService_handleSelectedAccountChange, _AccountActivityService_handleSystemNotification, _AccountActivityService_handleWebSocketStateChange, _AccountActivityService_subscribeToSelectedAccount, _AccountActivityService_unsubscribeFromAllAccountActivity, _AccountActivityService_convertToCaip10Address, _AccountActivityService_forceReconnection;
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
exports.AccountActivityService = exports.ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = exports.ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = void 0;
|
|
22
|
-
const utils_1 = require("@metamask/utils");
|
|
23
22
|
const logger_1 = require("../logger.cjs");
|
|
24
23
|
const BackendWebSocketService_1 = require("./BackendWebSocketService.cjs");
|
|
25
24
|
const SERVICE_NAME = 'AccountActivityService';
|
|
26
25
|
const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, SERVICE_NAME);
|
|
27
26
|
const MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'];
|
|
28
27
|
const SUBSCRIPTION_NAMESPACE = 'account-activity.v1';
|
|
29
|
-
// EVM subscriptions are always enabled.
|
|
30
|
-
const ALWAYS_SUPPORTED_CHAIN_PREFIXES = ['eip155'];
|
|
31
|
-
// Non-EVM chains are gated behind the
|
|
32
|
-
// per-network snaps-migration remote feature flags: a chain is
|
|
33
|
-
// enabled when its flag payload has `stage >= 1`.
|
|
34
|
-
const CHAIN_PREFIX_FEATURE_FLAGS = {
|
|
35
|
-
solana: 'networkAssetsSnapsMigrationSolana',
|
|
36
|
-
tron: 'networkAssetsSnapsMigrationTron',
|
|
37
|
-
stellar: 'networkAssetsSnapsMigrationStellar',
|
|
38
|
-
};
|
|
39
28
|
// Allowed actions that AccountActivityService can call on other controllers
|
|
40
29
|
exports.ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [
|
|
41
|
-
'
|
|
30
|
+
'AccountsController:getSelectedAccount',
|
|
42
31
|
'BackendWebSocketService:connect',
|
|
43
32
|
'BackendWebSocketService:forceReconnection',
|
|
44
33
|
'BackendWebSocketService:subscribe',
|
|
@@ -48,13 +37,11 @@ exports.ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [
|
|
|
48
37
|
'BackendWebSocketService:findSubscriptionsByChannelPrefix',
|
|
49
38
|
'BackendWebSocketService:addChannelCallback',
|
|
50
39
|
'BackendWebSocketService:removeChannelCallback',
|
|
51
|
-
'RemoteFeatureFlagController:getState',
|
|
52
40
|
];
|
|
53
41
|
// Allowed events that AccountActivityService can listen to
|
|
54
42
|
exports.ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [
|
|
55
|
-
'
|
|
43
|
+
'AccountsController:selectedAccountChange',
|
|
56
44
|
'BackendWebSocketService:connectionStateChanged',
|
|
57
|
-
'RemoteFeatureFlagController:stateChange',
|
|
58
45
|
];
|
|
59
46
|
// =============================================================================
|
|
60
47
|
// Main Service Class
|
|
@@ -123,24 +110,14 @@ class AccountActivityService {
|
|
|
123
110
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
111
|
((_request, fn) => fn?.()), "f");
|
|
125
112
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
126
|
-
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('
|
|
113
|
+
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('AccountsController:selectedAccountChange',
|
|
127
114
|
// Promise result intentionally not awaited
|
|
128
115
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
129
|
-
async () => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSelectedAccountChange).call(this));
|
|
116
|
+
async (account) => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSelectedAccountChange).call(this, account));
|
|
130
117
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('BackendWebSocketService:connectionStateChanged',
|
|
131
118
|
// Promise result intentionally not awaited
|
|
132
119
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
133
120
|
(connectionInfo) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleWebSocketStateChange).call(this, connectionInfo));
|
|
134
|
-
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe(
|
|
135
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
136
|
-
'RemoteFeatureFlagController:stateChange',
|
|
137
|
-
// Promise result intentionally not awaited
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
139
|
-
async () => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleFeatureFlagsStateChange).call(this),
|
|
140
|
-
// Only react to changes in the set of enabled chain prefixes. The
|
|
141
|
-
// messenger compares selector results with strict equality, so the
|
|
142
|
-
// selector must return a primitive rather than a fresh object.
|
|
143
|
-
(state) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_getSupportedChainPrefixes).call(this, state.remoteFeatureFlags).join(','));
|
|
144
121
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:addChannelCallback', {
|
|
145
122
|
channelName: `system-notifications.v1.${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}`,
|
|
146
123
|
callback: (notification) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSystemNotification).call(this, notification),
|
|
@@ -151,26 +128,22 @@ class AccountActivityService {
|
|
|
151
128
|
// =============================================================================
|
|
152
129
|
/**
|
|
153
130
|
* Subscribe to account activity (transactions and balance updates)
|
|
154
|
-
*
|
|
131
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
155
132
|
*
|
|
156
|
-
* @param subscription -
|
|
157
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
158
|
-
* or an `addresses` array for batch subscription
|
|
133
|
+
* @param subscription - Account subscription configuration with address
|
|
159
134
|
*/
|
|
160
|
-
async subscribe(
|
|
135
|
+
async subscribe(subscription) {
|
|
161
136
|
try {
|
|
162
137
|
await __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:connect');
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
.filter((channel) => !__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel));
|
|
168
|
-
if (channels.length === 0) {
|
|
138
|
+
// Create channel name from address
|
|
139
|
+
const channel = `${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}.${subscription.address}`;
|
|
140
|
+
// Check if already subscribed
|
|
141
|
+
if (__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
|
|
169
142
|
return;
|
|
170
143
|
}
|
|
171
144
|
// Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)
|
|
172
145
|
await __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:subscribe', {
|
|
173
|
-
channels,
|
|
146
|
+
channels: [channel],
|
|
174
147
|
channelType: __classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace, // e.g., 'account-activity.v1'
|
|
175
148
|
callback: (notification) => {
|
|
176
149
|
__classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleAccountActivityUpdate).call(this, notification.data);
|
|
@@ -183,21 +156,21 @@ class AccountActivityService {
|
|
|
183
156
|
}
|
|
184
157
|
}
|
|
185
158
|
/**
|
|
186
|
-
* Unsubscribe from account activity for specified
|
|
187
|
-
*
|
|
159
|
+
* Unsubscribe from account activity for specified address
|
|
160
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
188
161
|
*
|
|
189
|
-
* @param subscription -
|
|
190
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
162
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
191
163
|
*/
|
|
192
|
-
async unsubscribe(
|
|
164
|
+
async unsubscribe(subscription) {
|
|
165
|
+
const { address } = subscription;
|
|
193
166
|
try {
|
|
194
|
-
// Find
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (subscriptions.size === 0) {
|
|
167
|
+
// Find channel for the specified address
|
|
168
|
+
const channel = `${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}.${address}`;
|
|
169
|
+
const subscriptions = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
|
|
170
|
+
if (subscriptions.length === 0) {
|
|
199
171
|
return;
|
|
200
172
|
}
|
|
173
|
+
// Fast path: Direct unsubscribe using stored unsubscribe function
|
|
201
174
|
// Unsubscribe from all matching subscriptions
|
|
202
175
|
for (const subscriptionInfo of subscriptions) {
|
|
203
176
|
await subscriptionInfo.unsubscribe();
|
|
@@ -258,16 +231,20 @@ _AccountActivityService_messenger = new WeakMap(), _AccountActivityService_optio
|
|
|
258
231
|
}, _AccountActivityService_handleSelectedAccountChange =
|
|
259
232
|
/**
|
|
260
233
|
* Handle selected account change event
|
|
234
|
+
*
|
|
235
|
+
* @param newAccount - The newly selected account
|
|
261
236
|
*/
|
|
262
|
-
async function _AccountActivityService_handleSelectedAccountChange() {
|
|
263
|
-
|
|
237
|
+
async function _AccountActivityService_handleSelectedAccountChange(newAccount) {
|
|
238
|
+
if (!newAccount?.address) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
264
241
|
try {
|
|
242
|
+
// Convert new account to CAIP-10 format
|
|
243
|
+
const newAddress = __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Address).call(this, newAccount);
|
|
265
244
|
// First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions
|
|
266
245
|
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_unsubscribeFromAllAccountActivity).call(this);
|
|
267
|
-
//
|
|
268
|
-
await this.subscribe({
|
|
269
|
-
addresses: __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Addresses).call(this, selectedAccounts),
|
|
270
|
-
});
|
|
246
|
+
// Then, subscribe to the new selected account
|
|
247
|
+
await this.subscribe({ address: newAddress });
|
|
271
248
|
}
|
|
272
249
|
catch (error) {
|
|
273
250
|
log('Account change failed', { error });
|
|
@@ -339,10 +316,13 @@ async function _AccountActivityService_handleWebSocketStateChange(connectionInfo
|
|
|
339
316
|
* Subscribe to the currently selected account only
|
|
340
317
|
*/
|
|
341
318
|
async function _AccountActivityService_subscribeToSelectedAccount() {
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
319
|
+
const selectedAccount = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('AccountsController:getSelectedAccount');
|
|
320
|
+
if (!selectedAccount?.address) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
// Convert to CAIP-10 format and subscribe
|
|
324
|
+
const address = __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Address).call(this, selectedAccount);
|
|
325
|
+
await this.subscribe({ address });
|
|
346
326
|
}, _AccountActivityService_unsubscribeFromAllAccountActivity =
|
|
347
327
|
/**
|
|
348
328
|
* Unsubscribe from all account activity subscriptions for this service
|
|
@@ -354,47 +334,19 @@ async function _AccountActivityService_unsubscribeFromAllAccountActivity() {
|
|
|
354
334
|
for (const subscription of accountActivitySubscriptions) {
|
|
355
335
|
await subscription.unsubscribe();
|
|
356
336
|
}
|
|
357
|
-
},
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
// Skip unsupported accounts
|
|
363
|
-
return result;
|
|
364
|
-
}
|
|
365
|
-
result.push(`${accountPrefix}:0:${account.address}`);
|
|
366
|
-
return result;
|
|
367
|
-
}, []);
|
|
368
|
-
}, _AccountActivityService_getSupportedChainPrefixes = function _AccountActivityService_getSupportedChainPrefixes(remoteFeatureFlags = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('RemoteFeatureFlagController:getState').remoteFeatureFlags) {
|
|
369
|
-
const prefixes = [...ALWAYS_SUPPORTED_CHAIN_PREFIXES];
|
|
370
|
-
for (const [prefix, flagName] of Object.entries(CHAIN_PREFIX_FEATURE_FLAGS)) {
|
|
371
|
-
const flagValue = remoteFeatureFlags[flagName];
|
|
372
|
-
if ((0, utils_1.isObject)(flagValue) &&
|
|
373
|
-
typeof flagValue.stage === 'number' &&
|
|
374
|
-
flagValue.stage >= 1) {
|
|
375
|
-
prefixes.push(prefix);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
return prefixes;
|
|
379
|
-
}, _AccountActivityService_handleFeatureFlagsStateChange =
|
|
380
|
-
/**
|
|
381
|
-
* Handle remote feature flag changes: if the set of enabled chain prefixes
|
|
382
|
-
* changed while connected, resubscribe the selected account so new chains
|
|
383
|
-
* are picked up and disabled ones are dropped.
|
|
384
|
-
*/
|
|
385
|
-
async function _AccountActivityService_handleFeatureFlagsStateChange() {
|
|
386
|
-
try {
|
|
387
|
-
const { state } = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:getConnectionInfo');
|
|
388
|
-
if (state !== BackendWebSocketService_1.WebSocketState.CONNECTED) {
|
|
389
|
-
// Not connected: the next connection will subscribe with fresh flags
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_unsubscribeFromAllAccountActivity).call(this);
|
|
393
|
-
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_subscribeToSelectedAccount).call(this);
|
|
337
|
+
}, _AccountActivityService_convertToCaip10Address = function _AccountActivityService_convertToCaip10Address(account) {
|
|
338
|
+
// Check if account has EVM scopes
|
|
339
|
+
if (account.scopes.some((scope) => scope.startsWith('eip155:'))) {
|
|
340
|
+
// CAIP-10 format: eip155:0:address (subscribe to all EVM chains)
|
|
341
|
+
return `eip155:0:${account.address}`;
|
|
394
342
|
}
|
|
395
|
-
|
|
396
|
-
|
|
343
|
+
// Check if account has Solana scopes
|
|
344
|
+
if (account.scopes.some((scope) => scope.startsWith('solana:'))) {
|
|
345
|
+
// CAIP-10 format: solana:0:address (subscribe to all Solana chains)
|
|
346
|
+
return `solana:0:${account.address}`;
|
|
397
347
|
}
|
|
348
|
+
// For other chains or unknown scopes, return raw address
|
|
349
|
+
return account.address;
|
|
398
350
|
}, _AccountActivityService_forceReconnection =
|
|
399
351
|
/**
|
|
400
352
|
* Force WebSocket reconnection to clean up subscription state
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService.cjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;AAcH,2CAA2C;AAE3C,0CAA8D;AAY9D,2EAGmC;AAmBnC,MAAM,YAAY,GAAG,wBAAwB,CAAC;AAE9C,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,YAAY,CAAC,CAAC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AC;QAED,MAAM,uBAAA,IAAI,oGAAmC,MAAvC,IAAI,CAAqC,CAAC;QAChD,MAAM,uBAAA,IAAI,6FAA4B,MAAhC,IAAI,CAA8B,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qCAAqC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACxD,CAAC;AACH,CAAC;AAED;;GAEG;AACH,KAAK;IACH,GAAG,CAAC,+DAA+D,CAAC,CAAC;IAErE,yEAAyE;IACzE,kEAAkE;IAClE,MAAM,uBAAA,IAAI,yCAAW,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["/**\n * Account Activity Service for monitoring account transactions and balance changes\n *\n * This service subscribes to account activity and receives all transactions\n * and balance updates for those accounts via the comprehensive AccountActivityMessage format.\n */\n\nimport type {\n AccountTreeControllerSelectedAccountGroupChangeEvent,\n AccountTreeControllerGetAccountsFromSelectedAccountGroupAction,\n} from '@metamask/account-tree-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n FeatureFlags,\n RemoteFeatureFlagControllerGetStateAction,\n RemoteFeatureFlagControllerStateChangeEvent,\n} from '@metamask/remote-feature-flag-controller';\nimport { isObject } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n Transaction,\n AccountActivityMessage,\n BalanceUpdate,\n} from '../types';\nimport type { AccountActivityServiceMethodActions } from './AccountActivityService-method-action-types';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from './BackendWebSocketService';\nimport {\n WebSocketState,\n WebSocketSubscription,\n} from './BackendWebSocketService';\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\n\n// =============================================================================\n// Types and Constants\n// =============================================================================\n\n/**\n * System notification data for chain status updates\n */\nexport type SystemNotificationData = {\n /** Array of chain IDs affected (e.g., ['eip155:137', 'eip155:1']) */\n chainIds: string[];\n /** Status of the chains: 'down' or 'up' */\n status: 'down' | 'up';\n /** Timestamp of the notification */\n timestamp?: number;\n};\n\nconst SERVICE_NAME = 'AccountActivityService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'account-activity.v1';\n\n// EVM subscriptions are always enabled.\nconst ALWAYS_SUPPORTED_CHAIN_PREFIXES = ['eip155'] as const;\n\n// Non-EVM chains are gated behind the\n// per-network snaps-migration remote feature flags: a chain is\n// enabled when its flag payload has `stage >= 1`.\nconst CHAIN_PREFIX_FEATURE_FLAGS = {\n solana: 'networkAssetsSnapsMigrationSolana',\n tron: 'networkAssetsSnapsMigrationTron',\n stellar: 'networkAssetsSnapsMigrationStellar',\n} as const;\n\n/**\n * Account subscription options\n */\nexport type SubscriptionOptions = {\n /**\n * Array of addresses to subscribe to, each in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n */\n addresses: string[];\n};\n\n/**\n * Configuration options for the account activity service\n */\nexport type AccountActivityServiceOptions = {\n /** Custom subscription namespace (default: 'account-activity.v1') */\n subscriptionNamespace?: string;\n /** Optional callback to trace performance of account activity operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\n// Action types for the messaging system - using generated method actions\nexport type AccountActivityServiceActions = AccountActivityServiceMethodActions;\n\n// Allowed actions that AccountActivityService can call on other controllers\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n 'RemoteFeatureFlagController:getState',\n] as const;\n\n// Allowed events that AccountActivityService can listen to\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [\n 'AccountTreeController:selectedAccountGroupChange',\n 'BackendWebSocketService:connectionStateChanged',\n 'RemoteFeatureFlagController:stateChange',\n] as const;\n\nexport type AllowedActions =\n | AccountTreeControllerGetAccountsFromSelectedAccountGroupAction\n | BackendWebSocketServiceMethodActions\n | RemoteFeatureFlagControllerGetStateAction;\n\n// Event types for the messaging system\n\nexport type AccountActivityServiceTransactionUpdatedEvent = {\n type: `AccountActivityService:transactionUpdated`;\n payload: [Transaction];\n};\n\nexport type AccountActivityServiceBalanceUpdatedEvent = {\n type: `AccountActivityService:balanceUpdated`;\n payload: [{ address: string; chain: string; updates: BalanceUpdate[] }];\n};\n\nexport type AccountActivityServiceSubscriptionErrorEvent = {\n type: `AccountActivityService:subscriptionError`;\n payload: [{ addresses: string[]; error: string; operation: string }];\n};\n\nexport type AccountActivityServiceStatusChangedEvent = {\n type: `AccountActivityService:statusChanged`;\n payload: [\n {\n chainIds: string[];\n status: 'up' | 'down';\n timestamp?: number;\n },\n ];\n};\n\nexport type AccountActivityServiceEvents =\n | AccountActivityServiceTransactionUpdatedEvent\n | AccountActivityServiceBalanceUpdatedEvent\n | AccountActivityServiceSubscriptionErrorEvent\n | AccountActivityServiceStatusChangedEvent;\n\nexport type AllowedEvents =\n | AccountTreeControllerSelectedAccountGroupChangeEvent\n | BackendWebSocketServiceConnectionStateChangedEvent\n | RemoteFeatureFlagControllerStateChangeEvent;\n\nexport type AccountActivityServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n AccountActivityServiceActions | AllowedActions,\n AccountActivityServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * High-performance service for real-time account activity monitoring using optimized\n * WebSocket subscriptions with direct callback routing. Automatically subscribes to\n * the currently selected account and switches subscriptions when the selected account changes.\n * Receives transactions and balance updates using the comprehensive AccountActivityMessage format.\n *\n * Performance Features:\n * - Direct callback routing (no EventEmitter overhead)\n * - Minimal subscription tracking (no duplication with BackendWebSocketService)\n * - Optimized cleanup for mobile environments\n * - Single-account subscription (only selected account)\n * - Comprehensive balance updates with transfer tracking\n *\n * Architecture:\n * - Uses messenger pattern to communicate with BackendWebSocketService\n * - AccountActivityService tracks channel-to-subscriptionId mappings via messenger calls\n * - Automatically subscribes to selected account on initialization\n * - Switches subscriptions when selected account changes\n * - No direct dependency on BackendWebSocketService (uses messenger instead)\n *\n * @example\n * ```typescript\n * const service = new AccountActivityService({\n * messenger: activityMessenger,\n * });\n *\n * // Service automatically subscribes to the currently selected account\n * // When user switches accounts, service automatically resubscribes\n *\n * // All transactions and balance updates are received via optimized\n * // WebSocket callbacks and processed with zero-allocation routing\n * // Balance updates include comprehensive transfer details and post-transaction balances\n * ```\n */\nexport class AccountActivityService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: AccountActivityServiceMessenger;\n\n readonly #options: Required<Omit<AccountActivityServiceOptions, 'traceFn'>>;\n\n readonly #trace: TraceCallback;\n\n // Track chains that are currently up (based on system notifications)\n readonly #chainsUp: Set<string> = new Set();\n\n // =============================================================================\n // Constructor and Initialization\n // =============================================================================\n\n /**\n * Creates a new Account Activity service instance\n *\n * @param options - Configuration options including messenger\n */\n constructor(\n options: AccountActivityServiceOptions & {\n messenger: AccountActivityServiceMessenger;\n },\n ) {\n this.#messenger = options.messenger;\n\n // Set configuration with defaults\n this.#options = {\n subscriptionNamespace:\n options.subscriptionNamespace ?? SUBSCRIPTION_NAMESPACE,\n };\n\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n this.#messenger.subscribe(\n 'AccountTreeController:selectedAccountGroupChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => await this.#handleSelectedAccountChange(),\n );\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n this.#messenger.subscribe(\n // eslint-disable-next-line no-restricted-syntax\n 'RemoteFeatureFlagController:stateChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => await this.#handleFeatureFlagsStateChange(),\n // Only react to changes in the set of enabled chain prefixes. The\n // messenger compares selector results with strict equality, so the\n // selector must return a primitive rather than a fresh object.\n (state) =>\n this.#getSupportedChainPrefixes(state.remoteFeatureFlags).join(','),\n );\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Account Subscription Methods\n // =============================================================================\n\n /**\n * Subscribe to account activity (transactions and balance updates)\n * Addresses should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - The subscription configuration\n * @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format\n * or an `addresses` array for batch subscription\n */\n async subscribe({ addresses }: SubscriptionOptions): Promise<void> {\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n // Derive new subscriptions to be created from the provided addresses,\n // filtering out any channels that already have an active subscription\n const channels = addresses\n .map((address) => `${this.#options.subscriptionNamespace}.${address}`)\n .filter(\n (channel) =>\n !this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n ),\n );\n\n if (channels.length === 0) {\n return;\n }\n\n // Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels,\n channelType: this.#options.subscriptionNamespace, // e.g., 'account-activity.v1'\n callback: (notification: ServerNotificationMessage) => {\n this.#handleAccountActivityUpdate(\n notification.data as AccountActivityMessage,\n );\n },\n });\n } catch (error) {\n log('Subscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n /**\n * Unsubscribe from account activity for specified addresses\n * Addresses should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - The subscription configuration\n * @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format\n */\n async unsubscribe({ addresses }: SubscriptionOptions): Promise<void> {\n try {\n // Find all subscriptions for the specified addresses\n const subscriptions = new Set<WebSocketSubscription>(\n addresses\n .map((address) =>\n this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n `${this.#options.subscriptionNamespace}.${address}`,\n ),\n )\n .flat(),\n );\n\n if (subscriptions.size === 0) {\n return;\n }\n\n // Unsubscribe from all matching subscriptions\n for (const subscriptionInfo of subscriptions) {\n await subscriptionInfo.unsubscribe();\n }\n } catch (error) {\n log('Unsubscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n // =============================================================================\n // Private Methods - Event Handlers\n // =============================================================================\n\n /**\n * Handle account activity updates (transactions + balance changes)\n * Processes the comprehensive AccountActivityMessage format with detailed balance updates and transfers\n *\n * @param payload - The account activity message containing transaction and balance updates\n * @example AccountActivityMessage format handling:\n * Input: {\n * address: \"0xd14b52362b5b777ffa754c666ddec6722aaeee08\",\n * tx: { id: \"0x1cde...\", chain: \"eip155:8453\", status: \"confirmed\", timestamp: 1760099871, ... },\n * updates: [{\n * asset: { fungible: true, type: \"eip155:8453/erc20:0x833...\", unit: \"USDC\", decimals: 6 },\n * postBalance: { amount: \"0xc350\" },\n * transfers: [{ from: \"0x7b07...\", to: \"0xd14b...\", amount: \"0x2710\" }]\n * }]\n * }\n * Output: Transaction and balance updates published separately\n */\n #handleAccountActivityUpdate(payload: AccountActivityMessage): void {\n const { address, tx, updates } = payload;\n\n // Calculate time elapsed between transaction time and message receipt\n const txTimestampMs = tx.timestamp * 1000; // Convert Unix timestamp (seconds) to milliseconds\n const elapsedMs = Date.now() - txTimestampMs;\n\n log('Handling account activity update', {\n address,\n updateCount: updates.length,\n elapsedMs,\n });\n\n // Trace message receipt with latency from transaction time to now\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Transaction Message`,\n data: {\n chain: tx.chain,\n status: tx.status,\n elapsed_ms: elapsedMs,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: this.#options.subscriptionNamespace,\n },\n },\n () => {\n // Process transaction update\n this.#messenger.publish(\n `AccountActivityService:transactionUpdated`,\n tx,\n );\n\n // Publish comprehensive balance updates with transfer details\n this.#messenger.publish(`AccountActivityService:balanceUpdated`, {\n address,\n chain: tx.chain,\n updates,\n });\n },\n );\n }\n\n /**\n * Handle selected account change event\n */\n async #handleSelectedAccountChange(): Promise<void> {\n const selectedAccounts = this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n try {\n // First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions\n await this.#unsubscribeFromAllAccountActivity();\n\n // Subscribe to the new selected accounts in CAIP-10 format\n await this.subscribe({\n addresses: this.#convertToCaip10Addresses(selectedAccounts),\n });\n } catch (error) {\n log('Account change failed', { error });\n }\n }\n\n /**\n * Handle system notification for chain status changes\n * Publishes only the status change (delta) for affected chains\n *\n * @param notification - Server notification message containing chain status updates and timestamp\n */\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as SystemNotificationData;\n const { timestamp } = notification;\n\n // Validate required fields\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n // Track chain status\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n // Publish status change directly (delta update)\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(\n `WebSocket status change - Published tracked chains as ${data.status}`,\n {\n count: data.chainIds.length,\n chains: data.chainIds,\n status: data.status,\n },\n );\n }\n\n /**\n * Handle WebSocket connection state changes for fallback polling and resubscription\n *\n * @param connectionInfo - WebSocket connection state information\n */\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n // WebSocket connected - resubscribe to selected account\n // The system notification will automatically provide the list of chains that are up\n await this.#subscribeToSelectedAccount();\n } else if (state === WebSocketState.DISCONNECTED) {\n // On disconnect, flush all tracked chains as down\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log('WebSocket disconnection - Published tracked chains as down', {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n });\n\n // Clear the tracking set since all chains are now down\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private Methods - Subscription Management\n // =============================================================================\n\n /**\n * Subscribe to the currently selected account only\n */\n async #subscribeToSelectedAccount(): Promise<void> {\n const selectedAccounts = this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n await this.subscribe({\n addresses: this.#convertToCaip10Addresses(selectedAccounts),\n });\n }\n\n /**\n * Unsubscribe from all account activity subscriptions for this service\n * Finds all channels matching the service's namespace and unsubscribes from them\n */\n async #unsubscribeFromAllAccountActivity(): Promise<void> {\n const accountActivitySubscriptions = this.#messenger.call(\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n this.#options.subscriptionNamespace,\n );\n\n // Unsubscribe from all matching subscriptions\n for (const subscription of accountActivitySubscriptions) {\n await subscription.unsubscribe();\n }\n }\n\n // =============================================================================\n // Private Methods - Utility Functions\n // =============================================================================\n\n /**\n * Convert a list of InternalAccount addresses to CAIP-10 format, using the first\n * supported chain prefix matching the account's scopes\n *\n * @param accounts - The internal accounts to convert\n * @returns The CAIP-10 formatted addresses (e.g. [`eip155:0:address`], meaning\n * all chains of that namespace), or an empty array if none of the account's\n * scopes are supported\n */\n #convertToCaip10Addresses(accounts: InternalAccount[]): string[] {\n const supportedChainPrefixes = this.#getSupportedChainPrefixes();\n return accounts.reduce<string[]>((result, account) => {\n const accountPrefix = supportedChainPrefixes.find((prefix) =>\n account.scopes.some((scope) => scope.startsWith(`${prefix}:`)),\n );\n\n if (!accountPrefix) {\n // Skip unsupported accounts\n return result;\n }\n\n result.push(`${accountPrefix}:0:${account.address}`);\n return result;\n }, []);\n }\n\n /**\n * Get the chain prefixes currently enabled for subscriptions: EVM is always\n * enabled, while other chains are gated behind their per-network remote\n * feature flag (enabled when the flag payload has `stage >= 1`).\n *\n * @param remoteFeatureFlags - The remote feature flags state to check for enabled chains.\n * @returns An array of enabled CAIP-2 namespace prefixes (e.g. `['eip155', 'solana']`)\n */\n #getSupportedChainPrefixes(\n remoteFeatureFlags: FeatureFlags = this.#messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags,\n ): string[] {\n const prefixes: string[] = [...ALWAYS_SUPPORTED_CHAIN_PREFIXES];\n for (const [prefix, flagName] of Object.entries(\n CHAIN_PREFIX_FEATURE_FLAGS,\n )) {\n const flagValue = remoteFeatureFlags[flagName];\n if (\n isObject(flagValue) &&\n typeof flagValue.stage === 'number' &&\n flagValue.stage >= 1\n ) {\n prefixes.push(prefix);\n }\n }\n return prefixes;\n }\n\n /**\n * Handle remote feature flag changes: if the set of enabled chain prefixes\n * changed while connected, resubscribe the selected account so new chains\n * are picked up and disabled ones are dropped.\n */\n async #handleFeatureFlagsStateChange(): Promise<void> {\n try {\n const { state } = this.#messenger.call(\n 'BackendWebSocketService:getConnectionInfo',\n );\n if (state !== WebSocketState.CONNECTED) {\n // Not connected: the next connection will subscribe with fresh flags\n return;\n }\n\n await this.#unsubscribeFromAllAccountActivity();\n await this.#subscribeToSelectedAccount();\n } catch (error) {\n log('Feature flag change handling failed', { error });\n }\n }\n\n /**\n * Force WebSocket reconnection to clean up subscription state\n */\n async #forceReconnection(): Promise<void> {\n log('Forcing WebSocket reconnection to clean up subscription state');\n\n // Use the dedicated forceReconnection method which performs a controlled\n // disconnect-then-connect sequence to clean up subscription state\n await this.#messenger.call('BackendWebSocketService:forceReconnection');\n }\n\n // =============================================================================\n // Public Methods - Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources\n * Optimized for fast cleanup during service destruction or mobile app termination\n */\n destroy(): void {\n // Clean up system notification callback\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountActivityService.cjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;AAUH,0CAA8D;AAY9D,2EAA2D;AAmB3D,MAAM,YAAY,GAAG,wBAAwB,CAAC;AAE9C,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,MAAM,yBAAyB,GAAG,CAAC,WAAW,EAAE,aAAa,CAAU,CAAC;AAExE,MAAM,sBAAsB,GAAG,qBAAqB,CAAC;AA0BrD,4EAA4E;AAC/D,QAAA,wCAAwC,GAAG;IACtD,uCAAuC;IACvC,iCAAiC;IACj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AACpC,CAAC;AAED;;;GAGG;AACH,KAAK;IACH,MAAM,4BAA4B,GAAG,uBAAA,IAAI,yCAAW,CAAC,IAAI,CACvD,0DAA0D,EAC1D,uBAAA,IAAI,uCAAS,CAAC,qBAAqB,CACpC,CAAC;IAEF,8CAA8C;IAC9C,KAAK,MAAM,YAAY,IAAI,4BAA4B,EAAE,CAAC;QACxD,MAAM,YAAY,CAAC,WAAW,EAAE,CAAC;IACnC,CAAC;AACH,CAAC,2GAYuB,OAAwB;IAC9C,kCAAkC;IAClC,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;QAChE,iEAAiE;QACjE,OAAO,YAAY,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,CAAC;IAED,qCAAqC;IACrC,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;QAChE,oEAAoE;QACpE,OAAO,YAAY,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,CAAC;IAED,yDAAyD;IACzD,OAAO,OAAO,CAAC,OAAO,CAAC;AACzB,CAAC;AAED;;GAEG;AACH,KAAK;IACH,GAAG,CAAC,+DAA+D,CAAC,CAAC;IAErE,yEAAyE;IACzE,kEAAkE;IAClE,MAAM,uBAAA,IAAI,yCAAW,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["/**\n * Account Activity Service for monitoring account transactions and balance changes\n *\n * This service subscribes to account activity and receives all transactions\n * and balance updates for those accounts via the comprehensive AccountActivityMessage format.\n */\n\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n Transaction,\n AccountActivityMessage,\n BalanceUpdate,\n} from '../types';\nimport type { AccountActivityServiceMethodActions } from './AccountActivityService-method-action-types';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from './BackendWebSocketService';\nimport { WebSocketState } from './BackendWebSocketService';\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\n\n// =============================================================================\n// Types and Constants\n// =============================================================================\n\n/**\n * System notification data for chain status updates\n */\nexport type SystemNotificationData = {\n /** Array of chain IDs affected (e.g., ['eip155:137', 'eip155:1']) */\n chainIds: string[];\n /** Status of the chains: 'down' or 'up' */\n status: 'down' | 'up';\n /** Timestamp of the notification */\n timestamp?: number;\n};\n\nconst SERVICE_NAME = 'AccountActivityService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'account-activity.v1';\n\n/**\n * Account subscription options\n */\nexport type SubscriptionOptions = {\n address: string; // Should be in CAIP-10 format, e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\"\n};\n\n/**\n * Configuration options for the account activity service\n */\nexport type AccountActivityServiceOptions = {\n /** Custom subscription namespace (default: 'account-activity.v1') */\n subscriptionNamespace?: string;\n /** Optional callback to trace performance of account activity operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\n// Action types for the messaging system - using generated method actions\nexport type AccountActivityServiceActions = AccountActivityServiceMethodActions;\n\n// Allowed actions that AccountActivityService can call on other controllers\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [\n 'AccountsController:getSelectedAccount',\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\n// Allowed events that AccountActivityService can listen to\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [\n 'AccountsController:selectedAccountChange',\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions =\n | AccountsControllerGetSelectedAccountAction\n | BackendWebSocketServiceMethodActions;\n\n// Event types for the messaging system\n\nexport type AccountActivityServiceTransactionUpdatedEvent = {\n type: `AccountActivityService:transactionUpdated`;\n payload: [Transaction];\n};\n\nexport type AccountActivityServiceBalanceUpdatedEvent = {\n type: `AccountActivityService:balanceUpdated`;\n payload: [{ address: string; chain: string; updates: BalanceUpdate[] }];\n};\n\nexport type AccountActivityServiceSubscriptionErrorEvent = {\n type: `AccountActivityService:subscriptionError`;\n payload: [{ addresses: string[]; error: string; operation: string }];\n};\n\nexport type AccountActivityServiceStatusChangedEvent = {\n type: `AccountActivityService:statusChanged`;\n payload: [\n {\n chainIds: string[];\n status: 'up' | 'down';\n timestamp?: number;\n },\n ];\n};\n\nexport type AccountActivityServiceEvents =\n | AccountActivityServiceTransactionUpdatedEvent\n | AccountActivityServiceBalanceUpdatedEvent\n | AccountActivityServiceSubscriptionErrorEvent\n | AccountActivityServiceStatusChangedEvent;\n\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type AccountActivityServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n AccountActivityServiceActions | AllowedActions,\n AccountActivityServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * High-performance service for real-time account activity monitoring using optimized\n * WebSocket subscriptions with direct callback routing. Automatically subscribes to\n * the currently selected account and switches subscriptions when the selected account changes.\n * Receives transactions and balance updates using the comprehensive AccountActivityMessage format.\n *\n * Performance Features:\n * - Direct callback routing (no EventEmitter overhead)\n * - Minimal subscription tracking (no duplication with BackendWebSocketService)\n * - Optimized cleanup for mobile environments\n * - Single-account subscription (only selected account)\n * - Comprehensive balance updates with transfer tracking\n *\n * Architecture:\n * - Uses messenger pattern to communicate with BackendWebSocketService\n * - AccountActivityService tracks channel-to-subscriptionId mappings via messenger calls\n * - Automatically subscribes to selected account on initialization\n * - Switches subscriptions when selected account changes\n * - No direct dependency on BackendWebSocketService (uses messenger instead)\n *\n * @example\n * ```typescript\n * const service = new AccountActivityService({\n * messenger: activityMessenger,\n * });\n *\n * // Service automatically subscribes to the currently selected account\n * // When user switches accounts, service automatically resubscribes\n *\n * // All transactions and balance updates are received via optimized\n * // WebSocket callbacks and processed with zero-allocation routing\n * // Balance updates include comprehensive transfer details and post-transaction balances\n * ```\n */\nexport class AccountActivityService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: AccountActivityServiceMessenger;\n\n readonly #options: Required<Omit<AccountActivityServiceOptions, 'traceFn'>>;\n\n readonly #trace: TraceCallback;\n\n // Track chains that are currently up (based on system notifications)\n readonly #chainsUp: Set<string> = new Set();\n\n // =============================================================================\n // Constructor and Initialization\n // =============================================================================\n\n /**\n * Creates a new Account Activity service instance\n *\n * @param options - Configuration options including messenger\n */\n constructor(\n options: AccountActivityServiceOptions & {\n messenger: AccountActivityServiceMessenger;\n },\n ) {\n this.#messenger = options.messenger;\n\n // Set configuration with defaults\n this.#options = {\n subscriptionNamespace:\n options.subscriptionNamespace ?? SUBSCRIPTION_NAMESPACE,\n };\n\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n this.#messenger.subscribe(\n 'AccountsController:selectedAccountChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account: InternalAccount) =>\n await this.#handleSelectedAccountChange(account),\n );\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Account Subscription Methods\n // =============================================================================\n\n /**\n * Subscribe to account activity (transactions and balance updates)\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address\n */\n async subscribe(subscription: SubscriptionOptions): Promise<void> {\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n // Create channel name from address\n const channel = `${this.#options.subscriptionNamespace}.${subscription.address}`;\n\n // Check if already subscribed\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n return;\n }\n\n // Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: this.#options.subscriptionNamespace, // e.g., 'account-activity.v1'\n callback: (notification: ServerNotificationMessage) => {\n this.#handleAccountActivityUpdate(\n notification.data as AccountActivityMessage,\n );\n },\n });\n } catch (error) {\n log('Subscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n /**\n * Unsubscribe from account activity for specified address\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address to unsubscribe\n */\n async unsubscribe(subscription: SubscriptionOptions): Promise<void> {\n const { address } = subscription;\n try {\n // Find channel for the specified address\n const channel = `${this.#options.subscriptionNamespace}.${address}`;\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n if (subscriptions.length === 0) {\n return;\n }\n\n // Fast path: Direct unsubscribe using stored unsubscribe function\n // Unsubscribe from all matching subscriptions\n for (const subscriptionInfo of subscriptions) {\n await subscriptionInfo.unsubscribe();\n }\n } catch (error) {\n log('Unsubscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n // =============================================================================\n // Private Methods - Event Handlers\n // =============================================================================\n\n /**\n * Handle account activity updates (transactions + balance changes)\n * Processes the comprehensive AccountActivityMessage format with detailed balance updates and transfers\n *\n * @param payload - The account activity message containing transaction and balance updates\n * @example AccountActivityMessage format handling:\n * Input: {\n * address: \"0xd14b52362b5b777ffa754c666ddec6722aaeee08\",\n * tx: { id: \"0x1cde...\", chain: \"eip155:8453\", status: \"confirmed\", timestamp: 1760099871, ... },\n * updates: [{\n * asset: { fungible: true, type: \"eip155:8453/erc20:0x833...\", unit: \"USDC\", decimals: 6 },\n * postBalance: { amount: \"0xc350\" },\n * transfers: [{ from: \"0x7b07...\", to: \"0xd14b...\", amount: \"0x2710\" }]\n * }]\n * }\n * Output: Transaction and balance updates published separately\n */\n #handleAccountActivityUpdate(payload: AccountActivityMessage): void {\n const { address, tx, updates } = payload;\n\n // Calculate time elapsed between transaction time and message receipt\n const txTimestampMs = tx.timestamp * 1000; // Convert Unix timestamp (seconds) to milliseconds\n const elapsedMs = Date.now() - txTimestampMs;\n\n log('Handling account activity update', {\n address,\n updateCount: updates.length,\n elapsedMs,\n });\n\n // Trace message receipt with latency from transaction time to now\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Transaction Message`,\n data: {\n chain: tx.chain,\n status: tx.status,\n elapsed_ms: elapsedMs,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: this.#options.subscriptionNamespace,\n },\n },\n () => {\n // Process transaction update\n this.#messenger.publish(\n `AccountActivityService:transactionUpdated`,\n tx,\n );\n\n // Publish comprehensive balance updates with transfer details\n this.#messenger.publish(`AccountActivityService:balanceUpdated`, {\n address,\n chain: tx.chain,\n updates,\n });\n },\n );\n }\n\n /**\n * Handle selected account change event\n *\n * @param newAccount - The newly selected account\n */\n async #handleSelectedAccountChange(\n newAccount: InternalAccount | null,\n ): Promise<void> {\n if (!newAccount?.address) {\n return;\n }\n\n try {\n // Convert new account to CAIP-10 format\n const newAddress = this.#convertToCaip10Address(newAccount);\n\n // First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions\n await this.#unsubscribeFromAllAccountActivity();\n\n // Then, subscribe to the new selected account\n await this.subscribe({ address: newAddress });\n } catch (error) {\n log('Account change failed', { error });\n }\n }\n\n /**\n * Handle system notification for chain status changes\n * Publishes only the status change (delta) for affected chains\n *\n * @param notification - Server notification message containing chain status updates and timestamp\n */\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as SystemNotificationData;\n const { timestamp } = notification;\n\n // Validate required fields\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n // Track chain status\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n // Publish status change directly (delta update)\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(\n `WebSocket status change - Published tracked chains as ${data.status}`,\n {\n count: data.chainIds.length,\n chains: data.chainIds,\n status: data.status,\n },\n );\n }\n\n /**\n * Handle WebSocket connection state changes for fallback polling and resubscription\n *\n * @param connectionInfo - WebSocket connection state information\n */\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n // WebSocket connected - resubscribe to selected account\n // The system notification will automatically provide the list of chains that are up\n await this.#subscribeToSelectedAccount();\n } else if (state === WebSocketState.DISCONNECTED) {\n // On disconnect, flush all tracked chains as down\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log('WebSocket disconnection - Published tracked chains as down', {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n });\n\n // Clear the tracking set since all chains are now down\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private Methods - Subscription Management\n // =============================================================================\n\n /**\n * Subscribe to the currently selected account only\n */\n async #subscribeToSelectedAccount(): Promise<void> {\n const selectedAccount = this.#messenger.call(\n 'AccountsController:getSelectedAccount',\n );\n\n if (!selectedAccount?.address) {\n return;\n }\n\n // Convert to CAIP-10 format and subscribe\n const address = this.#convertToCaip10Address(selectedAccount);\n await this.subscribe({ address });\n }\n\n /**\n * Unsubscribe from all account activity subscriptions for this service\n * Finds all channels matching the service's namespace and unsubscribes from them\n */\n async #unsubscribeFromAllAccountActivity(): Promise<void> {\n const accountActivitySubscriptions = this.#messenger.call(\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n this.#options.subscriptionNamespace,\n );\n\n // Unsubscribe from all matching subscriptions\n for (const subscription of accountActivitySubscriptions) {\n await subscription.unsubscribe();\n }\n }\n\n // =============================================================================\n // Private Methods - Utility Functions\n // =============================================================================\n\n /**\n * Convert an InternalAccount address to CAIP-10 format or raw address\n *\n * @param account - The internal account to convert\n * @returns The CAIP-10 formatted address or raw address\n */\n #convertToCaip10Address(account: InternalAccount): string {\n // Check if account has EVM scopes\n if (account.scopes.some((scope) => scope.startsWith('eip155:'))) {\n // CAIP-10 format: eip155:0:address (subscribe to all EVM chains)\n return `eip155:0:${account.address}`;\n }\n\n // Check if account has Solana scopes\n if (account.scopes.some((scope) => scope.startsWith('solana:'))) {\n // CAIP-10 format: solana:0:address (subscribe to all Solana chains)\n return `solana:0:${account.address}`;\n }\n\n // For other chains or unknown scopes, return raw address\n return account.address;\n }\n\n /**\n * Force WebSocket reconnection to clean up subscription state\n */\n async #forceReconnection(): Promise<void> {\n log('Forcing WebSocket reconnection to clean up subscription state');\n\n // Use the dedicated forceReconnection method which performs a controlled\n // disconnect-then-connect sequence to clean up subscription state\n await this.#messenger.call('BackendWebSocketService:forceReconnection');\n }\n\n // =============================================================================\n // Public Methods - Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources\n * Optimized for fast cleanup during service destruction or mobile app termination\n */\n destroy(): void {\n // Clean up system notification callback\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n );\n }\n}\n"]}
|
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
* This service subscribes to account activity and receives all transactions
|
|
5
5
|
* and balance updates for those accounts via the comprehensive AccountActivityMessage format.
|
|
6
6
|
*/
|
|
7
|
-
import type {
|
|
7
|
+
import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
|
|
8
8
|
import type { TraceCallback } from "@metamask/controller-utils";
|
|
9
9
|
import type { Messenger } from "@metamask/messenger";
|
|
10
|
-
import type { RemoteFeatureFlagControllerGetStateAction, RemoteFeatureFlagControllerStateChangeEvent } from "@metamask/remote-feature-flag-controller";
|
|
11
10
|
import type { Transaction, BalanceUpdate } from "../types.cjs";
|
|
12
11
|
import type { AccountActivityServiceMethodActions } from "./AccountActivityService-method-action-types.cjs";
|
|
13
12
|
import type { BackendWebSocketServiceConnectionStateChangedEvent } from "./BackendWebSocketService.cjs";
|
|
@@ -28,10 +27,7 @@ declare const SERVICE_NAME = "AccountActivityService";
|
|
|
28
27
|
* Account subscription options
|
|
29
28
|
*/
|
|
30
29
|
export type SubscriptionOptions = {
|
|
31
|
-
|
|
32
|
-
* Array of addresses to subscribe to, each in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
33
|
-
*/
|
|
34
|
-
addresses: string[];
|
|
30
|
+
address: string;
|
|
35
31
|
};
|
|
36
32
|
/**
|
|
37
33
|
* Configuration options for the account activity service
|
|
@@ -43,9 +39,9 @@ export type AccountActivityServiceOptions = {
|
|
|
43
39
|
traceFn?: TraceCallback;
|
|
44
40
|
};
|
|
45
41
|
export type AccountActivityServiceActions = AccountActivityServiceMethodActions;
|
|
46
|
-
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS: readonly ["
|
|
47
|
-
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS: readonly ["
|
|
48
|
-
export type AllowedActions =
|
|
42
|
+
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS: readonly ["AccountsController:getSelectedAccount", "BackendWebSocketService:connect", "BackendWebSocketService:forceReconnection", "BackendWebSocketService:subscribe", "BackendWebSocketService:getConnectionInfo", "BackendWebSocketService:channelHasSubscription", "BackendWebSocketService:getSubscriptionsByChannel", "BackendWebSocketService:findSubscriptionsByChannelPrefix", "BackendWebSocketService:addChannelCallback", "BackendWebSocketService:removeChannelCallback"];
|
|
43
|
+
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS: readonly ["AccountsController:selectedAccountChange", "BackendWebSocketService:connectionStateChanged"];
|
|
44
|
+
export type AllowedActions = AccountsControllerGetSelectedAccountAction | BackendWebSocketServiceMethodActions;
|
|
49
45
|
export type AccountActivityServiceTransactionUpdatedEvent = {
|
|
50
46
|
type: `AccountActivityService:transactionUpdated`;
|
|
51
47
|
payload: [Transaction];
|
|
@@ -77,7 +73,7 @@ export type AccountActivityServiceStatusChangedEvent = {
|
|
|
77
73
|
];
|
|
78
74
|
};
|
|
79
75
|
export type AccountActivityServiceEvents = AccountActivityServiceTransactionUpdatedEvent | AccountActivityServiceBalanceUpdatedEvent | AccountActivityServiceSubscriptionErrorEvent | AccountActivityServiceStatusChangedEvent;
|
|
80
|
-
export type AllowedEvents =
|
|
76
|
+
export type AllowedEvents = AccountsControllerSelectedAccountChangeEvent | BackendWebSocketServiceConnectionStateChangedEvent;
|
|
81
77
|
export type AccountActivityServiceMessenger = Messenger<typeof SERVICE_NAME, AccountActivityServiceActions | AllowedActions, AccountActivityServiceEvents | AllowedEvents>;
|
|
82
78
|
/**
|
|
83
79
|
* High-performance service for real-time account activity monitoring using optimized
|
|
@@ -129,21 +125,18 @@ export declare class AccountActivityService {
|
|
|
129
125
|
});
|
|
130
126
|
/**
|
|
131
127
|
* Subscribe to account activity (transactions and balance updates)
|
|
132
|
-
*
|
|
128
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
133
129
|
*
|
|
134
|
-
* @param subscription -
|
|
135
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
136
|
-
* or an `addresses` array for batch subscription
|
|
130
|
+
* @param subscription - Account subscription configuration with address
|
|
137
131
|
*/
|
|
138
|
-
subscribe(
|
|
132
|
+
subscribe(subscription: SubscriptionOptions): Promise<void>;
|
|
139
133
|
/**
|
|
140
|
-
* Unsubscribe from account activity for specified
|
|
141
|
-
*
|
|
134
|
+
* Unsubscribe from account activity for specified address
|
|
135
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
142
136
|
*
|
|
143
|
-
* @param subscription -
|
|
144
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
137
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
145
138
|
*/
|
|
146
|
-
unsubscribe(
|
|
139
|
+
unsubscribe(subscription: SubscriptionOptions): Promise<void>;
|
|
147
140
|
/**
|
|
148
141
|
* Destroy the service and clean up all resources
|
|
149
142
|
* Optimized for fast cleanup during service destruction or mobile app termination
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService.d.cts","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,
|
|
1
|
+
{"version":3,"file":"AccountActivityService.d.cts","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,0CAA0C,EAC1C,4CAA4C,EAC7C,sCAAsC;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAEhE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EACV,WAAW,EAEX,aAAa,EACd,qBAAiB;AAClB,OAAO,KAAK,EAAE,mCAAmC,EAAE,yDAAqD;AACxG,OAAO,KAAK,EAEV,kDAAkD,EAEnD,sCAAkC;AAEnC,OAAO,KAAK,EAAE,oCAAoC,EAAE,0DAAsD;AAM1G;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,qEAAqE;IACrE,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,2CAA2C;IAC3C,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,QAAA,MAAM,YAAY,2BAA2B,CAAC;AAQ9C;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C,qEAAqE;IACrE,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,6FAA6F;IAC7F,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB,CAAC;AAOF,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CA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;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,sBAAsB;;IACjC;;OAEG;IACH,QAAQ,CAAC,IAAI,4BAAgB;IAe7B;;;;OAIG;gBAED,OAAO,EAAE,6BAA6B,GAAG;QACvC,SAAS,EAAE,+BAA+B,CAAC;KAC5C;IA6CH;;;;;OAKG;IACG,SAAS,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAiCjE;;;;;OAKG;IACG,WAAW,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAsRnE;;;OAGG;IACH,OAAO,IAAI,IAAI;CAOhB"}
|
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
* This service subscribes to account activity and receives all transactions
|
|
5
5
|
* and balance updates for those accounts via the comprehensive AccountActivityMessage format.
|
|
6
6
|
*/
|
|
7
|
-
import type {
|
|
7
|
+
import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
|
|
8
8
|
import type { TraceCallback } from "@metamask/controller-utils";
|
|
9
9
|
import type { Messenger } from "@metamask/messenger";
|
|
10
|
-
import type { RemoteFeatureFlagControllerGetStateAction, RemoteFeatureFlagControllerStateChangeEvent } from "@metamask/remote-feature-flag-controller";
|
|
11
10
|
import type { Transaction, BalanceUpdate } from "../types.mjs";
|
|
12
11
|
import type { AccountActivityServiceMethodActions } from "./AccountActivityService-method-action-types.mjs";
|
|
13
12
|
import type { BackendWebSocketServiceConnectionStateChangedEvent } from "./BackendWebSocketService.mjs";
|
|
@@ -28,10 +27,7 @@ declare const SERVICE_NAME = "AccountActivityService";
|
|
|
28
27
|
* Account subscription options
|
|
29
28
|
*/
|
|
30
29
|
export type SubscriptionOptions = {
|
|
31
|
-
|
|
32
|
-
* Array of addresses to subscribe to, each in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
33
|
-
*/
|
|
34
|
-
addresses: string[];
|
|
30
|
+
address: string;
|
|
35
31
|
};
|
|
36
32
|
/**
|
|
37
33
|
* Configuration options for the account activity service
|
|
@@ -43,9 +39,9 @@ export type AccountActivityServiceOptions = {
|
|
|
43
39
|
traceFn?: TraceCallback;
|
|
44
40
|
};
|
|
45
41
|
export type AccountActivityServiceActions = AccountActivityServiceMethodActions;
|
|
46
|
-
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS: readonly ["
|
|
47
|
-
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS: readonly ["
|
|
48
|
-
export type AllowedActions =
|
|
42
|
+
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS: readonly ["AccountsController:getSelectedAccount", "BackendWebSocketService:connect", "BackendWebSocketService:forceReconnection", "BackendWebSocketService:subscribe", "BackendWebSocketService:getConnectionInfo", "BackendWebSocketService:channelHasSubscription", "BackendWebSocketService:getSubscriptionsByChannel", "BackendWebSocketService:findSubscriptionsByChannelPrefix", "BackendWebSocketService:addChannelCallback", "BackendWebSocketService:removeChannelCallback"];
|
|
43
|
+
export declare const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS: readonly ["AccountsController:selectedAccountChange", "BackendWebSocketService:connectionStateChanged"];
|
|
44
|
+
export type AllowedActions = AccountsControllerGetSelectedAccountAction | BackendWebSocketServiceMethodActions;
|
|
49
45
|
export type AccountActivityServiceTransactionUpdatedEvent = {
|
|
50
46
|
type: `AccountActivityService:transactionUpdated`;
|
|
51
47
|
payload: [Transaction];
|
|
@@ -77,7 +73,7 @@ export type AccountActivityServiceStatusChangedEvent = {
|
|
|
77
73
|
];
|
|
78
74
|
};
|
|
79
75
|
export type AccountActivityServiceEvents = AccountActivityServiceTransactionUpdatedEvent | AccountActivityServiceBalanceUpdatedEvent | AccountActivityServiceSubscriptionErrorEvent | AccountActivityServiceStatusChangedEvent;
|
|
80
|
-
export type AllowedEvents =
|
|
76
|
+
export type AllowedEvents = AccountsControllerSelectedAccountChangeEvent | BackendWebSocketServiceConnectionStateChangedEvent;
|
|
81
77
|
export type AccountActivityServiceMessenger = Messenger<typeof SERVICE_NAME, AccountActivityServiceActions | AllowedActions, AccountActivityServiceEvents | AllowedEvents>;
|
|
82
78
|
/**
|
|
83
79
|
* High-performance service for real-time account activity monitoring using optimized
|
|
@@ -129,21 +125,18 @@ export declare class AccountActivityService {
|
|
|
129
125
|
});
|
|
130
126
|
/**
|
|
131
127
|
* Subscribe to account activity (transactions and balance updates)
|
|
132
|
-
*
|
|
128
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
133
129
|
*
|
|
134
|
-
* @param subscription -
|
|
135
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
136
|
-
* or an `addresses` array for batch subscription
|
|
130
|
+
* @param subscription - Account subscription configuration with address
|
|
137
131
|
*/
|
|
138
|
-
subscribe(
|
|
132
|
+
subscribe(subscription: SubscriptionOptions): Promise<void>;
|
|
139
133
|
/**
|
|
140
|
-
* Unsubscribe from account activity for specified
|
|
141
|
-
*
|
|
134
|
+
* Unsubscribe from account activity for specified address
|
|
135
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
142
136
|
*
|
|
143
|
-
* @param subscription -
|
|
144
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
137
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
145
138
|
*/
|
|
146
|
-
unsubscribe(
|
|
139
|
+
unsubscribe(subscription: SubscriptionOptions): Promise<void>;
|
|
147
140
|
/**
|
|
148
141
|
* Destroy the service and clean up all resources
|
|
149
142
|
* Optimized for fast cleanup during service destruction or mobile app termination
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService.d.mts","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK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S,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAiCjE;;;;;OAKG;IACG,WAAW,CAAC,YAAY,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAsRnE;;;OAGG;IACH,OAAO,IAAI,IAAI;CAOhB"}
|
|
@@ -15,27 +15,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
15
15
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
16
16
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
17
17
|
};
|
|
18
|
-
var _AccountActivityService_instances, _AccountActivityService_messenger, _AccountActivityService_options, _AccountActivityService_trace, _AccountActivityService_chainsUp, _AccountActivityService_handleAccountActivityUpdate, _AccountActivityService_handleSelectedAccountChange, _AccountActivityService_handleSystemNotification, _AccountActivityService_handleWebSocketStateChange, _AccountActivityService_subscribeToSelectedAccount, _AccountActivityService_unsubscribeFromAllAccountActivity,
|
|
19
|
-
import { isObject } from "@metamask/utils";
|
|
18
|
+
var _AccountActivityService_instances, _AccountActivityService_messenger, _AccountActivityService_options, _AccountActivityService_trace, _AccountActivityService_chainsUp, _AccountActivityService_handleAccountActivityUpdate, _AccountActivityService_handleSelectedAccountChange, _AccountActivityService_handleSystemNotification, _AccountActivityService_handleWebSocketStateChange, _AccountActivityService_subscribeToSelectedAccount, _AccountActivityService_unsubscribeFromAllAccountActivity, _AccountActivityService_convertToCaip10Address, _AccountActivityService_forceReconnection;
|
|
20
19
|
import { projectLogger, createModuleLogger } from "../logger.mjs";
|
|
21
20
|
import { WebSocketState } from "./BackendWebSocketService.mjs";
|
|
22
21
|
const SERVICE_NAME = 'AccountActivityService';
|
|
23
22
|
const log = createModuleLogger(projectLogger, SERVICE_NAME);
|
|
24
23
|
const MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'];
|
|
25
24
|
const SUBSCRIPTION_NAMESPACE = 'account-activity.v1';
|
|
26
|
-
// EVM subscriptions are always enabled.
|
|
27
|
-
const ALWAYS_SUPPORTED_CHAIN_PREFIXES = ['eip155'];
|
|
28
|
-
// Non-EVM chains are gated behind the
|
|
29
|
-
// per-network snaps-migration remote feature flags: a chain is
|
|
30
|
-
// enabled when its flag payload has `stage >= 1`.
|
|
31
|
-
const CHAIN_PREFIX_FEATURE_FLAGS = {
|
|
32
|
-
solana: 'networkAssetsSnapsMigrationSolana',
|
|
33
|
-
tron: 'networkAssetsSnapsMigrationTron',
|
|
34
|
-
stellar: 'networkAssetsSnapsMigrationStellar',
|
|
35
|
-
};
|
|
36
25
|
// Allowed actions that AccountActivityService can call on other controllers
|
|
37
26
|
export const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [
|
|
38
|
-
'
|
|
27
|
+
'AccountsController:getSelectedAccount',
|
|
39
28
|
'BackendWebSocketService:connect',
|
|
40
29
|
'BackendWebSocketService:forceReconnection',
|
|
41
30
|
'BackendWebSocketService:subscribe',
|
|
@@ -45,13 +34,11 @@ export const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [
|
|
|
45
34
|
'BackendWebSocketService:findSubscriptionsByChannelPrefix',
|
|
46
35
|
'BackendWebSocketService:addChannelCallback',
|
|
47
36
|
'BackendWebSocketService:removeChannelCallback',
|
|
48
|
-
'RemoteFeatureFlagController:getState',
|
|
49
37
|
];
|
|
50
38
|
// Allowed events that AccountActivityService can listen to
|
|
51
39
|
export const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [
|
|
52
|
-
'
|
|
40
|
+
'AccountsController:selectedAccountChange',
|
|
53
41
|
'BackendWebSocketService:connectionStateChanged',
|
|
54
|
-
'RemoteFeatureFlagController:stateChange',
|
|
55
42
|
];
|
|
56
43
|
// =============================================================================
|
|
57
44
|
// Main Service Class
|
|
@@ -120,24 +107,14 @@ export class AccountActivityService {
|
|
|
120
107
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
121
108
|
((_request, fn) => fn?.()), "f");
|
|
122
109
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
123
|
-
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('
|
|
110
|
+
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('AccountsController:selectedAccountChange',
|
|
124
111
|
// Promise result intentionally not awaited
|
|
125
112
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
126
|
-
async () => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSelectedAccountChange).call(this));
|
|
113
|
+
async (account) => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSelectedAccountChange).call(this, account));
|
|
127
114
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe('BackendWebSocketService:connectionStateChanged',
|
|
128
115
|
// Promise result intentionally not awaited
|
|
129
116
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
130
117
|
(connectionInfo) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleWebSocketStateChange).call(this, connectionInfo));
|
|
131
|
-
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").subscribe(
|
|
132
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
133
|
-
'RemoteFeatureFlagController:stateChange',
|
|
134
|
-
// Promise result intentionally not awaited
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
136
|
-
async () => await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleFeatureFlagsStateChange).call(this),
|
|
137
|
-
// Only react to changes in the set of enabled chain prefixes. The
|
|
138
|
-
// messenger compares selector results with strict equality, so the
|
|
139
|
-
// selector must return a primitive rather than a fresh object.
|
|
140
|
-
(state) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_getSupportedChainPrefixes).call(this, state.remoteFeatureFlags).join(','));
|
|
141
118
|
__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:addChannelCallback', {
|
|
142
119
|
channelName: `system-notifications.v1.${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}`,
|
|
143
120
|
callback: (notification) => __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleSystemNotification).call(this, notification),
|
|
@@ -148,26 +125,22 @@ export class AccountActivityService {
|
|
|
148
125
|
// =============================================================================
|
|
149
126
|
/**
|
|
150
127
|
* Subscribe to account activity (transactions and balance updates)
|
|
151
|
-
*
|
|
128
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
152
129
|
*
|
|
153
|
-
* @param subscription -
|
|
154
|
-
* @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format
|
|
155
|
-
* or an `addresses` array for batch subscription
|
|
130
|
+
* @param subscription - Account subscription configuration with address
|
|
156
131
|
*/
|
|
157
|
-
async subscribe(
|
|
132
|
+
async subscribe(subscription) {
|
|
158
133
|
try {
|
|
159
134
|
await __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:connect');
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.filter((channel) => !__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel));
|
|
165
|
-
if (channels.length === 0) {
|
|
135
|
+
// Create channel name from address
|
|
136
|
+
const channel = `${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}.${subscription.address}`;
|
|
137
|
+
// Check if already subscribed
|
|
138
|
+
if (__classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:channelHasSubscription', channel)) {
|
|
166
139
|
return;
|
|
167
140
|
}
|
|
168
141
|
// Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)
|
|
169
142
|
await __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:subscribe', {
|
|
170
|
-
channels,
|
|
143
|
+
channels: [channel],
|
|
171
144
|
channelType: __classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace, // e.g., 'account-activity.v1'
|
|
172
145
|
callback: (notification) => {
|
|
173
146
|
__classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_handleAccountActivityUpdate).call(this, notification.data);
|
|
@@ -180,21 +153,21 @@ export class AccountActivityService {
|
|
|
180
153
|
}
|
|
181
154
|
}
|
|
182
155
|
/**
|
|
183
|
-
* Unsubscribe from account activity for specified
|
|
184
|
-
*
|
|
156
|
+
* Unsubscribe from account activity for specified address
|
|
157
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
185
158
|
*
|
|
186
|
-
* @param subscription -
|
|
187
|
-
* @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format
|
|
159
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
188
160
|
*/
|
|
189
|
-
async unsubscribe(
|
|
161
|
+
async unsubscribe(subscription) {
|
|
162
|
+
const { address } = subscription;
|
|
190
163
|
try {
|
|
191
|
-
// Find
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (subscriptions.size === 0) {
|
|
164
|
+
// Find channel for the specified address
|
|
165
|
+
const channel = `${__classPrivateFieldGet(this, _AccountActivityService_options, "f").subscriptionNamespace}.${address}`;
|
|
166
|
+
const subscriptions = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:getSubscriptionsByChannel', channel);
|
|
167
|
+
if (subscriptions.length === 0) {
|
|
196
168
|
return;
|
|
197
169
|
}
|
|
170
|
+
// Fast path: Direct unsubscribe using stored unsubscribe function
|
|
198
171
|
// Unsubscribe from all matching subscriptions
|
|
199
172
|
for (const subscriptionInfo of subscriptions) {
|
|
200
173
|
await subscriptionInfo.unsubscribe();
|
|
@@ -254,16 +227,20 @@ _AccountActivityService_messenger = new WeakMap(), _AccountActivityService_optio
|
|
|
254
227
|
}, _AccountActivityService_handleSelectedAccountChange =
|
|
255
228
|
/**
|
|
256
229
|
* Handle selected account change event
|
|
230
|
+
*
|
|
231
|
+
* @param newAccount - The newly selected account
|
|
257
232
|
*/
|
|
258
|
-
async function _AccountActivityService_handleSelectedAccountChange() {
|
|
259
|
-
|
|
233
|
+
async function _AccountActivityService_handleSelectedAccountChange(newAccount) {
|
|
234
|
+
if (!newAccount?.address) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
260
237
|
try {
|
|
238
|
+
// Convert new account to CAIP-10 format
|
|
239
|
+
const newAddress = __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Address).call(this, newAccount);
|
|
261
240
|
// First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions
|
|
262
241
|
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_unsubscribeFromAllAccountActivity).call(this);
|
|
263
|
-
//
|
|
264
|
-
await this.subscribe({
|
|
265
|
-
addresses: __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Addresses).call(this, selectedAccounts),
|
|
266
|
-
});
|
|
242
|
+
// Then, subscribe to the new selected account
|
|
243
|
+
await this.subscribe({ address: newAddress });
|
|
267
244
|
}
|
|
268
245
|
catch (error) {
|
|
269
246
|
log('Account change failed', { error });
|
|
@@ -335,10 +312,13 @@ async function _AccountActivityService_handleWebSocketStateChange(connectionInfo
|
|
|
335
312
|
* Subscribe to the currently selected account only
|
|
336
313
|
*/
|
|
337
314
|
async function _AccountActivityService_subscribeToSelectedAccount() {
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
315
|
+
const selectedAccount = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('AccountsController:getSelectedAccount');
|
|
316
|
+
if (!selectedAccount?.address) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
// Convert to CAIP-10 format and subscribe
|
|
320
|
+
const address = __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_convertToCaip10Address).call(this, selectedAccount);
|
|
321
|
+
await this.subscribe({ address });
|
|
342
322
|
}, _AccountActivityService_unsubscribeFromAllAccountActivity =
|
|
343
323
|
/**
|
|
344
324
|
* Unsubscribe from all account activity subscriptions for this service
|
|
@@ -350,47 +330,19 @@ async function _AccountActivityService_unsubscribeFromAllAccountActivity() {
|
|
|
350
330
|
for (const subscription of accountActivitySubscriptions) {
|
|
351
331
|
await subscription.unsubscribe();
|
|
352
332
|
}
|
|
353
|
-
},
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
// Skip unsupported accounts
|
|
359
|
-
return result;
|
|
360
|
-
}
|
|
361
|
-
result.push(`${accountPrefix}:0:${account.address}`);
|
|
362
|
-
return result;
|
|
363
|
-
}, []);
|
|
364
|
-
}, _AccountActivityService_getSupportedChainPrefixes = function _AccountActivityService_getSupportedChainPrefixes(remoteFeatureFlags = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('RemoteFeatureFlagController:getState').remoteFeatureFlags) {
|
|
365
|
-
const prefixes = [...ALWAYS_SUPPORTED_CHAIN_PREFIXES];
|
|
366
|
-
for (const [prefix, flagName] of Object.entries(CHAIN_PREFIX_FEATURE_FLAGS)) {
|
|
367
|
-
const flagValue = remoteFeatureFlags[flagName];
|
|
368
|
-
if (isObject(flagValue) &&
|
|
369
|
-
typeof flagValue.stage === 'number' &&
|
|
370
|
-
flagValue.stage >= 1) {
|
|
371
|
-
prefixes.push(prefix);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
return prefixes;
|
|
375
|
-
}, _AccountActivityService_handleFeatureFlagsStateChange =
|
|
376
|
-
/**
|
|
377
|
-
* Handle remote feature flag changes: if the set of enabled chain prefixes
|
|
378
|
-
* changed while connected, resubscribe the selected account so new chains
|
|
379
|
-
* are picked up and disabled ones are dropped.
|
|
380
|
-
*/
|
|
381
|
-
async function _AccountActivityService_handleFeatureFlagsStateChange() {
|
|
382
|
-
try {
|
|
383
|
-
const { state } = __classPrivateFieldGet(this, _AccountActivityService_messenger, "f").call('BackendWebSocketService:getConnectionInfo');
|
|
384
|
-
if (state !== WebSocketState.CONNECTED) {
|
|
385
|
-
// Not connected: the next connection will subscribe with fresh flags
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_unsubscribeFromAllAccountActivity).call(this);
|
|
389
|
-
await __classPrivateFieldGet(this, _AccountActivityService_instances, "m", _AccountActivityService_subscribeToSelectedAccount).call(this);
|
|
333
|
+
}, _AccountActivityService_convertToCaip10Address = function _AccountActivityService_convertToCaip10Address(account) {
|
|
334
|
+
// Check if account has EVM scopes
|
|
335
|
+
if (account.scopes.some((scope) => scope.startsWith('eip155:'))) {
|
|
336
|
+
// CAIP-10 format: eip155:0:address (subscribe to all EVM chains)
|
|
337
|
+
return `eip155:0:${account.address}`;
|
|
390
338
|
}
|
|
391
|
-
|
|
392
|
-
|
|
339
|
+
// Check if account has Solana scopes
|
|
340
|
+
if (account.scopes.some((scope) => scope.startsWith('solana:'))) {
|
|
341
|
+
// CAIP-10 format: solana:0:address (subscribe to all Solana chains)
|
|
342
|
+
return `solana:0:${account.address}`;
|
|
393
343
|
}
|
|
344
|
+
// For other chains or unknown scopes, return raw address
|
|
345
|
+
return account.address;
|
|
394
346
|
}, _AccountActivityService_forceReconnection =
|
|
395
347
|
/**
|
|
396
348
|
* Force WebSocket reconnection to clean up subscription state
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountActivityService.mjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;;;;;;;;;;;;;AAcH,OAAO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AAClB,CAAC;AAED;;;;GAIG;AACH,KAAK;IACH,IAAI,CAAC;QACH,MAAM,EAAE,KAAK,EAAE,GAAG,uBAAA,IAAI,yCAAW,CAAC,IAAI,CACpC,2CAA2C,CAC5C,CAAC;QACF,IAAI,KAAK,KAAK,cAAc,CAAC,SAAS,EAAE,CAAC;YACvC,qEAAqE;YACrE,OAAO;QACT,CAAC;QAED,MAAM,uBAAA,IAAI,oGAAmC,MAAvC,IAAI,CAAqC,CAAC;QAChD,MAAM,uBAAA,IAAI,6FAA4B,MAAhC,IAAI,CAA8B,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,qCAAqC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACxD,CAAC;AACH,CAAC;AAED;;GAEG;AACH,KAAK;IACH,GAAG,CAAC,+DAA+D,CAAC,CAAC;IAErE,yEAAyE;IACzE,kEAAkE;IAClE,MAAM,uBAAA,IAAI,yCAAW,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["/**\n * Account Activity Service for monitoring account transactions and balance changes\n *\n * This service subscribes to account activity and receives all transactions\n * and balance updates for those accounts via the comprehensive AccountActivityMessage format.\n */\n\nimport type {\n AccountTreeControllerSelectedAccountGroupChangeEvent,\n AccountTreeControllerGetAccountsFromSelectedAccountGroupAction,\n} from '@metamask/account-tree-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n FeatureFlags,\n RemoteFeatureFlagControllerGetStateAction,\n RemoteFeatureFlagControllerStateChangeEvent,\n} from '@metamask/remote-feature-flag-controller';\nimport { isObject } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n Transaction,\n AccountActivityMessage,\n BalanceUpdate,\n} from '../types';\nimport type { AccountActivityServiceMethodActions } from './AccountActivityService-method-action-types';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from './BackendWebSocketService';\nimport {\n WebSocketState,\n WebSocketSubscription,\n} from './BackendWebSocketService';\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\n\n// =============================================================================\n// Types and Constants\n// =============================================================================\n\n/**\n * System notification data for chain status updates\n */\nexport type SystemNotificationData = {\n /** Array of chain IDs affected (e.g., ['eip155:137', 'eip155:1']) */\n chainIds: string[];\n /** Status of the chains: 'down' or 'up' */\n status: 'down' | 'up';\n /** Timestamp of the notification */\n timestamp?: number;\n};\n\nconst SERVICE_NAME = 'AccountActivityService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'account-activity.v1';\n\n// EVM subscriptions are always enabled.\nconst ALWAYS_SUPPORTED_CHAIN_PREFIXES = ['eip155'] as const;\n\n// Non-EVM chains are gated behind the\n// per-network snaps-migration remote feature flags: a chain is\n// enabled when its flag payload has `stage >= 1`.\nconst CHAIN_PREFIX_FEATURE_FLAGS = {\n solana: 'networkAssetsSnapsMigrationSolana',\n tron: 'networkAssetsSnapsMigrationTron',\n stellar: 'networkAssetsSnapsMigrationStellar',\n} as const;\n\n/**\n * Account subscription options\n */\nexport type SubscriptionOptions = {\n /**\n * Array of addresses to subscribe to, each in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n */\n addresses: string[];\n};\n\n/**\n * Configuration options for the account activity service\n */\nexport type AccountActivityServiceOptions = {\n /** Custom subscription namespace (default: 'account-activity.v1') */\n subscriptionNamespace?: string;\n /** Optional callback to trace performance of account activity operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\n// Action types for the messaging system - using generated method actions\nexport type AccountActivityServiceActions = AccountActivityServiceMethodActions;\n\n// Allowed actions that AccountActivityService can call on other controllers\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n 'RemoteFeatureFlagController:getState',\n] as const;\n\n// Allowed events that AccountActivityService can listen to\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [\n 'AccountTreeController:selectedAccountGroupChange',\n 'BackendWebSocketService:connectionStateChanged',\n 'RemoteFeatureFlagController:stateChange',\n] as const;\n\nexport type AllowedActions =\n | AccountTreeControllerGetAccountsFromSelectedAccountGroupAction\n | BackendWebSocketServiceMethodActions\n | RemoteFeatureFlagControllerGetStateAction;\n\n// Event types for the messaging system\n\nexport type AccountActivityServiceTransactionUpdatedEvent = {\n type: `AccountActivityService:transactionUpdated`;\n payload: [Transaction];\n};\n\nexport type AccountActivityServiceBalanceUpdatedEvent = {\n type: `AccountActivityService:balanceUpdated`;\n payload: [{ address: string; chain: string; updates: BalanceUpdate[] }];\n};\n\nexport type AccountActivityServiceSubscriptionErrorEvent = {\n type: `AccountActivityService:subscriptionError`;\n payload: [{ addresses: string[]; error: string; operation: string }];\n};\n\nexport type AccountActivityServiceStatusChangedEvent = {\n type: `AccountActivityService:statusChanged`;\n payload: [\n {\n chainIds: string[];\n status: 'up' | 'down';\n timestamp?: number;\n },\n ];\n};\n\nexport type AccountActivityServiceEvents =\n | AccountActivityServiceTransactionUpdatedEvent\n | AccountActivityServiceBalanceUpdatedEvent\n | AccountActivityServiceSubscriptionErrorEvent\n | AccountActivityServiceStatusChangedEvent;\n\nexport type AllowedEvents =\n | AccountTreeControllerSelectedAccountGroupChangeEvent\n | BackendWebSocketServiceConnectionStateChangedEvent\n | RemoteFeatureFlagControllerStateChangeEvent;\n\nexport type AccountActivityServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n AccountActivityServiceActions | AllowedActions,\n AccountActivityServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * High-performance service for real-time account activity monitoring using optimized\n * WebSocket subscriptions with direct callback routing. Automatically subscribes to\n * the currently selected account and switches subscriptions when the selected account changes.\n * Receives transactions and balance updates using the comprehensive AccountActivityMessage format.\n *\n * Performance Features:\n * - Direct callback routing (no EventEmitter overhead)\n * - Minimal subscription tracking (no duplication with BackendWebSocketService)\n * - Optimized cleanup for mobile environments\n * - Single-account subscription (only selected account)\n * - Comprehensive balance updates with transfer tracking\n *\n * Architecture:\n * - Uses messenger pattern to communicate with BackendWebSocketService\n * - AccountActivityService tracks channel-to-subscriptionId mappings via messenger calls\n * - Automatically subscribes to selected account on initialization\n * - Switches subscriptions when selected account changes\n * - No direct dependency on BackendWebSocketService (uses messenger instead)\n *\n * @example\n * ```typescript\n * const service = new AccountActivityService({\n * messenger: activityMessenger,\n * });\n *\n * // Service automatically subscribes to the currently selected account\n * // When user switches accounts, service automatically resubscribes\n *\n * // All transactions and balance updates are received via optimized\n * // WebSocket callbacks and processed with zero-allocation routing\n * // Balance updates include comprehensive transfer details and post-transaction balances\n * ```\n */\nexport class AccountActivityService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: AccountActivityServiceMessenger;\n\n readonly #options: Required<Omit<AccountActivityServiceOptions, 'traceFn'>>;\n\n readonly #trace: TraceCallback;\n\n // Track chains that are currently up (based on system notifications)\n readonly #chainsUp: Set<string> = new Set();\n\n // =============================================================================\n // Constructor and Initialization\n // =============================================================================\n\n /**\n * Creates a new Account Activity service instance\n *\n * @param options - Configuration options including messenger\n */\n constructor(\n options: AccountActivityServiceOptions & {\n messenger: AccountActivityServiceMessenger;\n },\n ) {\n this.#messenger = options.messenger;\n\n // Set configuration with defaults\n this.#options = {\n subscriptionNamespace:\n options.subscriptionNamespace ?? SUBSCRIPTION_NAMESPACE,\n };\n\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n this.#messenger.subscribe(\n 'AccountTreeController:selectedAccountGroupChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => await this.#handleSelectedAccountChange(),\n );\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n this.#messenger.subscribe(\n // eslint-disable-next-line no-restricted-syntax\n 'RemoteFeatureFlagController:stateChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => await this.#handleFeatureFlagsStateChange(),\n // Only react to changes in the set of enabled chain prefixes. The\n // messenger compares selector results with strict equality, so the\n // selector must return a primitive rather than a fresh object.\n (state) =>\n this.#getSupportedChainPrefixes(state.remoteFeatureFlags).join(','),\n );\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Account Subscription Methods\n // =============================================================================\n\n /**\n * Subscribe to account activity (transactions and balance updates)\n * Addresses should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - The subscription configuration\n * @param subscription.addresses - Array of addresses to subscribe to, each in CAIP-10 format\n * or an `addresses` array for batch subscription\n */\n async subscribe({ addresses }: SubscriptionOptions): Promise<void> {\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n // Derive new subscriptions to be created from the provided addresses,\n // filtering out any channels that already have an active subscription\n const channels = addresses\n .map((address) => `${this.#options.subscriptionNamespace}.${address}`)\n .filter(\n (channel) =>\n !this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n ),\n );\n\n if (channels.length === 0) {\n return;\n }\n\n // Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels,\n channelType: this.#options.subscriptionNamespace, // e.g., 'account-activity.v1'\n callback: (notification: ServerNotificationMessage) => {\n this.#handleAccountActivityUpdate(\n notification.data as AccountActivityMessage,\n );\n },\n });\n } catch (error) {\n log('Subscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n /**\n * Unsubscribe from account activity for specified addresses\n * Addresses should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - The subscription configuration\n * @param subscription.addresses - Array of addresses to unsubscribe from, each in CAIP-10 format\n */\n async unsubscribe({ addresses }: SubscriptionOptions): Promise<void> {\n try {\n // Find all subscriptions for the specified addresses\n const subscriptions = new Set<WebSocketSubscription>(\n addresses\n .map((address) =>\n this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n `${this.#options.subscriptionNamespace}.${address}`,\n ),\n )\n .flat(),\n );\n\n if (subscriptions.size === 0) {\n return;\n }\n\n // Unsubscribe from all matching subscriptions\n for (const subscriptionInfo of subscriptions) {\n await subscriptionInfo.unsubscribe();\n }\n } catch (error) {\n log('Unsubscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n // =============================================================================\n // Private Methods - Event Handlers\n // =============================================================================\n\n /**\n * Handle account activity updates (transactions + balance changes)\n * Processes the comprehensive AccountActivityMessage format with detailed balance updates and transfers\n *\n * @param payload - The account activity message containing transaction and balance updates\n * @example AccountActivityMessage format handling:\n * Input: {\n * address: \"0xd14b52362b5b777ffa754c666ddec6722aaeee08\",\n * tx: { id: \"0x1cde...\", chain: \"eip155:8453\", status: \"confirmed\", timestamp: 1760099871, ... },\n * updates: [{\n * asset: { fungible: true, type: \"eip155:8453/erc20:0x833...\", unit: \"USDC\", decimals: 6 },\n * postBalance: { amount: \"0xc350\" },\n * transfers: [{ from: \"0x7b07...\", to: \"0xd14b...\", amount: \"0x2710\" }]\n * }]\n * }\n * Output: Transaction and balance updates published separately\n */\n #handleAccountActivityUpdate(payload: AccountActivityMessage): void {\n const { address, tx, updates } = payload;\n\n // Calculate time elapsed between transaction time and message receipt\n const txTimestampMs = tx.timestamp * 1000; // Convert Unix timestamp (seconds) to milliseconds\n const elapsedMs = Date.now() - txTimestampMs;\n\n log('Handling account activity update', {\n address,\n updateCount: updates.length,\n elapsedMs,\n });\n\n // Trace message receipt with latency from transaction time to now\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Transaction Message`,\n data: {\n chain: tx.chain,\n status: tx.status,\n elapsed_ms: elapsedMs,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: this.#options.subscriptionNamespace,\n },\n },\n () => {\n // Process transaction update\n this.#messenger.publish(\n `AccountActivityService:transactionUpdated`,\n tx,\n );\n\n // Publish comprehensive balance updates with transfer details\n this.#messenger.publish(`AccountActivityService:balanceUpdated`, {\n address,\n chain: tx.chain,\n updates,\n });\n },\n );\n }\n\n /**\n * Handle selected account change event\n */\n async #handleSelectedAccountChange(): Promise<void> {\n const selectedAccounts = this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n try {\n // First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions\n await this.#unsubscribeFromAllAccountActivity();\n\n // Subscribe to the new selected accounts in CAIP-10 format\n await this.subscribe({\n addresses: this.#convertToCaip10Addresses(selectedAccounts),\n });\n } catch (error) {\n log('Account change failed', { error });\n }\n }\n\n /**\n * Handle system notification for chain status changes\n * Publishes only the status change (delta) for affected chains\n *\n * @param notification - Server notification message containing chain status updates and timestamp\n */\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as SystemNotificationData;\n const { timestamp } = notification;\n\n // Validate required fields\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n // Track chain status\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n // Publish status change directly (delta update)\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(\n `WebSocket status change - Published tracked chains as ${data.status}`,\n {\n count: data.chainIds.length,\n chains: data.chainIds,\n status: data.status,\n },\n );\n }\n\n /**\n * Handle WebSocket connection state changes for fallback polling and resubscription\n *\n * @param connectionInfo - WebSocket connection state information\n */\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n // WebSocket connected - resubscribe to selected account\n // The system notification will automatically provide the list of chains that are up\n await this.#subscribeToSelectedAccount();\n } else if (state === WebSocketState.DISCONNECTED) {\n // On disconnect, flush all tracked chains as down\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log('WebSocket disconnection - Published tracked chains as down', {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n });\n\n // Clear the tracking set since all chains are now down\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private Methods - Subscription Management\n // =============================================================================\n\n /**\n * Subscribe to the currently selected account only\n */\n async #subscribeToSelectedAccount(): Promise<void> {\n const selectedAccounts = this.#messenger.call(\n 'AccountTreeController:getAccountsFromSelectedAccountGroup',\n );\n\n await this.subscribe({\n addresses: this.#convertToCaip10Addresses(selectedAccounts),\n });\n }\n\n /**\n * Unsubscribe from all account activity subscriptions for this service\n * Finds all channels matching the service's namespace and unsubscribes from them\n */\n async #unsubscribeFromAllAccountActivity(): Promise<void> {\n const accountActivitySubscriptions = this.#messenger.call(\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n this.#options.subscriptionNamespace,\n );\n\n // Unsubscribe from all matching subscriptions\n for (const subscription of accountActivitySubscriptions) {\n await subscription.unsubscribe();\n }\n }\n\n // =============================================================================\n // Private Methods - Utility Functions\n // =============================================================================\n\n /**\n * Convert a list of InternalAccount addresses to CAIP-10 format, using the first\n * supported chain prefix matching the account's scopes\n *\n * @param accounts - The internal accounts to convert\n * @returns The CAIP-10 formatted addresses (e.g. [`eip155:0:address`], meaning\n * all chains of that namespace), or an empty array if none of the account's\n * scopes are supported\n */\n #convertToCaip10Addresses(accounts: InternalAccount[]): string[] {\n const supportedChainPrefixes = this.#getSupportedChainPrefixes();\n return accounts.reduce<string[]>((result, account) => {\n const accountPrefix = supportedChainPrefixes.find((prefix) =>\n account.scopes.some((scope) => scope.startsWith(`${prefix}:`)),\n );\n\n if (!accountPrefix) {\n // Skip unsupported accounts\n return result;\n }\n\n result.push(`${accountPrefix}:0:${account.address}`);\n return result;\n }, []);\n }\n\n /**\n * Get the chain prefixes currently enabled for subscriptions: EVM is always\n * enabled, while other chains are gated behind their per-network remote\n * feature flag (enabled when the flag payload has `stage >= 1`).\n *\n * @param remoteFeatureFlags - The remote feature flags state to check for enabled chains.\n * @returns An array of enabled CAIP-2 namespace prefixes (e.g. `['eip155', 'solana']`)\n */\n #getSupportedChainPrefixes(\n remoteFeatureFlags: FeatureFlags = this.#messenger.call(\n 'RemoteFeatureFlagController:getState',\n ).remoteFeatureFlags,\n ): string[] {\n const prefixes: string[] = [...ALWAYS_SUPPORTED_CHAIN_PREFIXES];\n for (const [prefix, flagName] of Object.entries(\n CHAIN_PREFIX_FEATURE_FLAGS,\n )) {\n const flagValue = remoteFeatureFlags[flagName];\n if (\n isObject(flagValue) &&\n typeof flagValue.stage === 'number' &&\n flagValue.stage >= 1\n ) {\n prefixes.push(prefix);\n }\n }\n return prefixes;\n }\n\n /**\n * Handle remote feature flag changes: if the set of enabled chain prefixes\n * changed while connected, resubscribe the selected account so new chains\n * are picked up and disabled ones are dropped.\n */\n async #handleFeatureFlagsStateChange(): Promise<void> {\n try {\n const { state } = this.#messenger.call(\n 'BackendWebSocketService:getConnectionInfo',\n );\n if (state !== WebSocketState.CONNECTED) {\n // Not connected: the next connection will subscribe with fresh flags\n return;\n }\n\n await this.#unsubscribeFromAllAccountActivity();\n await this.#subscribeToSelectedAccount();\n } catch (error) {\n log('Feature flag change handling failed', { error });\n }\n }\n\n /**\n * Force WebSocket reconnection to clean up subscription state\n */\n async #forceReconnection(): Promise<void> {\n log('Forcing WebSocket reconnection to clean up subscription state');\n\n // Use the dedicated forceReconnection method which performs a controlled\n // disconnect-then-connect sequence to clean up subscription state\n await this.#messenger.call('BackendWebSocketService:forceReconnection');\n }\n\n // =============================================================================\n // Public Methods - Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources\n * Optimized for fast cleanup during service destruction or mobile app termination\n */\n destroy(): void {\n // Clean up system notification callback\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountActivityService.mjs","sourceRoot":"","sources":["../../src/ws/AccountActivityService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;;;;;;;;;;;;;AAUH,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,sBAAkB;AAY9D,OAAO,EAAE,cAAc,EAAE,sCAAkC;AAm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AW,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["/**\n * Account Activity Service for monitoring account transactions and balance changes\n *\n * This service subscribes to account activity and receives all transactions\n * and balance updates for those accounts via the comprehensive AccountActivityMessage format.\n */\n\nimport type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { projectLogger, createModuleLogger } from '../logger';\nimport type {\n Transaction,\n AccountActivityMessage,\n BalanceUpdate,\n} from '../types';\nimport type { AccountActivityServiceMethodActions } from './AccountActivityService-method-action-types';\nimport type {\n WebSocketConnectionInfo,\n BackendWebSocketServiceConnectionStateChangedEvent,\n ServerNotificationMessage,\n} from './BackendWebSocketService';\nimport { WebSocketState } from './BackendWebSocketService';\nimport type { BackendWebSocketServiceMethodActions } from './BackendWebSocketService-method-action-types';\n\n// =============================================================================\n// Types and Constants\n// =============================================================================\n\n/**\n * System notification data for chain status updates\n */\nexport type SystemNotificationData = {\n /** Array of chain IDs affected (e.g., ['eip155:137', 'eip155:1']) */\n chainIds: string[];\n /** Status of the chains: 'down' or 'up' */\n status: 'down' | 'up';\n /** Timestamp of the notification */\n timestamp?: number;\n};\n\nconst SERVICE_NAME = 'AccountActivityService';\n\nconst log = createModuleLogger(projectLogger, SERVICE_NAME);\n\nconst MESSENGER_EXPOSED_METHODS = ['subscribe', 'unsubscribe'] as const;\n\nconst SUBSCRIPTION_NAMESPACE = 'account-activity.v1';\n\n/**\n * Account subscription options\n */\nexport type SubscriptionOptions = {\n address: string; // Should be in CAIP-10 format, e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\"\n};\n\n/**\n * Configuration options for the account activity service\n */\nexport type AccountActivityServiceOptions = {\n /** Custom subscription namespace (default: 'account-activity.v1') */\n subscriptionNamespace?: string;\n /** Optional callback to trace performance of account activity operations (default: no-op) */\n traceFn?: TraceCallback;\n};\n\n// =============================================================================\n// Action and Event Types\n// =============================================================================\n\n// Action types for the messaging system - using generated method actions\nexport type AccountActivityServiceActions = AccountActivityServiceMethodActions;\n\n// Allowed actions that AccountActivityService can call on other controllers\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_ACTIONS = [\n 'AccountsController:getSelectedAccount',\n 'BackendWebSocketService:connect',\n 'BackendWebSocketService:forceReconnection',\n 'BackendWebSocketService:subscribe',\n 'BackendWebSocketService:getConnectionInfo',\n 'BackendWebSocketService:channelHasSubscription',\n 'BackendWebSocketService:getSubscriptionsByChannel',\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n 'BackendWebSocketService:addChannelCallback',\n 'BackendWebSocketService:removeChannelCallback',\n] as const;\n\n// Allowed events that AccountActivityService can listen to\nexport const ACCOUNT_ACTIVITY_SERVICE_ALLOWED_EVENTS = [\n 'AccountsController:selectedAccountChange',\n 'BackendWebSocketService:connectionStateChanged',\n] as const;\n\nexport type AllowedActions =\n | AccountsControllerGetSelectedAccountAction\n | BackendWebSocketServiceMethodActions;\n\n// Event types for the messaging system\n\nexport type AccountActivityServiceTransactionUpdatedEvent = {\n type: `AccountActivityService:transactionUpdated`;\n payload: [Transaction];\n};\n\nexport type AccountActivityServiceBalanceUpdatedEvent = {\n type: `AccountActivityService:balanceUpdated`;\n payload: [{ address: string; chain: string; updates: BalanceUpdate[] }];\n};\n\nexport type AccountActivityServiceSubscriptionErrorEvent = {\n type: `AccountActivityService:subscriptionError`;\n payload: [{ addresses: string[]; error: string; operation: string }];\n};\n\nexport type AccountActivityServiceStatusChangedEvent = {\n type: `AccountActivityService:statusChanged`;\n payload: [\n {\n chainIds: string[];\n status: 'up' | 'down';\n timestamp?: number;\n },\n ];\n};\n\nexport type AccountActivityServiceEvents =\n | AccountActivityServiceTransactionUpdatedEvent\n | AccountActivityServiceBalanceUpdatedEvent\n | AccountActivityServiceSubscriptionErrorEvent\n | AccountActivityServiceStatusChangedEvent;\n\nexport type AllowedEvents =\n | AccountsControllerSelectedAccountChangeEvent\n | BackendWebSocketServiceConnectionStateChangedEvent;\n\nexport type AccountActivityServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n AccountActivityServiceActions | AllowedActions,\n AccountActivityServiceEvents | AllowedEvents\n>;\n\n// =============================================================================\n// Main Service Class\n// =============================================================================\n\n/**\n * High-performance service for real-time account activity monitoring using optimized\n * WebSocket subscriptions with direct callback routing. Automatically subscribes to\n * the currently selected account and switches subscriptions when the selected account changes.\n * Receives transactions and balance updates using the comprehensive AccountActivityMessage format.\n *\n * Performance Features:\n * - Direct callback routing (no EventEmitter overhead)\n * - Minimal subscription tracking (no duplication with BackendWebSocketService)\n * - Optimized cleanup for mobile environments\n * - Single-account subscription (only selected account)\n * - Comprehensive balance updates with transfer tracking\n *\n * Architecture:\n * - Uses messenger pattern to communicate with BackendWebSocketService\n * - AccountActivityService tracks channel-to-subscriptionId mappings via messenger calls\n * - Automatically subscribes to selected account on initialization\n * - Switches subscriptions when selected account changes\n * - No direct dependency on BackendWebSocketService (uses messenger instead)\n *\n * @example\n * ```typescript\n * const service = new AccountActivityService({\n * messenger: activityMessenger,\n * });\n *\n * // Service automatically subscribes to the currently selected account\n * // When user switches accounts, service automatically resubscribes\n *\n * // All transactions and balance updates are received via optimized\n * // WebSocket callbacks and processed with zero-allocation routing\n * // Balance updates include comprehensive transfer details and post-transaction balances\n * ```\n */\nexport class AccountActivityService {\n /**\n * The name of the service.\n */\n readonly name = SERVICE_NAME;\n\n readonly #messenger: AccountActivityServiceMessenger;\n\n readonly #options: Required<Omit<AccountActivityServiceOptions, 'traceFn'>>;\n\n readonly #trace: TraceCallback;\n\n // Track chains that are currently up (based on system notifications)\n readonly #chainsUp: Set<string> = new Set();\n\n // =============================================================================\n // Constructor and Initialization\n // =============================================================================\n\n /**\n * Creates a new Account Activity service instance\n *\n * @param options - Configuration options including messenger\n */\n constructor(\n options: AccountActivityServiceOptions & {\n messenger: AccountActivityServiceMessenger;\n },\n ) {\n this.#messenger = options.messenger;\n\n // Set configuration with defaults\n this.#options = {\n subscriptionNamespace:\n options.subscriptionNamespace ?? SUBSCRIPTION_NAMESPACE,\n };\n\n // Default to no-op trace function to keep core platform-agnostic\n this.#trace =\n options.traceFn ??\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (((_request: any, fn?: any) => fn?.()) as TraceCallback);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n this.#messenger.subscribe(\n 'AccountsController:selectedAccountChange',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account: InternalAccount) =>\n await this.#handleSelectedAccountChange(account),\n );\n this.#messenger.subscribe(\n 'BackendWebSocketService:connectionStateChanged',\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n (connectionInfo: WebSocketConnectionInfo) =>\n this.#handleWebSocketStateChange(connectionInfo),\n );\n this.#messenger.call('BackendWebSocketService:addChannelCallback', {\n channelName: `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n callback: (notification: ServerNotificationMessage) =>\n this.#handleSystemNotification(notification),\n });\n }\n\n // =============================================================================\n // Account Subscription Methods\n // =============================================================================\n\n /**\n * Subscribe to account activity (transactions and balance updates)\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address\n */\n async subscribe(subscription: SubscriptionOptions): Promise<void> {\n try {\n await this.#messenger.call('BackendWebSocketService:connect');\n\n // Create channel name from address\n const channel = `${this.#options.subscriptionNamespace}.${subscription.address}`;\n\n // Check if already subscribed\n if (\n this.#messenger.call(\n 'BackendWebSocketService:channelHasSubscription',\n channel,\n )\n ) {\n return;\n }\n\n // Create subscription using the proper subscribe method (this will be stored in WebSocketService's internal tracking)\n await this.#messenger.call('BackendWebSocketService:subscribe', {\n channels: [channel],\n channelType: this.#options.subscriptionNamespace, // e.g., 'account-activity.v1'\n callback: (notification: ServerNotificationMessage) => {\n this.#handleAccountActivityUpdate(\n notification.data as AccountActivityMessage,\n );\n },\n });\n } catch (error) {\n log('Subscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n /**\n * Unsubscribe from account activity for specified address\n * Address should be in CAIP-10 format (e.g., \"eip155:0:0x1234...\" or \"solana:0:ABC123...\")\n *\n * @param subscription - Account subscription configuration with address to unsubscribe\n */\n async unsubscribe(subscription: SubscriptionOptions): Promise<void> {\n const { address } = subscription;\n try {\n // Find channel for the specified address\n const channel = `${this.#options.subscriptionNamespace}.${address}`;\n const subscriptions = this.#messenger.call(\n 'BackendWebSocketService:getSubscriptionsByChannel',\n channel,\n );\n\n if (subscriptions.length === 0) {\n return;\n }\n\n // Fast path: Direct unsubscribe using stored unsubscribe function\n // Unsubscribe from all matching subscriptions\n for (const subscriptionInfo of subscriptions) {\n await subscriptionInfo.unsubscribe();\n }\n } catch (error) {\n log('Unsubscription failed, forcing reconnection', { error });\n await this.#forceReconnection();\n }\n }\n\n // =============================================================================\n // Private Methods - Event Handlers\n // =============================================================================\n\n /**\n * Handle account activity updates (transactions + balance changes)\n * Processes the comprehensive AccountActivityMessage format with detailed balance updates and transfers\n *\n * @param payload - The account activity message containing transaction and balance updates\n * @example AccountActivityMessage format handling:\n * Input: {\n * address: \"0xd14b52362b5b777ffa754c666ddec6722aaeee08\",\n * tx: { id: \"0x1cde...\", chain: \"eip155:8453\", status: \"confirmed\", timestamp: 1760099871, ... },\n * updates: [{\n * asset: { fungible: true, type: \"eip155:8453/erc20:0x833...\", unit: \"USDC\", decimals: 6 },\n * postBalance: { amount: \"0xc350\" },\n * transfers: [{ from: \"0x7b07...\", to: \"0xd14b...\", amount: \"0x2710\" }]\n * }]\n * }\n * Output: Transaction and balance updates published separately\n */\n #handleAccountActivityUpdate(payload: AccountActivityMessage): void {\n const { address, tx, updates } = payload;\n\n // Calculate time elapsed between transaction time and message receipt\n const txTimestampMs = tx.timestamp * 1000; // Convert Unix timestamp (seconds) to milliseconds\n const elapsedMs = Date.now() - txTimestampMs;\n\n log('Handling account activity update', {\n address,\n updateCount: updates.length,\n elapsedMs,\n });\n\n // Trace message receipt with latency from transaction time to now\n // Promise result intentionally not awaited\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#trace(\n {\n name: `${SERVICE_NAME} Transaction Message`,\n data: {\n chain: tx.chain,\n status: tx.status,\n elapsed_ms: elapsedMs,\n },\n tags: {\n service: SERVICE_NAME,\n notification_type: this.#options.subscriptionNamespace,\n },\n },\n () => {\n // Process transaction update\n this.#messenger.publish(\n `AccountActivityService:transactionUpdated`,\n tx,\n );\n\n // Publish comprehensive balance updates with transfer details\n this.#messenger.publish(`AccountActivityService:balanceUpdated`, {\n address,\n chain: tx.chain,\n updates,\n });\n },\n );\n }\n\n /**\n * Handle selected account change event\n *\n * @param newAccount - The newly selected account\n */\n async #handleSelectedAccountChange(\n newAccount: InternalAccount | null,\n ): Promise<void> {\n if (!newAccount?.address) {\n return;\n }\n\n try {\n // Convert new account to CAIP-10 format\n const newAddress = this.#convertToCaip10Address(newAccount);\n\n // First, unsubscribe from all current account activity subscriptions to avoid multiple subscriptions\n await this.#unsubscribeFromAllAccountActivity();\n\n // Then, subscribe to the new selected account\n await this.subscribe({ address: newAddress });\n } catch (error) {\n log('Account change failed', { error });\n }\n }\n\n /**\n * Handle system notification for chain status changes\n * Publishes only the status change (delta) for affected chains\n *\n * @param notification - Server notification message containing chain status updates and timestamp\n */\n #handleSystemNotification(notification: ServerNotificationMessage): void {\n const data = notification.data as SystemNotificationData;\n const { timestamp } = notification;\n\n // Validate required fields\n if (!data.chainIds || !Array.isArray(data.chainIds) || !data.status) {\n throw new Error(\n 'Invalid system notification data: missing chainIds or status',\n );\n }\n\n // Track chain status\n if (data.status === 'up') {\n for (const chainId of data.chainIds) {\n this.#chainsUp.add(chainId);\n }\n } else {\n for (const chainId of data.chainIds) {\n this.#chainsUp.delete(chainId);\n }\n }\n\n // Publish status change directly (delta update)\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: data.chainIds,\n status: data.status,\n timestamp,\n });\n\n log(\n `WebSocket status change - Published tracked chains as ${data.status}`,\n {\n count: data.chainIds.length,\n chains: data.chainIds,\n status: data.status,\n },\n );\n }\n\n /**\n * Handle WebSocket connection state changes for fallback polling and resubscription\n *\n * @param connectionInfo - WebSocket connection state information\n */\n async #handleWebSocketStateChange(\n connectionInfo: WebSocketConnectionInfo,\n ): Promise<void> {\n const { state } = connectionInfo;\n\n if (state === WebSocketState.CONNECTED) {\n // WebSocket connected - resubscribe to selected account\n // The system notification will automatically provide the list of chains that are up\n await this.#subscribeToSelectedAccount();\n } else if (state === WebSocketState.DISCONNECTED) {\n // On disconnect, flush all tracked chains as down\n const chainsToMarkDown = Array.from(this.#chainsUp);\n\n if (chainsToMarkDown.length > 0) {\n this.#messenger.publish(`AccountActivityService:statusChanged`, {\n chainIds: chainsToMarkDown,\n status: 'down',\n timestamp: Date.now(),\n });\n\n log('WebSocket disconnection - Published tracked chains as down', {\n count: chainsToMarkDown.length,\n chains: chainsToMarkDown,\n });\n\n // Clear the tracking set since all chains are now down\n this.#chainsUp.clear();\n }\n }\n }\n\n // =============================================================================\n // Private Methods - Subscription Management\n // =============================================================================\n\n /**\n * Subscribe to the currently selected account only\n */\n async #subscribeToSelectedAccount(): Promise<void> {\n const selectedAccount = this.#messenger.call(\n 'AccountsController:getSelectedAccount',\n );\n\n if (!selectedAccount?.address) {\n return;\n }\n\n // Convert to CAIP-10 format and subscribe\n const address = this.#convertToCaip10Address(selectedAccount);\n await this.subscribe({ address });\n }\n\n /**\n * Unsubscribe from all account activity subscriptions for this service\n * Finds all channels matching the service's namespace and unsubscribes from them\n */\n async #unsubscribeFromAllAccountActivity(): Promise<void> {\n const accountActivitySubscriptions = this.#messenger.call(\n 'BackendWebSocketService:findSubscriptionsByChannelPrefix',\n this.#options.subscriptionNamespace,\n );\n\n // Unsubscribe from all matching subscriptions\n for (const subscription of accountActivitySubscriptions) {\n await subscription.unsubscribe();\n }\n }\n\n // =============================================================================\n // Private Methods - Utility Functions\n // =============================================================================\n\n /**\n * Convert an InternalAccount address to CAIP-10 format or raw address\n *\n * @param account - The internal account to convert\n * @returns The CAIP-10 formatted address or raw address\n */\n #convertToCaip10Address(account: InternalAccount): string {\n // Check if account has EVM scopes\n if (account.scopes.some((scope) => scope.startsWith('eip155:'))) {\n // CAIP-10 format: eip155:0:address (subscribe to all EVM chains)\n return `eip155:0:${account.address}`;\n }\n\n // Check if account has Solana scopes\n if (account.scopes.some((scope) => scope.startsWith('solana:'))) {\n // CAIP-10 format: solana:0:address (subscribe to all Solana chains)\n return `solana:0:${account.address}`;\n }\n\n // For other chains or unknown scopes, return raw address\n return account.address;\n }\n\n /**\n * Force WebSocket reconnection to clean up subscription state\n */\n async #forceReconnection(): Promise<void> {\n log('Forcing WebSocket reconnection to clean up subscription state');\n\n // Use the dedicated forceReconnection method which performs a controlled\n // disconnect-then-connect sequence to clean up subscription state\n await this.#messenger.call('BackendWebSocketService:forceReconnection');\n }\n\n // =============================================================================\n // Public Methods - Cleanup\n // =============================================================================\n\n /**\n * Destroy the service and clean up all resources\n * Optimized for fast cleanup during service destruction or mobile app termination\n */\n destroy(): void {\n // Clean up system notification callback\n this.#messenger.call(\n 'BackendWebSocketService:removeChannelCallback',\n `system-notifications.v1.${this.#options.subscriptionNamespace}`,\n );\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/core-backend",
|
|
3
|
-
"version": "6.5.0-preview-
|
|
3
|
+
"version": "6.5.0-preview-b867f452d",
|
|
4
4
|
"description": "Core backend services for MetaMask",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -55,12 +55,11 @@
|
|
|
55
55
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@metamask/
|
|
58
|
+
"@metamask/accounts-controller": "^39.0.5",
|
|
59
59
|
"@metamask/controller-utils": "^12.3.0",
|
|
60
60
|
"@metamask/keyring-controller": "^27.1.0",
|
|
61
61
|
"@metamask/messenger": "^2.0.0",
|
|
62
62
|
"@metamask/profile-sync-controller": "^28.3.0",
|
|
63
|
-
"@metamask/remote-feature-flag-controller": "^4.2.2",
|
|
64
63
|
"@metamask/utils": "^11.11.0",
|
|
65
64
|
"@tanstack/query-core": "^5.62.16",
|
|
66
65
|
"async-mutex": "^0.5.0",
|