@drift-labs/sdk 0.2.0-master.40 → 0.2.0-master.42

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/src/types.ts CHANGED
@@ -5,26 +5,32 @@ import { BN, ZERO } from '.';
5
5
 
6
6
  export class ExchangeStatus {
7
7
  static readonly ACTIVE = { active: {} };
8
- static readonly FUNDINGPAUSED = { fundingPaused: {} };
9
- static readonly AMMPAUSED = { ammPaused: {} };
10
- static readonly FILLPAUSED = { fillPaused: {} };
11
- static readonly LIQPAUSED = { liqPaused: {} };
12
- static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
8
+ static readonly FUNDING_PAUSED = { fundingPaused: {} };
9
+ static readonly AMM_PAUSED = { ammPaused: {} };
10
+ static readonly FILL_PAUSED = { fillPaused: {} };
11
+ static readonly LIQ_PAUSED = { liqPaused: {} };
12
+ static readonly WITHDRAW_PAUSED = { withdrawPaused: {} };
13
13
  static readonly PAUSED = { paused: {} };
14
14
  }
15
15
 
16
16
  export class MarketStatus {
17
17
  static readonly INITIALIZED = { initialized: {} };
18
18
  static readonly ACTIVE = { active: {} };
19
- static readonly FUNDINGPAUSED = { fundingPaused: {} };
20
- static readonly AMMPAUSED = { ammPaused: {} };
21
- static readonly FILLPAUSED = { fillPaused: {} };
22
- static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
23
- static readonly REDUCEONLY = { reduceOnly: {} };
19
+ static readonly FUNDING_PAUSED = { fundingPaused: {} };
20
+ static readonly AMM_PAUSED = { ammPaused: {} };
21
+ static readonly FILL_PAUSED = { fillPaused: {} };
22
+ static readonly WITHDRAW_PAUSED = { withdrawPaused: {} };
23
+ static readonly REDUCE_ONLY = { reduceOnly: {} };
24
24
  static readonly SETTLEMENT = { settlement: {} };
25
25
  static readonly DELISTED = { delisted: {} };
26
26
  }
27
27
 
