@drift-labs/sdk 2.42.0-beta.14 → 2.42.0-beta.16

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.42.0-beta.14
1
+ 2.42.0-beta.16
@@ -5890,9 +5890,7 @@
5890
5890
  "docs": [
5891
5891
  "Whether the user is active, being liquidated or bankrupt"
5892
5892
  ],
5893
- "type": {
5894
- "defined": "UserStatus"
5895
- }
5893
+ "type": "u8"
5896
5894
  },
5897
5895
  {
5898
5896
  "name": "isMarginTradingEnabled",
@@ -8648,9 +8646,6 @@
8648
8646
  "type": {
8649
8647
  "kind": "enum",
8650
8648
  "variants": [
8651
- {
8652
- "name": "Active"
8653
- },
8654
8649
  {
8655
8650
  "name": "BeingLiquidated"
8656
8651
  },
package/lib/types.d.ts CHANGED
@@ -41,19 +41,10 @@ export declare class MarketStatus {
41
41
  delisted: {};
42
42
  };
43
43
  }
44
- export declare class UserStatus {
45
- static readonly ACTIVE: {
46
- active: {};
47
- };
48
- static readonly BEING_LIQUIDATED: {
49
- beingLiquidated: {};
50
- };
51
- static readonly BANKRUPT: {
52
- bankrupt: {};
53
- };
54
- static readonly REDUCE_ONLY: {
55
- reduceOnly: {};
56
- };
44
+ export declare enum UserStatus {
45
+ BEING_LIQUIDATED = 1,
46
+ BANKRUPT = 2,
47
+ REDUCE_ONLY = 4
57
48
  }
58
49
  export declare class ContractType {
59
50
  static readonly PERPETUAL: {
@@ -882,7 +873,7 @@ export type UserAccount = {
882
873
  spotPositions: SpotPosition[];
883
874
  perpPositions: PerpPosition[];
884
875
  orders: Order[];
885
- status: UserStatus;
876
+ status: number;
886
877
  nextLiquidationId: number;
887
878
  nextOrderId: number;
888
879
  maxMarginRatio: number;
package/lib/types.js CHANGED
@@ -27,13 +27,12 @@ MarketStatus.WITHDRAW_PAUSED = { withdrawPaused: {} };
27
27
  MarketStatus.REDUCE_ONLY = { reduceOnly: {} };
28
28
  MarketStatus.SETTLEMENT = { settlement: {} };
29
29
  MarketStatus.DELISTED = { delisted: {} };
30
- class UserStatus {
31
- }
32
- exports.UserStatus = UserStatus;
33
- UserStatus.ACTIVE = { active: {} };
34
- UserStatus.BEING_LIQUIDATED = { beingLiquidated: {} };
35
- UserStatus.BANKRUPT = { bankrupt: {} };
36
- UserStatus.REDUCE_ONLY = { reduceOnly: {} };
30
+ var UserStatus;
31
+ (function (UserStatus) {
32
+ UserStatus[UserStatus["BEING_LIQUIDATED"] = 1] = "BEING_LIQUIDATED";
33
+ UserStatus[UserStatus["BANKRUPT"] = 2] = "BANKRUPT";
34
+ UserStatus[UserStatus["REDUCE_ONLY"] = 4] = "REDUCE_ONLY";
35
+ })(UserStatus = exports.UserStatus || (exports.UserStatus = {}));
37
36
  class ContractType {
38
37
  }
39
38
  exports.ContractType = ContractType;
package/lib/user.d.ts CHANGED
@@ -4,9 +4,9 @@ import { PublicKey } from '@solana/web3.js';
4
4
  import { EventEmitter } from 'events';
5
5
  import StrictEventEmitter from 'strict-event-emitter-types';
6
6
  import { DriftClient } from './driftClient';
7
- import { MarginCategory, Order, UserAccount, PerpPosition, SpotPosition, PerpMarketAccount, HealthComponents } from './types';
8
- import { UserAccountSubscriber, UserAccountEvents, DataAndSlot } from './accounts/types';
9
- import { PositionDirection, BN, SpotMarketAccount, MarketType } from '.';
7
+ import { HealthComponents, MarginCategory, Order, PerpMarketAccount, PerpPosition, SpotPosition, UserAccount } from './types';
8
+ import { DataAndSlot, UserAccountEvents, UserAccountSubscriber } from './accounts/types';
9
+ import { BN, MarketType, PositionDirection, SpotMarketAccount } from '.';
10
10
  import { OraclePriceData } from './oracles/types';
11
11
  import { UserConfig } from './userConfig';
12
12
  import { StrictOraclePrice } from './oracles/strictOraclePrice';
package/lib/user.js CHANGED
@@ -670,9 +670,7 @@ class User {
670
670
  * @returns : number (value from [0, 100])
671
671
  */
672
672
  getHealth() {
673
- const userAccount = this.getUserAccount();
674
- if ((0, types_1.isVariant)(userAccount.status, 'beingLiquidated') ||
675
- (0, types_1.isVariant)(userAccount.status, 'bankrupt')) {
673
+ if (this.isBeingLiquidated()) {
676
674
  return 0;
677
675
  }
678
676
  const totalCollateral = this.getTotalCollateral('Maintenance');
@@ -1004,13 +1002,12 @@ class User {
1004
1002
  };
1005
1003
  }
1006
1004
  isBeingLiquidated() {
1007
- return (0, types_1.isOneOfVariant)(this.getUserAccount().status, [
1008
- 'beingLiquidated',
1009
- 'bankrupt',
1010
- ]);
1005
+ return ((this.getUserAccount().status &
1006
+ (types_1.UserStatus.BEING_LIQUIDATED | types_1.UserStatus.BANKRUPT)) >
1007
+ 0);
1011
1008
  }
1012
1009
  isBankrupt() {
1013
- return (0, types_1.isVariant)(this.getUserAccount().status, 'bankrupt');
1010
+ return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
1014
1011
  }
1015
1012
  /**
1016
1013
  * Checks if any user position cumulative funding differs from respective market cumulative funding
@@ -1335,7 +1332,7 @@ class User {
1335
1332
  const outContributionInitial = this.calculateSpotPositionFreeCollateralContribution(outSpotPosition, outStrictOraclePrice);
1336
1333
  const { totalAssetValue: outTotalAssetValueInitial, totalLiabilityValue: outTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(outSpotPosition, outStrictOraclePrice);
1337
1334
  const initialContribution = inContributionInitial.add(outContributionInitial);
1338
- const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents(undefined, 'Initial');
1335
+ const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents();
1339
1336
  if (!calculateSwap) {
1340
1337
  calculateSwap = (inSwap) => {
1341
1338
  return inSwap
@@ -1490,7 +1487,7 @@ class User {
1490
1487
  this.getEmptySpotPosition(outMarketIndex);
1491
1488
  const { totalAssetValue: inTotalAssetValueInitial, totalLiabilityValue: inTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(inSpotPosition, inStrictOraclePrice);
1492
1489
  const { totalAssetValue: outTotalAssetValueInitial, totalLiabilityValue: outTotalLiabilityValueInitial, } = this.calculateSpotPositionLeverageContribution(outSpotPosition, outStrictOraclePrice);
1493
- const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents(undefined, 'Initial');
1490
+ const { perpLiabilityValue, perpPnl, spotAssetValue, spotLiabilityValue } = this.getLeverageComponents();
1494
1491
  const inPositionAfter = this.cloneAndUpdateSpotPosition(inSpotPosition, inAmount.abs().neg(), inMarket);
1495
1492
  const outPositionAfter = this.cloneAndUpdateSpotPosition(outSpotPosition, outAmount.abs(), outMarket);
1496
1493
  const { totalAssetValue: inTotalAssetValueAfter, totalLiabilityValue: inTotalLiabilityValueAfter, } = this.calculateSpotPositionLeverageContribution(inPositionAfter, inStrictOraclePrice);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.42.0-beta.14",
3
+ "version": "2.42.0-beta.16",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -5890,9 +5890,7 @@
5890
5890
  "docs": [
5891
5891
  "Whether the user is active, being liquidated or bankrupt"
5892
5892
  ],
5893
- "type": {
5894
- "defined": "UserStatus"
5895
- }
5893
+ "type": "u8"
5896
5894
  },
5897
5895
  {
5898
5896
  "name": "isMarginTradingEnabled",
@@ -8648,9 +8646,6 @@
8648
8646
  "type": {
8649
8647
  "kind": "enum",
8650
8648
  "variants": [
8651
- {
8652
- "name": "Active"
8653
- },
8654
8649
  {
8655
8650
  "name": "BeingLiquidated"
8656
8651
  },
package/src/types.ts CHANGED
@@ -27,11 +27,10 @@ export class MarketStatus {
27
27
  static readonly DELISTED = { delisted: {} };
28
28
  }
29
29
 
30
- export class UserStatus {
31
- static readonly ACTIVE = { active: {} };
32
- static readonly BEING_LIQUIDATED = { beingLiquidated: {} };
33
- static readonly BANKRUPT = { bankrupt: {} };
34
- static readonly REDUCE_ONLY = { reduceOnly: {} };
30
+ export enum UserStatus {
31
+ BEING_LIQUIDATED = 1,
32
+ BANKRUPT = 2,
33
+ REDUCE_ONLY = 4,
35
34
  }
36
35
 
37
36
  export class ContractType {
@@ -828,7 +827,7 @@ export type UserAccount = {
828
827
  spotPositions: SpotPosition[];
829
828
  perpPositions: PerpPosition[];
830
829
  orders: Order[];
831
- status: UserStatus;
830
+ status: number;
832
831
  nextLiquidationId: number;
833
832
  nextOrderId: number;
834
833
  maxMarginRatio: number;
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,
@@ -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,