@drift-labs/sdk 2.67.0-beta.2 → 2.67.0-beta.3

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.67.0-beta.2
1
+ 2.67.0-beta.3
@@ -49,6 +49,7 @@ export declare class PollingDriftClientAccountSubscriber implements DriftClientA
49
49
  addSpotMarket(marketIndex: number): Promise<boolean>;
50
50
  addPerpMarket(marketIndex: number): Promise<boolean>;
51
51
  addOracle(oracleInfo: OracleInfo): Promise<boolean>;
52
+ private pauseForOracleToBeAdded;
52
53
  private setPerpOracleMap;
53
54
  private setSpotOracleMap;
54
55
  assertIsSubscribed(): void;
@@ -59,8 +59,8 @@ class PollingDriftClientAccountSubscriber {
59
59
  if (subscriptionSucceeded) {
60
60
  this.eventEmitter.emit('update');
61
61
  }
62
- this.setPerpOracleMap();
63
- this.setSpotOracleMap();
62
+ await this.setPerpOracleMap();
63
+ await this.setSpotOracleMap();
64
64
  this.isSubscribing = false;
65
65
  this.isSubscribed = subscriptionSucceeded;
66
66
  this.subscriptionPromiseResolver(subscriptionSucceeded);
@@ -244,7 +244,7 @@ class PollingDriftClientAccountSubscriber {
244
244
  await this.addPerpMarketAccountToPoll(marketIndex);
245
245
  const accountToPoll = this.accountsToPoll.get(marketPublicKey.toString());
246
246
  await this.addAccountToAccountLoader(accountToPoll);
247
- this.setPerpOracleMap();
247
+ await this.setPerpOracleMap();
248
248
  return true;
249
249
  }
250
250
  async addOracle(oracleInfo) {
@@ -253,25 +253,50 @@ class PollingDriftClientAccountSubscriber {
253
253
  return true;
254
254
  }
255
255
  this.addOracleToPoll(oracleInfo);
256
- const oracleToPoll = this.oraclesToPoll.get(oracleInfo.publicKey.toString());
256
+ const oracleString = oracleInfo.publicKey.toBase58();
257
+ const oracleToPoll = this.oraclesToPoll.get(oracleString);
257
258
  await this.addOracleToAccountLoader(oracleToPoll);
259
+ await this.pauseForOracleToBeAdded(3, oracleString);
258
260
  return true;
259
261
  }
260
- setPerpOracleMap() {
262
+ async pauseForOracleToBeAdded(tries, oracle) {
263
+ let i = 0;
264
+ while (i < tries) {
265
+ await new Promise((r) => setTimeout(r, this.accountLoader.pollingFrequency));
266
+ if (this.accountLoader.bufferAndSlotMap.has(oracle)) {
267
+ return;
268
+ }
269
+ i++;
270
+ }
271
+ console.log(`Pausing to find oracle ${oracle} failed`);
272
+ }
273
+ async setPerpOracleMap() {
261
274
  const perpMarkets = this.getMarketAccountsAndSlots();
262
275
  for (const perpMarket of perpMarkets) {
263
276
  const perpMarketAccount = perpMarket.data;
264
277
  const perpMarketIndex = perpMarketAccount.marketIndex;
265
278
  const oracle = perpMarketAccount.amm.oracle;
279
+ if (!this.oracles.has(oracle.toBase58())) {
280
+ await this.addOracle({
281
+ publicKey: oracle,
282
+ source: perpMarketAccount.amm.oracleSource,
283
+ });
284
+ }
266
285
  this.perpOracleMap.set(perpMarketIndex, oracle);
267
286
  }
268
287
  }
269
- setSpotOracleMap() {
288
+ async setSpotOracleMap() {
270
289
  const spotMarkets = this.getSpotMarketAccountsAndSlots();
271
290
  for (const spotMarket of spotMarkets) {
272
291
  const spotMarketAccount = spotMarket.data;
273
292
  const spotMarketIndex = spotMarketAccount.marketIndex;
274
293
  const oracle = spotMarketAccount.oracle;
294
+ if (!this.oracles.has(oracle.toBase58())) {
295
+ await this.addOracle({
296
+ publicKey: oracle,
297
+ source: spotMarketAccount.oracleSource,
298
+ });
299
+ }
275
300
  this.spotOracleMap.set(spotMarketIndex, oracle);
276
301
  }
277
302
  }
@@ -314,12 +339,7 @@ class PollingDriftClientAccountSubscriber {
314
339
  }
315
340
  if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
316
341
  // If the oracle has changed, we need to update the oracle map in background
317
- this.addOracle({
318
- source: perpMarketAccount.data.amm.oracleSource,
319
- publicKey: perpMarketAccount.data.amm.oracle,
320
- }).then(() => {
321
- this.setPerpOracleMap();
322
- });
342
+ this.setPerpOracleMap();
323
343
  }
324
344
  return this.getOraclePriceDataAndSlot(oracle);
325
345
  }
@@ -331,12 +351,7 @@ class PollingDriftClientAccountSubscriber {
331
351
  }
332
352
  if (!spotMarketAccount.data.oracle.equals(oracle)) {
333
353
  // If the oracle has changed, we need to update the oracle map in background
334
- this.addOracle({
335
- source: spotMarketAccount.data.oracleSource,
336
- publicKey: spotMarketAccount.data.oracle,
337
- }).then(() => {
338
- this.setSpotOracleMap();
339
- });
354
+ this.setSpotOracleMap();
340
355
  }
341
356
  return this.getOraclePriceDataAndSlot(oracle);
342
357
  }
