@drift-labs/sdk 0.1.34-master.5 → 0.1.36-master.1

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.
@@ -15,7 +15,6 @@ export declare class BulkAccountLoader {
15
15
  intervalId?: NodeJS.Timer;
16
16
  loadPromise?: Promise<void>;
17
17
  loadPromiseResolver: () => void;
18
- loggingEnabled: boolean;
19
18
  lastTimeLoadingPromiseCleared: number;
20
19
  constructor(connection: Connection, commitment: Commitment, pollingFrequency: number);
21
20
  addAccount(publicKey: PublicKey, callback: (buffer: Buffer) => void): string;
@@ -29,7 +28,7 @@ export declare class BulkAccountLoader {
29
28
  getAccountData(publicKey: PublicKey): Buffer | undefined;
30
29
  startPolling(): void;
31
30
  stopPolling(): void;
32
- log(msg: string, force?: boolean): void;
31
+ log(msg: string): void;
33
32
  updatePollingFrequency(pollingFrequency: number): void;
34
33
  }
35
34
  export {};
@@ -19,7 +19,6 @@ class BulkAccountLoader {
19
19
  this.accountsToLoad = new Map();
20
20
  this.accountData = new Map();
21
21
  this.errorCallbacks = new Map();
22
- this.loggingEnabled = false;
23
22
  this.lastTimeLoadingPromiseCleared = Date.now();
24
23
  this.connection = connection;
25
24
  this.commitment = commitment;
@@ -28,14 +27,11 @@ class BulkAccountLoader {
28
27
  addAccount(publicKey, callback) {
29
28
  const existingSize = this.accountsToLoad.size;
30
29
  const callbackId = (0, uuid_1.v4)();
31
- this.log(`Adding account ${publicKey.toString()} callback id ${callbackId}`);
32
30
  const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
33
31
  if (existingAccountToLoad) {
34
- this.log(`account already exists`);
35
32
  existingAccountToLoad.callbacks.set(callbackId, callback);
36
33
  }
37
34
  else {
38
- this.log(`account doesn't already exist. creating new callback map`);
39
35
  const callbacks = new Map();
40
36
  callbacks.set(callbackId, callback);
41
37
  const newAccountToLoad = {
@@ -52,7 +48,6 @@ class BulkAccountLoader {
52
48
  return callbackId;
53
49
  }
54
50
  removeAccount(publicKey, callbackId) {
55
- this.log(`Removing account ${publicKey.toString()} callback id ${callbackId}`);
56
51
  const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
57
52
  if (existingAccountToLoad) {
58
53
  existingAccountToLoad.callbacks.delete(callbackId);
@@ -83,11 +78,9 @@ class BulkAccountLoader {
83
78
  if (this.loadPromise) {
84
79
  const now = Date.now();
85
80
  if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
86
- this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
87
81
  this.loadPromise = undefined;
88
82
  }
89
83
  else {
90
- this.log(`Load promise exists. Returning early`);
91
84
  return this.loadPromise;
92
85
  }
93
86
  }
@@ -95,10 +88,8 @@ class BulkAccountLoader {
95
88
  this.loadPromiseResolver = resolver;
96
89
  });
97
90
  this.lastTimeLoadingPromiseCleared = Date.now();
98
- this.log(`Loading`);
99
91
  try {
100
92
  const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
101
- this.log(`${chunks.length} chunks`);
102
93
  yield Promise.all(chunks.map((chunk) => {
103
94
  return this.loadChunk(chunk);
104
95
  }));
@@ -109,10 +100,8 @@ class BulkAccountLoader {
109
100
  for (const [_, callback] of this.errorCallbacks) {
110
101
  callback(e);
111
102
  }
112
- this.log('finished error callbacks');
113
103
  }
114
104
  finally {
115
- this.log(`resetting load promise`);
116
105
  this.loadPromiseResolver();
117
106
  this.loadPromise = undefined;
118
107
  }
@@ -121,7 +110,6 @@ class BulkAccountLoader {
121
110
  loadChunk(accountsToLoad) {
122
111
  return __awaiter(this, void 0, void 0, function* () {
123
112
  if (accountsToLoad.length === 0) {
124
- this.log(`no accounts in chunk`);
125
113
  return;
126
114
  }
127
115
  const args = [
@@ -135,7 +123,7 @@ class BulkAccountLoader {
135
123
  this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
136
124
  );
137
125
  if (rpcResponse === null) {
138
- this.log('request to rpc timed out', true);
126
+ this.log('request to rpc timed out');
139
127
  return;
140
128
  }
141
129
  const newSlot = rpcResponse.result.context.slot;
@@ -150,7 +138,6 @@ class BulkAccountLoader {
150
138
  newBuffer = Buffer.from(raw, dataType);
151
139
  }
152
140
  if (!oldRPCResponse) {
153
- this.log('No old rpc response, updating account data');
154
141
  this.accountData.set(key, {
155
142
  slot: newSlot,
156
143
  buffer: newBuffer,
@@ -159,33 +146,23 @@ class BulkAccountLoader {
159
146
  continue;
160
147
  }
161
148
  if (newSlot <= oldRPCResponse.slot) {
162
- this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
163
149
  continue;
164
150
  }
165
151
  const oldBuffer = oldRPCResponse.buffer;
166
152
  if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
167
- this.log('new buffer, updating account data');
168
153
  this.accountData.set(key, {
169
154
  slot: newSlot,
170
155
  buffer: newBuffer,
171
156
  });
172
157
  this.handleAccountCallbacks(accountToLoad, newBuffer);
173
158
  }
174
- else {
175
- this.log('unable to update account for newest slot');
176
- this.log('oldBuffer ' + oldBuffer);
177
- this.log('newBuffer ' + newBuffer);
178
- this.log('buffers equal ' + (newBuffer === null || newBuffer === void 0 ? void 0 : newBuffer.equals(oldBuffer)));
179
- }
180
159
  }
181
160
  });
182
161
  }
183
162
  handleAccountCallbacks(accountToLoad, buffer) {
184
- this.log('handling account callbacks');
185
163
  for (const [_, callback] of accountToLoad.callbacks) {
186
164
  callback(buffer);
187
165
  }
188
- this.log('finished account callbacks');
189
166
  }
190
167
  getAccountData(publicKey) {
191
168
  const accountData = this.accountData.get(publicKey.toString());
@@ -195,20 +172,16 @@ class BulkAccountLoader {
195
172
  if (this.intervalId) {
196
173
  return;
197
174
  }
198
- this.log('startPolling');
199
175
  this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
200
176
  }
201
177
  stopPolling() {
202
178
  if (this.intervalId) {
203
179
  clearInterval(this.intervalId);
204
180
  this.intervalId = undefined;
205
- this.log('stopPolling');
206
181
  }
207
182
  }
208
- log(msg, force = false) {
209
- if (this.loggingEnabled || force) {
210
- console.log(msg);
211
- }
183
+ log(msg) {
184
+ console.log(msg);
212
185
  }
213
186
  updatePollingFrequency(pollingFrequency) {
214
187
  this.stopPolling();
@@ -41,8 +41,13 @@ class PollingClearingHouseAccountSubscriber {
41
41
  });
42
42
  yield this.updateAccountsToPoll();
43
43
  yield this.addToAccountLoader();
44
- yield this.fetch();
45
- const subscriptionSucceeded = this.didSubscriptionSucceed();
44
+ let subscriptionSucceeded = false;
45
+ let retries = 0;
46
+ while (!subscriptionSucceeded && retries < 5) {
47
+ yield this.fetch();
48
+ subscriptionSucceeded = this.didSubscriptionSucceed();
49
+ retries++;
50
+ }
46
51
  if (subscriptionSucceeded) {
47
52
  this.eventEmitter.emit('update');
48
53
  }
@@ -162,6 +167,9 @@ class PollingClearingHouseAccountSubscriber {
162
167
  // @ts-ignore
163
168
  this.eventEmitter.emit(accountToPoll.eventType, account);
164
169
  this.eventEmitter.emit('update');
170
+ if (!this.isSubscribed) {
171
+ this.isSubscribed = this.didSubscriptionSucceed();
172
+ }
165
173
  });
166
174
  }
167
175
  this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
@@ -23,4 +23,5 @@ export declare class PollingOracleSubscriber implements OracleSubscriber {
23
23
  unsubscribe(): Promise<void>;
24
24
  assertIsSubscribed(): void;
25
25
  getOraclePriceData(): OraclePriceData;
26
+ didSubscriptionSucceed(): boolean;
26
27
  }
@@ -26,10 +26,18 @@ class PollingOracleSubscriber {
26
26
  return true;
27
27
  }
28
28
  this.addToAccountLoader();
29
- yield this.fetch();
30
- this.eventEmitter.emit('update');
31
- this.isSubscribed = true;
32
- return true;
29
+ let subscriptionSucceeded = false;
30
+ let retries = 0;
31
+ while (!subscriptionSucceeded && retries < 5) {
32
+ yield this.fetch();
33
+ subscriptionSucceeded = this.didSubscriptionSucceed();
34
+ retries++;
35
+ }
36
+ if (subscriptionSucceeded) {
37
+ this.eventEmitter.emit('update');
38
+ }
39
+ this.isSubscribed = subscriptionSucceeded;
40
+ return subscriptionSucceeded;
33
41
  });
34
42
  }
35
43
  addToAccountLoader() {
@@ -75,5 +83,8 @@ class PollingOracleSubscriber {
75
83
  this.assertIsSubscribed();
76
84
  return this.oraclePriceData;
77
85
  }
86
+ didSubscriptionSucceed() {
87
+ return !!this.oraclePriceData;
88
+ }
78
89
  }
79
90
  exports.PollingOracleSubscriber = PollingOracleSubscriber;
@@ -22,4 +22,5 @@ export declare class PollingTokenAccountSubscriber implements TokenAccountSubscr
22
22
  unsubscribe(): Promise<void>;
23
23
  assertIsSubscribed(): void;
24
24
  getTokenAccount(): AccountInfo;
25
+ didSubscriptionSucceed(): boolean;
25
26
  }
@@ -26,10 +26,18 @@ class PollingTokenAccountSubscriber {
26
26
  return true;
27
27
  }
28
28
  this.addToAccountLoader();
29
- yield this.fetch();
30
- this.eventEmitter.emit('update');
31
- this.isSubscribed = true;
32
- return true;
29
+ let subscriptionSucceeded = false;
30
+ let retries = 0;
31
+ while (!subscriptionSucceeded && retries < 5) {
32
+ yield this.fetch();
33
+ subscriptionSucceeded = this.didSubscriptionSucceed();
34
+ retries++;
35
+ }
36
+ if (subscriptionSucceeded) {
37
+ this.eventEmitter.emit('update');
38
+ }
39
+ this.isSubscribed = subscriptionSucceeded;
40
+ return subscriptionSucceeded;
33
41
  });
34
42
  }
35
43
  addToAccountLoader() {
@@ -75,5 +83,8 @@ class PollingTokenAccountSubscriber {
75
83
  this.assertIsSubscribed();
76
84
  return this.tokenAccount;
77
85
  }
86
+ didSubscriptionSucceed() {
87
+ return !!this.tokenAccount;
88
+ }
78
89
  }
79
90
  exports.PollingTokenAccountSubscriber = PollingTokenAccountSubscriber;
@@ -30,8 +30,13 @@ class PollingUserAccountSubscriber {
30
30
  return true;
31
31
  }
32
32
  yield this.addToAccountLoader();
33
- yield this.fetchIfUnloaded();
34
- const subscriptionSucceeded = this.didSubscriptionSucceed();
33
+ let subscriptionSucceeded = false;
34
+ let retries = 0;
35
+ while (!subscriptionSucceeded && retries < 5) {
36
+ yield this.fetchIfUnloaded();
37
+ subscriptionSucceeded = this.didSubscriptionSucceed();
38
+ retries++;
39
+ }
35
40
  if (subscriptionSucceeded) {
36
41
  this.eventEmitter.emit('update');
37
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.34-master.5",
3
+ "version": "0.1.36-master.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -23,7 +23,6 @@ export class BulkAccountLoader {
23
23
  // to handle clients spamming load
24
24
  loadPromise?: Promise<void>;
25
25
  loadPromiseResolver: () => void;
26
- loggingEnabled = false;
27
26
  lastTimeLoadingPromiseCleared = Date.now();
28
27
 
29
28
  public constructor(
@@ -43,15 +42,10 @@ export class BulkAccountLoader {
43
42
  const existingSize = this.accountsToLoad.size;
44
43
 
45
44
  const callbackId = uuidv4();
46
- this.log(
47
- `Adding account ${publicKey.toString()} callback id ${callbackId}`
48
- );
49
45
  const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
50
46
  if (existingAccountToLoad) {
51
- this.log(`account already exists`);
52
47
  existingAccountToLoad.callbacks.set(callbackId, callback);
53
48
  } else {
54
- this.log(`account doesn't already exist. creating new callback map`);
55
49
  const callbacks = new Map<string, (buffer: Buffer) => void>();
56
50
  callbacks.set(callbackId, callback);
57
51
  const newAccountToLoad = {
@@ -72,9 +66,6 @@ export class BulkAccountLoader {
72
66
  }
73
67
 
74
68
  public removeAccount(publicKey: PublicKey, callbackId: string): void {
75
- this.log(
76
- `Removing account ${publicKey.toString()} callback id ${callbackId}`
77
- );
78
69
  const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
79
70
  if (existingAccountToLoad) {
80
71
  existingAccountToLoad.callbacks.delete(callbackId);
@@ -109,10 +100,8 @@ export class BulkAccountLoader {
109
100
  if (this.loadPromise) {
110
101
  const now = Date.now();
111
102
  if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
112
- this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
113
103
  this.loadPromise = undefined;
114
104
  } else {
115
- this.log(`Load promise exists. Returning early`);
116
105
  return this.loadPromise;
117
106
  }
118
107
  }
@@ -122,14 +111,11 @@ export class BulkAccountLoader {
122
111
  });
123
112
  this.lastTimeLoadingPromiseCleared = Date.now();
124
113
 
125
- this.log(`Loading`);
126
-
127
114
  try {
128
115
  const chunks = this.chunks(
129
116
  Array.from(this.accountsToLoad.values()),
130
117
  GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
131
118
  );
132
- this.log(`${chunks.length} chunks`);
133
119
 
134
120
  await Promise.all(
135
121
  chunks.map((chunk) => {
@@ -142,9 +128,7 @@ export class BulkAccountLoader {
142
128
  for (const [_, callback] of this.errorCallbacks) {
143
129
  callback(e);
144
130
  }
145
- this.log('finished error callbacks');
146
131
  } finally {
147
- this.log(`resetting load promise`);
148
132
  this.loadPromiseResolver();
149
133
  this.loadPromise = undefined;
150
134
  }
@@ -152,7 +136,6 @@ export class BulkAccountLoader {
152
136
 
153
137
  async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
154
138
  if (accountsToLoad.length === 0) {
155
- this.log(`no accounts in chunk`);
156
139
  return;
157
140
  }
158
141
 
@@ -170,7 +153,7 @@ export class BulkAccountLoader {
170
153
  );
171
154
 
172
155
  if (rpcResponse === null) {
173
- this.log('request to rpc timed out', true);
156
+ this.log('request to rpc timed out');
174
157
  return;
175
158
  }
176
159
 
@@ -189,7 +172,6 @@ export class BulkAccountLoader {
189
172
  }
190
173
 
191
174
  if (!oldRPCResponse) {
192
- this.log('No old rpc response, updating account data');
193
175
  this.accountData.set(key, {
194
176
  slot: newSlot,
195
177
  buffer: newBuffer,
@@ -199,33 +181,24 @@ export class BulkAccountLoader {
199
181
  }
200
182
 
201
183
  if (newSlot <= oldRPCResponse.slot) {
202
- this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
203
184
  continue;
204
185
  }
205
186
 
206
187
  const oldBuffer = oldRPCResponse.buffer;
207
188
  if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
208
- this.log('new buffer, updating account data');
209
189
  this.accountData.set(key, {
210
190
  slot: newSlot,
211
191
  buffer: newBuffer,
212
192
  });
213
193
  this.handleAccountCallbacks(accountToLoad, newBuffer);
214
- } else {
215
- this.log('unable to update account for newest slot');
216
- this.log('oldBuffer ' + oldBuffer);
217
- this.log('newBuffer ' + newBuffer);
218
- this.log('buffers equal ' + newBuffer?.equals(oldBuffer));
219
194
  }
220
195
  }
221
196
  }
222
197
 
223
198
  handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer): void {
224
- this.log('handling account callbacks');
225
199
  for (const [_, callback] of accountToLoad.callbacks) {
226
200
  callback(buffer);
227
201
  }
228
- this.log('finished account callbacks');
229
202
  }
230
203
 
231
204
  public getAccountData(publicKey: PublicKey): Buffer | undefined {
@@ -238,8 +211,6 @@ export class BulkAccountLoader {
238
211
  return;
239
212
  }
240
213
 
241
- this.log('startPolling');
242
-
243
214
  this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
244
215
  }
245
216
 
@@ -247,15 +218,11 @@ export class BulkAccountLoader {
247
218
  if (this.intervalId) {
248
219
  clearInterval(this.intervalId);
249
220
  this.intervalId = undefined;
250
-
251
- this.log('stopPolling');
252
221
  }
253
222
  }
254
223
 
255
- public log(msg: string, force = false): void {
256
- if (this.loggingEnabled || force) {
257
- console.log(msg);
258
- }
224
+ public log(msg: string): void {
225
+ console.log(msg);
259
226
  }
260
227
 
261
228
  public updatePollingFrequency(pollingFrequency: number): void {
@@ -85,8 +85,14 @@ export class PollingClearingHouseAccountSubscriber
85
85
 
86
86
  await this.updateAccountsToPoll();
87
87
  await this.addToAccountLoader();
88
- await this.fetch();
89
- const subscriptionSucceeded = this.didSubscriptionSucceed();
88
+
89
+ let subscriptionSucceeded = false;
90
+ let retries = 0;
91
+ while (!subscriptionSucceeded && retries < 5) {
92
+ await this.fetch();
93
+ subscriptionSucceeded = this.didSubscriptionSucceed();
94
+ retries++;
95
+ }
90
96
 
91
97
  if (subscriptionSucceeded) {
92
98
  this.eventEmitter.emit('update');
@@ -237,6 +243,10 @@ export class PollingClearingHouseAccountSubscriber
237
243
  // @ts-ignore
238
244
  this.eventEmitter.emit(accountToPoll.eventType, account);
239
245
  this.eventEmitter.emit('update');
246
+
247
+ if (!this.isSubscribed) {
248
+ this.isSubscribed = this.didSubscriptionSucceed();
249
+ }
240
250
  }
241
251
  );
242
252
  }
@@ -37,11 +37,21 @@ export class PollingOracleSubscriber implements OracleSubscriber {
37
37
  }
38
38
 
39
39
  this.addToAccountLoader();
40
- await this.fetch();
41
- this.eventEmitter.emit('update');
42
40
 
43
- this.isSubscribed = true;
44
- return true;
41
+ let subscriptionSucceeded = false;
42
+ let retries = 0;
43
+ while (!subscriptionSucceeded && retries < 5) {
44
+ await this.fetch();
45
+ subscriptionSucceeded = this.didSubscriptionSucceed();
46
+ retries++;
47
+ }
48
+
49
+ if (subscriptionSucceeded) {
50
+ this.eventEmitter.emit('update');
51
+ }
52
+
53
+ this.isSubscribed = subscriptionSucceeded;
54
+ return subscriptionSucceeded;
45
55
  }
46
56
 
47
57
  addToAccountLoader(): void {
@@ -100,4 +110,8 @@ export class PollingOracleSubscriber implements OracleSubscriber {
100
110
  this.assertIsSubscribed();
101
111
  return this.oraclePriceData;
102
112
  }
113
+
114
+ didSubscriptionSucceed(): boolean {
115
+ return !!this.oraclePriceData;
116
+ }
103
117
  }
@@ -36,11 +36,20 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
36
36
  }
37
37
 
38
38
  this.addToAccountLoader();
39
- await this.fetch();
40
- this.eventEmitter.emit('update');
39
+ let subscriptionSucceeded = false;
40
+ let retries = 0;
41
+ while (!subscriptionSucceeded && retries < 5) {
42
+ await this.fetch();
43
+ subscriptionSucceeded = this.didSubscriptionSucceed();
44
+ retries++;
45
+ }
46
+
47
+ if (subscriptionSucceeded) {
48
+ this.eventEmitter.emit('update');
49
+ }
41
50
 
42
- this.isSubscribed = true;
43
- return true;
51
+ this.isSubscribed = subscriptionSucceeded;
52
+ return subscriptionSucceeded;
44
53
  }
45
54
 
46
55
  addToAccountLoader(): void {
@@ -96,4 +105,8 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
96
105
  this.assertIsSubscribed();
97
106
  return this.tokenAccount;
98
107
  }
108
+
109
+ didSubscriptionSucceed(): boolean {
110
+ return !!this.tokenAccount;
111
+ }
99
112
  }
@@ -52,9 +52,14 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
52
52
  }
53
53
 
54
54
  await this.addToAccountLoader();
55
- await this.fetchIfUnloaded();
56
55
 
57
- const subscriptionSucceeded = this.didSubscriptionSucceed();
56
+ let subscriptionSucceeded = false;
57
+ let retries = 0;
58
+ while (!subscriptionSucceeded && retries < 5) {
59
+ await this.fetchIfUnloaded();
60
+ subscriptionSucceeded = this.didSubscriptionSucceed();
61
+ retries++;
62
+ }
58
63
 
59
64
  if (subscriptionSucceeded) {
60
65
  this.eventEmitter.emit('update');