@drift-labs/sdk 2.136.0-beta.0 → 2.136.0-beta.2

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.
Files changed (77) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/accounts/types.d.ts +2 -0
  3. package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +76 -3
  4. package/lib/browser/accounts/webSocketAccountSubscriberV2.js +211 -39
  5. package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +87 -0
  6. package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.js +444 -0
  7. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +145 -0
  8. package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.js +744 -0
  9. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +22 -0
  10. package/lib/browser/accounts/websocketProgramUserAccountSubscriber.js +54 -0
  11. package/lib/browser/driftClient.js +22 -18
  12. package/lib/browser/driftClientConfig.d.ts +7 -2
  13. package/lib/browser/factory/bigNum.d.ts +2 -2
  14. package/lib/browser/factory/bigNum.js +20 -5
  15. package/lib/browser/index.d.ts +4 -0
  16. package/lib/browser/index.js +9 -1
  17. package/lib/browser/memcmp.d.ts +2 -0
  18. package/lib/browser/memcmp.js +19 -1
  19. package/lib/browser/oracles/oracleId.d.ts +5 -0
  20. package/lib/browser/oracles/oracleId.js +46 -1
  21. package/lib/browser/user.js +12 -5
  22. package/lib/browser/userConfig.d.ts +3 -0
  23. package/lib/node/accounts/types.d.ts +2 -0
  24. package/lib/node/accounts/types.d.ts.map +1 -1
  25. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +76 -3
  26. package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
  27. package/lib/node/accounts/webSocketAccountSubscriberV2.js +211 -39
  28. package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +88 -0
  29. package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts.map +1 -0
  30. package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.js +444 -0
  31. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +146 -0
  32. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -0
  33. package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +744 -0
  34. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +23 -0
  35. package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -0
  36. package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +54 -0
  37. package/lib/node/driftClient.d.ts.map +1 -1
  38. package/lib/node/driftClient.js +22 -18
  39. package/lib/node/driftClientConfig.d.ts +7 -2
  40. package/lib/node/driftClientConfig.d.ts.map +1 -1
  41. package/lib/node/factory/bigNum.d.ts +2 -2
  42. package/lib/node/factory/bigNum.d.ts.map +1 -1
  43. package/lib/node/factory/bigNum.js +20 -5
  44. package/lib/node/index.d.ts +4 -0
  45. package/lib/node/index.d.ts.map +1 -1
  46. package/lib/node/index.js +9 -1
  47. package/lib/node/memcmp.d.ts +2 -0
  48. package/lib/node/memcmp.d.ts.map +1 -1
  49. package/lib/node/memcmp.js +19 -1
  50. package/lib/node/oracles/oracleId.d.ts +5 -0
  51. package/lib/node/oracles/oracleId.d.ts.map +1 -1
  52. package/lib/node/oracles/oracleId.js +46 -1
  53. package/lib/node/user.d.ts.map +1 -1
  54. package/lib/node/user.js +12 -5
  55. package/lib/node/userConfig.d.ts +3 -0
  56. package/lib/node/userConfig.d.ts.map +1 -1
  57. package/package.json +1 -1
  58. package/src/accounts/README_WebSocketAccountSubscriberV2.md +41 -0
  59. package/src/accounts/types.ts +3 -0
  60. package/src/accounts/webSocketAccountSubscriberV2.ts +243 -42
  61. package/src/accounts/webSocketDriftClientAccountSubscriberV2.ts +745 -0
  62. package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +995 -0
  63. package/src/accounts/websocketProgramUserAccountSubscriber.ts +94 -0
  64. package/src/driftClient.ts +13 -7
  65. package/src/driftClientConfig.ts +15 -8
  66. package/src/factory/bigNum.ts +22 -5
  67. package/src/index.ts +4 -0
  68. package/src/memcmp.ts +17 -0
  69. package/src/oracles/oracleId.ts +34 -0
  70. package/src/user.ts +21 -9
  71. package/src/userConfig.ts +3 -0
  72. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +0 -53
  73. package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +0 -453
  74. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +0 -54
  75. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +0 -1
  76. package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +0 -453
  77. package/src/accounts/webSocketProgramAccountSubscriberV2.ts +0 -596
