@drift-labs/sdk 2.82.0-beta.5 → 2.82.0-beta.6

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 (55) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/types.d.ts +4 -0
  3. package/lib/accounts/webSocketAccountSubscriber.d.ts +3 -3
  4. package/lib/accounts/webSocketAccountSubscriber.js +15 -8
  5. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
  6. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +5 -5
  7. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +2 -2
  8. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +5 -3
  9. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
  10. package/lib/accounts/webSocketProgramAccountSubscriber.js +15 -9
  11. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
  12. package/lib/accounts/webSocketUserAccountSubscriber.js +3 -3
  13. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +3 -3
  15. package/lib/auctionSubscriber/auctionSubscriber.d.ts +2 -2
  16. package/lib/auctionSubscriber/auctionSubscriber.js +3 -3
  17. package/lib/auctionSubscriber/types.d.ts +1 -0
  18. package/lib/driftClient.js +13 -8
  19. package/lib/driftClientConfig.d.ts +1 -0
  20. package/lib/orderSubscriber/OrderSubscriber.js +6 -3
  21. package/lib/orderSubscriber/WebsocketSubscription.d.ts +4 -3
  22. package/lib/orderSubscriber/WebsocketSubscription.js +3 -3
  23. package/lib/orderSubscriber/types.d.ts +1 -0
  24. package/lib/tx/utils.js +5 -1
  25. package/lib/user.js +5 -2
  26. package/lib/userConfig.d.ts +1 -0
  27. package/lib/userMap/WebsocketSubscription.d.ts +4 -3
  28. package/lib/userMap/WebsocketSubscription.js +3 -3
  29. package/lib/userMap/userMap.js +4 -1
  30. package/lib/userMap/userMapConfig.d.ts +1 -0
  31. package/lib/userStats.js +6 -3
  32. package/lib/userStatsConfig.d.ts +1 -0
  33. package/package.json +1 -1
  34. package/src/accounts/types.ts +5 -0
  35. package/src/accounts/webSocketAccountSubscriber.ts +34 -22
  36. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +7 -6
  37. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +6 -4
  38. package/src/accounts/webSocketProgramAccountSubscriber.ts +32 -22
  39. package/src/accounts/webSocketUserAccountSubscriber.ts +5 -4
  40. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +5 -4
  41. package/src/auctionSubscriber/auctionSubscriber.ts +10 -4
  42. package/src/auctionSubscriber/types.ts +1 -0
  43. package/src/driftClient.ts +6 -1
  44. package/src/driftClientConfig.ts +1 -0
  45. package/src/orderSubscriber/OrderSubscriber.ts +4 -1
  46. package/src/orderSubscriber/WebsocketSubscription.ts +6 -5
  47. package/src/orderSubscriber/types.ts +1 -0
  48. package/src/tx/utils.ts +5 -1
  49. package/src/user.ts +4 -1
  50. package/src/userConfig.ts +1 -0
  51. package/src/userMap/WebsocketSubscription.ts +6 -5
  52. package/src/userMap/userMap.ts +4 -1
  53. package/src/userMap/userMapConfig.ts +1 -0
  54. package/src/userStats.ts +4 -1
  55. package/src/userStatsConfig.ts +1 -0
package/lib/tx/utils.js CHANGED
@@ -30,7 +30,11 @@ async function getSignedTransactionMap(wallet, txsToSign, keys) {
30
30
  keysWithTx.push(keys[index]);
31
31
  }
32
32
  });
33
- const signedTxs = await wallet.signAllTransactions(txsToSign.filter((tx) => tx !== undefined));
33
+ const signedTxs = await wallet.signAllTransactions(txsToSign
34
+ .map((tx) => {
35
+ return tx;
36
+ })
37
+ .filter((tx) => tx !== undefined));
34
38
  signedTxs.forEach((signedTx, index) => {
35
39
  signedTxMap[keysWithTx[index]] = signedTx;
36
40
  });
