@drift-labs/sdk 0.2.0-master.29 → 0.2.0-master.30

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 (106) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
  4. package/lib/accounts/types.d.ts +4 -6
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
  6. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
  7. package/lib/addresses/marketAddresses.d.ts +1 -3
  8. package/lib/addresses/pda.d.ts +5 -5
  9. package/lib/addresses/pda.js +5 -5
  10. package/lib/admin.d.ts +27 -28
  11. package/lib/admin.js +12 -37
  12. package/lib/clearingHouse.d.ts +61 -62
  13. package/lib/clearingHouse.js +92 -110
  14. package/lib/clearingHouseConfig.d.ts +2 -4
  15. package/lib/clearingHouseUser.d.ts +16 -16
  16. package/lib/clearingHouseUser.js +16 -16
  17. package/lib/config.d.ts +2 -4
  18. package/lib/config.js +1 -1
  19. package/lib/constants/numericConstants.d.ts +7 -8
  20. package/lib/constants/numericConstants.js +17 -18
  21. package/lib/constants/perpMarkets.d.ts +2 -3
  22. package/lib/constants/perpMarkets.js +3 -3
  23. package/lib/constants/spotMarkets.d.ts +2 -1
  24. package/lib/constants/spotMarkets.js +6 -4
  25. package/lib/dlob/DLOB.d.ts +13 -13
  26. package/lib/dlob/DLOB.js +36 -40
  27. package/lib/dlob/DLOBNode.js +2 -2
  28. package/lib/events/sort.js +1 -1
  29. package/lib/examples/makeTradeExample.js +3 -3
  30. package/lib/idl/clearing_house.json +359 -310
  31. package/lib/math/amm.d.ts +2 -2
  32. package/lib/math/amm.js +10 -10
  33. package/lib/math/conversion.js +1 -1
  34. package/lib/math/funding.js +9 -9
  35. package/lib/math/margin.js +3 -3
  36. package/lib/math/market.d.ts +4 -4
  37. package/lib/math/market.js +7 -7
  38. package/lib/math/oracles.js +6 -8
  39. package/lib/math/position.d.ts +2 -2
  40. package/lib/math/position.js +9 -9
  41. package/lib/math/repeg.js +7 -6
  42. package/lib/math/spotBalance.js +5 -5
  43. package/lib/math/spotPosition.js +2 -2
  44. package/lib/math/trade.d.ts +6 -6
  45. package/lib/math/trade.js +15 -19
  46. package/lib/oracles/pythClient.js +1 -1
  47. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  48. package/lib/oracles/switchboardClient.js +1 -1
  49. package/lib/types.d.ts +63 -51
  50. package/lib/types.js +1 -1
  51. package/package.json +2 -1
  52. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
  53. package/src/accounts/types.ts +4 -5
  54. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
  55. package/src/addresses/marketAddresses.ts +1 -2
  56. package/src/addresses/pda.ts +10 -10
  57. package/src/admin.ts +42 -79
  58. package/src/clearingHouse.ts +164 -212
  59. package/src/clearingHouseConfig.ts +2 -3
  60. package/src/clearingHouseUser.ts +35 -35
  61. package/src/config.ts +3 -4
  62. package/src/constants/numericConstants.ts +19 -21
  63. package/src/constants/perpMarkets.ts +5 -5
  64. package/src/constants/spotMarkets.ts +8 -5
  65. package/src/dlob/DLOB.ts +54 -70
  66. package/src/dlob/DLOBNode.ts +5 -6
  67. package/src/events/sort.ts +1 -1
  68. package/src/examples/makeTradeExample.js +2 -2
  69. package/src/examples/makeTradeExample.ts +5 -8
  70. package/src/idl/clearing_house.json +359 -310
  71. package/src/math/amm.ts +14 -11
  72. package/src/math/conversion.ts +2 -2
  73. package/src/math/funding.ts +13 -11
  74. package/src/math/margin.ts +4 -5
  75. package/src/math/market.ts +5 -5
  76. package/src/math/oracles.ts +9 -9
  77. package/src/math/position.ts +11 -19
  78. package/src/math/repeg.ts +8 -7
  79. package/src/math/spotBalance.ts +6 -6
  80. package/src/math/spotPosition.ts +2 -2
  81. package/src/math/trade.ts +17 -21
  82. package/src/oracles/pythClient.ts +2 -2
  83. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  84. package/src/oracles/switchboardClient.ts +2 -2
  85. package/src/types.ts +69 -51
  86. package/tests/dlob/helpers.ts +56 -4
  87. package/src/addresses/marketAddresses.js +0 -26
  88. package/src/constants/banks.js +0 -42
  89. package/src/constants/markets.js +0 -42
  90. package/src/events/txEventCache.js +0 -71
  91. package/src/factory/bigNum.js +0 -390
  92. package/src/factory/oracleClient.js +0 -20
  93. package/src/math/auction.js +0 -42
  94. package/src/math/conversion.js +0 -11
  95. package/src/math/funding.js +0 -248
  96. package/src/math/repeg.js +0 -128
  97. package/src/math/trade.js +0 -253
  98. package/src/math/utils.js +0 -26
  99. package/src/math/utils.js.map +0 -1
  100. package/src/oracles/oracleClientCache.js +0 -19
  101. package/src/oracles/pythClient.js +0 -46
  102. package/src/oracles/quoteAssetOracleClient.js +0 -32
  103. package/src/oracles/switchboardClient.js +0 -69
  104. package/src/oracles/types.js +0 -2
  105. package/src/userName.js +0 -20
  106. package/src/wallet.js +0 -35