@@ -59,8 +59,8 @@ class WebSocketDriftClientAccountSubscriber {
59
59
  // subscribe to oracles
60
60
  await this.subscribeToOracles();
61
61
  this.eventEmitter.emit('update');
62
- this.setPerpOracleMap();
63
- this.setSpotOracleMap();
62
+ await this.setPerpOracleMap();
63
+ await this.setSpotOracleMap();
64
64
  this.isSubscribing = false;
65
65
  this.isSubscribed = true;
66
66
  this.subscriptionPromiseResolver(true);
@@ -157,7 +157,7 @@ class WebSocketDriftClientAccountSubscriber {
157
157
  return true;
158
158
  }
159
159
  const subscriptionSuccess = this.subscribeToSpotMarketAccount(marketIndex);
160
- this.setSpotOracleMap();
160
+ await this.setSpotOracleMap();
161
161
  return subscriptionSuccess;
162
162
  }
163
163
  async addPerpMarket(marketIndex) {
@@ -165,7 +165,7 @@ class WebSocketDriftClientAccountSubscriber {
165
165
  return true;
166
166
  }
167
167
  const subscriptionSuccess = this.subscribeToPerpMarketAccount(marketIndex);
168
- this.setPerpOracleMap();
168
+ await this.setPerpOracleMap();
169
169
  return subscriptionSuccess;
170
170
  }
171
171
  async addOracle(oracleInfo) {
@@ -177,7 +177,7 @@ class WebSocketDriftClientAccountSubscriber {
177
177
  }
178
178
  return this.subscribeToOracle(oracleInfo);
179
179
  }
