@drift-labs/sdk 2.42.0-beta.9 → 2.43.0-beta.0

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/user.ts CHANGED
@@ -3,66 +3,66 @@ import { EventEmitter } from 'events';
3
3
  import StrictEventEmitter from 'strict-event-emitter-types';
4
4
  import { DriftClient } from './driftClient';
5
5
  import {
6
+ HealthComponents,
6
7
  isVariant,
7
8
  MarginCategory,
8
9
  Order,
9
- UserAccount,
10
+ PerpMarketAccount,
10
11
  PerpPosition,
11
12
  SpotPosition,
12
- isOneOfVariant,
13
- PerpMarketAccount,
14
- HealthComponents,
13
+ UserAccount,
14
+ UserStatus,
15
15
  UserStatsAccount,
16
16
  } from './types';
17
17
  import { calculateEntryPrice, positionIsAvailable } from './math/position';
18
18
  import {
19
- PRICE_PRECISION,
19
+ AMM_RESERVE_PRECISION,
20
+ AMM_RESERVE_PRECISION_EXP,
20
21
  AMM_TO_QUOTE_PRECISION_RATIO,
21
- ZERO,
22
- TEN_THOUSAND,
22
+ BASE_PRECISION,
23
23
  BN_MAX,
24
- QUOTE_PRECISION,
25
- AMM_RESERVE_PRECISION,
24
+ FIVE_MINUTE,
26
25
  MARGIN_PRECISION,
27
- SPOT_MARKET_WEIGHT_PRECISION,
26
+ ONE,
27
+ OPEN_ORDER_MARGIN_REQUIREMENT,
28
+ PRICE_PRECISION,
29
+ QUOTE_PRECISION,
28
30
  QUOTE_SPOT_MARKET_INDEX,
31
+ SPOT_MARKET_WEIGHT_PRECISION,
29
32
  TEN,
30
- OPEN_ORDER_MARGIN_REQUIREMENT,
31
- FIVE_MINUTE,
32
- BASE_PRECISION,
33
- ONE,
33
+ TEN_THOUSAND,
34
34
  TWO,
35
- AMM_RESERVE_PRECISION_EXP,
35
+ ZERO,
36
36
  } from './constants/numericConstants';
37
37
  import {
38
- UserAccountSubscriber,
39
- UserAccountEvents,
40
38
  DataAndSlot,
39
+ UserAccountEvents,
40
+ UserAccountSubscriber,
41
41
  } from './accounts/types';
42
42
  import {
43
- calculateReservePrice,
43
+ BN,
44
44
  calculateBaseAssetValue,
45
+ calculateMarketMarginRatio,
45
46
  calculatePositionFundingPNL,
46
47
  calculatePositionPNL,
48
+ calculateReservePrice,
49
+ calculateSpotMarketMarginRatio,
47
50
  calculateUnrealizedAssetWeight,
48
- calculateMarketMarginRatio,
49
- PositionDirection,
50
- BN,
51
- SpotMarketAccount,
51
+ getBalance,
52
+ getSignedTokenAmount,
53
+ getStrictTokenValue,
52
54
  getTokenValue,
53
55
  MarketType,
54
- getStrictTokenValue,
55
- calculateSpotMarketMarginRatio,
56
- getSignedTokenAmount,
57
- SpotBalanceType,
56
+ PositionDirection,
58
57
  sigNum,
59
- getBalance,
58
+ SpotBalanceType,
59
+ SpotMarketAccount,
60
60
  } from '.';
61
61
  import {
62
- getTokenAmount,
63
62
  calculateAssetWeight,
64
63
  calculateLiabilityWeight,
65
64
  calculateWithdrawLimit,
65
+ getTokenAmount,
66
66
  } from './math/spotBalance';
67
67
  import { calculateMarketOpenBidAsk } from './math/amm';