28
+ export class UserStatus {
29
+ static readonly ACTIVE = { active: {} };
30
+ static readonly BEING_LIQUIDATED = { beingLiquidated: {} };
31
+ static readonly BANKRUPT = { bankrupt: {} };
32
+ }
33
+
28
34
  export class ContractType {
29
35
  static readonly PERPETUAL = { perpetual: {} };
30
36
  static readonly FUTURE = { future: {} };
@@ -90,14 +96,6 @@ export class OrderStatus {
90
96
  static readonly OPEN = { open: {} };
91
97
  }
92
98
 
93
- export class OrderDiscountTier {
94
- static readonly NONE = { none: {} };
95
- static readonly FIRST = { first: {} };
96
- static readonly SECOND = { second: {} };
97
- static readonly THIRD = { third: {} };
98
- static readonly FOURTH = { fourth: {} };
99
- }
100
-
101
99
  export class OrderAction {
102
100
  static readonly PLACE = { place: {} };
103
101
  static readonly CANCEL = { cancel: {} };
@@ -108,23 +106,46 @@ export class OrderAction {
108
106
 
109
107
  export class OrderActionExplanation {
110
108
  static readonly NONE = { none: {} };
109
+ static readonly INSUFFICIENT_FREE_COLLATERAL = {
110
+ insufficientFreeCollateral: {},
111
+ };
111
112
  static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
112
113
  oraclePriceBreachedLimitPrice: {},
113
114
  };
114
115
  static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
115
116
  marketOrderFilledToLimitPrice: {},
116
117
  };
117
- static readonly CANCELED_FOR_LIQUIDATION = {
118
- canceledForLiquidation: {},
118
+ static readonly ORDER_EXPIRED = {
119
+ orderExpired: {},
120
+ };
121
+ static readonly LIQUIDATION = {
122
+ liquidation: {},
123
+ };
124
+ static readonly ORDER_FILLED_WITH_AMM = {
125
+ orderFilledWithAMM: {},
119
126
  };
120
- static readonly MARKET_ORDER_AUCTION_EXPIRED = {
121
- marketOrderAuctionExpired: {},
127
+ static readonly ORDER_FILLED_WITH_AMM_JIT = {
128
+ orderFilledWithAMMJit: {},
129
+ };
130
+ static readonly ORDER_FILLED_WITH_MATCH = {
131
+ orderFilledWithMatch: {},
132
+ };
133
+ static readonly MARKET_EXPIRED = {
134
+ marketExpired: {},
135
+ };
136
+ static readonly RISK_INCREASING_ORDER = {
137
+ riskingIncreasingOrder: {},
138
+ };
139
+ static readonly ORDER_FILLED_WITH_SERUM = {
140
+ orderFillWithSerum: {},
122
141
  };
123
142
  }
124
143
 
125
144
  export class OrderTriggerCondition {
126
145
  static readonly ABOVE = { above: {} };
127
146
  static readonly BELOW = { below: {} };
147
+ static readonly TRIGGERED_ABOVE = { triggeredAbove: {} }; // above condition has been triggered
148
+ static readonly TRIGGERED_BELOW = { triggeredBelow: {} }; // below condition has been triggered
128
149
  }
129
150
 
130
151
  export class SpotFulfillmentType {
@@ -136,6 +157,23 @@ export class SpotFulfillmentStatus {
136
157
  static readonly DISABLED = { disabled: {} };
137
158
  }
138
159
 
160
+ export class DepositExplanation {
161
+ static readonly NONE = { none: {} };
162
+ static readonly TRANSFER = { transfer: {} };
163
+ }
164
+
165
+ export class SettlePnlExplanation {
166
+ static readonly NONE = { none: {} };
167
+ static readonly EXPIRED_POSITION = { expiredPosition: {} };
168
+ }
169
+
170
+ export class StakeAction {
171
+ static readonly STAKE = { stake: {} };
172
+ static readonly UNSTAKE_REQUEST = { unstakeRequest: {} };
173
+ static readonly UNSTAKE_CANCEL_REQUEST = { unstakeCancelRequest: {} };
174
+ static readonly UNSTAKE = { unstake: {} };
175
+ }
176
+
139
177
  export function isVariant(object: unknown, type: string) {
140
178
  return object.hasOwnProperty(type);
141
179
  }
@@ -190,7 +228,10 @@ export type DepositRecord = {
190
228
  marketWithdrawBalance: BN;
191
229
  marketCumulativeDepositInterest: BN;
192
230
  marketCumulativeBorrowInterest: BN;
231
+ totalDepositsAfter: BN;
232
+ totalWithdrawsAfter: BN;
193
233
  depositRecordId: BN;
234
+ explanation: DepositExplanation;
194
235
  transferUser?: PublicKey;
195
236
  };
196
237
 
@@ -220,22 +261,40 @@ export type CurveRecord = {
220
261
  sqrtKAfter: BN;
221
262
  baseAssetAmountLong: BN;
222
263
  baseAssetAmountShort: BN;
223
- baseAssetAmount: BN;
224
- openInterest: BN;
264
+ baseAssetAmountWithAmm: BN;
265
+ totalFee: BN;
266
+ totalFeeMinusDistributions: BN;
267
+ adjustmentCost: BN;
268
+ numberOfUsers: BN;
225
269
  oraclePrice: BN;
226
- tradeId: BN;
270
+ fillRecord: BN;
227
271
  };
228
272
 
229
273
  export declare type InsuranceFundRecord = {
230
274
  ts: BN;
231
- bankIndex: BN;
232
- marketIndex: number;
275
+ spotMarketIndex: number;
276
+ perpMarketIndex: number;
233
277
  userIfFactor: number;
234
278
  totalIfFactor: number;
235
279
  vaultAmountBefore: BN;
236
280
  insuranceVaultAmountBefore: BN;
281
+ totalIfSharesBefore: BN;
282
+ totalIfSharesAfter: BN;
237
283
  amount: BN;
284
+ };
285
+
286
+ export declare type InsuranceFundStakeRecord = {
287
+ ts: BN;
288
+ userAuthority: PublicKey;
289
+ action: StakeAction;
290
+ amount: BN;
291
+ marketIndex: number;
292
+ insuranceVaultAmountBefore: BN;
293
+ ifSharesBefore: BN;
294
+ userIfSharesBefore: BN;
238
295
  totalIfSharesBefore: BN;
296
+ ifSharesAfter: BN;
297
+ userIfSharesAfter: BN;
239
298
  totalIfSharesAfter: BN;
240
299
  };
241
300
 
@@ -291,7 +350,9 @@ export type LiquidationRecord = {
291
350
  liquidationType: LiquidationType;
292
351
  marginRequirement: BN;
293
352
  totalCollateral: BN;
353
+ marginFreed: BN;
294
354
  liquidationId: number;
355
+ bankrupt: boolean;
295
356
  canceledOrderIds: BN[];
296
357
  liquidatePerp: LiquidatePerpRecord;
297
358
  liquidateSpot: LiquidateSpotRecord;
@@ -327,6 +388,7 @@ export type LiquidatePerpRecord = {
327
388
  userOrderId: BN;
328
389
  liquidatorOrderId: BN;
329
390
  fillRecordId: BN;
391
+ liquidatorFee: BN;
330
392
  ifFee: BN;
331
393
  };
332
394
 
@@ -383,6 +445,7 @@ export type SettlePnlRecord = {
383
445
  quoteAssetAmountAfter: BN;
384
446
  quoteEntryAmount: BN;
385
447
  settlePrice: BN;
448
+ explanation: SettlePnlExplanation;
386
449
  };
387
450
 
388
451
  export type OrderRecord = {
@@ -406,13 +469,13 @@ export type OrderActionRecord = {
406
469
  makerFee: BN | null;
407
470
  referrerReward: number | null;
408
471
  quoteAssetAmountSurplus: BN | null;
472
+ spotFulfillmentMethodFee: BN | null;
409
473
  taker: PublicKey | null;
410
474
  takerOrderId: number | null;
411
475
  takerOrderDirection: PositionDirection | null;
412
476
  takerOrderBaseAssetAmount: BN | null;
413
477
  takerOrderCumulativeBaseAssetAmountFilled: BN | null;
414
478
  takerOrderCumulativeQuoteAssetAmountFilled: BN | null;
415
- takerOrderFee: BN | null;
416
479
  maker: PublicKey | null;
417
480
  makerOrderId: number | null;
418
481
  makerOrderDirection: PositionDirection | null;
@@ -428,6 +491,8 @@ export type StateAccount = {
428
491
  whitelistMint: PublicKey;
429
492
  discountMint: PublicKey;
430
493
  oracleGuardRails: OracleGuardRails;
494
+ numberOfAuthorities: BN;
495
+ numberOfSubAccounts: BN;
431
496
  numberOfMarkets: number;
432
497
  numberOfSpotMarkets: number;
433
498
  minPerpAuctionDuration: number;
@@ -482,7 +547,7 @@ export type HistoricalOracleData = {
482
547
  lastOracleDelay: BN;
483
548
  lastOracleConf: BN;
484
549
  lastOraclePriceTwap: BN;
485
- lastOraclePriceTwap5min: BN;
550
+ lastOraclePriceTwap5Min: BN;
486
551
  lastOraclePriceTwapTs: BN;
487
552
  };
488
553
 
@@ -490,7 +555,7 @@ export type HistoricalIndexData = {
490
555
  lastIndexBidPrice: BN;
491
556
  lastIndexAskPrice: BN;
492
557
  lastIndexPriceTwap: BN;
493
- lastIndexPriceTwap5min: BN;
558
+ lastIndexPriceTwap5Min: BN;
494
559
  lastIndexPriceTwapTs: BN;
495
560
  };
496
561
 
@@ -530,6 +595,8 @@ export type SpotMarketAccount = {
530
595
  maxBorrowRate: number;
531
596
  cumulativeDepositInterest: BN;
532
597
  cumulativeBorrowInterest: BN;
598
+ totalSocialLoss: BN;
599
+ totalQuoteSocialLoss: BN;
533
600
  depositBalance: BN;
534
601
  borrowBalance: BN;
535
602
  maxTokenDeposits: BN;
@@ -570,7 +637,7 @@ export type AMM = {
570
637
  lastFundingRate: BN;
571
638
  lastFundingRateTs: BN;
572
639
  lastMarkPriceTwap: BN;
573
- lastMarkPriceTwap5min: BN;
640
+ lastMarkPriceTwap5Min: BN;
574
641
  lastMarkPriceTwapTs: BN;
575
642
  lastTradeTs: BN;
576
643
 
@@ -628,7 +695,7 @@ export type AMM = {
628
695
  maxOpenInterest: BN;
629
696
  maxBaseAssetReserve: BN;
630
697
  minBaseAssetReserve: BN;
631
- cumulativeSocialLoss: BN;
698
+ totalSocialLoss: BN;
632
699
 
633
700
  quoteBreakEvenAmountLong: BN;
634
701
  quoteBreakEvenAmountShort: BN;
@@ -636,11 +703,12 @@ export type AMM = {
636
703
  quoteEntryAmountShort: BN;
637
704
 
638
705
  markStd: BN;
706
+ oracleStd: BN;
639
707
  longIntensityCount: number;
640
708
  longIntensityVolume: BN;
641
709
  shortIntensityCount: number;
642
710
  shortIntensityVolume: BN;
643
- volume24h: BN;
711
+ volume24H: BN;
644
712
  minOrderSize: BN;
645
713
  maxPositionSize: BN;
646
714
 
@@ -670,7 +738,7 @@ export type PerpPosition = {
670
738
 
671
739
  export type UserStatsAccount = {
672
740
  numberOfSubAccounts: number;
673
- maxSubAccountId: number;
741
+ numberOfSubAccountsCreated: number;
674
742
  makerVolume30D: BN;
675
743
  takerVolume30D: BN;
676
744
  fillerVolume30D: BN;
@@ -699,8 +767,7 @@ export type UserAccount = {
699
767
  spotPositions: SpotPosition[];
700
768
  perpPositions: PerpPosition[];
701
769
  orders: Order[];
702
- isBeingLiquidated: boolean;
703
- isBankrupt: boolean;
770
+ status: UserStatus;
704
771
  nextLiquidationId: number;
705
772
  nextOrderId: number;
706
773
  maxMarginRatio: number;
@@ -708,6 +775,7 @@ export type UserAccount = {
708
775
  settledPerpPnl: BN;
709
776
  totalDeposits: BN;
710
777
  totalWithdraws: BN;
778
+ totalSocialLoss: BN;
711
779
  cumulativePerpFunding: BN;
712
780
  };
713
781
 
@@ -738,7 +806,6 @@ export type Order = {
738
806
  reduceOnly: boolean;
739
807
  triggerPrice: BN;
740
808
  triggerCondition: OrderTriggerCondition;
741
- triggered: boolean;
742
809
  existingPositionDirection: PositionDirection;
743
810
  postOnly: boolean;
744
811
  immediateOrCancel: boolean;
package/src/user.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  UserAccount,
10
10
  PerpPosition,
11
11
  SpotPosition,
12
+ isOneOfVariant,
12
13
  } from './types';
13
14
  import { calculateEntryPrice } from './math/position';
14
15
  import {
@@ -1024,7 +1025,12 @@ export class User {
1024
1025
 
1025
1026
  // if user being liq'd, can continue to be liq'd until total collateral above the margin requirement plus buffer
1026
1027
  let liquidationBuffer = undefined;
1027
- if (this.getUserAccount().isBeingLiquidated) {
1028
+ const isBeingLiquidated = isVariant(
1029
+ this.getUserAccount().status,
1030
+ 'beingLiquidated'
1031
+ );
1032
+
1033
+ if (isBeingLiquidated) {
1028
1034
  liquidationBuffer = new BN(
1029
1035
  this.driftClient.getStateAccount().liquidationMarginBufferRatio
1030
1036
  );
@@ -1034,6 +1040,17 @@ export class User {
1034
1040
  return totalCollateral.lt(maintenanceRequirement);
1035
1041
  }
1036
1042
 
1043
+ public isBeingLiquidated(): boolean {
1044
+ return isOneOfVariant(this.getUserAccount().status, [
1045
+ 'beingLiquidated',
1046
+ 'bankrupt',
1047
+ ]);
1048
+ }
1049
+
1050
+ public isBankrupt(): boolean {
1051
+ return isVariant(this.getUserAccount().status, 'bankrupt');
1052
+ }
1053
+
1037
1054
  /**
1038
1055
  * Checks if any user position cumulative funding differs from respective market cumulative funding
1039
1056
  * @returns
@@ -55,14 +55,14 @@ export const mockAMM: AMM = {
55
55
  lastFundingRate: new BN(0),
56
56
  lastFundingRateTs: new BN(0),
57
57
  lastMarkPriceTwap: new BN(0),
58
- lastMarkPriceTwap5min: new BN(0),
58
+ lastMarkPriceTwap5Min: new BN(0),
59
59
  lastMarkPriceTwapTs: new BN(0),
60
60
  historicalOracleData: {
61
61
  lastOraclePrice: new BN(0),
62
62
  lastOracleConf: new BN(0),
63
63
  lastOracleDelay: new BN(0),
64
64
  lastOraclePriceTwap: new BN(0),
65
- lastOraclePriceTwap5min: new BN(0),
65
+ lastOraclePriceTwap5Min: new BN(0),
66
66
  lastOraclePriceTwapTs: new BN(0),
67
67
  },
68
68
  lastOracleReservePriceSpreadPct: new BN(0),
@@ -112,7 +112,7 @@ export const mockAMM: AMM = {
112
112
  ammJitIntensity: 0,
113
113
  maxBaseAssetReserve: new BN(0),
114
114
  minBaseAssetReserve: new BN(0),
115
- cumulativeSocialLoss: new BN(0),
115
+ totalSocialLoss: new BN(0),
116
116
  baseAssetAmountPerLp: new BN(0),
117
117
  quoteAssetAmountPerLp: new BN(0),
118
118
 
@@ -122,11 +122,12 @@ export const mockAMM: AMM = {
122
122
  quoteEntryAmountShort: new BN(0),
123
123
 
124
124
  markStd: new BN(0),
125
+ oracleStd: new BN(0),
125
126
  longIntensityCount: 0,
126
127
  longIntensityVolume: new BN(0),
127
128
  shortIntensityCount: 0,
128
129
  shortIntensityVolume: new BN(0),
129
- volume24h: new BN(0),
130
+ volume24H: new BN(0),
130
131
  minOrderSize: new BN(0),
131
132
  maxPositionSize: new BN(0),
132
133
 
@@ -279,6 +280,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
279
280
  maxBorrowRate: 0,
280
281
  cumulativeDepositInterest: new BN(0),
281
282
  cumulativeBorrowInterest: new BN(0),
283
+ totalSocialLoss: new BN(0),
284
+ totalQuoteSocialLoss: new BN(0),
282
285
  depositBalance: new BN(0),
283
286
  borrowBalance: new BN(0),
284
287
  lastInterestTs: new BN(0),
@@ -309,14 +312,14 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
309
312
  lastOracleConf: new BN(0),
310
313
  lastOracleDelay: new BN(0),
311
314
  lastOraclePriceTwap: new BN(0),
312
- lastOraclePriceTwap5min: new BN(0),
315
+ lastOraclePriceTwap5Min: new BN(0),
313
316
  lastOraclePriceTwapTs: new BN(0),
314
317
  },
315
318
  historicalIndexData: {
316
319
  lastIndexBidPrice: new BN(0),
317
320
  lastIndexAskPrice: new BN(0),
318
321
  lastIndexPriceTwap: new BN(0),
319
- lastIndexPriceTwap5min: new BN(0),
322
+ lastIndexPriceTwap5Min: new BN(0),
320
323
  lastIndexPriceTwapTs: new BN(0),
321
324
  },
322
325
  },
@@ -351,6 +354,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
351
354
  maxBorrowRate: 0,
352
355
  cumulativeDepositInterest: new BN(0),
353
356
  cumulativeBorrowInterest: new BN(0),
357
+ totalSocialLoss: new BN(0),
358
+ totalQuoteSocialLoss: new BN(0),
354
359
  depositBalance: new BN(0),
355
360
  borrowBalance: new BN(0),
356
361
  lastInterestTs: new BN(0),
@@ -381,14 +386,14 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
381
386
  lastOracleConf: new BN(0),
382
387
  lastOracleDelay: new BN(0),
383
388
  lastOraclePriceTwap: new BN(0),
384
- lastOraclePriceTwap5min: new BN(0),
389
+ lastOraclePriceTwap5Min: new BN(0),
385
390
  lastOraclePriceTwapTs: new BN(0),
386
391
  },
387
392
  historicalIndexData: {
388
393
  lastIndexBidPrice: new BN(0),
389
394
  lastIndexAskPrice: new BN(0),
390
395
  lastIndexPriceTwap: new BN(0),
391
- lastIndexPriceTwap5min: new BN(0),
396
+ lastIndexPriceTwap5Min: new BN(0),
392
397
  lastIndexPriceTwapTs: new BN(0),
393
398
  },
394
399
  },
@@ -423,6 +428,8 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
423
428
  maxBorrowRate: 0,
424
429
  cumulativeDepositInterest: new BN(0),
425
430
  cumulativeBorrowInterest: new BN(0),
431
+ totalSocialLoss: new BN(0),
432
+ totalQuoteSocialLoss: new BN(0),
426
433
  depositBalance: new BN(0),
427
434
  borrowBalance: new BN(0),
428
435
  lastInterestTs: new BN(0),
@@ -453,14 +460,14 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
453
460
  lastOracleConf: new BN(0),
454
461
  lastOracleDelay: new BN(0),
455
462
  lastOraclePriceTwap: new BN(0),
456
- lastOraclePriceTwap5min: new BN(0),
463
+ lastOraclePriceTwap5Min: new BN(0),
457
464
  lastOraclePriceTwapTs: new BN(0),
458
465
  },
459
466
  historicalIndexData: {
460
467
  lastIndexBidPrice: new BN(0),
461
468
  lastIndexAskPrice: new BN(0),
462
469
  lastIndexPriceTwap: new BN(0),
463
- lastIndexPriceTwap5min: new BN(0),
470
+ lastIndexPriceTwap5Min: new BN(0),
464
471
  lastIndexPriceTwapTs: new BN(0),
465
472
  },
466
473
  },
@@ -477,6 +484,8 @@ export const mockStateAccount: StateAccount = {
477
484
  minPerpAuctionDuration: 0,
478
485
  numberOfMarkets: 0,
479
486
  numberOfSpotMarkets: 0,
487
+ numberOfSubAccounts: new BN(0),
488
+ numberOfAuthorities: new BN(0),
480
489
  oracleGuardRails: {
481
490
  priceDivergence: {
482
491
  markOracleDivergenceNumerator: new BN(0),
@@ -63,7 +63,6 @@ function insertOrderToDLOB(
63
63
  reduceOnly: false,
64
64
  triggerPrice: new BN(0),
65
65
  triggerCondition: OrderTriggerCondition.ABOVE,
66
- triggered: false,
67
66
  existingPositionDirection: PositionDirection.LONG,
68
67
  postOnly: false,
69
68
  immediateOrCancel: false,
@@ -113,7 +112,6 @@ function insertTriggerOrderToDLOB(
113
112
  reduceOnly: false,
114
113
  triggerPrice,
115
114
  triggerCondition,
116
- triggered: false,
117
115
  existingPositionDirection: PositionDirection.LONG,
118
116
  postOnly: false,
119
117
  immediateOrCancel: true,