180
- setPerpOracleMap() {
180
+ async setPerpOracleMap() {
181
181
  const perpMarkets = this.getMarketAccountsAndSlots();
182
182
  for (const perpMarket of perpMarkets) {
183
183
  if (!perpMarket) {
@@ -186,10 +186,16 @@ class WebSocketDriftClientAccountSubscriber {
186
186
  const perpMarketAccount = perpMarket.data;
187
187
  const perpMarketIndex = perpMarketAccount.marketIndex;
188
188
  const oracle = perpMarketAccount.amm.oracle;
189
+ if (!this.oracleSubscribers.has(oracle.toBase58())) {
190
+ await this.addOracle({
191
+ publicKey: oracle,
192
+ source: perpMarket.data.amm.oracleSource,
193
+ });
194
+ }
189
195
  this.perpOracleMap.set(perpMarketIndex, oracle);
190
196
  }
191
197
  }
192
- setSpotOracleMap() {
198
+ async setSpotOracleMap() {
193
199
  const spotMarkets = this.getSpotMarketAccountsAndSlots();
194
200
  for (const spotMarket of spotMarkets) {
195
201
  if (!spotMarket) {
@@ -198,6 +204,12 @@ class WebSocketDriftClientAccountSubscriber {
198
204
  const spotMarketAccount = spotMarket.data;
199
205
  const spotMarketIndex = spotMarketAccount.marketIndex;
200
206
  const oracle = spotMarketAccount.oracle;
207
+ if (!this.oracleSubscribers.has(oracle.toBase58())) {
208
+ await this.addOracle({
209
+ publicKey: oracle,
210
+ source: spotMarketAccount.oracleSource,
211
+ });
212
+ }
201
213
  this.spotOracleMap.set(spotMarketIndex, oracle);
202
214
  }
203
215
  }
@@ -242,12 +254,7 @@ class WebSocketDriftClientAccountSubscriber {
242
254
  }
243
255
  if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
244
256
  // If the oracle has changed, we need to update the oracle map in background
245
- this.addOracle({
246
- source: perpMarketAccount.data.amm.oracleSource,
247
- publicKey: perpMarketAccount.data.amm.oracle,
248
- }).then(() => {
249
- this.setPerpOracleMap();
250
- });
257
+ this.setPerpOracleMap();
251
258
  }
252
259
  return this.getOraclePriceDataAndSlot(oracle);
253
260
  }
@@ -259,12 +266,7 @@ class WebSocketDriftClientAccountSubscriber {
259
266
  }
260
267
  if (!spotMarketAccount.data.oracle.equals(oracle)) {
261
268
  // If the oracle has changed, we need to update the oracle map in background
262
- this.addOracle({
263
- source: spotMarketAccount.data.oracleSource,
264
- publicKey: spotMarketAccount.data.oracle,
265
- }).then(() => {
266
- this.setSpotOracleMap();
267
- });
269
+ this.setSpotOracleMap();
268
270
  }
269
271
  return this.getOraclePriceDataAndSlot(oracle);
270
272
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.67.0-beta.2",
3
+ "version": "2.67.0-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -116,8 +116,8 @@ export class PollingDriftClientAccountSubscriber
116
116
  this.eventEmitter.emit('update');
117
117
  }
118
118
 
119
- this.setPerpOracleMap();
120
- this.setSpotOracleMap();
119
+ await this.setPerpOracleMap();
120
+ await this.setSpotOracleMap();
121
121
 
122
122
  this.isSubscribing = false;
123
123
  this.isSubscribed = subscriptionSucceeded;
@@ -389,7 +389,7 @@ export class PollingDriftClientAccountSubscriber
389
389
  await this.addPerpMarketAccountToPoll(marketIndex);
390
390
  const accountToPoll = this.accountsToPoll.get(marketPublicKey.toString());
391
391
  await this.addAccountToAccountLoader(accountToPoll);
392
- this.setPerpOracleMap();
392
+ await this.setPerpOracleMap();
393
393
  return true;
394
394
  }
395
395
 
@@ -402,29 +402,60 @@ export class PollingDriftClientAccountSubscriber
402
402
  }
403
403
 
404
404
  this.addOracleToPoll(oracleInfo);
405
- const oracleToPoll = this.oraclesToPoll.get(
406
- oracleInfo.publicKey.toString()
407
- );
405
+ const oracleString = oracleInfo.publicKey.toBase58();
406
+ const oracleToPoll = this.oraclesToPoll.get(oracleString);
408
407
  await this.addOracleToAccountLoader(oracleToPoll);
408
+
409
+ await this.pauseForOracleToBeAdded(3, oracleString);
410
+
409
411
  return true;
410
412
  }
411
413
 
