@drift-labs/sdk 2.10.0-beta.3 → 2.10.0-beta.4

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 (57) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +1 -2
  2. package/lib/accounts/types.d.ts +7 -7
  3. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
  4. package/lib/accounts/webSocketAccountSubscriber.js +12 -1
  5. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +1 -1
  6. package/lib/accounts/webSocketUserAccountSubscriber.js +4 -1
  7. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +1 -1
  8. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +4 -1
  9. package/lib/config.d.ts +2 -2
  10. package/lib/constants/perpMarkets.d.ts +1 -1
  11. package/lib/constants/spotMarkets.d.ts +1 -1
  12. package/lib/dlob/DLOB.d.ts +4 -4
  13. package/lib/dlob/DLOBNode.d.ts +2 -2
  14. package/lib/dlob/DLOBOrders.d.ts +2 -2
  15. package/lib/dlob/NodeList.d.ts +1 -1
  16. package/lib/driftClient.d.ts +1 -1
  17. package/lib/driftClient.js +6 -6
  18. package/lib/driftClientConfig.d.ts +3 -3
  19. package/lib/events/fetchLogs.d.ts +2 -2
  20. package/lib/events/types.d.ts +13 -13
  21. package/lib/factory/bigNum.js +4 -4
  22. package/lib/idl/drift.json +1 -1
  23. package/lib/math/amm.d.ts +1 -1
  24. package/lib/math/trade.d.ts +1 -1
  25. package/lib/oracles/types.d.ts +2 -2
  26. package/lib/serum/types.d.ts +1 -1
  27. package/lib/slot/SlotSubscriber.d.ts +1 -1
  28. package/lib/tx/retryTxSender.d.ts +1 -1
  29. package/lib/tx/types.d.ts +1 -1
  30. package/lib/types.d.ts +43 -43
  31. package/lib/user.d.ts +1 -1
  32. package/lib/user.js +8 -8
  33. package/lib/userConfig.d.ts +2 -2
  34. package/lib/userMap/userMap.js +3 -1
  35. package/lib/userMap/userStatsMap.js +3 -1
  36. package/lib/userStats.d.ts +1 -1
  37. package/lib/userStats.js +2 -2
  38. package/lib/userStatsConfig.d.ts +2 -2
  39. package/package.json +1 -1
  40. package/src/accounts/types.ts +4 -2
  41. package/src/accounts/webSocketAccountSubscriber.ts +14 -1
  42. package/src/accounts/webSocketUserAccountSubscriber.ts +6 -1
  43. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +6 -1
  44. package/src/assert/assert.js +9 -0
  45. package/src/examples/makeTradeExample.js +157 -0
  46. package/src/idl/drift.json +1 -1
  47. package/src/token/index.js +38 -0
  48. package/src/tx/types.js +2 -0
  49. package/src/tx/utils.js +17 -0
  50. package/src/user.ts +2 -2
  51. package/src/userMap/userMap.ts +5 -1
  52. package/src/userMap/userStatsMap.ts +7 -1
  53. package/src/userStats.ts +6 -2
  54. package/src/util/computeUnits.js +27 -0
  55. package/src/util/getTokenAddress.js +9 -0
  56. package/src/util/promiseTimeout.js +14 -0
  57. package/src/util/tps.js +27 -0
package/lib/types.d.ts CHANGED
@@ -317,8 +317,8 @@ export declare enum TradeSide {
317
317
  Buy = 1,
318
318
  Sell = 2
319
319
  }