68
68
  import {
@@ -1200,12 +1200,7 @@ export class User {
1200
1200
  * @returns : number (value from [0, 100])
1201
1201
  */
1202
1202
  public getHealth(): number {
1203
- const userAccount = this.getUserAccount();
1204
-
1205
- if (
1206
- isVariant(userAccount.status, 'beingLiquidated') ||
1207
- isVariant(userAccount.status, 'bankrupt')
1208
- ) {
1203
+ if (this.isBeingLiquidated()) {
1209
1204
  return 0;
1210
1205
  }
1211
1206
 
@@ -1821,14 +1816,15 @@ export class User {
1821
1816
  }
1822
1817
 
1823
1818
  public isBeingLiquidated(): boolean {
1824
- return isOneOfVariant(this.getUserAccount().status, [
1825
- 'beingLiquidated',
1826
- 'bankrupt',
1827
- ]);
1819
+ return (
1820
+ (this.getUserAccount().status &
1821
+ (UserStatus.BEING_LIQUIDATED | UserStatus.BANKRUPT)) >
1822
+ 0
1823
+ );
1828
1824
  }
1829
1825
 
1830
1826
  public isBankrupt(): boolean {
1831
- return isVariant(this.getUserAccount().status, 'bankrupt');
1827
+ return (this.getUserAccount().status & UserStatus.BANKRUPT) > 0;
1832
1828
  }
1833
1829
 
1834
1830
  /**
@@ -2412,7 +2408,7 @@ export class User {
2412
2408
  );
2413
2409
 
2414
2410
  const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } =
2415
- this.getLeverageComponents(undefined, 'Initial');
2411
+ this.getLeverageComponents();
2416
2412
 
2417
2413
  if (!calculateSwap) {
2418
2414
  calculateSwap = (inSwap: BN) => {
@@ -2708,7 +2704,7 @@ export class User {
2708
2704
  );
2709
2705
 
2710
2706
  const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } =
2711
- this.getLeverageComponents(undefined, 'Initial');
2707
+ this.getLeverageComponents();
2712
2708
 
2713
2709
  const inPositionAfter = this.cloneAndUpdateSpotPosition(
2714
2710
  inSpotPosition,
@@ -3152,12 +3148,23 @@ export class User {
3152
3148
  };
3153
3149
  }
3154
3150
 
3155
- public canMakeIdle(slot: BN, slotsBeforeIdle: BN): boolean {
3151
+ public canMakeIdle(slot: BN): boolean {
3156
3152
  const userAccount = this.getUserAccount();
3157
3153
  if (userAccount.idle) {
3158
3154
  return false;
3159
3155
  }
3160
3156
 
3157
+ const { totalAssetValue, totalLiabilityValue } =
3158
+ this.getSpotMarketAssetAndLiabilityValue();
3159
+ const equity = totalAssetValue.sub(totalLiabilityValue);
3160
+
3161
+ let slotsBeforeIdle: BN;
3162
+ if (equity.lt(QUOTE_PRECISION)) {
3163
+ slotsBeforeIdle = new BN(9000); // 1 hour
3164
+ } else {
3165
+ slotsBeforeIdle = new BN(1512000); // 1 week
3166
+ }
3167
+
3161
3168
  const userLastActiveSlot = userAccount.lastActiveSlot;
3162
3169
  const slotsSinceLastActive = slot.sub(userLastActiveSlot);
3163
3170
  if (slotsSinceLastActive.lt(slotsBeforeIdle)) {
@@ -9,7 +9,6 @@ import {
9
9
  OrderType,
10
10
  PositionDirection,
11
11
  OrderTriggerCondition,
12
- UserStatus,
13
12
  UserAccount,
14
13
  ZERO,
15
14
  } from '../../src';
@@ -67,7 +66,7 @@ export const mockUserAccount: UserAccount = {
67
66
  orders: Array.from({ length: 8 }, function () {
68
67
  return Object.assign({}, mockOrder);
69
68
  }),
70
- status: UserStatus.ACTIVE,
69
+ status: 0,
71
70
  nextLiquidationId: 0,
72
71
  nextOrderId: 0,
73
72
  maxMarginRatio: 0,