@@ -0,0 +1,744 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.WebSocketProgramAccountsSubscriberV2 = void 0;
7
+ const web3_js_1 = require("@solana/web3.js");
8
+ const gill_1 = require("gill");
9
+ const bs58_1 = __importDefault(require("bs58"));
10
+ /**
11
+ * WebSocketProgramAccountsSubscriberV2
12
+ *
13
+ * High-level overview
14
+ * - WebSocket-first subscriber for Solana program accounts that also layers in
15
+ * targeted polling to detect missed updates reliably.
16
+ * - Emits decoded account updates via the provided `onChange` callback.
17
+ * - Designed to focus extra work on the specific accounts the consumer cares
18
+ * about ("monitored accounts") while keeping baseline WS behavior for the
19
+ * full program subscription.
20
+ *
21
+ * Why polling if this is a WebSocket subscriber?
22
+ * - WS infra can stall, drop, or reorder notifications under network stress or
23
+ * provider hiccups. When that happens, critical account changes can be missed.
24
+ * - To mitigate this, the class accepts a set of accounts (provided via constructor) to monitor
25
+ * and uses light polling to verify whether a WS change was missed.
26
+ * - If polling detects a newer slot with different data than the last seen
27
+ * buffer, a centralized resubscription is triggered to restore a clean stream.
28
+ *
29
+ * Initial fetch (on subscribe)
30
+ * - On `subscribe()`, we first perform a single batched fetch of all monitored
31
+ * accounts ("initial monitor fetch").
32
+ * - Purpose: seed the internal `bufferAndSlotMap` and emit the latest state so
33
+ * consumers have up-to-date data immediately, even before WS events arrive.
34
+ * - This step does not decide resubscription; it only establishes ground truth.
35
+ *
36
+ * Continuous polling (only for monitored accounts)
37
+ * - After seeding, each monitored account is put into a monitoring cycle:
38
+ * 1) If no WS notification for an account is observed for `pollingIntervalMs`,
39
+ * we enqueue it for a batched fetch (buffered for a short window).
40
+ * 2) Once an account enters the "currently polling" set, a shared batch poll
41
+ * runs every `pollingIntervalMs` across all such accounts.
42
+ * 3) If WS notifications resume for an account, that account is removed from
43
+ * the polling set and returns to passive monitoring.
44
+ * - Polling compares the newly fetched buffer with the last stored buffer at a
45
+ * later slot. A difference indicates a missed update; we schedule a single
46
+ * resubscription (coalesced across accounts) to re-sync.
47
+ *
48
+ * Accounts the consumer cares about
49
+ * - Provide accounts up-front via the constructor `accountsToMonitor`, or add
50
+ * them dynamically with `addAccountToMonitor()` and remove with
51
+ * `removeAccountFromMonitor()`.
52
+ * - Only these accounts incur additional polling safeguards; other accounts are
53
+ * still processed from the WS stream normally.
54
+ *
55
+ * Resubscription strategy
56
+ * - Missed updates from any monitored account are coalesced and trigger a single
57
+ * resubscription after a short delay. This avoids rapid churn.
58
+ * - If `resubOpts.resubTimeoutMs` is set, an inactivity timer also performs a
59
+ * batch check of monitored accounts. If a missed update is found, the same
60
+ * centralized resubscription flow is used.
61
+ *
62
+ * Tuning knobs
63
+ * - `setPollingInterval(ms)`: adjust how often monitoring/polling runs
64
+ * (default 30s). Shorter = faster detection, higher RPC load.
65
+ * - Debounced immediate poll (~100ms): batches accounts added to polling right after inactivity.
66
+ * - Batch size for `getMultipleAccounts` is limited to 100, requests are chunked
67
+ * and processed concurrently.
68
+ */
69
+ class WebSocketProgramAccountsSubscriberV2 {
70
+ constructor(subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
71
+ filters: [],
72
+ }, resubOpts, accountsToMonitor // Optional list of accounts to poll
73
+ ) {
74
+ var _a;
75
+ this.bufferAndSlotMap = new Map();
76
+ this.isUnsubscribing = false;
77
+ this.receivingData = false;
78
+ // Polling logic for specific accounts
79
+ this.accountsToMonitor = new Set();
80
+ this.pollingIntervalMs = 30000; // 30 seconds
81
+ this.pollingTimeouts = new Map();
82
+ this.lastWsNotificationTime = new Map(); // Track last WS notification time per account
83
+ this.accountsCurrentlyPolling = new Set(); // Track which accounts are being polled
84
+ this.debouncedImmediatePollMs = 100; // configurable short window
85
+ // Centralized resubscription handling
86
+ this.missedChangeDetected = false; // Flag to track if any missed change was detected
87
+ this.accountsWithMissedUpdates = new Set(); // Track which accounts had missed updates
88
+ this.subscriptionName = subscriptionName;
89
+ this.accountDiscriminator = accountDiscriminator;
90
+ this.program = program;
91
+ this.decodeBuffer = decodeBufferFn;
92
+ this.resubOpts = resubOpts !== null && resubOpts !== void 0 ? resubOpts : {
93
+ resubTimeoutMs: 30000,
94
+ usePollingInsteadOfResub: true,
95
+ logResubMessages: false,
96
+ };
97
+ if (((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) < 1000) {
98
+ console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
99
+ }
100
+ this.options = options;
101
+ this.receivingData = false;
102
+ // Initialize accounts to monitor
103
+ if (accountsToMonitor) {
104
+ accountsToMonitor.forEach((account) => {
105
+ this.accountsToMonitor.add(account.toBase58());
106
+ });
107
+ }
108
+ // Initialize gill client using the same RPC URL as the program provider
109
+ const rpcUrl = this.program.provider.connection
110
+ .rpcEndpoint;
111
+ const { rpc, rpcSubscriptions } = (0, gill_1.createSolanaClient)({
112
+ urlOrMoniker: rpcUrl,
113
+ });
114
+ this.rpc = rpc;
115
+ this.rpcSubscriptions = rpcSubscriptions;
116
+ }
117
+ async handleNotificationLoop(notificationPromise) {
118
+ var _a;
119
+ try {
120
+ const subscriptionIterable = await notificationPromise;
121
+ for await (const notification of subscriptionIterable) {
122
+ try {
123
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
124
+ this.receivingData = true;
125
+ clearTimeout(this.timeoutId);
126
+ this.handleRpcResponse(notification.context, notification.value.pubkey, notification.value.account.data);
127
+ this.setTimeout();
128
+ }
129
+ else {
130
+ this.handleRpcResponse(notification.context, notification.value.pubkey, notification.value.account.data);
131
+ }
132
+ }
133
+ catch (error) {
134
+ console.error(`Error handling RPC response for pubkey ${notification.value.pubkey}:`, error);
135
+ }
136
+ }
137
+ }
138
+ catch (error) {
139
+ console.error(`[${this.subscriptionName}] Error in notification loop:`, error);
140
+ }
141
+ }
142
+ async subscribe(onChange) {
143
+ var _a, _b;
144
+ /**
145
+ * Start the WebSocket subscription and initialize polling safeguards.
146
+ *
147
+ * Flow
148
+ * - Seeds all monitored accounts with a single batched RPC fetch and emits
149
+ * their current state.
150
+ * - Subscribes to program notifications via WS using gill.
151
+ * - If `resubOpts.resubTimeoutMs` is set, starts an inactivity timer that
152
+ * batch-checks monitored accounts when WS goes quiet.
153
+ * - Begins monitoring for accounts that may need polling when WS
154
+ * notifications are not observed within `pollingIntervalMs`.
155
+ *
156
+ * @param onChange Callback invoked with decoded account data when an update
157
+ * is detected (via WS or batch RPC fetch).
158
+ */
159
+ const startTime = performance.now();
160
+ if (this.listenerId != null || this.isUnsubscribing) {
161
+ return;
162
+ }
163
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
164
+ console.log(`[${this.subscriptionName}] initializing subscription. This many monitored accounts: ${this.accountsToMonitor.size}`);
165
+ }
166
+ this.onChange = onChange;
167
+ // initial fetch of monitored data - only fetch and populate, don't check for missed changes
168
+ await this.fetchAndPopulateAllMonitoredAccounts();
169
+ // Create abort controller for proper cleanup
170
+ const abortController = new AbortController();
171
+ this.abortController = abortController;
172
+ this.listenerId = Math.random(); // Unique ID for logging purposes
173
+ if ((_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs) {
174
+ this.receivingData = true;
175
+ this.setTimeout();
176
+ }
177
+ // Subscribe to program account changes using gill's rpcSubscriptions
178
+ const programId = this.program.programId.toBase58();
179
+ if ((0, gill_1.isAddress)(programId)) {
180
+ const subscriptionPromise = this.rpcSubscriptions
181
+ .programNotifications(programId, {
182
+ commitment: this.options.commitment,
183
+ encoding: 'base64',
184
+ filters: this.options.filters.map((filter) => {
185
+ // Convert filter bytes from base58 to base64 if needed
186
+ let bytes = filter.memcmp.bytes;
187
+ if (typeof bytes === 'string' &&
188
+ /^[1-9A-HJ-NP-Za-km-z]+$/.test(bytes)) {
189
+ // Looks like base58 - convert to base64
190
+ const decoded = bs58_1.default.decode(bytes);
191
+ bytes = Buffer.from(decoded).toString('base64');
192
+ }
193
+ return {
194
+ memcmp: {
195
+ offset: BigInt(filter.memcmp.offset),
196
+ bytes: bytes,
197
+ encoding: 'base64',
198
+ },
199
+ };
200
+ }),
201
+ })
202
+ .subscribe({
203
+ abortSignal: abortController.signal,
204
+ });
205
+ // Start notification loop without awaiting
206
+ this.handleNotificationLoop(subscriptionPromise);
207
+ // Start monitoring for accounts that may need polling if no WS event is received
208
+ this.startMonitoringForAccounts();
209
+ }
210
+ const endTime = performance.now();
211
+ console.log(`[PROFILING] ${this.subscriptionName}.subscribe() completed in ${endTime - startTime}ms`);
212
+ }
213
+ setTimeout() {
214
+ var _a;
215
+ if (!this.onChange) {
216
+ throw new Error('onChange callback function must be set');
217
+ }
218
+ this.timeoutId = setTimeout(async () => {
219
+ var _a, _b;
220
+ if (this.isUnsubscribing) {
221
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
222
+ return;
223
+ }
224
+ if (this.receivingData) {
225
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
226
+ console.log(`No ws data from ${this.subscriptionName} in ${(_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs}ms, checking for missed changes`);
227
+ }
228
+ // Check for missed changes in monitored accounts
229
+ const missedChangeDetected = await this.fetchAllMonitoredAccounts();
230
+ if (missedChangeDetected) {
231
+ // Signal missed change with a generic identifier since we don't have specific account IDs from this context
232
+ this.signalMissedChange('timeout-check');
233
+ }
234
+ else {
235
+ // No missed changes, continue monitoring
236
+ this.receivingData = false;
237
+ this.setTimeout();
238
+ }
239
+ }
240
+ }, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
241
+ }
242
+ handleRpcResponse(context, accountId, accountInfo) {
243
+ const newSlot = Number(context.slot);
244
+ let newBuffer = undefined;
245
+ if (accountInfo) {
246
+ // Handle different data formats from gill
247
+ if (Array.isArray(accountInfo)) {
248
+ // If it's a tuple [data, encoding]
249
+ const [data, encoding] = accountInfo;
250
+ if (encoding === 'base58') {
251
+ // Convert base58 to buffer using bs58
252
+ newBuffer = Buffer.from(bs58_1.default.decode(data));
253
+ }
254
+ else {
255
+ newBuffer = Buffer.from(data, 'base64');
256
+ }
257
+ }
258
+ }
259
+ const accountIdString = accountId.toString();
260
+ const existingBufferAndSlot = this.bufferAndSlotMap.get(accountIdString);
261
+ // Track WebSocket notification time for this account
262
+ this.lastWsNotificationTime.set(accountIdString, Date.now());
263
+ // If this account was being polled, stop polling it if the buffer has changed
264
+ if (this.accountsCurrentlyPolling.has(accountIdString) &&
265
+ !(existingBufferAndSlot === null || existingBufferAndSlot === void 0 ? void 0 : existingBufferAndSlot.buffer.equals(newBuffer))) {
266
+ this.accountsCurrentlyPolling.delete(accountIdString);
267
+ // If no more accounts are being polled, stop batch polling
268
+ if (this.accountsCurrentlyPolling.size === 0 &&
269
+ this.batchPollingTimeout) {
270
+ clearTimeout(this.batchPollingTimeout);
271
+ this.batchPollingTimeout = undefined;
272
+ }
273
+ }
274
+ if (!existingBufferAndSlot) {
275
+ if (newBuffer) {
276
+ this.updateBufferAndHandleChange(newBuffer, newSlot, accountIdString);
277
+ }
278
+ return;
279
+ }
280
+ if (newSlot < existingBufferAndSlot.slot) {
281
+ return;
282
+ }
283
+ const oldBuffer = existingBufferAndSlot.buffer;
284
+ if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
285
+ this.updateBufferAndHandleChange(newBuffer, newSlot, accountIdString);
286
+ }
287
+ }
288
+ startMonitoringForAccounts() {
289
+ // Clear any existing polling timeouts
290
+ this.clearPollingTimeouts();
291
+ // Start monitoring for each account in the accountsToMonitor set
292
+ this.accountsToMonitor.forEach((accountIdString) => {
293
+ this.startMonitoringForAccount(accountIdString);
294
+ });
295
+ }
296
+ startMonitoringForAccount(accountIdString) {
297
+ // Clear existing timeout for this account
298
+ const existingTimeout = this.pollingTimeouts.get(accountIdString);
299
+ if (existingTimeout) {
300
+ clearTimeout(existingTimeout);
301
+ }
302
+ // Set up monitoring timeout - only start polling if no WS notification in 30s
303
+ const timeoutId = setTimeout(async () => {
304
+ var _a;
305
+ // Check if we've received a WS notification for this account recently
306
+ const lastNotificationTime = this.lastWsNotificationTime.get(accountIdString) || 0;
307
+ const currentTime = Date.now();
308
+ if (!lastNotificationTime ||
309
+ currentTime - lastNotificationTime >= this.pollingIntervalMs) {
310
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
311
+ console.debug(`[${this.subscriptionName}] No recent WS notification for ${accountIdString}, adding to polling set`);
312
+ }
313
+ // No recent WS notification: add to polling and schedule debounced poll
314
+ this.accountsCurrentlyPolling.add(accountIdString);
315
+ this.scheduleDebouncedImmediatePoll();
316
+ }
317
+ else {
318
+ // We received a WS notification recently, continue monitoring
319
+ this.startMonitoringForAccount(accountIdString);
320
+ }
321
+ }, this.pollingIntervalMs);
322
+ this.pollingTimeouts.set(accountIdString, timeoutId);
323
+ }
324
+ scheduleDebouncedImmediatePoll() {
325
+ if (this.debouncedImmediatePollTimeout) {
326
+ clearTimeout(this.debouncedImmediatePollTimeout);
327
+ }
328
+ this.debouncedImmediatePollTimeout = setTimeout(async () => {
329
+ var _a;
330
+ try {
331
+ await this.pollAllAccounts();
332
+ // After the immediate poll, ensure continuous batch polling is active
333
+ if (!this.batchPollingTimeout &&
334
+ this.accountsCurrentlyPolling.size > 0) {
335
+ this.startBatchPolling();
336
+ }
337
+ }
338
+ catch (e) {
339
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
340
+ console.log(`[${this.subscriptionName}] Error during debounced immediate poll:`, e);
341
+ }
342
+ }
343
+ }, this.debouncedImmediatePollMs);
344
+ }
345
+ startBatchPolling() {
346
+ var _a;
347
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
348
+ console.debug(`[${this.subscriptionName}] Scheduling batch polling`);
349
+ }
350
+ // Clear existing batch polling timeout
351
+ if (this.batchPollingTimeout) {
352
+ clearTimeout(this.batchPollingTimeout);
353
+ }
354
+ // Set up batch polling interval
355
+ this.batchPollingTimeout = setTimeout(async () => {
356
+ await this.pollAllAccounts();
357
+ // Schedule next batch poll
358
+ this.startBatchPolling();
359
+ }, this.pollingIntervalMs);
360
+ }
361
+ async pollAllAccounts() {
362
+ var _a, _b;
363
+ try {
364
+ // Get all accounts currently being polled
365
+ const accountsToPoll = Array.from(this.accountsCurrentlyPolling);
366
+ if (accountsToPoll.length === 0) {
367
+ return;
368
+ }
369
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
370
+ console.debug(`[${this.subscriptionName}] Polling all accounts`, accountsToPoll.length, 'accounts');
371
+ }
372
+ // Use the shared batch fetch method
373
+ await this.fetchAccountsBatch(accountsToPoll);
374
+ }
375
+ catch (error) {
376
+ if ((_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.logResubMessages) {
377
+ console.log(`[${this.subscriptionName}] Error batch polling accounts:`, error);
378
+ }
379
+ }
380
+ }
381
+ /**
382
+ * Fetches and populates all monitored accounts data without checking for missed changes
383
+ * This is used during initial subscription to populate data
384
+ */
385
+ async fetchAndPopulateAllMonitoredAccounts() {
386
+ var _a;
387
+ try {
388
+ // Get all accounts currently being polled
389
+ const accountsToMonitor = Array.from(this.accountsToMonitor);
390
+ if (accountsToMonitor.length === 0) {
391
+ return;
392
+ }
393
+ // Fetch all accounts in a single batch request
394
+ const accountAddresses = accountsToMonitor.map((accountId) => accountId);
395
+ const rpcResponse = await this.rpc
396
+ .getMultipleAccounts(accountAddresses, {
397
+ commitment: this.options.commitment,
398
+ encoding: 'base64',
399
+ })
400
+ .send();
401
+ const currentSlot = Number(rpcResponse.context.slot);
402
+ // Process each account response
403
+ for (let i = 0; i < accountsToMonitor.length; i++) {
404
+ const accountIdString = accountsToMonitor[i];
405
+ const accountInfo = rpcResponse.value[i];
406
+ if (!accountInfo) {
407
+ continue;
408
+ }
409
+ const existingBufferAndSlot = this.bufferAndSlotMap.get(accountIdString);
410
+ if (!existingBufferAndSlot) {
411
+ // Account not in our map yet, add it
412
+ let newBuffer = undefined;
413
+ if (accountInfo) {
414
+ if (Array.isArray(accountInfo.data)) {
415
+ const [data, encoding] = accountInfo.data;
416
+ newBuffer = Buffer.from(data, encoding);
417
+ }
418
+ }
419
+ if (newBuffer) {
420
+ this.updateBufferAndHandleChange(newBuffer, currentSlot, accountIdString);
421
+ }
422
+ continue;
423
+ }
424
+ // For initial population, just update the slot if we have newer data
425
+ if (currentSlot > existingBufferAndSlot.slot) {
426
+ let newBuffer = undefined;
427
+ if (accountInfo.data) {
428
+ if (Array.isArray(accountInfo.data)) {
429
+ const [data, encoding] = accountInfo.data;
430
+ if (encoding === 'base58') {
431
+ newBuffer = Buffer.from(bs58_1.default.decode(data));
432
+ }
433
+ else {
434
+ newBuffer = Buffer.from(data, 'base64');
435
+ }
436
+ }
437
+ }
438
+ // Update with newer data if available
439
+ if (newBuffer) {
440
+ this.updateBufferAndHandleChange(newBuffer, currentSlot, accountIdString);
441
+ }
442
+ }
443
+ }
444
+ }
445
+ catch (error) {
446
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
447
+ console.log(`[${this.subscriptionName}] Error fetching and populating monitored accounts:`, error);
448
+ }
449
+ }
450
+ }
451
+ /**
452
+ * Fetches all monitored accounts and checks for missed changes
453
+ * Returns true if a missed change was detected and resubscription is needed
454
+ */
455
+ async fetchAllMonitoredAccounts() {
456
+ var _a, _b;
457
+ try {
458
+ // Get all accounts currently being polled
459
+ const accountsToMonitor = Array.from(this.accountsToMonitor);
460
+ if (accountsToMonitor.length === 0) {
461
+ return false;
462
+ }
463
+ // Fetch all accounts in a single batch request
464
+ const accountAddresses = accountsToMonitor.map((accountId) => accountId);
465
+ const rpcResponse = await this.rpc
466
+ .getMultipleAccounts(accountAddresses, {
467
+ commitment: this.options.commitment,
468
+ encoding: 'base64',
469
+ })
470
+ .send();
471
+ const currentSlot = Number(rpcResponse.context.slot);
472
+ // Process each account response
473
+ for (let i = 0; i < accountsToMonitor.length; i++) {
474
+ const accountIdString = accountsToMonitor[i];
475
+ const accountInfo = rpcResponse.value[i];
476
+ if (!accountInfo) {
477
+ continue;
478
+ }
479
+ const existingBufferAndSlot = this.bufferAndSlotMap.get(accountIdString);
480
+ if (!existingBufferAndSlot) {
481
+ // Account not in our map yet, add it
482
+ let newBuffer = undefined;
483
+ if (accountInfo.data) {
484
+ if (Array.isArray(accountInfo.data)) {
485
+ const [data, encoding] = accountInfo.data;
486
+ newBuffer = Buffer.from(data, encoding);
487
+ }
488
+ }
489
+ if (newBuffer) {
490
+ this.updateBufferAndHandleChange(newBuffer, currentSlot, accountIdString);
491
+ }
492
+ continue;
493
+ }
494
+ // Check if we missed an update
495
+ if (currentSlot > existingBufferAndSlot.slot) {
496
+ let newBuffer = undefined;
497
+ if (accountInfo.data) {
498
+ if (Array.isArray(accountInfo.data)) {
499
+ const [data, encoding] = accountInfo.data;
500
+ if (encoding === 'base58') {
501
+ newBuffer = Buffer.from(bs58_1.default.decode(data));
502
+ }
503
+ else {
504
+ newBuffer = Buffer.from(data, 'base64');
505
+ }
506
+ }
507
+ }
508
+ // Check if buffer has changed
509
+ if (newBuffer &&
510
+ (!existingBufferAndSlot.buffer ||
511
+ !newBuffer.equals(existingBufferAndSlot.buffer))) {
512
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
513
+ console.log(`[${this.subscriptionName}] Batch polling detected missed update for account ${accountIdString}, resubscribing`);
514
+ }
515
+ // We missed an update, return true to indicate resubscription is needed
516
+ return true;
517
+ }
518
+ }
519
+ }
520
+ // No missed changes detected
521
+ return false;
522
+ }
523
+ catch (error) {
524
+ if ((_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.logResubMessages) {
525
+ console.log(`[${this.subscriptionName}] Error batch polling accounts:`, error);
526
+ }
527
+ return false;
528
+ }
529
+ }
530
+ async fetchAccountsBatch(accountIds) {
531
+ var _a;
532
+ try {
533
+ // Chunk account IDs into groups of 100 (getMultipleAccounts limit)
534
+ const chunkSize = 100;
535
+ const chunks = [];
536
+ for (let i = 0; i < accountIds.length; i += chunkSize) {
537
+ chunks.push(accountIds.slice(i, i + chunkSize));
538
+ }
539
+ // Process all chunks concurrently
540
+ await Promise.all(chunks.map(async (chunk) => {
541
+ var _a;
542
+ const accountAddresses = chunk.map((accountId) => accountId);
543
+ const rpcResponse = await this.rpc
544
+ .getMultipleAccounts(accountAddresses, {
545
+ commitment: this.options.commitment,
546
+ encoding: 'base64',
547
+ })
548
+ .send();
549
+ const currentSlot = Number(rpcResponse.context.slot);
550
+ // Process each account response in this chunk
551
+ for (let i = 0; i < chunk.length; i++) {
552
+ const accountIdString = chunk[i];
553
+ const accountInfo = rpcResponse.value[i];
554
+ if (!accountInfo) {
555
+ continue;
556
+ }
557
+ const existingBufferAndSlot = this.bufferAndSlotMap.get(accountIdString);
558
+ if (!existingBufferAndSlot) {
559
+ // Account not in our map yet, add it
560
+ let newBuffer = undefined;
561
+ if (accountInfo.data) {
562
+ if (Array.isArray(accountInfo.data)) {
563
+ const [data, encoding] = accountInfo.data;
564
+ newBuffer = Buffer.from(data, encoding);
565
+ }
566
+ }
567
+ if (newBuffer) {
568
+ this.updateBufferAndHandleChange(newBuffer, currentSlot, accountIdString);
569
+ }
570
+ continue;
571
+ }
572
+ // Check if we missed an update
573
+ if (currentSlot > existingBufferAndSlot.slot) {
574
+ let newBuffer = undefined;
575
+ if (accountInfo.data) {
576
+ if (Array.isArray(accountInfo.data)) {
577
+ const [data, encoding] = accountInfo.data;
578
+ if (encoding === 'base58') {
579
+ newBuffer = Buffer.from(bs58_1.default.decode(data));
580
+ }
581
+ else {
582
+ newBuffer = Buffer.from(data, 'base64');
583
+ }
584
+ }
585
+ }
586
+ // Check if buffer has changed
587
+ if (newBuffer &&
588
+ (!existingBufferAndSlot.buffer ||
589
+ !newBuffer.equals(existingBufferAndSlot.buffer))) {
590
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
591
+ console.log(`[${this.subscriptionName}] Batch polling detected missed update for account ${accountIdString}, signaling resubscription`);
592
+ }
593
+ // Signal missed change instead of immediately resubscribing
594
+ this.signalMissedChange(accountIdString);
595
+ return;
596
+ }
597
+ }
598
+ }
599
+ }));
600
+ }
601
+ catch (error) {
602
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
603
+ console.log(`[${this.subscriptionName}] Error fetching accounts batch:`, error);
604
+ }
605
+ }
606
+ }
607
+ clearPollingTimeouts() {
608
+ this.pollingTimeouts.forEach((timeoutId) => {
609
+ clearTimeout(timeoutId);
610
+ });
611
+ this.pollingTimeouts.clear();
612
+ // Clear batch polling timeout
613
+ if (this.batchPollingTimeout) {
614
+ clearTimeout(this.batchPollingTimeout);
615
+ this.batchPollingTimeout = undefined;
616
+ }
617
+ // Clear initial fetch timeout
618
+ // if (this.initialFetchTimeout) {
619
+ // clearTimeout(this.initialFetchTimeout);
620
+ // this.initialFetchTimeout = undefined;
621
+ // }
622
+ // Clear resubscription timeout
623
+ if (this.resubscriptionTimeout) {
624
+ clearTimeout(this.resubscriptionTimeout);
625
+ this.resubscriptionTimeout = undefined;
626
+ }
627
+ // Clear accounts currently polling
628
+ this.accountsCurrentlyPolling.clear();
629
+ // Clear accounts pending initial monitor fetch
630
+ // this.accountsPendingInitialMonitorFetch.clear();
631
+ // Reset missed change flag and clear accounts with missed updates
632
+ this.missedChangeDetected = false;
633
+ this.accountsWithMissedUpdates.clear();
634
+ }
635
+ /**
636
+ * Centralized resubscription handler that only resubscribes once after checking all accounts
637
+ */
638
+ async handleResubscription() {
639
+ var _a;
640
+ if (this.missedChangeDetected) {
641
+ if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
642
+ console.log(`[${this.subscriptionName}] Missed change detected for ${this.accountsWithMissedUpdates.size} accounts: ${Array.from(this.accountsWithMissedUpdates).join(', ')}, resubscribing`);
643
+ }
644
+ await this.unsubscribe(true);
645
+ this.receivingData = false;
646
+ await this.subscribe(this.onChange);
647
+ this.missedChangeDetected = false;
648
+ this.accountsWithMissedUpdates.clear();
649
+ }
650
+ }
651
+ /**
652
+ * Signal that a missed change was detected and schedule resubscription
653
+ */
654
+ signalMissedChange(accountIdString) {
655
+ if (!this.missedChangeDetected) {
656
+ this.missedChangeDetected = true;
657
+ this.accountsWithMissedUpdates.add(accountIdString);
658
+ // Clear any existing resubscription timeout
659
+ if (this.resubscriptionTimeout) {
660
+ clearTimeout(this.resubscriptionTimeout);
661
+ }
662
+ // Schedule resubscription after a short delay to allow for batch processing
663
+ this.resubscriptionTimeout = setTimeout(async () => {
664
+ await this.handleResubscription();
665
+ }, 100); // 100ms delay to allow for batch processing
666
+ }
667
+ else {
668
+ // If already detected, just add the account to the set
669
+ this.accountsWithMissedUpdates.add(accountIdString);
670
+ }
671
+ }
672
+ unsubscribe(onResub = false) {
673
+ if (!onResub) {
674
+ this.resubOpts.resubTimeoutMs = undefined;
675
+ }
676
+ this.isUnsubscribing = true;
677
+ clearTimeout(this.timeoutId);
678
+ this.timeoutId = undefined;
679
+ // Clear polling timeouts
680
+ this.clearPollingTimeouts();
681
+ // Abort the WebSocket subscription
682
+ if (this.abortController) {
683
+ this.abortController.abort('unsubscribing');
684
+ this.abortController = undefined;
685
+ }
686
+ this.listenerId = undefined;
687
+ this.isUnsubscribing = false;
688
+ return Promise.resolve();
689
+ }
690
+ // Method to add accounts to the polling list
691
+ /**
692
+ * Add an account to the monitored set.
693
+ * - Monitored accounts are subject to initial fetch and periodic batch polls
694
+ * if WS notifications are not observed within `pollingIntervalMs`.
695
+ */
696
+ addAccountToMonitor(accountId) {
697
+ const accountIdString = accountId.toBase58();
698
+ this.accountsToMonitor.add(accountIdString);
699
+ // If already subscribed, start monitoring for this account
700
+ if (this.listenerId != null && !this.isUnsubscribing) {
701
+ this.startMonitoringForAccount(accountIdString);
702
+ }
703
+ }
704
+ // Method to remove accounts from the polling list
705
+ removeAccountFromMonitor(accountId) {
706
+ const accountIdString = accountId.toBase58();
707
+ this.accountsToMonitor.delete(accountIdString);
708
+ // Clear monitoring timeout for this account
709
+ const timeoutId = this.pollingTimeouts.get(accountIdString);
710
+ if (timeoutId) {
711
+ clearTimeout(timeoutId);
712
+ this.pollingTimeouts.delete(accountIdString);
713
+ }
714
+ // Remove from currently polling set if it was being polled
715
+ this.accountsCurrentlyPolling.delete(accountIdString);
716
+ // If no more accounts are being polled, stop batch polling
717
+ if (this.accountsCurrentlyPolling.size === 0 && this.batchPollingTimeout) {
718
+ clearTimeout(this.batchPollingTimeout);
719
+ this.batchPollingTimeout = undefined;
720
+ }
721
+ }
722
+ // Method to set polling interval
723
+ /**
724
+ * Set the monitoring/polling interval for monitored accounts.
725
+ * Shorter intervals detect missed updates sooner but increase RPC load.
726
+ */
727
+ setPollingInterval(intervalMs) {
728
+ this.pollingIntervalMs = intervalMs;
729
+ // Restart monitoring with new interval if already subscribed
730
+ if (this.listenerId != null && !this.isUnsubscribing) {
731
+ this.startMonitoringForAccounts();
732
+ }
733
+ }
734
+ updateBufferAndHandleChange(newBuffer, newSlot, accountIdString) {
735
+ this.bufferAndSlotMap.set(accountIdString, {
736
+ buffer: newBuffer,
737
+ slot: newSlot,
738
+ });
739
+ const account = this.decodeBuffer(this.accountDiscriminator, newBuffer);
740
+ const accountIdPubkey = new web3_js_1.PublicKey(accountIdString);
741
+ this.onChange(accountIdPubkey, account, { slot: newSlot }, newBuffer);
742
+ }
743
+ }
744
+ exports.WebSocketProgramAccountsSubscriberV2 = WebSocketProgramAccountsSubscriberV2;