package/lib/user.js CHANGED
@@ -22,7 +22,7 @@ class User {
22
22
  this._isSubscribed = val;
23
23
  }
24
24
  constructor(config) {
25
- var _a, _b, _c, _d;
25
+ var _a, _b, _c, _d, _e;
26
26
  this._isSubscribed = false;
27
27
  this.driftClient = config.driftClient;
28
28
  this.userAccountPublicKey = config.userAccountPublicKey;
@@ -33,7 +33,10 @@ class User {
33
33
  this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
34
34
  }
35
35
  else {
36
- this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey, (_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs, (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.commitment);
36
+ this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey, {
37
+ resubTimeoutMs: (_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs,
38
+ logResubMessages: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.logResubMessages,
39
+ }, (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.commitment);
37
40
  }
38
41
  this.eventEmitter = this.accountSubscriber.eventEmitter;
39
42
  }
@@ -10,6 +10,7 @@ export type UserConfig = {
10
10
  export type UserSubscriptionConfig = {
11
11
  type: 'websocket';
12
12
  resubTimeoutMs?: number;
13
+ logResubMessages?: boolean;
13
14
  commitment?: Commitment;
14
15
  } | {
15
16
  type: 'polling';
@@ -2,19 +2,20 @@
2
2
  import { UserMap } from './userMap';
3
3
  import { UserAccount } from '../types';
4
4
  import { Commitment } from '@solana/web3.js';
5
+ import { ResubOpts } from '../accounts/types';
5
6
  export declare class WebsocketSubscription {
6
7
  private userMap;
7
8
  private commitment;
8
9
  private skipInitialLoad;
9
- private resubTimeoutMs?;
10
+ private resubOpts?;
10
11
  private includeIdle?;
11
12
  private decodeFn;
12
13
  private subscriber;
13
- constructor({ userMap, commitment, skipInitialLoad, resubTimeoutMs, includeIdle, decodeFn, }: {
14
+ constructor({ userMap, commitment, skipInitialLoad, resubOpts, includeIdle, decodeFn, }: {
14
15
  userMap: UserMap;
15
16
  commitment: Commitment;
16
17
  skipInitialLoad?: boolean;
17
- resubTimeoutMs?: number;
18
+ resubOpts?: ResubOpts;
18
19
  includeIdle?: boolean;
19
20
  decodeFn: (name: string, data: Buffer) => UserAccount;
20
21
  });
@@ -4,11 +4,11 @@ exports.WebsocketSubscription = void 0;
4
4
  const memcmp_1 = require("../memcmp");
5
5
  const webSocketProgramAccountSubscriber_1 = require("../accounts/webSocketProgramAccountSubscriber");
6
6
  class WebsocketSubscription {
7
- constructor({ userMap, commitment, skipInitialLoad = false, resubTimeoutMs, includeIdle = false, decodeFn, }) {
7
+ constructor({ userMap, commitment, skipInitialLoad = false, resubOpts, includeIdle = false, decodeFn, }) {
8
8
  this.userMap = userMap;
9
9
  this.commitment = commitment;
10
10
  this.skipInitialLoad = skipInitialLoad;
11
- this.resubTimeoutMs = resubTimeoutMs;
11
+ this.resubOpts = resubOpts;
12
12
  this.includeIdle = includeIdle || false;
13
13
  this.decodeFn = decodeFn;
14
14
  }
@@ -21,7 +21,7 @@ class WebsocketSubscription {
21
21
  this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('UserMap', 'User', this.userMap.driftClient.program, this.decodeFn, {
22
22
  filters,
23
23
  commitment: this.commitment,
24
- }, this.resubTimeoutMs);
24
+ }, this.resubOpts);
25
25
  }
26
26
  await this.subscriber.subscribe((accountId, account, context) => {
27
27
  const userKey = accountId.toBase58();
@@ -56,7 +56,10 @@ class UserMap {
56
56
  this.subscription = new WebsocketSubscription_1.WebsocketSubscription({
57
57
  userMap: this,
58
58
  commitment: this.commitment,
59
- resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
59
+ resubOpts: {
60
+ resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
61
+ logResubMessages: config.subscriptionConfig.logResubMessages,
62
+ },
60
63
  skipInitialLoad: config.skipInitialLoad,
61
64
  decodeFn,
62
65
  });
@@ -13,6 +13,7 @@ export type UserMapConfig = {
13
13
  } | {
14
14
  type: 'websocket';
15
15
  resubTimeoutMs?: number;
16
+ logResubMessages?: boolean;
16
17
  commitment?: Commitment;
17
18
  };
18
19
  skipInitialLoad?: boolean;
package/lib/userStats.js CHANGED
@@ -7,17 +7,20 @@ const webSocketUserStatsAccountSubsriber_1 = require("./accounts/webSocketUserSt
7
7
  const pda_1 = require("./addresses/pda");
8
8
  class UserStats {
9
9
  constructor(config) {
10
- var _a, _b, _c;
10
+ var _a, _b, _c, _d, _e;
11
11
  this.driftClient = config.driftClient;
12
12
  this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
13
13
  if (((_a = config.accountSubscription) === null || _a === void 0 ? void 0 : _a.type) === 'polling') {
14
14
  this.accountSubscriber = new pollingUserStatsAccountSubscriber_1.PollingUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey, config.accountSubscription.accountLoader);
15
15
  }
16
16
  else if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'websocket') {
17
- this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey, config.accountSubscription.resubTimeoutMs, config.accountSubscription.commitment);
17
+ this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey, {
18
+ resubTimeoutMs: (_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs,
19
+ logResubMessages: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.logResubMessages,
20
+ }, config.accountSubscription.commitment);
18
21
  }
19
22
  else {
20
- throw new Error(`Unknown user stats account subscription type: ${(_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type}`);
23
+ throw new Error(`Unknown user stats account subscription type: ${(_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.type}`);
21
24
  }
22
25
  }
23
26
  async subscribe(userStatsAccount) {
@@ -9,6 +9,7 @@ export type UserStatsConfig = {
9
9
  export type UserStatsSubscriptionConfig = {
10
10
  type: 'websocket';
11
11
  resubTimeoutMs?: number;
12
+ logResubMessages?: boolean;
12
13
  commitment?: Commitment;
13
14
  } | {
14
15
  type: 'polling';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.82.0-beta.5",
3
+ "version": "2.82.0-beta.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -184,6 +184,11 @@ export type DataAndSlot<T> = {
184
184
  slot: number;
185
185
  };
186
186
 
187
+ export type ResubOpts = {
188
+ resubTimeoutMs?: number;
189
+ logResubMessages?: boolean;
190
+ };
191
+
187
192
  export interface UserStatsAccountEvents {
188
193
  userStatsAccountUpdate: (payload: UserStatsAccount) => void;
189
194
  update: void;
@@ -1,4 +1,9 @@
1
- import { DataAndSlot, BufferAndSlot, AccountSubscriber } from './types';
1
+ import {
2
+ DataAndSlot,
3
+ BufferAndSlot,
4
+ AccountSubscriber,
5
+ ResubOpts,
6
+ } from './types';
2
7
  import { AnchorProvider, Program } from '@coral-xyz/anchor';
3
8
  import { AccountInfo, Commitment, Context, PublicKey } from '@solana/web3.js';
4
9
  import { capitalize } from './utils';
@@ -13,7 +18,9 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
13
18
  decodeBufferFn: (buffer: Buffer) => T;
14
19
  onChange: (data: T) => void;
15
20
  listenerId?: number;
16
- resubTimeoutMs?: number;
21
+
22
+ resubOpts?: ResubOpts;
23
+
17
24
  commitment?: Commitment;
18
25
  isUnsubscribing = false;
19
26
 
@@ -26,15 +33,15 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
26
33
  program: Program,
27
34
  accountPublicKey: PublicKey,
28
35
  decodeBuffer?: (buffer: Buffer) => T,
29
- resubTimeoutMs?: number,
36
+ resubOpts?: ResubOpts,
30
37
  commitment?: Commitment
31
38
  ) {
32
39
  this.accountName = accountName;
33
40
  this.program = program;
34
41
  this.accountPublicKey = accountPublicKey;
35
42
  this.decodeBufferFn = decodeBuffer;
36
- this.resubTimeoutMs = resubTimeoutMs;
37
- if (this.resubTimeoutMs < 1000) {
43
+ this.resubOpts = resubOpts;
44
+ if (this.resubOpts?.resubTimeoutMs < 1000) {
38
45
  console.log(
39
46
  'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
40
47
  );
@@ -57,7 +64,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
57
64
  this.listenerId = this.program.provider.connection.onAccountChange(
58
65
  this.accountPublicKey,
59
66
  (accountInfo, context) => {
60
- if (this.resubTimeoutMs) {
67
+ if (this.resubOpts?.resubTimeoutMs) {
61
68
  this.receivingData = true;
62
69
  clearTimeout(this.timeoutId);
63
70
  this.handleRpcResponse(context, accountInfo);
@@ -69,7 +76,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
69
76
  this.commitment
70
77
  );
71
78
 
72
- if (this.resubTimeoutMs) {
79
+ if (this.resubOpts?.resubTimeoutMs) {
73
80
  this.receivingData = true;
74
81
  this.setTimeout();
75
82
  }
@@ -91,21 +98,26 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
91
98
  if (!this.onChange) {
92
99
  throw new Error('onChange callback function must be set');
93
100
  }
94
- this.timeoutId = setTimeout(async () => {
95
- if (this.isUnsubscribing) {
96
- // If we are in the process of unsubscribing, do not attempt to resubscribe
97
- return;
98
- }
101
+ this.timeoutId = setTimeout(
102
+ async () => {
103
+ if (this.isUnsubscribing) {
104
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
105
+ return;
106
+ }
99
107
 
100
- if (this.receivingData) {
101
- console.log(
102
- `No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`
103
- );
104
- await this.unsubscribe(true);
105
- this.receivingData = false;
106
- await this.subscribe(this.onChange);
107
- }
108
- }, this.resubTimeoutMs);
108
+ if (this.receivingData) {
109
+ if (this.resubOpts?.logResubMessages) {
110
+ console.log(
111
+ `No ws data from ${this.accountName} in ${this.resubOpts.resubTimeoutMs}ms, resubscribing`
112
+ );
113
+ }
114
+ await this.unsubscribe(true);
115
+ this.receivingData = false;
116
+ await this.subscribe(this.onChange);
117
+ }
118
+ },
119
+ this.resubOpts?.resubTimeoutMs
120
+ );
109
121
  }
110
122
 
111
123
  async fetch(): Promise<void> {
@@ -172,7 +184,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
172
184
 
173
185
  unsubscribe(onResub = false): Promise<void> {
174
186
  if (!onResub) {
175
- this.resubTimeoutMs = undefined;
187
+ this.resubOpts.resubTimeoutMs = undefined;
176
188
  }
177
189
  this.isUnsubscribing = true;
178
190
  clearTimeout(this.timeoutId);
@@ -2,6 +2,7 @@ import {
2
2
  DriftClientAccountSubscriber,
3
3
  DriftClientAccountEvents,
4
4
  DataAndSlot,
5
+ ResubOpts,
5
6
  } from './types';
6
7
  import { AccountSubscriber, NotSubscribedError } from './types';
7
8
  import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
@@ -32,7 +33,7 @@ export class WebSocketDriftClientAccountSubscriber
32
33
  oracleInfos: OracleInfo[];
33
34
  oracleClientCache = new OracleClientCache();
34
35
 
35
- resubTimeoutMs?: number;
36
+ resubOpts?: ResubOpts;
36
37
  shouldFindAllMarketsAndOracles: boolean;
37
38
 
38
39
  eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
@@ -59,7 +60,7 @@ export class WebSocketDriftClientAccountSubscriber
59
60
  spotMarketIndexes: number[],
60
61
  oracleInfos: OracleInfo[],
61
62
  shouldFindAllMarketsAndOracles: boolean,
62
- resubTimeoutMs?: number,
63
+ resubOpts?: ResubOpts,
63
64
  commitment?: Commitment
64
65
  ) {
65
66
  this.isSubscribed = false;
@@ -69,7 +70,7 @@ export class WebSocketDriftClientAccountSubscriber
69
70
  this.spotMarketIndexes = spotMarketIndexes;
70
71
  this.oracleInfos = oracleInfos;
71
72
  this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
72
- this.resubTimeoutMs = resubTimeoutMs;
73
+ this.resubOpts = resubOpts;
73
74
  this.commitment = commitment;
74
75
  }
75
76
 
@@ -152,7 +153,7 @@ export class WebSocketDriftClientAccountSubscriber
152
153
  this.program,
153
154
  perpMarketPublicKey,
154
155
  undefined,
155
- this.resubTimeoutMs,
156
+ this.resubOpts,
156
157
  this.commitment
157
158
  );
158
159
  await accountSubscriber.subscribe((data: PerpMarketAccount) => {
@@ -180,7 +181,7 @@ export class WebSocketDriftClientAccountSubscriber
180
181
  this.program,
181
182
  marketPublicKey,
182
183
  undefined,
183
- this.resubTimeoutMs,
184
+ this.resubOpts,
184
185
  this.commitment
185
186
  );
186
187
  await accountSubscriber.subscribe((data: SpotMarketAccount) => {
@@ -214,7 +215,7 @@ export class WebSocketDriftClientAccountSubscriber
214
215
  (buffer: Buffer) => {
215
216
  return client.getOraclePriceDataFromBuffer(buffer);
216
217
  },
217
- this.resubTimeoutMs,
218
+ this.resubOpts,
218
219
  this.commitment
219
220
  );
220
221
 
@@ -16,7 +16,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
16
16
  implements InsuranceFundStakeAccountSubscriber
17
17
  {
18
18
  isSubscribed: boolean;
19
- reconnectTimeoutMs?: number;
19
+ resubTimeoutMs?: number;
20
20
  commitment?: Commitment;
21
21
  program: Program;
22
22
  eventEmitter: StrictEventEmitter<
@@ -30,7 +30,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
30
30
  public constructor(
31
31
  program: Program,
32
32
  insuranceFundStakeAccountPublicKey: PublicKey,
33
- reconnectTimeoutMs?: number,
33
+ resubTimeoutMs?: number,
34
34
  commitment?: Commitment
35
35
  ) {
36
36
  this.isSubscribed = false;
@@ -38,7 +38,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
38
38
  this.insuranceFundStakeAccountPublicKey =
39
39
  insuranceFundStakeAccountPublicKey;
40
40
  this.eventEmitter = new EventEmitter();
41
- this.reconnectTimeoutMs = reconnectTimeoutMs;
41
+ this.resubTimeoutMs = resubTimeoutMs;
42
42
  this.commitment = commitment;
43
43
  }
44
44
 
@@ -55,7 +55,9 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
55
55
  this.program,
56
56
  this.insuranceFundStakeAccountPublicKey,
57
57
  undefined,
58
- this.reconnectTimeoutMs,
58
+ {
59
+ resubTimeoutMs: this.resubTimeoutMs,
60
+ },
59
61
  this.commitment
60
62
  );
61
63
 
@@ -1,4 +1,9 @@
1
- import { DataAndSlot, BufferAndSlot, ProgramAccountSubscriber } from './types';
1
+ import {
2
+ DataAndSlot,
3
+ BufferAndSlot,
4
+ ProgramAccountSubscriber,
5
+ ResubOpts,
6
+ } from './types';
2
7
  import { AnchorProvider, Program } from '@coral-xyz/anchor';
3
8
  import {
4
9
  Commitment,
@@ -25,7 +30,7 @@ export class WebSocketProgramAccountSubscriber<T>
25
30
  buffer: Buffer
26
31
  ) => void;
27
32
  listenerId?: number;
28
- resubTimeoutMs?: number;
33
+ resubOpts?: ResubOpts;
29
34
  isUnsubscribing = false;
30
35
  timeoutId?: NodeJS.Timeout;
31
36
  options: { filters: MemcmpFilter[]; commitment?: Commitment };
@@ -40,14 +45,14 @@ export class WebSocketProgramAccountSubscriber<T>
40
45
  options: { filters: MemcmpFilter[]; commitment?: Commitment } = {
41
46
  filters: [],
42
47
  },
43
- resubTimeoutMs?: number
48
+ resubOpts?: ResubOpts
44
49
  ) {
45
50
  this.subscriptionName = subscriptionName;
46
51
  this.accountDiscriminator = accountDiscriminator;
47
52
  this.program = program;
48
53
  this.decodeBuffer = decodeBufferFn;
49
- this.resubTimeoutMs = resubTimeoutMs;
50
- if (this.resubTimeoutMs < 1000) {
54
+ this.resubOpts = resubOpts;
55
+ if (this.resubOpts?.resubTimeoutMs < 1000) {
51
56
  console.log(
52
57
  'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
53
58
  );
@@ -73,7 +78,7 @@ export class WebSocketProgramAccountSubscriber<T>
73
78
  this.listenerId = this.program.provider.connection.onProgramAccountChange(
74
79
  this.program.programId,
75
80
  (keyedAccountInfo, context) => {
76
- if (this.resubTimeoutMs) {
81
+ if (this.resubOpts?.resubTimeoutMs) {
77
82
  this.receivingData = true;
78
83
  clearTimeout(this.timeoutId);
79
84
  this.handleRpcResponse(context, keyedAccountInfo);
@@ -87,7 +92,7 @@ export class WebSocketProgramAccountSubscriber<T>
87
92
  this.options.filters
88
93
  );
89
94
 
90
- if (this.resubTimeoutMs) {
95
+ if (this.resubOpts?.resubTimeoutMs) {
91
96
  this.receivingData = true;
92
97
  this.setTimeout();
93
98
  }
@@ -97,21 +102,26 @@ export class WebSocketProgramAccountSubscriber<T>
97
102
  if (!this.onChange) {
98
103
  throw new Error('onChange callback function must be set');
99
104
  }
100
- this.timeoutId = setTimeout(async () => {
101
- if (this.isUnsubscribing) {
102
- // If we are in the process of unsubscribing, do not attempt to resubscribe
103
- return;
104
- }
105
+ this.timeoutId = setTimeout(
106
+ async () => {
107
+ if (this.isUnsubscribing) {
108
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
109
+ return;
110
+ }
105
111
 
106
- if (this.receivingData) {
107
- console.log(
108
- `No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`
109
- );
110
- await this.unsubscribe(true);
111
- this.receivingData = false;
112
- await this.subscribe(this.onChange);
113
- }
114
- }, this.resubTimeoutMs);
112
+ if (this.receivingData) {
113
+ if (this.resubOpts?.logResubMessages) {
114
+ console.log(
115
+ `No ws data from ${this.subscriptionName} in ${this.resubOpts?.resubTimeoutMs}ms, resubscribing`
116
+ );
117
+ }
118
+ await this.unsubscribe(true);
119
+ this.receivingData = false;
120
+ await this.subscribe(this.onChange);
121
+ }
122
+ },
123
+ this.resubOpts?.resubTimeoutMs
124
+ );
115
125
  }
116
126
 
117
127
  handleRpcResponse(
@@ -163,7 +173,7 @@ export class WebSocketProgramAccountSubscriber<T>
163
173
 
164
174
  unsubscribe(onResub = false): Promise<void> {
165
175
  if (!onResub) {
166
- this.resubTimeoutMs = undefined;
176
+ this.resubOpts.resubTimeoutMs = undefined;
167
177
  }
168
178
  this.isUnsubscribing = true;
169
179
  clearTimeout(this.timeoutId);
@@ -4,6 +4,7 @@ import {
4
4
  NotSubscribedError,
5
5
  UserAccountEvents,
6
6
  UserAccountSubscriber,
7
+ ResubOpts,
7
8
  } from './types';
8
9
  import { Program } from '@coral-xyz/anchor';
9
10
  import StrictEventEmitter from 'strict-event-emitter-types';
@@ -14,7 +15,7 @@ import { UserAccount } from '../types';
14
15
 
15
16
  export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
16
17
  isSubscribed: boolean;
17
- reconnectTimeoutMs?: number;
18
+ resubOpts?: ResubOpts;
18
19
  commitment?: Commitment;
19
20
  program: Program;
20
21
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
@@ -25,14 +26,14 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
25
26
  public constructor(
26
27
  program: Program,
27
28
  userAccountPublicKey: PublicKey,
28
- reconnectTimeoutMs?: number,
29
+ resubOpts?: ResubOpts,
29
30
  commitment?: Commitment
30
31
  ) {
31
32
  this.isSubscribed = false;
32
33
  this.program = program;
34
+ this.resubOpts = resubOpts;
33
35
  this.userAccountPublicKey = userAccountPublicKey;
34
36
  this.eventEmitter = new EventEmitter();
35
- this.reconnectTimeoutMs = reconnectTimeoutMs;
36
37
  this.commitment = commitment;
37
38
  }
38
39
 
@@ -46,7 +47,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
46
47
  this.program,
47
48
  this.userAccountPublicKey,
48
49
  undefined,
49
- this.reconnectTimeoutMs,
50
+ this.resubOpts,
50
51
  this.commitment
51
52
  );
52
53
 
@@ -4,6 +4,7 @@ import {
4
4
  NotSubscribedError,
5
5
  UserStatsAccountSubscriber,
6
6
  UserStatsAccountEvents,
7
+ ResubOpts,
7
8
  } from './types';
8
9
  import { Program } from '@coral-xyz/anchor';
9
10
  import StrictEventEmitter from 'strict-event-emitter-types';
@@ -16,7 +17,7 @@ export class WebSocketUserStatsAccountSubscriber
16
17
  implements UserStatsAccountSubscriber
17
18
  {
18
19
  isSubscribed: boolean;
19
- reconnectTimeoutMs?: number;
20
+ resubOpts?: ResubOpts;
20
21
  commitment?: Commitment;
21
22
  program: Program;
22
23
  eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
@@ -27,14 +28,14 @@ export class WebSocketUserStatsAccountSubscriber
27
28
  public constructor(
28
29
  program: Program,
29
30
  userStatsAccountPublicKey: PublicKey,
30
- reconnectTimeoutMs?: number,
31
+ resubOpts?: ResubOpts,
31
32
  commitment?: Commitment
32
33
  ) {
33
34
  this.isSubscribed = false;
34
35
  this.program = program;
35
36
  this.userStatsAccountPublicKey = userStatsAccountPublicKey;
36
37
  this.eventEmitter = new EventEmitter();
37
- this.reconnectTimeoutMs = reconnectTimeoutMs;
38
+ this.resubOpts = resubOpts;
38
39
  this.commitment = commitment;
39
40
  }
40
41
 
@@ -48,7 +49,7 @@ export class WebSocketUserStatsAccountSubscriber
48
49
  this.program,
49
50
  this.userStatsAccountPublicKey,
50
51
  undefined,
51
- this.reconnectTimeoutMs,
52
+ this.resubOpts,
52
53
  this.commitment
53
54
  );
54
55
 
@@ -6,20 +6,26 @@ import { EventEmitter } from 'events';
6
6
  import { UserAccount } from '../types';
7
7
  import { ConfirmOptions, Context, PublicKey } from '@solana/web3.js';
8
8
  import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
9
+ import { ResubOpts } from '../accounts/types';
9
10
 
10
11
  export class AuctionSubscriber {
11
12
  private driftClient: DriftClient;
12
13
  private opts: ConfirmOptions;
13
- private resubTimeoutMs?: number;
14
+ private resubOpts?: ResubOpts;
14
15
 
15
16
  eventEmitter: StrictEventEmitter<EventEmitter, AuctionSubscriberEvents>;
16
17
  private subscriber: WebSocketProgramAccountSubscriber<UserAccount>;
17
18
 
18
- constructor({ driftClient, opts, resubTimeoutMs }: AuctionSubscriberConfig) {
19
+ constructor({
20
+ driftClient,
21
+ opts,
22
+ resubTimeoutMs,
23
+ logResubMessages,
24
+ }: AuctionSubscriberConfig) {
19
25
  this.driftClient = driftClient;
20
26
  this.opts = opts || this.driftClient.opts;
21
27
  this.eventEmitter = new EventEmitter();
22
- this.resubTimeoutMs = resubTimeoutMs;
28
+ this.resubOpts = { resubTimeoutMs, logResubMessages };
23
29
  }
24
30
 
25
31
  public async subscribe() {
@@ -35,7 +41,7 @@ export class AuctionSubscriber {
35
41
  filters: [getUserFilter(), getUserWithAuctionFilter()],
36
42
  commitment: this.opts.commitment,
37
43
  },
38
- this.resubTimeoutMs
44
+ this.resubOpts
39
45
  );
40
46
  }
41
47
 
@@ -6,6 +6,7 @@ export type AuctionSubscriberConfig = {
6
6
  driftClient: DriftClient;
7
7
  opts?: ConfirmOptions;
8
8
  resubTimeoutMs?: number;
9
+ logResubMessages?: boolean;
9
10
  };
10
11
 
11
12
  export interface AuctionSubscriberEvents {
@@ -251,11 +251,13 @@ export class DriftClient {
251
251
  this.userAccountSubscriptionConfig = {
252
252
  type: 'websocket',
253
253
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
254
+ logResubMessages: config.accountSubscription?.logResubMessages,
254
255
  commitment: config.accountSubscription?.commitment,
255
256
  };
256
257
  this.userStatsAccountSubscriptionConfig = {
257
258
  type: 'websocket',
258
259
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
260
+ logResubMessages: config.accountSubscription?.logResubMessages,
259
261
  commitment: config.accountSubscription?.commitment,
260
262
  };
261
263
  }
@@ -298,7 +300,10 @@ export class DriftClient {
298
300
  config.spotMarketIndexes ?? [],
299
301
  config.oracleInfos ?? [],
300
302
  noMarketsAndOraclesSpecified,
301
- config.accountSubscription?.resubTimeoutMs,
303
+ {
304
+ resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
305
+ logResubMessages: config.accountSubscription?.logResubMessages,
306
+ },
302
307
  config.accountSubscription?.commitment
303
308
  );
304
309
  }
@@ -39,6 +39,7 @@ export type DriftClientSubscriptionConfig =
39
39
  | {
40
40
  type: 'websocket';
41
41
  resubTimeoutMs?: number;
42
+ logResubMessages?: boolean;
42
43
  commitment?: Commitment;
43
44
  }
44
45
  | {
@@ -39,7 +39,10 @@ export class OrderSubscriber {
39
39
  orderSubscriber: this,
40
40
  commitment: this.commitment,
41
41
  skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
42
- resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
42
+ resubOpts: {
43
+ resubTimeoutMs: config.subscriptionConfig?.resubTimeoutMs,
44
+ logResubMessages: config.subscriptionConfig?.logResubMessages,
45
+ },
43
46
  resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
44
47
  decoded: config.decodeData,
45
48
  });