@metamask-previews/core-backend 0.0.0-preview-bc80f5a1
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 +26 -0
- package/LICENSE +20 -0
- package/README.md +360 -0
- package/dist/AccountActivityService-method-action-types.cjs +7 -0
- package/dist/AccountActivityService-method-action-types.cjs.map +1 -0
- package/dist/AccountActivityService-method-action-types.d.cts +30 -0
- package/dist/AccountActivityService-method-action-types.d.cts.map +1 -0
- package/dist/AccountActivityService-method-action-types.d.mts +30 -0
- package/dist/AccountActivityService-method-action-types.d.mts.map +1 -0
- package/dist/AccountActivityService-method-action-types.mjs +6 -0
- package/dist/AccountActivityService-method-action-types.mjs.map +1 -0
- package/dist/AccountActivityService.cjs +380 -0
- package/dist/AccountActivityService.cjs.map +1 -0
- package/dist/AccountActivityService.d.cts +148 -0
- package/dist/AccountActivityService.d.cts.map +1 -0
- package/dist/AccountActivityService.d.mts +148 -0
- package/dist/AccountActivityService.d.mts.map +1 -0
- package/dist/AccountActivityService.mjs +376 -0
- package/dist/AccountActivityService.mjs.map +1 -0
- package/dist/BackendWebSocketService-method-action-types.cjs +7 -0
- package/dist/BackendWebSocketService-method-action-types.cjs.map +1 -0
- package/dist/BackendWebSocketService-method-action-types.d.cts +146 -0
- package/dist/BackendWebSocketService-method-action-types.d.cts.map +1 -0
- package/dist/BackendWebSocketService-method-action-types.d.mts +146 -0
- package/dist/BackendWebSocketService-method-action-types.d.mts.map +1 -0
- package/dist/BackendWebSocketService-method-action-types.mjs +6 -0
- package/dist/BackendWebSocketService-method-action-types.mjs.map +1 -0
- package/dist/BackendWebSocketService.cjs +841 -0
- package/dist/BackendWebSocketService.cjs.map +1 -0
- package/dist/BackendWebSocketService.d.cts +366 -0
- package/dist/BackendWebSocketService.d.cts.map +1 -0
- package/dist/BackendWebSocketService.d.mts +366 -0
- package/dist/BackendWebSocketService.d.mts.map +1 -0
- package/dist/BackendWebSocketService.mjs +836 -0
- package/dist/BackendWebSocketService.mjs.map +1 -0
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger.cjs +7 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +4 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +71 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +71 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +80 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **Initial release of `@metamask/core-backend` package** - Core backend services for MetaMask serving as the data layer between Backend services and Frontend applications ([#6722](https://github.com/MetaMask/core/pull/6722))
|
|
13
|
+
- **BackendWebSocketService** - WebSocket client providing authenticated real-time data delivery with:
|
|
14
|
+
- Connection management and automatic reconnection with exponential backoff
|
|
15
|
+
- Message routing and subscription management
|
|
16
|
+
- Authentication integration with `AuthenticationController`
|
|
17
|
+
- Type-safe messenger-based API for controller integration
|
|
18
|
+
- **AccountActivityService** - High-level service for monitoring account activity with:
|
|
19
|
+
- Real-time account activity monitoring via WebSocket subscriptions
|
|
20
|
+
- Balance update notifications for integration with `TokenBalancesController`
|
|
21
|
+
- Chain status change notifications for dynamic polling coordination
|
|
22
|
+
- Account subscription management with automatic cleanup
|
|
23
|
+
- **Type definitions** - Comprehensive TypeScript types for transactions, balances, WebSocket messages, and service configurations
|
|
24
|
+
- **Logging infrastructure** - Structured logging with module-specific loggers for debugging and monitoring
|
|
25
|
+
|
|
26
|
+
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
# `@metamask/core-backend`
|
|
2
|
+
|
|
3
|
+
Core backend services for MetaMask, serving as the data layer between Backend services (REST APIs, WebSocket services) and Frontend applications (Extension, Mobile). Provides authenticated real-time data delivery including account activity monitoring, price updates, and WebSocket connection management with type-safe controller integration.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [`@metamask/core-backend`](#metamaskcore-backend)
|
|
8
|
+
- [Table of Contents](#table-of-contents)
|
|
9
|
+
- [Installation](#installation)
|
|
10
|
+
- [Quick Start](#quick-start)
|
|
11
|
+
- [Basic Usage](#basic-usage)
|
|
12
|
+
- [Integration with Controllers](#integration-with-controllers)
|
|
13
|
+
- [Architecture \& Design](#architecture--design)
|
|
14
|
+
- [Layered Architecture](#layered-architecture)
|
|
15
|
+
- [Dependencies Structure](#dependencies-structure)
|
|
16
|
+
- [Data Flow](#data-flow)
|
|
17
|
+
- [Sequence Diagram: Real-time Account Activity Flow](#sequence-diagram-real-time-account-activity-flow)
|
|
18
|
+
- [Key Flow Characteristics](#key-flow-characteristics)
|
|
19
|
+
- [API Reference](#api-reference)
|
|
20
|
+
- [BackendWebSocketService](#backendwebsocketservice)
|
|
21
|
+
- [Constructor Options](#constructor-options)
|
|
22
|
+
- [Methods](#methods)
|
|
23
|
+
- [AccountActivityService](#accountactivityservice)
|
|
24
|
+
- [Constructor Options](#constructor-options-1)
|
|
25
|
+
- [Methods](#methods-1)
|
|
26
|
+
- [Events Published](#events-published)
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
yarn add @metamask/core-backend
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
or
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install @metamask/core-backend
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick Start
|
|
41
|
+
|
|
42
|
+
### Basic Usage
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import {
|
|
46
|
+
BackendWebSocketService,
|
|
47
|
+
AccountActivityService,
|
|
48
|
+
} from '@metamask/core-backend';
|
|
49
|
+
|
|
50
|
+
// Initialize Backend WebSocket service
|
|
51
|
+
const backendWebSocketService = new BackendWebSocketService({
|
|
52
|
+
messenger: backendWebSocketServiceMessenger,
|
|
53
|
+
url: 'wss://api.metamask.io/ws',
|
|
54
|
+
timeout: 15000,
|
|
55
|
+
requestTimeout: 20000,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Initialize Account Activity service
|
|
59
|
+
const accountActivityService = new AccountActivityService({
|
|
60
|
+
messenger: accountActivityMessenger,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Connect and subscribe to account activity
|
|
64
|
+
await backendWebSocketService.connect();
|
|
65
|
+
await accountActivityService.subscribe({
|
|
66
|
+
address: 'eip155:0:0x742d35cc6634c0532925a3b8d40c4e0e2c6e4e6',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Listen for real-time updates
|
|
70
|
+
messenger.subscribe('AccountActivityService:transactionUpdated', (tx) => {
|
|
71
|
+
console.log('New transaction:', tx);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
messenger.subscribe(
|
|
75
|
+
'AccountActivityService:balanceUpdated',
|
|
76
|
+
({ address, updates }) => {
|
|
77
|
+
console.log(`Balance updated for ${address}:`, updates);
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Integration with Controllers
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// Coordinate with TokenBalancesController for fallback polling
|
|
86
|
+
messenger.subscribe(
|
|
87
|
+
'BackendWebSocketService:connectionStateChanged',
|
|
88
|
+
(info) => {
|
|
89
|
+
if (info.state === 'CONNECTED') {
|
|
90
|
+
// Reduce polling when WebSocket is active
|
|
91
|
+
messenger.call(
|
|
92
|
+
'TokenBalancesController:updateChainPollingConfigs',
|
|
93
|
+
{ '0x1': { interval: 600000 } }, // 10 min backup polling
|
|
94
|
+
{ immediateUpdate: false },
|
|
95
|
+
);
|
|
96
|
+
} else {
|
|
97
|
+
// Increase polling when WebSocket is down
|
|
98
|
+
const defaultInterval = messenger.call(
|
|
99
|
+
'TokenBalancesController:getDefaultPollingInterval',
|
|
100
|
+
);
|
|
101
|
+
messenger.call(
|
|
102
|
+
'TokenBalancesController:updateChainPollingConfigs',
|
|
103
|
+
{ '0x1': { interval: defaultInterval } },
|
|
104
|
+
{ immediateUpdate: true },
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Listen for account changes and manage subscriptions
|
|
111
|
+
messenger.subscribe(
|
|
112
|
+
'AccountsController:selectedAccountChange',
|
|
113
|
+
async (selectedAccount) => {
|
|
114
|
+
if (selectedAccount) {
|
|
115
|
+
await accountActivityService.subscribe({
|
|
116
|
+
address: selectedAccount.address,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Architecture & Design
|
|
124
|
+
|
|
125
|
+
### Layered Architecture
|
|
126
|
+
|
|
127
|
+
```mermaid
|
|
128
|
+
graph TD
|
|
129
|
+
subgraph "FRONTEND"
|
|
130
|
+
subgraph "Presentation Layer"
|
|
131
|
+
FE[Frontend Applications<br/>MetaMask Extension, Mobile, etc.]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
subgraph "Integration Layer"
|
|
135
|
+
IL[Controllers, State Management, UI]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
subgraph "Data layer (core-backend)"
|
|
139
|
+
subgraph "Domain Services"
|
|
140
|
+
AAS[AccountActivityService]
|
|
141
|
+
PUS[PriceUpdateService<br/>future]
|
|
142
|
+
CS[Custom Services...]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
subgraph "Transport Layer"
|
|
146
|
+
WSS[WebSocketService<br/>• Connection management<br/>• Automatic reconnection<br/>• Message routing<br/>• Subscription management]
|
|
147
|
+
HTTP[HTTP Service<br/>• REST API calls<br/>• Request/response handling<br/>• Error handling<br/>future]
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
subgraph "BACKEND"
|
|
153
|
+
BS[Backend Services<br/>REST APIs, WebSocket Services, etc.]
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
%% Flow connections
|
|
157
|
+
FE --> IL
|
|
158
|
+
IL --> AAS
|
|
159
|
+
IL --> PUS
|
|
160
|
+
IL --> CS
|
|
161
|
+
AAS --> WSS
|
|
162
|
+
AAS --> HTTP
|
|
163
|
+
PUS --> WSS
|
|
164
|
+
PUS --> HTTP
|
|
165
|
+
CS --> WSS
|
|
166
|
+
CS --> HTTP
|
|
167
|
+
WSS <--> BS
|
|
168
|
+
HTTP <--> BS
|
|
169
|
+
|
|
170
|
+
%% Styling
|
|
171
|
+
classDef frontend fill:#e1f5fe
|
|
172
|
+
classDef backend fill:#f3e5f5
|
|
173
|
+
classDef service fill:#e8f5e8
|
|
174
|
+
classDef transport fill:#fff3e0
|
|
175
|
+
|
|
176
|
+
class FE,IL frontend
|
|
177
|
+
class BS backend
|
|
178
|
+
class AAS,PUS,CS service
|
|
179
|
+
class WSS,HTTP transport
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Dependencies Structure
|
|
183
|
+
|
|
184
|
+
```mermaid
|
|
185
|
+
graph BT
|
|
186
|
+
%% External Controllers
|
|
187
|
+
AC["AccountsController<br/>(Auto-generated types)"]
|
|
188
|
+
AuthC["AuthenticationController<br/>(Auto-generated types)"]
|
|
189
|
+
TBC["TokenBalancesController<br/>(External Integration)"]
|
|
190
|
+
|
|
191
|
+
%% Core Services
|
|
192
|
+
AA["AccountActivityService"]
|
|
193
|
+
WS["BackendWebSocketService"]
|
|
194
|
+
|
|
195
|
+
%% Dependencies & Type Imports
|
|
196
|
+
AC -.->|"Import types<br/>(DRY)" | AA
|
|
197
|
+
AuthC -.->|"Import types<br/>(DRY)" | WS
|
|
198
|
+
WS -->|"Messenger calls"| AA
|
|
199
|
+
AA -.->|"Event publishing"| TBC
|
|
200
|
+
|
|
201
|
+
%% Styling
|
|
202
|
+
classDef core fill:#f3e5f5
|
|
203
|
+
classDef integration fill:#fff3e0
|
|
204
|
+
classDef controller fill:#e8f5e8
|
|
205
|
+
|
|
206
|
+
class WS,AA core
|
|
207
|
+
class TBC integration
|
|
208
|
+
class AC,AuthC controller
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Data Flow
|
|
212
|
+
|
|
213
|
+
#### Sequence Diagram: Real-time Account Activity Flow
|
|
214
|
+
|
|
215
|
+
```mermaid
|
|
216
|
+
sequenceDiagram
|
|
217
|
+
participant TBC as TokenBalancesController
|
|
218
|
+
participant AA as AccountActivityService
|
|
219
|
+
participant WS as BackendWebSocketService
|
|
220
|
+
participant HTTP as HTTP Services<br/>(APIs & RPC)
|
|
221
|
+
participant Backend as WebSocket Endpoint<br/>(Backend)
|
|
222
|
+
|
|
223
|
+
Note over TBC,Backend: Initial Setup
|
|
224
|
+
TBC->>HTTP: Initial balance fetch via HTTP<br/>(first request for current state)
|
|
225
|
+
|
|
226
|
+
WS->>Backend: WebSocket connection request
|
|
227
|
+
Backend->>WS: Connection established
|
|
228
|
+
WS->>AA: WebSocket connection status notification<br/>(BackendWebSocketService:connectionStateChanged)<br/>{state: 'CONNECTED'}
|
|
229
|
+
|
|
230
|
+
par StatusChanged Event
|
|
231
|
+
AA->>TBC: Chain availability notification<br/>(AccountActivityService:statusChanged)<br/>{chainIds: ['0x1', '0x89', ...], status: 'up'}
|
|
232
|
+
TBC->>TBC: Increase polling interval from 20s to 10min<br/>(.updateChainPollingConfigs({0x89: 600000}))
|
|
233
|
+
and Account Subscription
|
|
234
|
+
AA->>AA: call('AccountsController:getSelectedAccount')
|
|
235
|
+
AA->>WS: subscribe({channels, callback})
|
|
236
|
+
WS->>Backend: {event: 'subscribe', channels: ['account-activity.v1.eip155:0:0x123...']}
|
|
237
|
+
Backend->>WS: {event: 'subscribe-response', subscriptionId: 'sub-456'}
|
|
238
|
+
WS->>AA: Subscription sucessful
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
Note over TBC,Backend: User Account Change
|
|
242
|
+
|
|
243
|
+
par StatusChanged Event
|
|
244
|
+
TBC->>HTTP: Fetch balances for new account<br/>(fill transition gap)
|
|
245
|
+
and Account Subscription
|
|
246
|
+
AA->>AA: User switched to different account<br/>(AccountsController:selectedAccountChange)
|
|
247
|
+
AA->>WS: subscribe (new account)
|
|
248
|
+
WS->>Backend: {event: 'subscribe', channels: ['account-activity.v1.eip155:0:0x456...']}
|
|
249
|
+
Backend->>WS: {event: 'subscribe-response', subscriptionId: 'sub-789'}
|
|
250
|
+
AA->>WS: unsubscribe (previous account)
|
|
251
|
+
WS->>Backend: {event: 'unsubscribe', subscriptionId: 'sub-456'}
|
|
252
|
+
Backend->>WS: {event: 'unsubscribe-response'}
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
Note over TBC,Backend: Real-time Data Flow
|
|
257
|
+
|
|
258
|
+
Backend->>WS: {event: 'notification', channel: 'account-activity.v1.eip155:0:0x123...',<br/>data: {address, tx, updates}}
|
|
259
|
+
WS->>AA: Direct callback routing
|
|
260
|
+
AA->>AA: Validate & process AccountActivityMessage
|
|
261
|
+
|
|
262
|
+
par Balance Update
|
|
263
|
+
AA->>TBC: Real-time balance change notification<br/>(AccountActivityService:balanceUpdated)<br/>{address, chain, updates}
|
|
264
|
+
TBC->>TBC: Update balance state directly<br/>(or fallback poll if error)
|
|
265
|
+
and Transaction and Activity Update (Not yet implemented)
|
|
266
|
+
AA->>AA: Process transaction data<br/>(AccountActivityService:transactionUpdated)<br/>{tx: Transaction}
|
|
267
|
+
Note right of AA: Future: Forward to TransactionController<br/>for transaction state management<br/>(pending → confirmed → finalized)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
Note over TBC,Backend: System Notifications
|
|
271
|
+
|
|
272
|
+
Backend->>WS: {event: 'system-notification', data: {chainIds: ['eip155:137'], status: 'down'}}
|
|
273
|
+
WS->>AA: System notification received
|
|
274
|
+
AA->>AA: Process chain status change
|
|
275
|
+
AA->>TBC: Chain status notification<br/>(AccountActivityService:statusChanged)<br/>{chainIds: ['eip155:137'], status: 'down'}
|
|
276
|
+
TBC->>TBC: Decrease polling interval from 10min to 20s<br/>(.updateChainPollingConfigs({0x89: 20000}))
|
|
277
|
+
TBC->>HTTP: Fetch balances immediately
|
|
278
|
+
|
|
279
|
+
Backend->>WS: {event: 'system-notification', data: {chainIds: ['eip155:137'], status: 'up'}}
|
|
280
|
+
WS->>AA: System notification received
|
|
281
|
+
AA->>AA: Process chain status change
|
|
282
|
+
AA->>TBC: Chain status notification<br/>(AccountActivityService:statusChanged)<br/>{chainIds: ['eip155:137'], status: 'up'}
|
|
283
|
+
TBC->>TBC: Increase polling interval from 20s to 10min<br/>(.updateChainPollingConfigs({0x89: 600000}))
|
|
284
|
+
|
|
285
|
+
Note over TBC,Backend: Connection Health Management
|
|
286
|
+
|
|
287
|
+
Backend-->>WS: Connection lost
|
|
288
|
+
WS->>TBC: WebSocket connection status notification<br/>(BackendWebSocketService:connectionStateChanged)<br/>{state: 'DISCONNECTED'}
|
|
289
|
+
TBC->>TBC: Decrease polling interval from 10min to 20s(.updateChainPollingConfigs({0x89: 20000}))
|
|
290
|
+
TBC->>HTTP: Fetch balances immediately
|
|
291
|
+
WS->>WS: Automatic reconnection<br/>with exponential backoff
|
|
292
|
+
WS->>Backend: Reconnection successful - Restart initial setup
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
#### Key Flow Characteristics
|
|
296
|
+
|
|
297
|
+
1. **Initial Setup**: BackendWebSocketService establishes connection, then AccountActivityService simultaneously notifies all chains are up AND subscribes to selected account, TokenBalancesController increases polling interval to 10 min, then makes initial HTTP request for current balance state
|
|
298
|
+
2. **User Account Changes**: When users switch accounts, AccountActivityService unsubscribes from old account, TokenBalancesController makes HTTP calls to fill data gaps, then AccountActivityService subscribes to new account
|
|
299
|
+
3. **Real-time Updates**: Backend pushes data through: Backend → BackendWebSocketService → AccountActivityService → TokenBalancesController (+ future TransactionController integration)
|
|
300
|
+
4. **System Notifications**: Backend sends chain status updates (up/down) through WebSocket, AccountActivityService processes and forwards to TokenBalancesController which adjusts polling intervals and fetches balances immediately on chain down (chain down: 10min→20s + immediate fetch, chain up: 20s→10min)
|
|
301
|
+
5. **Parallel Processing**: Transaction and balance updates processed simultaneously - AccountActivityService publishes both transactionUpdated (future) and balanceUpdated events in parallel
|
|
302
|
+
6. **Dynamic Polling**: TokenBalancesController adjusts HTTP polling intervals based on WebSocket connection health (10 min when connected, 20s when disconnected)
|
|
303
|
+
7. **Direct Balance Processing**: Real-time balance updates bypass HTTP polling and update TokenBalancesController state directly
|
|
304
|
+
8. **Connection Resilience**: Automatic reconnection with resubscription to selected account
|
|
305
|
+
9. **Ultra-Simple Error Handling**: Any error anywhere → force reconnection (no nested try-catch)
|
|
306
|
+
|
|
307
|
+
## API Reference
|
|
308
|
+
|
|
309
|
+
### BackendWebSocketService
|
|
310
|
+
|
|
311
|
+
The core WebSocket client providing connection management, authentication, and message routing.
|
|
312
|
+
|
|
313
|
+
#### Constructor Options
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
interface BackendWebSocketServiceOptions {
|
|
317
|
+
messenger: BackendWebSocketServiceMessenger;
|
|
318
|
+
url: string;
|
|
319
|
+
timeout?: number;
|
|
320
|
+
reconnectDelay?: number;
|
|
321
|
+
maxReconnectDelay?: number;
|
|
322
|
+
requestTimeout?: number;
|
|
323
|
+
enableAuthentication?: boolean;
|
|
324
|
+
enabledCallback?: () => boolean;
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Methods
|
|
329
|
+
|
|
330
|
+
- `connect(): Promise<void>` - Establish authenticated WebSocket connection
|
|
331
|
+
- `disconnect(): Promise<void>` - Close WebSocket connection
|
|
332
|
+
- `subscribe(options: SubscriptionOptions): Promise<SubscriptionResult>` - Subscribe to channels
|
|
333
|
+
- `sendRequest(message: ClientRequestMessage): Promise<ServerResponseMessage>` - Send request/response messages
|
|
334
|
+
- `channelHasSubscription(channel: string): boolean` - Check subscription status
|
|
335
|
+
- `findSubscriptionsByChannelPrefix(prefix: string): SubscriptionInfo[]` - Find subscriptions by prefix
|
|
336
|
+
- `getConnectionInfo(): WebSocketConnectionInfo` - Get detailed connection state
|
|
337
|
+
|
|
338
|
+
### AccountActivityService
|
|
339
|
+
|
|
340
|
+
High-level service for monitoring account activity using WebSocket data.
|
|
341
|
+
|
|
342
|
+
#### Constructor Options
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
interface AccountActivityServiceOptions {
|
|
346
|
+
messenger: AccountActivityServiceMessenger;
|
|
347
|
+
subscriptionNamespace?: string;
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### Methods
|
|
352
|
+
|
|
353
|
+
- `subscribe(subscription: SubscriptionOptions): Promise<void>` - Subscribe to account activity
|
|
354
|
+
- `unsubscribe(subscription: SubscriptionOptions): Promise<void>` - Unsubscribe from account activity
|
|
355
|
+
|
|
356
|
+
#### Events Published
|
|
357
|
+
|
|
358
|
+
- `AccountActivityService:balanceUpdated` - Real-time balance changes
|
|
359
|
+
- `AccountActivityService:transactionUpdated` - Transaction status updates
|
|
360
|
+
- `AccountActivityService:statusChanged` - Chain/service status changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.cjs","sourceRoot":"","sources":["../src/AccountActivityService-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\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"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { AccountActivityService } from "./AccountActivityService.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Subscribe to account activity (transactions and balance updates)
|
|
8
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
9
|
+
*
|
|
10
|
+
* @param subscription - Account subscription configuration with address
|
|
11
|
+
*/
|
|
12
|
+
export type AccountActivityServiceSubscribeAction = {
|
|
13
|
+
type: `AccountActivityService:subscribe`;
|
|
14
|
+
handler: AccountActivityService['subscribe'];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
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...")
|
|
19
|
+
*
|
|
20
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
21
|
+
*/
|
|
22
|
+
export type AccountActivityServiceUnsubscribeAction = {
|
|
23
|
+
type: `AccountActivityService:unsubscribe`;
|
|
24
|
+
handler: AccountActivityService['unsubscribe'];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Union of all AccountActivityService action types.
|
|
28
|
+
*/
|
|
29
|
+
export type AccountActivityServiceMethodActions = AccountActivityServiceSubscribeAction | AccountActivityServiceUnsubscribeAction;
|
|
30
|
+
//# sourceMappingURL=AccountActivityService-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.d.cts","sourceRoot":"","sources":["../src/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"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { AccountActivityService } from "./AccountActivityService.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Subscribe to account activity (transactions and balance updates)
|
|
8
|
+
* Address should be in CAIP-10 format (e.g., "eip155:0:0x1234..." or "solana:0:ABC123...")
|
|
9
|
+
*
|
|
10
|
+
* @param subscription - Account subscription configuration with address
|
|
11
|
+
*/
|
|
12
|
+
export type AccountActivityServiceSubscribeAction = {
|
|
13
|
+
type: `AccountActivityService:subscribe`;
|
|
14
|
+
handler: AccountActivityService['subscribe'];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
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...")
|
|
19
|
+
*
|
|
20
|
+
* @param subscription - Account subscription configuration with address to unsubscribe
|
|
21
|
+
*/
|
|
22
|
+
export type AccountActivityServiceUnsubscribeAction = {
|
|
23
|
+
type: `AccountActivityService:unsubscribe`;
|
|
24
|
+
handler: AccountActivityService['unsubscribe'];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Union of all AccountActivityService action types.
|
|
28
|
+
*/
|
|
29
|
+
export type AccountActivityServiceMethodActions = AccountActivityServiceSubscribeAction | AccountActivityServiceUnsubscribeAction;
|
|
30
|
+
//# sourceMappingURL=AccountActivityService-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.d.mts","sourceRoot":"","sources":["../src/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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountActivityService-method-action-types.mjs","sourceRoot":"","sources":["../src/AccountActivityService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\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"]}
|