412
- private setPerpOracleMap() {
414
+ private async pauseForOracleToBeAdded(
415
+ tries: number,
416
+ oracle: string
417
+ ): Promise<void> {
418
+ let i = 0;
419
+ while (i < tries) {
420
+ await new Promise((r) =>
421
+ setTimeout(r, this.accountLoader.pollingFrequency)
422
+ );
423
+ if (this.accountLoader.bufferAndSlotMap.has(oracle)) {
424
+ return;
425
+ }
426
+ i++;
427
+ }
428
+ console.log(`Pausing to find oracle ${oracle} failed`);
429
+ }
430
+
431
+ private async setPerpOracleMap() {
413
432
  const perpMarkets = this.getMarketAccountsAndSlots();
414
433
  for (const perpMarket of perpMarkets) {
415
434
  const perpMarketAccount = perpMarket.data;
416
435
  const perpMarketIndex = perpMarketAccount.marketIndex;
417
436
  const oracle = perpMarketAccount.amm.oracle;
437
+ if (!this.oracles.has(oracle.toBase58())) {
438
+ await this.addOracle({
439
+ publicKey: oracle,
440
+ source: perpMarketAccount.amm.oracleSource,
441
+ });
442
+ }
418
443
  this.perpOracleMap.set(perpMarketIndex, oracle);
419
444
  }
420
445
  }
421
446
 
422
- private setSpotOracleMap() {
447
+ private async setSpotOracleMap() {
423
448
  const spotMarkets = this.getSpotMarketAccountsAndSlots();
424
449
  for (const spotMarket of spotMarkets) {
425
450
  const spotMarketAccount = spotMarket.data;
426
451
  const spotMarketIndex = spotMarketAccount.marketIndex;
427
452
  const oracle = spotMarketAccount.oracle;
453
+ if (!this.oracles.has(oracle.toBase58())) {
454
+ await this.addOracle({
455
+ publicKey: oracle,
456
+ source: spotMarketAccount.oracleSource,
457
+ });
458
+ }
428
459
  this.spotOracleMap.set(spotMarketIndex, oracle);
429
460
  }
430
461
  }
@@ -481,18 +512,14 @@ export class PollingDriftClientAccountSubscriber
481
512
  ): DataAndSlot<OraclePriceData> | undefined {
482
513
  const perpMarketAccount = this.getMarketAccountAndSlot(marketIndex);
483
514
  const oracle = this.perpOracleMap.get(marketIndex);
515
+
484
516
  if (!perpMarketAccount || !oracle) {
485
517
  return undefined;
486
518
  }
487
519
 
488
520
  if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
489
521
  // If the oracle has changed, we need to update the oracle map in background
490
- this.addOracle({
491
- source: perpMarketAccount.data.amm.oracleSource,
492
- publicKey: perpMarketAccount.data.amm.oracle,
493
- }).then(() => {
494
- this.setPerpOracleMap();
495
- });
522
+ this.setPerpOracleMap();
496
523
  }
497
524
 
498
525
  return this.getOraclePriceDataAndSlot(oracle);
@@ -506,14 +533,10 @@ export class PollingDriftClientAccountSubscriber
506
533
  if (!spotMarketAccount || !oracle) {
507
534
  return undefined;
508
535
  }
536
+
509
537
  if (!spotMarketAccount.data.oracle.equals(oracle)) {
510
538
  // If the oracle has changed, we need to update the oracle map in background
511
- this.addOracle({
512
- source: spotMarketAccount.data.oracleSource,
513
- publicKey: spotMarketAccount.data.oracle,
514
- }).then(() => {
515
- this.setSpotOracleMap();
516
- });
539
+ this.setSpotOracleMap();
517
540
  }
518
541
 
519
542
  return this.getOraclePriceDataAndSlot(oracle);
@@ -125,8 +125,8 @@ export class WebSocketDriftClientAccountSubscriber
125
125
 
126
126
  this.eventEmitter.emit('update');
127
127
 
128
- this.setPerpOracleMap();
129
- this.setSpotOracleMap();
128
+ await this.setPerpOracleMap();
129
+ await this.setSpotOracleMap();
130
130
 
131
131
  this.isSubscribing = false;
132
132
  this.isSubscribed = true;
@@ -286,7 +286,7 @@ export class WebSocketDriftClientAccountSubscriber
286
286
  return true;
287
287
  }