@@ -29,7 +29,7 @@ function convertPythPrice(price, exponent) {
29
29
  exponent = Math.abs(exponent);
30
30
  const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
31
31
  return new anchor_1.BN(price * Math.pow(10, exponent))
32
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
32
+ .mul(numericConstants_1.PRICE_PRECISION)
33
33
  .div(pythPrecision);
34
34
  }
35
35
  exports.convertPythPrice = convertPythPrice;
@@ -4,7 +4,7 @@ exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
4
4
  const anchor_1 = require("@project-serum/anchor");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  exports.QUOTE_ORACLE_PRICE_DATA = {
7
- price: numericConstants_1.MARK_PRICE_PRECISION,
7
+ price: numericConstants_1.PRICE_PRECISION,
8
8
  slot: new anchor_1.BN(0),
9
9
  confidence: new anchor_1.BN(1),
10
10
  hasSufficientNumberOfDataPoints: true,
@@ -53,6 +53,6 @@ function getSwitchboardProgram(connection) {
53
53
  function convertSwitchboardDecimal(switchboardDecimal) {
54
54
  const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
55
55
  return switchboardDecimal.mantissa
56
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
56
+ .mul(numericConstants_1.PRICE_PRECISION)
57
57
  .div(switchboardPrecision);
58
58
  }
package/lib/types.d.ts CHANGED
@@ -194,7 +194,7 @@ export declare type DepositRecord = {
194
194
  deposit?: any;
195
195
  withdraw?: any;
196
196
  };
197
- marketIndex: BN;
197
+ marketIndex: number;
198
198
  amount: BN;
199
199
  oraclePrice: BN;
200
200
  referrer: PublicKey;
@@ -204,7 +204,7 @@ export declare type DepositRecord = {
204
204
  export declare type CurveRecord = {
205
205
  ts: BN;
206
206
  recordId: BN;
207
- marketIndex: BN;
207
+ marketIndex: number;
208
208
  pegMultiplierBefore: BN;
209
209
  baseAssetReserveBefore: BN;
210
210
  quoteAssetReserveBefore: BN;
@@ -223,7 +223,7 @@ export declare type CurveRecord = {
223
223
  export declare type InsuranceFundRecord = {
224
224
  ts: BN;
225
225
  bankIndex: BN;
226
- marketIndex: BN;
226
+ marketIndex: number;
227
227
  userIfFactor: BN;
228
228
  totalIfFactor: BN;
229
229
  vaultAmountBefore: BN;
@@ -237,7 +237,7 @@ export declare type LPRecord = {
237
237
  user: PublicKey;
238
238
  action: LPAction;
239
239
  nShares: BN;
240
- marketIndex: BN;
240
+ marketIndex: number;
241
241
  deltaBaseAssetAmount: BN;
242
242
  deltaQuoteAssetAmount: BN;
243
243
  pnl: BN;
@@ -256,7 +256,7 @@ export declare class LPAction {
256
256
  export declare type FundingRateRecord = {
257
257
  ts: BN;
258
258
  recordId: BN;
259
- marketIndex: BN;
259
+ marketIndex: number;
260
260
  fundingRate: BN;
261
261
  fundingRateLong: BN;
262
262
  fundingRateShort: BN;
@@ -272,7 +272,7 @@ export declare type FundingPaymentRecord = {
272
272
  ts: BN;
273
273
  userAuthority: PublicKey;
274
274
  user: PublicKey;
275
- marketIndex: BN;
275
+ marketIndex: number;
276
276
  fundingPayment: BN;
277
277
  baseAssetAmount: BN;
278
278
  userLastCumulativeFunding: BN;
@@ -317,7 +317,7 @@ export declare class LiquidationType {
317
317
  };
318
318
  }
319
319
  export declare type LiquidatePerpRecord = {
320
- marketIndex: BN;
320
+ marketIndex: number;
321
321
  oraclePrice: BN;
322
322
  baseAssetAmount: BN;
323
323
  quoteAssetAmount: BN;
@@ -330,44 +330,44 @@ export declare type LiquidatePerpRecord = {
330
330
  ifFee: BN;
331
331
  };
332
332
  export declare type LiquidateBorrowRecord = {
333
- assetMarketIndex: BN;
333
+ assetMarketIndex: number;
334
334
  assetPrice: BN;
335
335
  assetTransfer: BN;
336
- liabilityMarketIndex: BN;
336
+ liabilityMarketIndex: number;
337
337
  liabilityPrice: BN;
338
338
  liabilityTransfer: BN;
339
339
  ifFee: BN;
340
340
  };
341
341
  export declare type LiquidateBorrowForPerpPnlRecord = {
342
- perpMarketIndex: BN;
342
+ perpMarketIndex: number;
343
343
  marketOraclePrice: BN;
344
344
  pnlTransfer: BN;
345
- liabilityMarketIndex: BN;
345
+ liabilityMarketIndex: number;
346
346
  liabilityPrice: BN;
347
347
  liabilityTransfer: BN;
348
348
  };
349
349
  export declare type LiquidatePerpPnlForDepositRecord = {
350
- perpMarketIndex: BN;
350
+ perpMarketIndex: number;
351
351
  marketOraclePrice: BN;
352
352
  pnlTransfer: BN;
353
- assetMarketIndex: BN;
353
+ assetMarketIndex: number;
354
354
  assetPrice: BN;
355
355
  assetTransfer: BN;
356
356
  };
357
357
  export declare type PerpBankruptcyRecord = {
358
- marketIndex: BN;
358
+ marketIndex: number;
359
359
  pnl: BN;
360
360
  cumulativeFundingRateDelta: BN;
361
361
  };
362
362
  export declare type BorrowBankruptcyRecord = {
363
- marketIndex: BN;
363
+ marketIndex: number;
364
364
  borrowAmount: BN;
365
365
  cumulativeDepositInterestDelta: BN;
366
366
  };
367
367
  export declare type SettlePnlRecord = {
368
368
  ts: BN;
369
369
  user: PublicKey;
370
- marketIndex: BN;
370
+ marketIndex: number;
371
371
  pnl: BN;
372
372
  baseAssetAmount: BN;
373
373
  quoteAssetAmountAfter: BN;
@@ -383,33 +383,30 @@ export declare type OrderActionRecord = {
383
383
  ts: BN;
384
384
  action: OrderAction;
385
385
  actionExplanation: OrderActionExplanation;
386
- marketIndex: BN;
386
+ marketIndex: number;
387
+ marketType: MarketType;
387
388
  filler: PublicKey | null;
388
389
  fillerReward: BN | null;
389
390
  fillRecordId: BN | null;
390
- referrer: PublicKey | null;
391
391
  baseAssetAmountFilled: BN | null;
392
392
  quoteAssetAmountFilled: BN | null;
393
- takerPnl: BN | null;
394
- makerPnl: BN | null;
395
393
  takerFee: BN | null;
396
- makerRebate: BN | null;
397
- referrerReward: BN | null;
398
- refereeDiscount: BN | null;
394
+ makerFee: BN | null;
395
+ referrerReward: number | null;
399
396
  quoteAssetAmountSurplus: BN | null;
400
397
  taker: PublicKey | null;
401
398
  takerOrderId: BN | null;
402
399
  takerOrderDirection: PositionDirection | null;
403
400
  takerOrderBaseAssetAmount: BN | null;
404
- takerOrderBaseAssetAmountFilled: BN | null;
405
- takerOrderQuoteAssetAmountFilled: BN | null;
401
+ takerOrderCumulativeBaseAssetAmountFilled: BN | null;
402
+ takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
406
403
  takerOrderFee: BN | null;
407
404
  maker: PublicKey | null;
408
405
  makerOrderId: BN | null;
409
406
  makerOrderDirection: PositionDirection | null;
410
407
  makerOrderBaseAssetAmount: BN | null;
411
- makerOrderBaseAssetAmountFilled: BN | null;
412
- makerOrderQuoteAssetAmountFilled: BN | null;
408
+ makerOrderCumulativeBaseAssetAmountFilled: BN | null;
409
+ makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
413
410
  makerOrderFee: BN | null;
414
411
  oraclePrice: BN;
415
412
  };
@@ -418,15 +415,14 @@ export declare type StateAccount = {
418
415
  fundingPaused: boolean;
419
416
  exchangePaused: boolean;
420
417
  adminControlsPrices: boolean;
421
- insuranceVault: PublicKey;
422
418
  totalFee: BN;
423
419
  totalFeeWithdrawn: BN;
424
420
  whitelistMint: PublicKey;
425
421
  discountMint: PublicKey;
426
422
  oracleGuardRails: OracleGuardRails;
427
423
  maxDeposit: BN;
428
- numberOfMarkets: BN;
429
- numberOfSpotMarkets: BN;
424
+ numberOfMarkets: number;
425
+ numberOfSpotMarkets: number;
430
426
  minOrderQuoteAssetAmount: BN;
431
427
  signer: PublicKey;
432
428
  signerNonce: number;
@@ -434,6 +430,7 @@ export declare type StateAccount = {
434
430
  minPerpAuctionDuration: number;
435
431
  defaultSpotAuctionDuration: number;
436
432
  liquidationMarginBufferRatio: number;
433
+ srmVault: PublicKey;
437
434
  perpFeeStructure: FeeStructure;
438
435
  spotFeeStructure: FeeStructure;
439
436
  };
@@ -442,7 +439,7 @@ export declare type PerpMarketAccount = {
442
439
  contractType: ContractType;
443
440
  expiryTs: BN;
444
441
  settlementPrice: BN;
445
- marketIndex: BN;
442
+ marketIndex: number;
446
443
  pubkey: PublicKey;
447
444
  amm: AMM;
448
445
  baseAssetAmount: BN;
@@ -466,11 +463,30 @@ export declare type PerpMarketAccount = {
466
463
  quoteSettledInsurance: BN;
467
464
  quoteMaxInsurance: BN;
468
465
  };
466
+ export declare type HistoricalOracleData = {
467
+ lastOraclePrice: BN;
468
+ lastOracleDelay: BN;
469
+ lastOracleConf: BN;
470
+ lastOraclePriceTwap: BN;
471
+ lastOraclePriceTwap5min: BN;
472
+ lastOraclePriceTwapTs: BN;
473
+ };
474
+ export declare type HistoricalIndexData = {
475
+ lastIndexBidPrice: BN;
476
+ lastIndexAskPrice: BN;
477
+ lastIndexPriceTwap: BN;
478
+ lastIndexPriceTwap5Min: BN;
479
+ lastIndexPriceTwapTs: BN;
480
+ };
469
481
  export declare type SpotMarketAccount = {
470
- marketIndex: BN;
482
+ marketIndex: number;
471
483
  pubkey: PublicKey;
472
484
  mint: PublicKey;
473
485
  vault: PublicKey;
486
+ oracle: PublicKey;
487
+ oracleSource: OracleSource;
488
+ historicalOracleData: HistoricalOracleData;
489
+ historicalIndexData: HistoricalIndexData;
474
490
  insuranceFundVault: PublicKey;
475
491
  insuranceWithdrawEscrowPeriod: BN;
476
492
  revenuePool: PoolBalance;
@@ -489,7 +505,6 @@ export declare type SpotMarketAccount = {
489
505
  borrowBalance: BN;
490
506
  lastInterestTs: BN;
491
507
  lastTwapTs: BN;
492
- oracle: PublicKey;
493
508
  initialAssetWeight: BN;
494
509
  maintenanceAssetWeight: BN;
495
510
  initialLiabilityWeight: BN;
@@ -519,21 +534,17 @@ export declare type AMM = {
519
534
  lastMarkPriceTwap: BN;
520
535
  lastMarkPriceTwap5min: BN;
521
536
  lastMarkPriceTwapTs: BN;
522
- lastOraclePriceTwap: BN;
523
- lastOraclePriceTwap5min: BN;
524
- lastOraclePriceTwapTs: BN;
525
- lastOracleMarkSpreadPct: BN;
526
- lastOracleConfPct: BN;
527
537
  oracle: PublicKey;
528
538
  oracleSource: OracleSource;
539
+ historicalOracleData: HistoricalOracleData;
540
+ lastOracleMarkSpreadPct: BN;
541
+ lastOracleConfPct: BN;
529
542
  fundingPeriod: BN;
530
543
  quoteAssetReserve: BN;
531
544
  pegMultiplier: BN;
532
545
  cumulativeFundingRateLong: BN;
533
546
  cumulativeFundingRateShort: BN;
534
547
  cumulativeFundingRateLp: BN;
535
- cumulativeRepegRebateLong: BN;
536
- cumulativeRepegRebateShort: BN;
537
548
  totalFeeMinusDistributions: BN;
538
549
  totalFeeWithdrawn: BN;
539
550
  totalFee: BN;
@@ -546,7 +557,6 @@ export declare type AMM = {
546
557
  baseAssetAmountStepSize: BN;
547
558
  maxBaseAssetAmountRatio: number;
548
559
  maxSlippageRatio: number;
549
- lastOraclePrice: BN;
550
560
  baseSpread: number;
551
561
  curveUpdateIntensity: number;
552
562
  netBaseAssetAmount: BN;
@@ -575,10 +585,10 @@ export declare type PerpPosition = {
575
585
  baseAssetAmount: BN;
576
586
  remainderBaseAssetAmount: BN;
577
587
  lastCumulativeFundingRate: BN;
578
- marketIndex: BN;
588
+ marketIndex: number;
579
589
  quoteAssetAmount: BN;
580
590
  quoteEntryAmount: BN;
581
- openOrders: BN;
591
+ openOrders: number;
582
592
  openBids: BN;
583
593
  openAsks: BN;
584
594
  settledPnl: BN;
@@ -622,12 +632,13 @@ export declare type UserAccount = {
622
632
  customMarginRatio: number;
623
633
  };
624
634
  export declare type SpotPosition = {
625
- marketIndex: BN;
635
+ marketIndex: number;
626
636
  balanceType: SpotBalanceType;
627
637
  balance: BN;
628
638
  openOrders: number;
629
639
  openBids: BN;
630
640
  openAsks: BN;
641
+ cumulativeDeposits: BN;
631
642
  };
632
643
  export declare type Order = {
633
644
  status: OrderStatus;
@@ -637,7 +648,7 @@ export declare type Order = {
637
648
  slot: BN;
638
649
  orderId: BN;
639
650
  userOrderId: number;
640
- marketIndex: BN;
651
+ marketIndex: number;
641
652
  price: BN;
642
653
  baseAssetAmount: BN;
643
654
  baseAssetAmountFilled: BN;
@@ -665,7 +676,7 @@ export declare type OrderParams = {
665
676
  direction: PositionDirection;
666
677
  baseAssetAmount: BN;
667
678
  price: BN;
668
- marketIndex: BN;
679
+ marketIndex: number;
669
680
  reduceOnly: boolean;
670
681
  postOnly: boolean;
671
682
  immediateOrCancel: boolean;
@@ -679,7 +690,7 @@ export declare type OrderParams = {
679
690
  };
680
691
  export declare type NecessaryOrderParams = {
681
692
  orderType: OrderType;
682
- marketIndex: BN;
693
+ marketIndex: number;
683
694
  baseAssetAmount: BN;
684
695
  direction: PositionDirection;
685
696
  };
@@ -699,7 +710,7 @@ export declare const DefaultOrderParams: {
699
710
  };
700
711
  baseAssetAmount: BN;
701
712
  price: BN;
702
- marketIndex: BN;
713
+ marketIndex: number;
703
714
  reduceOnly: boolean;
704
715
  postOnly: boolean;
705
716
  immediateOrCancel: boolean;
@@ -762,7 +773,8 @@ export declare type OracleGuardRails = {
762
773
  markOracleDivergenceDenominator: BN;
763
774
  };
764
775
  validity: {
765
- slotsBeforeStale: BN;
776
+ slotsBeforeStaleForAmm: BN;
777
+ slotsBeforeStaleForMargin: BN;
766
778
  confidenceIntervalMaxSize: BN;
767
779
  tooVolatileRatio: BN;
768
780
  };
@@ -770,7 +782,7 @@ export declare type OracleGuardRails = {
770
782
  };
771
783
  export declare type MarginCategory = 'Initial' | 'Maintenance';
772
784
  export declare type InsuranceFundStake = {
773
- marketIndex: BN;
785
+ marketIndex: number;
774
786
  authority: PublicKey;
775
787
  ifShares: BN;
776
788
  ifBase: BN;
@@ -782,7 +794,7 @@ export declare type SerumV3FulfillmentConfigAccount = {
782
794
  fulfillmentType: SpotFulfillmentType;
783
795
  status: SpotFulfillmentStatus;
784
796
  pubkey: PublicKey;
785
- marketIndex: BN;
797
+ marketIndex: number;
786
798
  serumProgramId: PublicKey;
787
799
  serumMarket: PublicKey;
788
800
  serumRequestQueue: PublicKey;
package/lib/types.js CHANGED
@@ -154,7 +154,7 @@ exports.DefaultOrderParams = {
154
154
  direction: PositionDirection.LONG,
155
155
  baseAssetAmount: _1.ZERO,
156
156
  price: _1.ZERO,
157
- marketIndex: _1.ZERO,
157
+ marketIndex: 0,
158
158
  reduceOnly: false,
159
159
  postOnly: false,
160
160
  immediateOrCancel: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.2.0-master.29",
3
+ "version": "0.2.0-master.30",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -31,6 +31,7 @@
31
31
  "access": "public"
32
32
  },
33
33
  "dependencies": {
34
+ "@project-serum/serum": "^0.13.38",
34
35
  "@project-serum/anchor": "0.24.2",
35
36
  "@pythnetwork/client": "2.5.3",
36
37
  "@solana/spl-token": "^0.1.6",
@@ -6,7 +6,7 @@ import {
6
6
  NotSubscribedError,
7
7
  OraclesToPoll,
8
8
  } from './types';
9
- import { BN, Program } from '@project-serum/anchor';
9
+ import { Program } from '@project-serum/anchor';
10
10
  import StrictEventEmitter from 'strict-event-emitter-types';
11
11
  import { EventEmitter } from 'events';
12
12
  import {
@@ -32,8 +32,8 @@ export class PollingClearingHouseAccountSubscriber
32
32
  {
33
33
  isSubscribed: boolean;
34
34
  program: Program;
35
- perpMarketIndexes: BN[];
36
- spotMarketIndexes: BN[];
35
+ perpMarketIndexes: number[];
36
+ spotMarketIndexes: number[];
37
37
  oracleInfos: OracleInfo[];
38
38
  oracleClientCache = new OracleClientCache();
39
39
 
@@ -57,8 +57,8 @@ export class PollingClearingHouseAccountSubscriber
57
57
  public constructor(
58
58
  program: Program,
59
59
  accountLoader: BulkAccountLoader,
60
- perpMarketIndexes: BN[],
61
- spotMarketIndexes: BN[],
60
+ perpMarketIndexes: number[],
61
+ spotMarketIndexes: number[],
62
62
  oracleInfos: OracleInfo[]
63
63
  ) {
64
64
  this.isSubscribed = false;
@@ -132,7 +132,7 @@ export class PollingClearingHouseAccountSubscriber
132
132
  return true;
133
133
  }
134
134
 
135
- async addMarketAccountToPoll(marketIndex: BN): Promise<boolean> {
135
+ async addMarketAccountToPoll(marketIndex: number): Promise<boolean> {
136
136
  const marketPublicKey = await getMarketPublicKey(
137
137
  this.program.programId,
138
138
  marketIndex
@@ -142,7 +142,7 @@ export class PollingClearingHouseAccountSubscriber
142
142
  key: 'perpMarket',
143
143
  publicKey: marketPublicKey,
144
144
  eventType: 'perpMarketAccountUpdate',
145
- mapKey: marketIndex.toNumber(),
145
+ mapKey: marketIndex,
146
146
  });
147
147
 
148
148
  return true;
@@ -156,7 +156,7 @@ export class PollingClearingHouseAccountSubscriber
156
156
  return true;
157
157
  }
158
158
 
159
- async addSpotMarketAccountToPoll(marketIndex: BN): Promise<boolean> {
159
+ async addSpotMarketAccountToPoll(marketIndex: number): Promise<boolean> {
160
160
  const marketPublicKey = await getSpotMarketPublicKey(
161
161
  this.program.programId,
162
162
  marketIndex
@@ -166,7 +166,7 @@ export class PollingClearingHouseAccountSubscriber
166
166
  key: 'spotMarket',
167
167
  publicKey: marketPublicKey,
168
168
  eventType: 'spotMarketAccountUpdate',
169
- mapKey: marketIndex.toNumber(),
169
+ mapKey: marketIndex,
170
170
  });
171
171
  return true;
172
172
  }
@@ -353,14 +353,14 @@ export class PollingClearingHouseAccountSubscriber
353
353
  this.isSubscribed = false;
354
354
  }
355
355
 
356
- async addSpotMarket(marketIndex: BN): Promise<boolean> {
356
+ async addSpotMarket(marketIndex: number): Promise<boolean> {
357
357
  await this.addSpotMarketAccountToPoll(marketIndex);
358
358
  const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
359
359
  this.addAccountToAccountLoader(accountToPoll);
360
360
  return true;
361
361
  }
362
362
 
363
- async addPerpMarket(marketIndex: BN): Promise<boolean> {
363
+ async addPerpMarket(marketIndex: number): Promise<boolean> {
364
364
  await this.addMarketAccountToPoll(marketIndex);
365
365
  const accountToPoll = this.accountsToPoll.get(marketIndex.toString());
366
366
  this.addAccountToAccountLoader(accountToPoll);
@@ -394,9 +394,9 @@ export class PollingClearingHouseAccountSubscriber
394
394
  }
395
395
 
396
396
  public getMarketAccountAndSlot(
397
- marketIndex: BN
397
+ marketIndex: number
398
398
  ): DataAndSlot<PerpMarketAccount> | undefined {
399
- return this.perpMarket.get(marketIndex.toNumber());
399
+ return this.perpMarket.get(marketIndex);
400
400
  }
401
401
 
402
402
  public getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[] {
@@ -404,9 +404,9 @@ export class PollingClearingHouseAccountSubscriber
404
404
  }
405
405
 
406
406
  public getSpotMarketAccountAndSlot(
407
- marketIndex: BN
407
+ marketIndex: number
408
408
  ): DataAndSlot<SpotMarketAccount> | undefined {
409
- return this.spotMarket.get(marketIndex.toNumber());
409
+ return this.spotMarket.get(marketIndex);
410
410
  }
411
411
 
412
412
  public getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[] {
@@ -11,7 +11,6 @@ import { EventEmitter } from 'events';
11
11
  import { PublicKey } from '@solana/web3.js';
12
12
  import { AccountInfo } from '@solana/spl-token';
13
13
  import { OracleInfo, OraclePriceData } from '..';
14
- import { BN } from '@project-serum/anchor';
15
14
 
16
15
  export interface AccountSubscriber<T> {
17
16
  dataAndSlot?: DataAndSlot<T>;
@@ -42,17 +41,17 @@ export interface ClearingHouseAccountSubscriber {
42
41
  fetch(): Promise<void>;
43
42
  unsubscribe(): Promise<void>;
44
43
 
45
- addPerpMarket(marketIndex: BN): Promise<boolean>;
46
- addSpotMarket(marketIndex: BN): Promise<boolean>;
44
+ addPerpMarket(marketIndex: number): Promise<boolean>;
45
+ addSpotMarket(marketIndex: number): Promise<boolean>;
47
46
  addOracle(oracleInfo: OracleInfo): Promise<boolean>;
48
47
 
49
48
  getStateAccountAndSlot(): DataAndSlot<StateAccount>;
50
49
  getMarketAccountAndSlot(
51
- marketIndex: BN
50
+ marketIndex: number
52
51
  ): DataAndSlot<PerpMarketAccount> | undefined;
53
52
  getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
54
53
  getSpotMarketAccountAndSlot(
55
- marketIndex: BN
54
+ marketIndex: number
56
55
  ): DataAndSlot<SpotMarketAccount> | undefined;
57
56
  getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
58
57
  getOraclePriceDataAndSlot(
@@ -5,7 +5,7 @@ import {
5
5
  } from './types';
6
6
  import { AccountSubscriber, NotSubscribedError } from './types';
7
7
  import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
8
- import { BN, Program } from '@project-serum/anchor';
8
+ import { Program } from '@project-serum/anchor';
9
9
  import StrictEventEmitter from 'strict-event-emitter-types';
10
10
  import { EventEmitter } from 'events';
11
11
  import {
@@ -25,8 +25,8 @@ export class WebSocketClearingHouseAccountSubscriber
25
25
  {
26
26
  isSubscribed: boolean;
27
27
  program: Program;
28
- perpMarketIndexes: BN[];
29
- spotMarketIndexes: BN[];
28
+ perpMarketIndexes: number[];
29
+ spotMarketIndexes: number[];
30
30
  oracleInfos: OracleInfo[];
31
31
  oracleClientCache = new OracleClientCache();
32
32
 
@@ -48,8 +48,8 @@ export class WebSocketClearingHouseAccountSubscriber
48
48
 
49
49
  public constructor(
50
50
  program: Program,
51
- perpMarketIndexes: BN[],
52
- spotMarketIndexes: BN[],
51
+ perpMarketIndexes: number[],
52
+ spotMarketIndexes: number[],
53
53
  oracleInfos: OracleInfo[]
54
54
  ) {
55
55
  this.isSubscribed = false;
@@ -115,7 +115,7 @@ export class WebSocketClearingHouseAccountSubscriber
115
115
  return true;
116
116
  }
117
117
 
118
- async subscribeToMarketAccount(marketIndex: BN): Promise<boolean> {
118
+ async subscribeToMarketAccount(marketIndex: number): Promise<boolean> {
119
119
  const marketPublicKey = await getMarketPublicKey(
120
120
  this.program.programId,
121
121
  marketIndex
@@ -129,10 +129,7 @@ export class WebSocketClearingHouseAccountSubscriber
129
129
  this.eventEmitter.emit('perpMarketAccountUpdate', data);
130
130
  this.eventEmitter.emit('update');
131
131
  });
132
- this.perpMarketAccountSubscribers.set(
133
- marketIndex.toNumber(),
134
- accountSubscriber
135
- );
132
+ this.perpMarketAccountSubscribers.set(marketIndex, accountSubscriber);
136
133
  return true;
137
134
  }
138
135
 
@@ -143,7 +140,7 @@ export class WebSocketClearingHouseAccountSubscriber
143
140
  return true;
144
141
  }
145
142
 
146
- async subscribeToSpotMarketAccount(marketIndex: BN): Promise<boolean> {
143
+ async subscribeToSpotMarketAccount(marketIndex: number): Promise<boolean> {
147
144
  const marketPublicKey = await getSpotMarketPublicKey(
148
145
  this.program.programId,
149
146
  marketIndex
@@ -157,10 +154,7 @@ export class WebSocketClearingHouseAccountSubscriber
157
154
  this.eventEmitter.emit('spotMarketAccountUpdate', data);
158
155
  this.eventEmitter.emit('update');
159
156
  });
160
- this.spotMarketAccountSubscribers.set(
161
- marketIndex.toNumber(),
162
- accountSubscriber
163
- );
157
+ this.spotMarketAccountSubscribers.set(marketIndex, accountSubscriber);
164
158
  return true;
165
159
  }
166
160
 
@@ -252,15 +246,15 @@ export class WebSocketClearingHouseAccountSubscriber
252
246
  this.isSubscribed = false;
253
247
  }
254
248
 
255
- async addSpotMarket(marketIndex: BN): Promise<boolean> {
256
- if (this.spotMarketAccountSubscribers.has(marketIndex.toNumber())) {
249
+ async addSpotMarket(marketIndex: number): Promise<boolean> {
250
+ if (this.spotMarketAccountSubscribers.has(marketIndex)) {
257
251
  return true;
258
252
  }
259
253
  return this.subscribeToSpotMarketAccount(marketIndex);
260
254
  }
261
255
 
262
- async addPerpMarket(marketIndex: BN): Promise<boolean> {
263
- if (this.perpMarketAccountSubscribers.has(marketIndex.toNumber())) {
256
+ async addPerpMarket(marketIndex: number): Promise<boolean> {
257
+ if (this.perpMarketAccountSubscribers.has(marketIndex)) {
264
258
  return true;
265
259
  }
266
260
  return this.subscribeToMarketAccount(marketIndex);
@@ -292,11 +286,10 @@ export class WebSocketClearingHouseAccountSubscriber
292
286
  }
293
287
 
294
288
  public getMarketAccountAndSlot(
295
- marketIndex: BN
289
+ marketIndex: number
296
290
  ): DataAndSlot<PerpMarketAccount> | undefined {
297
291
  this.assertIsSubscribed();
298
- return this.perpMarketAccountSubscribers.get(marketIndex.toNumber())
299
- .dataAndSlot;
292
+ return this.perpMarketAccountSubscribers.get(marketIndex).dataAndSlot;
300
293
  }
301
294
 
302
295
  public getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[] {
@@ -306,11 +299,10 @@ export class WebSocketClearingHouseAccountSubscriber
306
299
  }
307
300
 
308
301
  public getSpotMarketAccountAndSlot(
309
- marketIndex: BN
302
+ marketIndex: number
310
303
  ): DataAndSlot<SpotMarketAccount> | undefined {
311
304
  this.assertIsSubscribed();
312
- return this.spotMarketAccountSubscribers.get(marketIndex.toNumber())
313
- .dataAndSlot;
305
+ return this.spotMarketAccountSubscribers.get(marketIndex).dataAndSlot;
314
306
  }
315
307
 
316
308
  public getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[] {
@@ -1,11 +1,10 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
- import { BN } from '@project-serum/anchor';
3
2
  import { getMarketPublicKey } from './pda';
4
3
 
5
4
  const CACHE = new Map<string, PublicKey>();
6
5
  export async function getMarketAddress(
7
6
  programId: PublicKey,
8
- marketIndex: BN
7
+ marketIndex: number
9
8
  ): Promise<PublicKey> {
10
9
  const cacheKey = `${programId.toString()}-${marketIndex.toString()}`;
11
10
  if (CACHE.has(cacheKey)) {