@drift-labs/sdk 2.19.0 → 2.20.0-beta.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.
Files changed (81) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +2 -1
  2. package/lib/accounts/bulkAccountLoader.js +7 -7
  3. package/lib/accounts/fetch.d.ts +1 -0
  4. package/lib/accounts/fetch.js +8 -4
  5. package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
  6. package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
  7. package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
  8. package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
  9. package/lib/accounts/types.d.ts +5 -4
  10. package/lib/accounts/webSocketAccountSubscriber.js +1 -1
  11. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
  12. package/lib/addresses/marketAddresses.js +1 -1
  13. package/lib/addresses/pda.js +5 -1
  14. package/lib/adminClient.js +61 -57
  15. package/lib/config.d.ts +2 -2
  16. package/lib/constants/perpMarkets.d.ts +1 -1
  17. package/lib/constants/spotMarkets.d.ts +1 -1
  18. package/lib/dlob/DLOB.d.ts +6 -5
  19. package/lib/dlob/DLOB.js +105 -75
  20. package/lib/dlob/DLOBNode.d.ts +2 -2
  21. package/lib/dlob/DLOBNode.js +7 -7
  22. package/lib/dlob/DLOBOrders.d.ts +2 -2
  23. package/lib/dlob/NodeList.d.ts +1 -1
  24. package/lib/dlob/NodeList.js +2 -2
  25. package/lib/driftClient.d.ts +5 -4
  26. package/lib/driftClient.js +139 -112
  27. package/lib/driftClientConfig.d.ts +3 -3
  28. package/lib/events/eventSubscriber.js +2 -2
  29. package/lib/events/fetchLogs.d.ts +2 -2
  30. package/lib/events/pollingLogProvider.js +1 -1
  31. package/lib/events/types.d.ts +14 -14
  32. package/lib/examples/loadDlob.js +2 -2
  33. package/lib/examples/makeTradeExample.js +9 -9
  34. package/lib/factory/bigNum.js +13 -13
  35. package/lib/factory/oracleClient.js +4 -4
  36. package/lib/idl/drift.json +52 -1
  37. package/lib/index.js +5 -1
  38. package/lib/math/amm.d.ts +1 -1
  39. package/lib/math/amm.js +23 -23
  40. package/lib/math/auction.js +6 -6
  41. package/lib/math/exchangeStatus.js +2 -2
  42. package/lib/math/funding.js +2 -2
  43. package/lib/math/margin.js +4 -4
  44. package/lib/math/market.js +15 -15
  45. package/lib/math/oracles.js +1 -1
  46. package/lib/math/orders.js +27 -24
  47. package/lib/math/position.js +5 -5
  48. package/lib/math/repeg.js +1 -1
  49. package/lib/math/spotBalance.js +9 -9
  50. package/lib/math/spotMarket.js +3 -3
  51. package/lib/math/spotPosition.js +3 -3
  52. package/lib/math/trade.d.ts +1 -1
  53. package/lib/math/trade.js +42 -42
  54. package/lib/math/utils.js +1 -1
  55. package/lib/oracles/oracleClientCache.js +1 -1
  56. package/lib/oracles/pythClient.js +1 -1
  57. package/lib/oracles/types.d.ts +2 -2
  58. package/lib/serum/types.d.ts +1 -1
  59. package/lib/slot/SlotSubscriber.d.ts +1 -1
  60. package/lib/tokenFaucet.js +5 -1
  61. package/lib/tx/retryTxSender.d.ts +1 -1
  62. package/lib/tx/retryTxSender.js +1 -1
  63. package/lib/tx/types.d.ts +1 -1
  64. package/lib/types.d.ts +66 -45
  65. package/lib/user.js +63 -63
  66. package/lib/userConfig.d.ts +2 -2
  67. package/lib/userMap/userMap.js +1 -1
  68. package/lib/userMap/userStatsMap.js +3 -3
  69. package/lib/userStats.js +2 -2
  70. package/lib/userStatsConfig.d.ts +2 -2
  71. package/package.json +1 -1
  72. package/src/accounts/bulkAccountLoader.ts +5 -5
  73. package/src/accounts/fetch.ts +8 -0
  74. package/src/dlob/DLOB.ts +55 -8
  75. package/src/driftClient.ts +57 -20
  76. package/src/idl/drift.json +52 -1
  77. package/src/math/orders.ts +5 -1
  78. package/src/types.ts +23 -1
  79. package/src/userMap/userStatsMap.ts +1 -4
  80. package/tests/amm/test.ts +24 -28
  81. package/tests/dlob/test.ts +67 -73