288
288
  const subscriptionSuccess = this.subscribeToSpotMarketAccount(marketIndex);
289
- this.setSpotOracleMap();
289
+ await this.setSpotOracleMap();
290
290
  return subscriptionSuccess;
291
291
  }
292
292
 
@@ -295,7 +295,7 @@ export class WebSocketDriftClientAccountSubscriber
295
295
  return true;
296
296
  }
297
297
  const subscriptionSuccess = this.subscribeToPerpMarketAccount(marketIndex);
298
- this.setPerpOracleMap();
298
+ await this.setPerpOracleMap();
299
299
  return subscriptionSuccess;
300
300
  }
301
301
 
@@ -311,7 +311,7 @@ export class WebSocketDriftClientAccountSubscriber
311
311
  return this.subscribeToOracle(oracleInfo);
312
312
  }
313
313
 
314
- private setPerpOracleMap() {
314
+ private async setPerpOracleMap() {
315
315
  const perpMarkets = this.getMarketAccountsAndSlots();
316
316
  for (const perpMarket of perpMarkets) {
317
317
  if (!perpMarket) {
@@ -320,11 +320,17 @@ export class WebSocketDriftClientAccountSubscriber
320
320
  const perpMarketAccount = perpMarket.data;
321
321
  const perpMarketIndex = perpMarketAccount.marketIndex;
322
322
  const oracle = perpMarketAccount.amm.oracle;
323
+ if (!this.oracleSubscribers.has(oracle.toBase58())) {
324
+ await this.addOracle({
325
+ publicKey: oracle,
326
+ source: perpMarket.data.amm.oracleSource,
327
+ });
328
+ }
323
329
  this.perpOracleMap.set(perpMarketIndex, oracle);
324
330
  }
325
331
  }
326
332
 
327
- private setSpotOracleMap() {
333
+ private async setSpotOracleMap() {
328
334
  const spotMarkets = this.getSpotMarketAccountsAndSlots();
329
335
  for (const spotMarket of spotMarkets) {
330
336
  if (!spotMarket) {
@@ -333,6 +339,12 @@ export class WebSocketDriftClientAccountSubscriber
333
339
  const spotMarketAccount = spotMarket.data;
334
340
  const spotMarketIndex = spotMarketAccount.marketIndex;
335
341
  const oracle = spotMarketAccount.oracle;
342
+ if (!this.oracleSubscribers.has(oracle.toBase58())) {
343
+ await this.addOracle({
344
+ publicKey: oracle,
345
+ source: spotMarketAccount.oracleSource,
346
+ });
347
+ }
336
348
  this.spotOracleMap.set(spotMarketIndex, oracle);
337
349
  }
338
350
  }
@@ -400,12 +412,7 @@ export class WebSocketDriftClientAccountSubscriber
400
412
 
401
413
  if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
402
414
  // If the oracle has changed, we need to update the oracle map in background
403
- this.addOracle({
404
- source: perpMarketAccount.data.amm.oracleSource,
405
- publicKey: perpMarketAccount.data.amm.oracle,
406
- }).then(() => {
407
- this.setPerpOracleMap();
408
- });
415
+ this.setPerpOracleMap();
409
416
  }
410
417
 
411
418
  return this.getOraclePriceDataAndSlot(oracle);
@@ -422,12 +429,7 @@ export class WebSocketDriftClientAccountSubscriber
422
429
 
423
430
  if (!spotMarketAccount.data.oracle.equals(oracle)) {
424
431
  // If the oracle has changed, we need to update the oracle map in background
425
- this.addOracle({
426
- source: spotMarketAccount.data.oracleSource,
427
- publicKey: spotMarketAccount.data.oracle,
428
- }).then(() => {
429
- this.setSpotOracleMap();
430
- });
432
+ this.setSpotOracleMap();
431
433
  }
432
434
 
433
435
  return this.getOraclePriceDataAndSlot(oracle);