@drift-labs/sdk 2.48.0-beta.15 → 2.48.0-beta.17

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.48.0-beta.15
1
+ 2.48.0-beta.17
@@ -139,7 +139,7 @@ class BulkAccountLoader {
139
139
  accountsToLoad.forEach((accountToLoad, j) => {
140
140
  const key = accountToLoad.publicKey.toBase58();
141
141
  const oldRPCResponse = this.bufferAndSlotMap.get(key);
142
- if (oldRPCResponse && newSlot <= oldRPCResponse.slot) {
142
+ if (oldRPCResponse && newSlot < oldRPCResponse.slot) {
143
143
  return;
144
144
  }
145
145
  let newBuffer = undefined;
@@ -89,7 +89,7 @@ class WebSocketAccountSubscriber {
89
89
  }
90
90
  return;
91
91
  }
92
- if (newSlot <= this.bufferAndSlot.slot) {
92
+ if (newSlot < this.bufferAndSlot.slot) {
93
93
  return;
94
94
  }
95
95
  const oldBuffer = this.bufferAndSlot.buffer;
@@ -75,7 +75,7 @@ class WebSocketProgramAccountSubscriber {
75
75
  }
76
76
  return;
77
77
  }
78
- if (newSlot <= this.bufferAndSlot.slot) {
78
+ if (newSlot < this.bufferAndSlot.slot) {
79
79
  return;
80
80
  }
81
81
  const oldBuffer = this.bufferAndSlot.buffer;
@@ -51,7 +51,7 @@ class WebSocketUserAccountSubscriber {
51
51
  updateData(userAccount, slot) {
52
52
  var _a;
53
53
  const currentDataSlot = ((_a = this.userDataAccountSubscriber.dataAndSlot) === null || _a === void 0 ? void 0 : _a.slot) || 0;
54
- if (currentDataSlot < slot) {
54
+ if (currentDataSlot <= slot) {
55
55
  this.userDataAccountSubscriber.setData(userAccount, slot);
56
56
  this.eventEmitter.emit('userAccountUpdate', userAccount);
57
57
  this.eventEmitter.emit('update');
@@ -27,7 +27,7 @@ class EventSubscriber {
27
27
  }
28
28
  this.eventEmitter = new events_1.EventEmitter();
29
29
  if (this.options.logProviderConfig.type === 'websocket') {
30
- this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.address, this.options.commitment);
30
+ this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.address, this.options.commitment, this.options.resubTimeoutMs);
31
31
  }
32
32
  else {
33
33
  this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.address, options.commitment, this.options.logProviderConfig.frequency, this.options.logProviderConfig.batchSize);
@@ -10,6 +10,7 @@ export type EventSubscriptionOptions = {
10
10
  maxTx?: number;
11
11
  logProviderConfig?: LogProviderConfig;
12
12
  untilTx?: TransactionSignature;
13
+ resubTimeoutMs?: number;
13
14
  };
14
15
  export declare const DefaultEventSubscriptionOptions: EventSubscriptionOptions;
15
16
  export type EventSubscriptionOrderBy = 'blockchain' | 'client';
@@ -4,9 +4,15 @@ export declare class WebSocketLogProvider implements LogProvider {
4
4
  private connection;
5
5
  private address;
6
6
  private commitment;
7
+ private resubTimeoutMs?;
7
8
  private subscriptionId;
8
- constructor(connection: Connection, address: PublicKey, commitment: Commitment);
9
+ private isUnsubscribing;
10
+ private receivingData;
11
+ private timeoutId?;
12
+ private callback?;
13
+ constructor(connection: Connection, address: PublicKey, commitment: Commitment, resubTimeoutMs?: number);
9
14
  subscribe(callback: logProviderCallback): boolean;
10
15
  isSubscribed(): boolean;
11
16
  unsubscribe(): Promise<boolean>;
17
+ private setTimeout;
12
18
  }
@@ -2,29 +2,70 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WebSocketLogProvider = void 0;
4
4
  class WebSocketLogProvider {
5
- constructor(connection, address, commitment) {
5
+ constructor(connection, address, commitment, resubTimeoutMs) {
6
6
  this.connection = connection;
7
7
  this.address = address;
8
8
  this.commitment = commitment;
9
+ this.resubTimeoutMs = resubTimeoutMs;
10
+ this.isUnsubscribing = false;
11
+ this.receivingData = false;
9
12
  }
10
13
  subscribe(callback) {
11
14
  if (this.subscriptionId) {
12
15
  return true;
13
16
  }
17
+ this.callback = callback;
14
18
  this.subscriptionId = this.connection.onLogs(this.address, (logs, ctx) => {
19
+ if (this.resubTimeoutMs && !this.isUnsubscribing) {
20
+ this.receivingData = true;
21
+ clearTimeout(this.timeoutId);
22
+ this.setTimeout();
23
+ }
15
24
  callback(logs.signature, ctx.slot, logs.logs, undefined);
16
25
  }, this.commitment);
26
+ if (this.resubTimeoutMs) {
27
+ this.setTimeout();
28
+ }
17
29
  return true;
18
30
  }
19
31
  isSubscribed() {
20
32
  return this.subscriptionId !== undefined;
21
33
  }
22
34
  async unsubscribe() {
35
+ this.isUnsubscribing = true;
36
+ clearTimeout(this.timeoutId);
23
37
  if (this.subscriptionId !== undefined) {
24
- await this.connection.removeOnLogsListener(this.subscriptionId);
25
- this.subscriptionId = undefined;
38
+ this.connection
39
+ .removeOnLogsListener(this.subscriptionId)
40
+ .then(() => {
41
+ this.subscriptionId = undefined;
42
+ this.isUnsubscribing = false;
43
+ return true;
44
+ })
45
+ .catch((err) => {
46
+ console.log('Error unsubscribing from logs: ', err);
47
+ this.isUnsubscribing = false;
48
+ return false;
49
+ });
26
50
  }
27
- return true;
51
+ else {
52
+ this.isUnsubscribing = false;
53
+ return true;
54
+ }
55
+ }
56
+ setTimeout() {
57
+ this.timeoutId = setTimeout(async () => {
58
+ if (this.isUnsubscribing) {
59
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
60
+ return;
61
+ }
62
+ if (this.receivingData) {
63
+ console.log(`No log data in ${this.resubTimeoutMs}ms, resubscribing`);
64
+ await this.unsubscribe();
65
+ this.receivingData = false;
66
+ this.subscribe(this.callback);
67
+ }
68
+ }, this.resubTimeoutMs);
28
69
  }
29
70
  }
30
71
  exports.WebSocketLogProvider = WebSocketLogProvider;
@@ -77,7 +77,7 @@ class OrderSubscriber {
77
77
  this.mostRecentSlot = slot;
78
78
  }
79
79
  const slotAndUserAccount = this.usersAccounts.get(key);
80
- if (!slotAndUserAccount || slotAndUserAccount.slot < slot) {
80
+ if (!slotAndUserAccount || slotAndUserAccount.slot <= slot) {
81
81
  let userAccount;
82
82
  // Polling leads to a lot of redundant decoding, so we only decode if data is from a fresh slot
83
83
  if (dataType === 'raw') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.48.0-beta.15",
3
+ "version": "2.48.0-beta.17",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -193,7 +193,7 @@ export class BulkAccountLoader {
193
193
  const key = accountToLoad.publicKey.toBase58();
194
194
  const oldRPCResponse = this.bufferAndSlotMap.get(key);
195
195
 
196
- if (oldRPCResponse && newSlot <= oldRPCResponse.slot) {
196
+ if (oldRPCResponse && newSlot < oldRPCResponse.slot) {
197
197
  return;
198
198
  }
199
199
 
@@ -134,7 +134,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
134
134
  return;
135
135
  }
136
136
 
137
- if (newSlot <= this.bufferAndSlot.slot) {
137
+ if (newSlot < this.bufferAndSlot.slot) {
138
138
  return;
139
139
  }
140
140
 
@@ -125,7 +125,7 @@ export class WebSocketProgramAccountSubscriber<T>
125
125
  return;
126
126
  }
127
127
 
128
- if (newSlot <= this.bufferAndSlot.slot) {
128
+ if (newSlot < this.bufferAndSlot.slot) {
129
129
  return;
130
130
  }
131
131
 
@@ -94,7 +94,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
94
94
  public updateData(userAccount: UserAccount, slot: number) {
95
95
  const currentDataSlot =
96
96
  this.userDataAccountSubscriber.dataAndSlot?.slot || 0;
97
- if (currentDataSlot < slot) {
97
+ if (currentDataSlot <= slot) {
98
98
  this.userDataAccountSubscriber.setData(userAccount, slot);
99
99
  this.eventEmitter.emit('userAccountUpdate', userAccount);
100
100
  this.eventEmitter.emit('update');
@@ -56,7 +56,8 @@ export class EventSubscriber {
56
56
  this.logProvider = new WebSocketLogProvider(
57
57
  this.connection,
58
58
  this.address,
59
- this.options.commitment
59
+ this.options.commitment,
60
+ this.options.resubTimeoutMs
60
61
  );
61
62
  } else {
62
63
  this.logProvider = new PollingLogProvider(
@@ -28,6 +28,7 @@ export type EventSubscriptionOptions = {
28
28
  // when the subscription starts, client might want to backtrack and fetch old tx's
29
29
  // this specifies how far to backtrack
30
30
  untilTx?: TransactionSignature;
31
+ resubTimeoutMs?: number;
31
32
  };
32
33
 
33
34
  export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
@@ -3,10 +3,15 @@ import { Commitment, Connection, PublicKey } from '@solana/web3.js';
3
3
 
4
4
  export class WebSocketLogProvider implements LogProvider {
5
5
  private subscriptionId: number;
6
+ private isUnsubscribing = false;
7
+ private receivingData = false;
8
+ private timeoutId?: NodeJS.Timeout;
9
+ private callback?: logProviderCallback;
6
10
  public constructor(
7
11
  private connection: Connection,
8
12
  private address: PublicKey,
9
- private commitment: Commitment
13
+ private commitment: Commitment,
14
+ private resubTimeoutMs?: number
10
15
  ) {}
11
16
 
12
17
  public subscribe(callback: logProviderCallback): boolean {
@@ -14,13 +19,24 @@ export class WebSocketLogProvider implements LogProvider {
14
19
  return true;
15
20
  }
16
21
 
22
+ this.callback = callback;
17
23
  this.subscriptionId = this.connection.onLogs(
18
24
  this.address,
19
25
  (logs, ctx) => {
26
+ if (this.resubTimeoutMs && !this.isUnsubscribing) {
27
+ this.receivingData = true;
28
+ clearTimeout(this.timeoutId);
29
+ this.setTimeout();
30
+ }
20
31
  callback(logs.signature, ctx.slot, logs.logs, undefined);
21
32
  },
22
33
  this.commitment
23
34
  );
35
+
36
+ if (this.resubTimeoutMs) {
37
+ this.setTimeout();
38
+ }
39
+
24
40
  return true;
25
41
  }
26
42
 
@@ -29,10 +45,41 @@ export class WebSocketLogProvider implements LogProvider {
29
45
  }
30
46
 
31
47
  public async unsubscribe(): Promise<boolean> {
48
+ this.isUnsubscribing = true;
49
+ clearTimeout(this.timeoutId);
50
+
32
51
  if (this.subscriptionId !== undefined) {
33
- await this.connection.removeOnLogsListener(this.subscriptionId);
34
- this.subscriptionId = undefined;
52
+ this.connection
53
+ .removeOnLogsListener(this.subscriptionId)
54
+ .then(() => {
55
+ this.subscriptionId = undefined;
56
+ this.isUnsubscribing = false;
57
+ return true;
58
+ })
59
+ .catch((err) => {
60
+ console.log('Error unsubscribing from logs: ', err);
61
+ this.isUnsubscribing = false;
62
+ return false;
63
+ });
64
+ } else {
65
+ this.isUnsubscribing = false;
66
+ return true;
35
67
  }
36
- return true;
68
+ }
69
+
70
+ private setTimeout(): void {
71
+ this.timeoutId = setTimeout(async () => {
72
+ if (this.isUnsubscribing) {
73
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
74
+ return;
75
+ }
76
+
77
+ if (this.receivingData) {
78
+ console.log(`No log data in ${this.resubTimeoutMs}ms, resubscribing`);
79
+ await this.unsubscribe();
80
+ this.receivingData = false;
81
+ this.subscribe(this.callback);
82
+ }
83
+ }, this.resubTimeoutMs);
37
84
  }
38
85
  }
@@ -116,7 +116,7 @@ export class OrderSubscriber {
116
116
  }
117
117
 
118
118
  const slotAndUserAccount = this.usersAccounts.get(key);
119
- if (!slotAndUserAccount || slotAndUserAccount.slot < slot) {
119
+ if (!slotAndUserAccount || slotAndUserAccount.slot <= slot) {
120
120
  let userAccount: UserAccount;
121
121
  // Polling leads to a lot of redundant decoding, so we only decode if data is from a fresh slot
122
122
  if (dataType === 'raw') {