package/lib/types.d.ts CHANGED
@@ -308,8 +308,8 @@ export declare enum TradeSide {
308
308
  Buy = 1,
309
309
  Sell = 2
310
310
  }
311
- export declare type CandleResolution = '1' | '5' | '15' | '60' | '240' | 'D' | 'W' | 'M';
312
- export declare type NewUserRecord = {
311
+ export type CandleResolution = '1' | '5' | '15' | '60' | '240' | 'D' | 'W' | 'M';
312
+ export type NewUserRecord = {
313
313
  ts: BN;
314
314
  userAuthority: PublicKey;
315
315
  user: PublicKey;
@@ -317,7 +317,7 @@ export declare type NewUserRecord = {
317
317
  name: number[];
318
318
  referrer: PublicKey;
319
319
  };
320
- export declare type DepositRecord = {
320
+ export type DepositRecord = {
321
321
  ts: BN;
322
322
  userAuthority: PublicKey;
323
323
  user: PublicKey;
@@ -338,7 +338,7 @@ export declare type DepositRecord = {
338
338
  explanation: DepositExplanation;
339
339
  transferUser?: PublicKey;
340
340
  };
341
- export declare type SpotInterestRecord = {
341
+ export type SpotInterestRecord = {
342
342
  ts: BN;
343
343
  marketIndex: number;
344
344
  depositBalance: BN;
@@ -349,7 +349,7 @@ export declare type SpotInterestRecord = {
349
349
  optimalBorrowRate: number;
350
350
  maxBorrowRate: number;
351
351
  };
352
- export declare type CurveRecord = {
352
+ export type CurveRecord = {
353
353
  ts: BN;
354
354
  recordId: BN;
355
355
  marketIndex: number;
@@ -397,7 +397,7 @@ export declare type InsuranceFundStakeRecord = {
397
397
  userIfSharesAfter: BN;
398
398
  totalIfSharesAfter: BN;
399
399
  };
400
- export declare type LPRecord = {
400
+ export type LPRecord = {
401
401
  ts: BN;
402
402
  user: PublicKey;
403
403
  action: LPAction;
@@ -418,7 +418,7 @@ export declare class LPAction {
418
418
  settleLiquidity: {};
419
419
  };
420
420
  }
421
- export declare type FundingRateRecord = {
421
+ export type FundingRateRecord = {
422
422
  ts: BN;
423
423
  recordId: BN;
424
424
  marketIndex: number;
@@ -433,7 +433,7 @@ export declare type FundingRateRecord = {
433
433
  baseAssetAmountWithAmm: BN;
434
434
  baseAssetAmountWithUnsettledLp: BN;
435
435
  };
436
- export declare type FundingPaymentRecord = {
436
+ export type FundingPaymentRecord = {
437
437
  ts: BN;
438
438
  userAuthority: PublicKey;
439
439
  user: PublicKey;
@@ -444,7 +444,7 @@ export declare type FundingPaymentRecord = {
444
444
  ammCumulativeFundingLong: BN;
445
445
  ammCumulativeFundingShort: BN;
446
446
  };
447
- export declare type LiquidationRecord = {
447
+ export type LiquidationRecord = {
448
448
  ts: BN;
449
449
  user: PublicKey;
450
450
  liquidator: PublicKey;
@@ -482,7 +482,7 @@ export declare class LiquidationType {
482
482
  liquidateSpot: {};
483
483
  };
484
484
  }
485
- export declare type LiquidatePerpRecord = {
485
+ export type LiquidatePerpRecord = {
486
486
  marketIndex: number;
487
487
  oraclePrice: BN;
488
488
  baseAssetAmount: BN;
@@ -494,7 +494,7 @@ export declare type LiquidatePerpRecord = {
494
494
  liquidatorFee: BN;
495
495
  ifFee: BN;
496
496
  };
497
- export declare type LiquidateSpotRecord = {
497
+ export type LiquidateSpotRecord = {
498
498
  assetMarketIndex: number;
499
499
  assetPrice: BN;
500
500
  assetTransfer: BN;
@@ -503,7 +503,7 @@ export declare type LiquidateSpotRecord = {
503
503
  liabilityTransfer: BN;
504
504
  ifFee: BN;
505
505
  };
506
- export declare type LiquidateBorrowForPerpPnlRecord = {
506
+ export type LiquidateBorrowForPerpPnlRecord = {
507
507
  perpMarketIndex: number;
508
508
  marketOraclePrice: BN;
509
509
  pnlTransfer: BN;
@@ -511,7 +511,7 @@ export declare type LiquidateBorrowForPerpPnlRecord = {
511
511
  liabilityPrice: BN;
512
512
  liabilityTransfer: BN;
513
513
  };
514
- export declare type LiquidatePerpPnlForDepositRecord = {
514
+ export type LiquidatePerpPnlForDepositRecord = {
515
515
  perpMarketIndex: number;
516
516
  marketOraclePrice: BN;
517
517
  pnlTransfer: BN;
@@ -519,7 +519,7 @@ export declare type LiquidatePerpPnlForDepositRecord = {
519
519
  assetPrice: BN;
520
520
  assetTransfer: BN;
521
521
  };
522
- export declare type PerpBankruptcyRecord = {
522
+ export type PerpBankruptcyRecord = {
523
523
  marketIndex: number;
524
524
  pnl: BN;
525
525
  ifPayment: BN;
@@ -527,13 +527,13 @@ export declare type PerpBankruptcyRecord = {
527
527
  clawbackUserPayment: BN | null;
528
528
  cumulativeFundingRateDelta: BN;
529
529
  };
530
- export declare type SpotBankruptcyRecord = {
530
+ export type SpotBankruptcyRecord = {
531
531
  marketIndex: number;
532
532
  borrowAmount: BN;
533
533
  cumulativeDepositInterestDelta: BN;
534
534
  ifPayment: BN;
535
535
  };
536
- export declare type SettlePnlRecord = {
536
+ export type SettlePnlRecord = {
537
537
  ts: BN;
538
538
  user: PublicKey;
539
539
  marketIndex: number;
@@ -544,12 +544,12 @@ export declare type SettlePnlRecord = {
544
544
  settlePrice: BN;
545
545
  explanation: SettlePnlExplanation;
546
546
  };
547
- export declare type OrderRecord = {
547
+ export type OrderRecord = {
548
548
  ts: BN;
549
549
  user: PublicKey;
550
550
  order: Order;
551
551
  };
552
- export declare type OrderActionRecord = {
552
+ export type OrderActionRecord = {
553
553
  ts: BN;
554
554
  action: OrderAction;
555
555
  actionExplanation: OrderActionExplanation;
@@ -579,7 +579,7 @@ export declare type OrderActionRecord = {
579
579
  makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
580
580
  oraclePrice: BN;
581
581
  };
582
- export declare type StateAccount = {
582
+ export type StateAccount = {
583
583
  admin: PublicKey;
584
584
  exchangeStatus: number;
585
585
  whitelistMint: PublicKey;
@@ -603,7 +603,7 @@ export declare type StateAccount = {
603
603
  initialPctToLiquidate: number;
604
604
  liquidationDuration: number;
605
605
  };
606
- export declare type PerpMarketAccount = {
606
+ export type PerpMarketAccount = {
607
607
  status: MarketStatus;
608
608
  contractType: ContractType;
609
609
  contractTier: ContractTier;
@@ -636,7 +636,7 @@ export declare type PerpMarketAccount = {
636
636
  quoteMaxInsurance: BN;
637
637
  };
638
638
  };
639
- export declare type HistoricalOracleData = {
639
+ export type HistoricalOracleData = {
640
640
  lastOraclePrice: BN;
641
641
  lastOracleDelay: BN;
642
642
  lastOracleConf: BN;
@@ -644,14 +644,14 @@ export declare type HistoricalOracleData = {
644
644
  lastOraclePriceTwap5Min: BN;
645
645
  lastOraclePriceTwapTs: BN;
646
646
  };
647
- export declare type HistoricalIndexData = {
647
+ export type HistoricalIndexData = {
648
648
  lastIndexBidPrice: BN;
649
649
  lastIndexAskPrice: BN;
650
650
  lastIndexPriceTwap: BN;
651
651
  lastIndexPriceTwap5Min: BN;
652
652
  lastIndexPriceTwapTs: BN;
653
653
  };
654
- export declare type SpotMarketAccount = {
654
+ export type SpotMarketAccount = {
655
655
  status: MarketStatus;
656
656
  assetTier: AssetTier;
657
657
  marketIndex: number;
@@ -706,11 +706,11 @@ export declare type SpotMarketAccount = {
706
706
  totalSpotFee: BN;
707
707
  ordersEnabled: boolean;
708
708
  };
709
- export declare type PoolBalance = {
709
+ export type PoolBalance = {
710
710
  scaledBalance: BN;
711
711
  marketIndex: number;
712
712
  };
713
- export declare type AMM = {
713
+ export type AMM = {
714
714
  baseAssetReserve: BN;
715
715
  sqrtK: BN;
716
716
  cumulativeFundingRate: BN;
@@ -788,7 +788,7 @@ export declare type AMM = {
788
788
  askBaseAssetReserve: BN;
789
789
  askQuoteAssetReserve: BN;
790
790
  };
791
- export declare type PerpPosition = {
791
+ export type PerpPosition = {
792
792
  baseAssetAmount: BN;
793
793
  lastCumulativeFundingRate: BN;
794
794
  marketIndex: number;
@@ -804,7 +804,7 @@ export declare type PerpPosition = {
804
804
  lastBaseAssetAmountPerLp: BN;
805
805
  lastQuoteAssetAmountPerLp: BN;
806
806
  };
807
- export declare type UserStatsAccount = {
807
+ export type UserStatsAccount = {
808
808
  numberOfSubAccounts: number;
809
809
  numberOfSubAccountsCreated: number;
810
810
  makerVolume30D: BN;
@@ -826,7 +826,7 @@ export declare type UserStatsAccount = {
826
826
  authority: PublicKey;
827
827
  ifStakedQuoteAssetAmount: BN;
828
828
  };
829
- export declare type UserAccount = {
829
+ export type UserAccount = {
830
830
  authority: PublicKey;
831
831
  delegate: PublicKey;
832
832
  name: number[];
@@ -848,7 +848,7 @@ export declare type UserAccount = {
848
848
  liquidationStartSlot: BN;
849
849
  isMarginTradingEnabled: boolean;
850
850
  };
851
- export declare type SpotPosition = {
851
+ export type SpotPosition = {
852
852
  marketIndex: number;
853
853
  balanceType: SpotBalanceType;
854
854
  scaledBalance: BN;
@@ -857,7 +857,7 @@ export declare type SpotPosition = {
857
857
  openAsks: BN;
858
858
  cumulativeDeposits: BN;
859
859
  };
860
- export declare type Order = {
860
+ export type Order = {
861
861
  status: OrderStatus;
862
862
  orderType: OrderType;
863
863
  marketType: MarketType;
@@ -883,7 +883,7 @@ export declare type Order = {
883
883
  auctionEndPrice: BN;
884
884
  maxTs: BN;
885
885
  };
886
- export declare type OrderParams = {
886
+ export type OrderParams = {
887
887
  orderType: OrderType;
888
888
  marketType: MarketType;
889
889
  userOrderId: number;
@@ -914,33 +914,33 @@ export declare class PostOnlyParams {
914
914
  tryPostOnly: {};
915
915
  };
916
916
  }
917
- export declare type NecessaryOrderParams = {
917
+ export type NecessaryOrderParams = {
918
918
  orderType: OrderType;
919
919
  marketIndex: number;
920
920
  baseAssetAmount: BN;
921
921
  direction: PositionDirection;
922
922
  };
923
- export declare type OptionalOrderParams = {
923
+ export type OptionalOrderParams = {
924
924
  [Property in keyof OrderParams]?: OrderParams[Property];
925
925
  } & NecessaryOrderParams;
926
926
  export declare const DefaultOrderParams: OrderParams;
927
- export declare type MakerInfo = {
927
+ export type MakerInfo = {
928
928
  maker: PublicKey;
929
929
  makerStats: PublicKey;
930
930
  makerUserAccount: UserAccount;
931
- order: Order;
931
+ order?: Order;
932
932
  };
933
- export declare type TakerInfo = {
933
+ export type TakerInfo = {
934
934
  taker: PublicKey;
935
935
  takerStats: PublicKey;
936
936
  takerUserAccount: UserAccount;
937
937
  order: Order;
938
938
  };
939
- export declare type ReferrerInfo = {
939
+ export type ReferrerInfo = {
940
940
  referrer: PublicKey;
941
941
  referrerStats: PublicKey;
942
942
  };
943
- export declare type TxParams = {
943
+ export type TxParams = {
944
944
  computeUnits?: number;
945
945
  computeUnitsPrice?: number;
946
946
  };
@@ -949,7 +949,7 @@ export interface IWallet {
949
949
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
950
950
  publicKey: PublicKey;
951
951
  }
952
- export declare type FeeStructure = {
952
+ export type FeeStructure = {
953
953
  feeTiers: FeeTier[];
954
954
  makerRebateNumerator: BN;
955
955
  makerRebateDenominator: BN;
@@ -957,7 +957,7 @@ export declare type FeeStructure = {
957
957
  flatFillerFee: BN;
958
958
  referrerRewardEpochUpperBound: BN;
959
959
  };
960
- export declare type FeeTier = {
960
+ export type FeeTier = {
961
961
  feeNumerator: number;
962
962
  feeDenominator: number;
963
963
  makerRebateNumerator: number;
@@ -967,12 +967,12 @@ export declare type FeeTier = {
967
967
  refereeFeeNumerator: number;
968
968
  refereeFeeDenominator: number;
969
969
  };
970
- export declare type OrderFillerRewardStructure = {
970
+ export type OrderFillerRewardStructure = {
971
971
  rewardNumerator: BN;
972
972
  rewardDenominator: BN;
973
973
  timeBasedRewardLowerBound: BN;
974
974
  };
975
- export declare type OracleGuardRails = {
975
+ export type OracleGuardRails = {
976
976
  priceDivergence: {
977
977
  markOracleDivergenceNumerator: BN;
978
978
  markOracleDivergenceDenominator: BN;
@@ -984,8 +984,8 @@ export declare type OracleGuardRails = {
984
984
  tooVolatileRatio: BN;
985
985
  };
986
986
  };
987
- export declare type MarginCategory = 'Initial' | 'Maintenance';
988
- export declare type InsuranceFundStake = {
987
+ export type MarginCategory = 'Initial' | 'Maintenance';
988
+ export type InsuranceFundStake = {
989
989
  marketIndex: number;
990
990
  authority: PublicKey;
991
991
  ifShares: BN;
@@ -994,7 +994,7 @@ export declare type InsuranceFundStake = {
994
994
  lastWithdrawRequestValue: BN;
995
995
  lastWithdrawRequestTs: BN;
996
996
  };
997
- export declare type SerumV3FulfillmentConfigAccount = {
997
+ export type SerumV3FulfillmentConfigAccount = {
998
998
  fulfillmentType: SpotFulfillmentType;
999
999
  status: SpotFulfillmentStatus;
1000
1000
  pubkey: PublicKey;
@@ -1010,3 +1010,24 @@ export declare type SerumV3FulfillmentConfigAccount = {
1010
1010
  serumOpenOrders: PublicKey;
1011
1011
  serumSignerNonce: BN;
1012
1012
  };
1013
+ export type PerpMarketExtendedInfo = {
1014
+ marketIndex: number;
1015
+ /**
1016
+ * Min order size measured in base asset, using base precision
1017
+ */
1018
+ minOrderSize: BN;
1019
+ /**
1020
+ * Margin maintenance percentage, using margin precision (1e4)
1021
+ */
1022
+ marginMaintenance: number;
1023
+ /**
1024
+ * Max insurance available, measured in quote asset, using quote preicision
1025
+ */
1026
+ availableInsurance: BN;
1027
+ /**
1028
+ * Pnl pool available, this is measured in quote asset, using quote precision.
1029
+ * Should be generated by using getTokenAmount and passing in the scaled balance of the base asset + quote spot account
1030
+ */
1031
+ pnlPoolValue: BN;
1032
+ contractTier: ContractTier;
1033
+ };