320
- export type CandleResolution = '1' | '5' | '15' | '60' | '240' | 'D' | 'W' | 'M';
321
- export type NewUserRecord = {
320
+ export declare type CandleResolution = '1' | '5' | '15' | '60' | '240' | 'D' | 'W' | 'M';
321
+ export declare type NewUserRecord = {
322
322
  ts: BN;
323
323
  userAuthority: PublicKey;
324
324
  user: PublicKey;
@@ -326,7 +326,7 @@ export type NewUserRecord = {
326
326
  name: number[];
327
327
  referrer: PublicKey;
328
328
  };
329
- export type DepositRecord = {
329
+ export declare type DepositRecord = {
330
330
  ts: BN;
331
331
  userAuthority: PublicKey;
332
332
  user: PublicKey;
@@ -347,7 +347,7 @@ export type DepositRecord = {
347
347
  explanation: DepositExplanation;
348
348
  transferUser?: PublicKey;
349
349
  };
350
- export type SpotInterestRecord = {
350
+ export declare type SpotInterestRecord = {
351
351
  ts: BN;
352
352
  marketIndex: number;
353
353
  depositBalance: BN;
@@ -358,7 +358,7 @@ export type SpotInterestRecord = {
358
358
  optimalBorrowRate: number;
359
359
  maxBorrowRate: number;
360
360
  };
361
- export type CurveRecord = {
361
+ export declare type CurveRecord = {
362
362
  ts: BN;
363
363
  recordId: BN;
364
364
  marketIndex: number;
@@ -406,7 +406,7 @@ export declare type InsuranceFundStakeRecord = {
406
406
  userIfSharesAfter: BN;
407
407
  totalIfSharesAfter: BN;
408
408
  };
409
- export type LPRecord = {
409
+ export declare type LPRecord = {
410
410
  ts: BN;
411
411
  user: PublicKey;
412
412
  action: LPAction;
@@ -427,7 +427,7 @@ export declare class LPAction {
427
427
  settleLiquidity: {};
428
428
  };
429
429
  }
430
- export type FundingRateRecord = {
430
+ export declare type FundingRateRecord = {
431
431
  ts: BN;
432
432
  recordId: BN;
433
433
  marketIndex: number;
@@ -442,7 +442,7 @@ export type FundingRateRecord = {
442
442
  baseAssetAmountWithAmm: BN;
443
443
  baseAssetAmountWithUnsettledLp: BN;
444
444
  };
445
- export type FundingPaymentRecord = {
445
+ export declare type FundingPaymentRecord = {
446
446
  ts: BN;
447
447
  userAuthority: PublicKey;
448
448
  user: PublicKey;
@@ -453,7 +453,7 @@ export type FundingPaymentRecord = {
453
453
  ammCumulativeFundingLong: BN;
454
454
  ammCumulativeFundingShort: BN;
455
455
  };
456
- export type LiquidationRecord = {
456
+ export declare type LiquidationRecord = {
457
457
  ts: BN;
458
458
  user: PublicKey;
459
459
  liquidator: PublicKey;
@@ -491,7 +491,7 @@ export declare class LiquidationType {
491
491
  liquidateSpot: {};
492
492
  };
493
493
  }
494
- export type LiquidatePerpRecord = {
494
+ export declare type LiquidatePerpRecord = {
495
495
  marketIndex: number;
496
496
  oraclePrice: BN;
497
497
  baseAssetAmount: BN;
@@ -503,7 +503,7 @@ export type LiquidatePerpRecord = {
503
503
  liquidatorFee: BN;
504
504
  ifFee: BN;
505
505
  };
506
- export type LiquidateSpotRecord = {
506
+ export declare type LiquidateSpotRecord = {
507
507
  assetMarketIndex: number;
508
508
  assetPrice: BN;
509
509
  assetTransfer: BN;
@@ -512,7 +512,7 @@ export type LiquidateSpotRecord = {
512
512
  liabilityTransfer: BN;
513
513
  ifFee: BN;
514
514
  };
515
- export type LiquidateBorrowForPerpPnlRecord = {
515
+ export declare type LiquidateBorrowForPerpPnlRecord = {
516
516
  perpMarketIndex: number;
517
517
  marketOraclePrice: BN;
518
518
  pnlTransfer: BN;
@@ -520,7 +520,7 @@ export type LiquidateBorrowForPerpPnlRecord = {
520
520
  liabilityPrice: BN;
521
521
  liabilityTransfer: BN;
522
522
  };
523
- export type LiquidatePerpPnlForDepositRecord = {
523
+ export declare type LiquidatePerpPnlForDepositRecord = {
524
524
  perpMarketIndex: number;
525
525
  marketOraclePrice: BN;
526
526
  pnlTransfer: BN;
@@ -528,7 +528,7 @@ export type LiquidatePerpPnlForDepositRecord = {
528
528
  assetPrice: BN;
529
529
  assetTransfer: BN;
530
530
  };
531
- export type PerpBankruptcyRecord = {
531
+ export declare type PerpBankruptcyRecord = {
532
532
  marketIndex: number;
533
533
  pnl: BN;
534
534
  ifPayment: BN;
@@ -536,13 +536,13 @@ export type PerpBankruptcyRecord = {
536
536
  clawbackUserPayment: BN | null;
537
537
  cumulativeFundingRateDelta: BN;
538
538
  };
539
- export type SpotBankruptcyRecord = {
539
+ export declare type SpotBankruptcyRecord = {
540
540
  marketIndex: number;
541
541
  borrowAmount: BN;
542
542
  cumulativeDepositInterestDelta: BN;
543
543
  ifPayment: BN;
544
544
  };
545
- export type SettlePnlRecord = {
545
+ export declare type SettlePnlRecord = {
546
546
  ts: BN;
547
547
  user: PublicKey;
548
548
  marketIndex: number;
@@ -553,12 +553,12 @@ export type SettlePnlRecord = {
553
553
  settlePrice: BN;
554
554
  explanation: SettlePnlExplanation;
555
555
  };
556
- export type OrderRecord = {
556
+ export declare type OrderRecord = {
557
557
  ts: BN;
558
558
  user: PublicKey;
559
559
  order: Order;
560
560
  };
561
- export type OrderActionRecord = {
561
+ export declare type OrderActionRecord = {
562
562
  ts: BN;
563
563
  action: OrderAction;
564
564
  actionExplanation: OrderActionExplanation;
@@ -588,7 +588,7 @@ export type OrderActionRecord = {
588
588
  makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
589
589
  oraclePrice: BN;
590
590
  };
591
- export type StateAccount = {
591
+ export declare type StateAccount = {
592
592
  admin: PublicKey;
593
593
  exchangeStatus: ExchangeStatus;
594
594
  whitelistMint: PublicKey;
@@ -612,7 +612,7 @@ export type StateAccount = {
612
612
  initialPctToLiquidate: number;
613
613
  liquidationDuration: number;
614
614
  };
615
- export type PerpMarketAccount = {
615
+ export declare type PerpMarketAccount = {
616
616
  status: MarketStatus;
617
617
  contractType: ContractType;
618
618
  contractTier: ContractTier;
@@ -645,7 +645,7 @@ export type PerpMarketAccount = {
645
645
  quoteMaxInsurance: BN;
646
646
  };
647
647
  };
648
- export type HistoricalOracleData = {
648
+ export declare type HistoricalOracleData = {
649
649
  lastOraclePrice: BN;
650
650
  lastOracleDelay: BN;
651
651
  lastOracleConf: BN;
@@ -653,14 +653,14 @@ export type HistoricalOracleData = {
653
653
  lastOraclePriceTwap5Min: BN;
654
654
  lastOraclePriceTwapTs: BN;
655
655
  };
656
- export type HistoricalIndexData = {
656
+ export declare type HistoricalIndexData = {
657
657
  lastIndexBidPrice: BN;
658
658
  lastIndexAskPrice: BN;
659
659
  lastIndexPriceTwap: BN;
660
660
  lastIndexPriceTwap5Min: BN;
661
661
  lastIndexPriceTwapTs: BN;
662
662
  };
663
- export type SpotMarketAccount = {
663
+ export declare type SpotMarketAccount = {
664
664
  status: MarketStatus;
665
665
  assetTier: AssetTier;
666
666
  marketIndex: number;
@@ -715,11 +715,11 @@ export type SpotMarketAccount = {
715
715
  totalSpotFee: BN;
716
716
  ordersEnabled: boolean;
717
717
  };
718
- export type PoolBalance = {
718
+ export declare type PoolBalance = {
719
719
  scaledBalance: BN;
720
720
  marketIndex: number;
721
721
  };
722
- export type AMM = {
722
+ export declare type AMM = {
723
723
  baseAssetReserve: BN;
724
724
  sqrtK: BN;
725
725
  cumulativeFundingRate: BN;
@@ -797,7 +797,7 @@ export type AMM = {
797
797
  askBaseAssetReserve: BN;
798
798
  askQuoteAssetReserve: BN;
799
799
  };
800
- export type PerpPosition = {
800
+ export declare type PerpPosition = {
801
801
  baseAssetAmount: BN;
802
802
  lastCumulativeFundingRate: BN;
803
803
  marketIndex: number;
@@ -813,7 +813,7 @@ export type PerpPosition = {
813
813
  lastBaseAssetAmountPerLp: BN;
814
814
  lastQuoteAssetAmountPerLp: BN;
815
815
  };
816
- export type UserStatsAccount = {
816
+ export declare type UserStatsAccount = {
817
817
  numberOfSubAccounts: number;
818
818
  numberOfSubAccountsCreated: number;
819
819
  makerVolume30D: BN;
@@ -835,7 +835,7 @@ export type UserStatsAccount = {
835
835
  authority: PublicKey;
836
836
  ifStakedQuoteAssetAmount: BN;
837
837
  };
838
- export type UserAccount = {
838
+ export declare type UserAccount = {
839
839
  authority: PublicKey;
840
840
  delegate: PublicKey;
841
841
  name: number[];
@@ -857,7 +857,7 @@ export type UserAccount = {
857
857
  liquidationStartSlot: BN;
858
858
  isMarginTradingEnabled: boolean;
859
859
  };
860
- export type SpotPosition = {
860
+ export declare type SpotPosition = {
861
861
  marketIndex: number;
862
862
  balanceType: SpotBalanceType;
863
863
  scaledBalance: BN;
@@ -866,7 +866,7 @@ export type SpotPosition = {
866
866
  openAsks: BN;
867
867
  cumulativeDeposits: BN;
868
868
  };
869
- export type Order = {
869
+ export declare type Order = {
870
870
  status: OrderStatus;
871
871
  orderType: OrderType;
872
872
  marketType: MarketType;
@@ -892,7 +892,7 @@ export type Order = {
892
892
  auctionEndPrice: BN;
893
893
  maxTs: BN;
894
894
  };
895
- export type OrderParams = {
895
+ export declare type OrderParams = {
896
896
  orderType: OrderType;
897
897
  marketType: MarketType;
898
898
  userOrderId: number;
@@ -912,29 +912,29 @@ export type OrderParams = {
912
912
  auctionStartPrice: BN | null;
913
913
  auctionEndPrice: BN | null;
914
914
  };
915
- export type NecessaryOrderParams = {
915
+ export declare type NecessaryOrderParams = {
916
916
  orderType: OrderType;
917
917
  marketIndex: number;
918
918
  baseAssetAmount: BN;
919
919
  direction: PositionDirection;
920
920
  };
921
- export type OptionalOrderParams = {
921
+ export declare type OptionalOrderParams = {
922
922
  [Property in keyof OrderParams]?: OrderParams[Property];
923
923
  } & NecessaryOrderParams;
924
924
  export declare const DefaultOrderParams: OrderParams;
925
- export type MakerInfo = {
925
+ export declare type MakerInfo = {
926
926
  maker: PublicKey;
927
927
  makerStats: PublicKey;
928
928
  makerUserAccount: UserAccount;
929
929
  order: Order;
930
930
  };
931
- export type TakerInfo = {
931
+ export declare type TakerInfo = {
932
932
  taker: PublicKey;
933
933
  takerStats: PublicKey;
934
934
  takerUserAccount: UserAccount;
935
935
  order: Order;
936
936
  };
937
- export type ReferrerInfo = {
937
+ export declare type ReferrerInfo = {
938
938
  referrer: PublicKey;
939
939
  referrerStats: PublicKey;
940
940
  };
@@ -943,7 +943,7 @@ export interface IWallet {
943
943
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
944
944
  publicKey: PublicKey;
945
945
  }
946
- export type FeeStructure = {
946
+ export declare type FeeStructure = {
947
947
  feeTiers: FeeTier[];
948
948
  makerRebateNumerator: BN;
949
949
  makerRebateDenominator: BN;
@@ -951,7 +951,7 @@ export type FeeStructure = {
951
951
  flatFillerFee: BN;
952
952
  referrerRewardEpochUpperBound: BN;
953
953
  };
954
- export type FeeTier = {
954
+ export declare type FeeTier = {
955
955
  feeNumerator: number;
956
956
  feeDenominator: number;
957
957
  makerRebateNumerator: number;
@@ -961,12 +961,12 @@ export type FeeTier = {
961
961
  refereeFeeNumerator: number;
962
962
  refereeFeeDenominator: number;
963
963
  };
964
- export type OrderFillerRewardStructure = {
964
+ export declare type OrderFillerRewardStructure = {
965
965
  rewardNumerator: BN;
966
966
  rewardDenominator: BN;
967
967
  timeBasedRewardLowerBound: BN;
968
968
  };
969
- export type OracleGuardRails = {
969
+ export declare type OracleGuardRails = {
970
970
  priceDivergence: {
971
971
  markOracleDivergenceNumerator: BN;
972
972
  markOracleDivergenceDenominator: BN;
@@ -978,8 +978,8 @@ export type OracleGuardRails = {
978
978
  tooVolatileRatio: BN;
979
979
  };
980
980
  };
981
- export type MarginCategory = 'Initial' | 'Maintenance';
982
- export type InsuranceFundStake = {
981
+ export declare type MarginCategory = 'Initial' | 'Maintenance';
982
+ export declare type InsuranceFundStake = {
983
983
  marketIndex: number;
984
984
  authority: PublicKey;
985
985
  ifShares: BN;
@@ -988,7 +988,7 @@ export type InsuranceFundStake = {
988
988
  lastWithdrawRequestValue: BN;
989
989
  lastWithdrawRequestTs: BN;
990
990
  };
991
- export type SerumV3FulfillmentConfigAccount = {
991
+ export declare type SerumV3FulfillmentConfigAccount = {
992
992
  fulfillmentType: SpotFulfillmentType;
993
993
  status: SpotFulfillmentStatus;
994
994
  pubkey: PublicKey;
package/lib/user.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare class User {
22
22
  * Subscribe to User state accounts
23
23
  * @returns SusbcriptionSuccess result
24
24
  */
25
- subscribe(): Promise<boolean>;
25
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
26
26
  /**
27
27
  * Forces the accountSubscriber to fetch account updates from rpc
28
28
  */
package/lib/user.js CHANGED
@@ -12,12 +12,6 @@ const pollingUserAccountSubscriber_1 = require("./accounts/pollingUserAccountSub
12
12
  const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccountSubscriber");
13
13
  const spotPosition_1 = require("./math/spotPosition");
14
14
  class User {
15
- get isSubscribed() {
16
- return this._isSubscribed && this.accountSubscriber.isSubscribed;
17
- }
18
- set isSubscribed(val) {
19
- this._isSubscribed = val;
20
- }
21
15
  constructor(config) {
22
16
  var _a;
23
17
  this._isSubscribed = false;
@@ -31,12 +25,18 @@ class User {
31
25
  }
32
26
  this.eventEmitter = this.accountSubscriber.eventEmitter;
33
27
  }
28
+ get isSubscribed() {
29
+ return this._isSubscribed && this.accountSubscriber.isSubscribed;
30
+ }
31
+ set isSubscribed(val) {
32
+ this._isSubscribed = val;
33
+ }
34
34
  /**
35
35
  * Subscribe to User state accounts
36
36
  * @returns SusbcriptionSuccess result
37
37
  */
38
- async subscribe() {
39
- this.isSubscribed = await this.accountSubscriber.subscribe();
38
+ async subscribe(userAccount) {
39
+ this.isSubscribed = await this.accountSubscriber.subscribe(userAccount);
40
40
  return this.isSubscribed;
41
41
  }
42
42
  /**
@@ -1,12 +1,12 @@
1
1
  import { DriftClient } from './driftClient';
2
2
  import { PublicKey } from '@solana/web3.js';
3
3
  import { BulkAccountLoader } from './accounts/bulkAccountLoader';
4
- export type UserConfig = {
4
+ export declare type UserConfig = {
5
5
  accountSubscription?: UserSubscriptionConfig;
6
6
  driftClient: DriftClient;
7
7
  userAccountPublicKey: PublicKey;
8
8
  };
9
- export type UserSubscriptionConfig = {
9
+ export declare type UserSubscriptionConfig = {
10
10
  type: 'websocket';
11
11
  } | {
12
12
  type: 'polling';
@@ -11,6 +11,7 @@ class UserMap {
11
11
  }
12
12
  async fetchAllUsers() {
13
13
  const userArray = [];
14
+ const userAccountArray = [];
14
15
  const programUserAccounts = (await this.driftClient.program.account.user.all());
15
16
  for (const programUserAccount of programUserAccounts) {
16
17
  if (this.userMap.has(programUserAccount.publicKey.toString())) {
@@ -22,12 +23,13 @@ class UserMap {
22
23
  accountSubscription: this.accountSubscription,
23
24
  });
24
25
  userArray.push(user);
26
+ userAccountArray.push(programUserAccount.account);
25
27
  }
26
28
  if (this.accountSubscription.type === 'polling') {
27
29
  await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
28
30
  }
29
31
  else {
30
- await Promise.all(userArray.map((user) => user.subscribe()));
32
+ await Promise.all(userArray.map((user, i) => user.subscribe(userAccountArray[i])));
31
33
  }
32
34
  for (const user of userArray) {
33
35
  this.userMap.set(user.getUserAccountPublicKey().toString(), user);
@@ -14,6 +14,7 @@ class UserStatsMap {
14
14
  }
15
15
  async fetchAllUserStats() {
16
16
  const userStatArray = [];
17
+ const userStatsAccountArray = [];
17
18
  const programUserAccounts = (await this.driftClient.program.account.userStats.all());
18
19
  for (const programUserAccount of programUserAccounts) {
19
20
  const userStat = programUserAccount.account;
@@ -26,12 +27,13 @@ class UserStatsMap {
26
27
  accountSubscription: this.accountSubscription,
27
28
  });
28
29
  userStatArray.push(chUserStat);
30
+ userStatsAccountArray.push(userStat);
29
31
  }
30
32
  if (this.accountSubscription.type === 'polling') {
31
33
  await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
32
34
  }
33
35
  else {
34
- await Promise.all(userStatArray.map((userStat) => userStat.subscribe()));
36
+ await Promise.all(userStatArray.map((userStat, i) => userStat.subscribe(userStatsAccountArray[i])));
35
37
  }
36
38
  for (const userStat of userStatArray) {
37
39
  this.userStatsMap.set(userStat.getAccount().authority.toString(), userStat);
@@ -9,7 +9,7 @@ export declare class UserStats {
9
9
  accountSubscriber: UserStatsAccountSubscriber;
10
10
  isSubscribed: boolean;
11
11
  constructor(config: UserStatsConfig);
12
- subscribe(): Promise<boolean>;
12
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
13
13
  fetchAccounts(): Promise<void>;
14
14
  unsubscribe(): Promise<void>;
15
15
  getAccountAndSlot(): DataAndSlot<UserStatsAccount>;
package/lib/userStats.js CHANGED
@@ -17,8 +17,8 @@ class UserStats {
17
17
  this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey);
18
18
  }
19
19
  }
20
- async subscribe() {
21
- this.isSubscribed = await this.accountSubscriber.subscribe();
20
+ async subscribe(userStatsAccount) {
21
+ this.isSubscribed = await this.accountSubscriber.subscribe(userStatsAccount);
22
22
  return this.isSubscribed;
23
23
  }
24
24
  async fetchAccounts() {
@@ -1,12 +1,12 @@
1
1
  import { DriftClient } from './driftClient';
2
2
  import { PublicKey } from '@solana/web3.js';
3
3
  import { BulkAccountLoader } from './accounts/bulkAccountLoader';
4
- export type UserStatsConfig = {
4
+ export declare type UserStatsConfig = {
5
5
  accountSubscription?: UserStatsSubscriptionConfig;
6
6
  driftClient: DriftClient;
7
7
  userStatsAccountPublicKey: PublicKey;
8
8
  };
9
- export type UserStatsSubscriptionConfig = {
9
+ export declare type UserStatsSubscriptionConfig = {
10
10
  type: 'websocket';
11
11
  } | {
12
12
  type: 'polling';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.10.0-beta.3",
3
+ "version": "2.10.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -17,6 +17,8 @@ export interface AccountSubscriber<T> {
17
17
  subscribe(onChange: (data: T) => void): Promise<void>;
18
18
  fetch(): Promise<void>;
19
19
  unsubscribe(): Promise<void>;
20
+
21
+ setData(userAccount: T): void;
20
22
  }
21
23
 
22
24
  export class NotSubscribedError extends Error {
@@ -69,7 +71,7 @@ export interface UserAccountSubscriber {
69
71
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
70
72
  isSubscribed: boolean;
71
73
 
72
- subscribe(): Promise<boolean>;
74
+ subscribe(userAccount?: UserAccount): Promise<boolean>;
73
75
  fetch(): Promise<void>;
74
76
  unsubscribe(): Promise<void>;
75
77
 
@@ -144,7 +146,7 @@ export interface UserStatsAccountSubscriber {
144
146
  eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
145
147
  isSubscribed: boolean;
146
148
 
147
- subscribe(): Promise<boolean>;
149
+ subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
148
150
  fetch(): Promise<void>;
149
151
  unsubscribe(): Promise<void>;
150
152
 
@@ -32,7 +32,9 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
32
32
  }
33
33
 
34
34
  this.onChange = onChange;
35
- await this.fetch();
35
+ if (!this.dataAndSlot) {
36
+ await this.fetch();
37
+ }
36
38
 
37
39
  this.listenerId = this.program.provider.connection.onAccountChange(
38
40
  this.accountPublicKey,
@@ -43,6 +45,17 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
43
45
  );
44
46
  }
45
47
 
48
+ setData(data: T): void {
49
+ if (this.dataAndSlot) {
50
+ return;
51
+ }
52
+
53
+ this.dataAndSlot = {
54
+ data,
55
+ slot: undefined,
56
+ };
57
+ }
58
+
46
59
  async fetch(): Promise<void> {
47
60
  const rpcResponse =
48
61
  await this.program.provider.connection.getAccountInfoAndContext(
@@ -27,7 +27,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
27
27
  this.eventEmitter = new EventEmitter();
28
28
  }
29
29
 
30
- async subscribe(): Promise<boolean> {
30
+ async subscribe(userAccount?: UserAccount): Promise<boolean> {
31
31
  if (this.isSubscribed) {
32
32
  return true;
33
33
  }
@@ -37,6 +37,11 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
37
37
  this.program,
38
38
  this.userAccountPublicKey
39
39
  );
40
+
41
+ if (userAccount) {
42
+ this.userDataAccountSubscriber.setData(userAccount);
43
+ }
44
+
40
45
  await this.userDataAccountSubscriber.subscribe((data: UserAccount) => {
41
46
  this.eventEmitter.emit('userAccountUpdate', data);
42
47
  this.eventEmitter.emit('update');
@@ -29,7 +29,7 @@ export class WebSocketUserStatsAccountSubscriber
29
29
  this.eventEmitter = new EventEmitter();
30
30
  }
31
31
 
32
- async subscribe(): Promise<boolean> {
32
+ async subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean> {
33
33
  if (this.isSubscribed) {
34
34
  return true;
35
35
  }
@@ -39,6 +39,11 @@ export class WebSocketUserStatsAccountSubscriber
39
39
  this.program,
40
40
  this.userStatsAccountPublicKey
41
41
  );
42
+
43
+ if (userStatsAccount) {
44
+ this.userStatsAccountSubscriber.setData(userStatsAccount);
45
+ }
46
+
42
47
  await this.userStatsAccountSubscriber.subscribe(
43
48
  (data: UserStatsAccount) => {
44
49
  this.eventEmitter.emit('userStatsAccountUpdate', data);
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assert = void 0;
4
+ function assert(condition, error) {
5
+ if (!condition) {
6
+ throw new Error(error || 'Unspecified AssertionError');
7
+ }
8
+ }
9
+ exports.assert = assert;