@drift-labs/sdk 2.94.0-beta.3 → 2.94.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.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.94.0-beta.3
1
+ 2.94.0-beta.4
package/lib/user.d.ts CHANGED
@@ -4,7 +4,7 @@ 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 { HealthComponents, MarginCategory, Order, PerpMarketAccount, PerpPosition, SpotPosition, UserAccount, UserStatus } from './types';
7
+ import { HealthComponent, HealthComponents, MarginCategory, Order, PerpMarketAccount, PerpPosition, SpotPosition, UserAccount, UserStatus } from './types';
8
8
  import { DataAndSlot, UserAccountEvents, UserAccountSubscriber } from './accounts/types';
9
9
  import { BN, MarketType, PositionDirection, SpotMarketAccount } from '.';
10
10
  import { OraclePriceData } from './oracles/types';
@@ -387,6 +387,12 @@ export declare class User {
387
387
  perpTier: number;
388
388
  spotTier: number;
389
389
  };
390
+ getPerpPositionHealth({ marginCategory, perpPosition, oraclePriceData, quoteOraclePriceData, }: {
391
+ marginCategory: MarginCategory;
392
+ perpPosition: PerpPosition;
393
+ oraclePriceData?: OraclePriceData;
394
+ quoteOraclePriceData?: OraclePriceData;
395
+ }): HealthComponent;
390
396
  getHealthComponents({ marginCategory, }: {
391
397
  marginCategory: MarginCategory;
392
398
  }): HealthComponents;
package/lib/user.js CHANGED
@@ -1988,6 +1988,37 @@ class User {
1988
1988
  spotTier: safestSpotTier,
1989
1989
  };
1990
1990
  }
1991
+ getPerpPositionHealth({ marginCategory, perpPosition, oraclePriceData, quoteOraclePriceData, }) {
1992
+ const settledLpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, perpPosition)[0];
1993
+ const perpMarket = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
1994
+ const _oraclePriceData = oraclePriceData ||
1995
+ this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
1996
+ const oraclePrice = _oraclePriceData.price;
1997
+ const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, _1.calculateWorstCasePerpLiabilityValue)(settledLpPosition, perpMarket, oraclePrice);
1998
+ const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
1999
+ const _quoteOraclePriceData = quoteOraclePriceData ||
2000
+ this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
2001
+ let marginRequirement = worstCaseLiabilityValue
2002
+ .mul(_quoteOraclePriceData.price)
2003
+ .div(numericConstants_1.PRICE_PRECISION)
2004
+ .mul(marginRatio)
2005
+ .div(numericConstants_1.MARGIN_PRECISION);
2006
+ marginRequirement = marginRequirement.add(new _1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
2007
+ if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
2008
+ marginRequirement = marginRequirement.add(_1.BN.max(numericConstants_1.QUOTE_PRECISION, oraclePrice
2009
+ .mul(perpMarket.amm.orderStepSize)
2010
+ .mul(numericConstants_1.QUOTE_PRECISION)
2011
+ .div(numericConstants_1.AMM_RESERVE_PRECISION)
2012
+ .div(numericConstants_1.PRICE_PRECISION)));
2013
+ }
2014
+ return {
2015
+ marketIndex: perpMarket.marketIndex,
2016
+ size: worstCaseBaseAmount,
2017
+ value: worstCaseLiabilityValue,
2018
+ weight: marginRatio,
2019
+ weightedValue: marginRequirement,
2020
+ };
2021
+ }
1991
2022
  getHealthComponents({ marginCategory, }) {
1992
2023
  const healthComponents = {
1993
2024
  deposits: [],
@@ -1996,34 +2027,16 @@ class User {
1996
2027
  perpPnl: [],
1997
2028
  };
1998
2029
  for (const perpPosition of this.getActivePerpPositions()) {
1999
- const settledLpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, perpPosition)[0];
2000
2030
  const perpMarket = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
2001
2031
  const oraclePriceData = this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
2002
- const oraclePrice = oraclePriceData.price;
2003
- const { worstCaseBaseAssetAmount: worstCaseBaseAmount, worstCaseLiabilityValue, } = (0, _1.calculateWorstCasePerpLiabilityValue)(settledLpPosition, perpMarket, oraclePrice);
2004
- const marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(perpMarket, worstCaseBaseAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio));
2005
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
2006
2032
  const quoteOraclePriceData = this.driftClient.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
2007
- let marginRequirement = worstCaseLiabilityValue
2008
- .mul(quoteOraclePriceData.price)
2009
- .div(numericConstants_1.PRICE_PRECISION)
2010
- .mul(marginRatio)
2011
- .div(numericConstants_1.MARGIN_PRECISION);
2012
- marginRequirement = marginRequirement.add(new _1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
2013
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
2014
- marginRequirement = marginRequirement.add(_1.BN.max(numericConstants_1.QUOTE_PRECISION, oraclePrice
2015
- .mul(perpMarket.amm.orderStepSize)
2016
- .mul(numericConstants_1.QUOTE_PRECISION)
2017
- .div(numericConstants_1.AMM_RESERVE_PRECISION)
2018
- .div(numericConstants_1.PRICE_PRECISION)));
2019
- }
2020
- healthComponents.perpPositions.push({
2021
- marketIndex: perpMarket.marketIndex,
2022
- size: worstCaseBaseAmount,
2023
- value: worstCaseLiabilityValue,
2024
- weight: marginRatio,
2025
- weightedValue: marginRequirement,
2026
- });
2033
+ healthComponents.perpPositions.push(this.getPerpPositionHealth({
2034
+ marginCategory,
2035
+ perpPosition,
2036
+ oraclePriceData,
2037
+ quoteOraclePriceData,
2038
+ }));
2039
+ const quoteSpotMarket = this.driftClient.getSpotMarketAccount(perpMarket.quoteSpotMarketIndex);
2027
2040
  const settledPerpPosition = this.getPerpPositionWithLPSettle(perpPosition.marketIndex, perpPosition)[0];
2028
2041
  const positionUnrealizedPnl = (0, _1.calculatePositionPNL)(perpMarket, settledPerpPosition, true, oraclePriceData);
2029
2042
  let pnlWeight;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.94.0-beta.3",
3
+ "version": "2.94.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
package/src/user.ts CHANGED
@@ -3,6 +3,7 @@ import { EventEmitter } from 'events';
3
3
  import StrictEventEmitter from 'strict-event-emitter-types';
4
4
  import { DriftClient } from './driftClient';
5
5
  import {
6
+ HealthComponent,
6
7
  HealthComponents,
7
8
  isVariant,
8
9
  MarginCategory,
@@ -3701,6 +3702,82 @@ export class User {
3701
3702
  };
3702
3703
  }
3703
3704
 
3705
+ public getPerpPositionHealth({
3706
+ marginCategory,
3707
+ perpPosition,
3708
+ oraclePriceData,
3709
+ quoteOraclePriceData,
3710
+ }: {
3711
+ marginCategory: MarginCategory;
3712
+ perpPosition: PerpPosition;
3713
+ oraclePriceData?: OraclePriceData;
3714
+ quoteOraclePriceData?: OraclePriceData;
3715
+ }): HealthComponent {
3716
+ const settledLpPosition = this.getPerpPositionWithLPSettle(
3717
+ perpPosition.marketIndex,
3718
+ perpPosition
3719
+ )[0];
3720
+ const perpMarket = this.driftClient.getPerpMarketAccount(
3721
+ perpPosition.marketIndex
3722
+ );
3723
+ const _oraclePriceData =
3724
+ oraclePriceData ||
3725
+ this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
3726
+ const oraclePrice = _oraclePriceData.price;
3727
+ const {
3728
+ worstCaseBaseAssetAmount: worstCaseBaseAmount,
3729
+ worstCaseLiabilityValue,
3730
+ } = calculateWorstCasePerpLiabilityValue(
3731
+ settledLpPosition,
3732
+ perpMarket,
3733
+ oraclePrice
3734
+ );
3735
+
3736
+ const marginRatio = new BN(
3737
+ calculateMarketMarginRatio(
3738
+ perpMarket,
3739
+ worstCaseBaseAmount.abs(),
3740
+ marginCategory,
3741
+ this.getUserAccount().maxMarginRatio
3742
+ )
3743
+ );
3744
+
3745
+ const _quoteOraclePriceData =
3746
+ quoteOraclePriceData ||
3747
+ this.driftClient.getOracleDataForSpotMarket(QUOTE_SPOT_MARKET_INDEX);
3748
+
3749
+ let marginRequirement = worstCaseLiabilityValue
3750
+ .mul(_quoteOraclePriceData.price)
3751
+ .div(PRICE_PRECISION)
3752
+ .mul(marginRatio)
3753
+ .div(MARGIN_PRECISION);
3754
+
3755
+ marginRequirement = marginRequirement.add(
3756
+ new BN(perpPosition.openOrders).mul(OPEN_ORDER_MARGIN_REQUIREMENT)
3757
+ );
3758
+
3759
+ if (perpPosition.lpShares.gt(ZERO)) {
3760
+ marginRequirement = marginRequirement.add(
3761
+ BN.max(
3762
+ QUOTE_PRECISION,
3763
+ oraclePrice
3764
+ .mul(perpMarket.amm.orderStepSize)
3765
+ .mul(QUOTE_PRECISION)
3766
+ .div(AMM_RESERVE_PRECISION)
3767
+ .div(PRICE_PRECISION)
3768
+ )
3769
+ );
3770
+ }
3771
+
3772
+ return {
3773
+ marketIndex: perpMarket.marketIndex,
3774
+ size: worstCaseBaseAmount,
3775
+ value: worstCaseLiabilityValue,
3776
+ weight: marginRatio,
3777
+ weightedValue: marginRequirement,
3778
+ };
3779
+ }
3780
+
3704
3781
  public getHealthComponents({
3705
3782
  marginCategory,
3706
3783
  }: {
@@ -3714,72 +3791,30 @@ export class User {
3714
3791
  };
3715
3792
 
3716
3793
  for (const perpPosition of this.getActivePerpPositions()) {
3717
- const settledLpPosition = this.getPerpPositionWithLPSettle(
3718
- perpPosition.marketIndex,
3719
- perpPosition
3720
- )[0];
3721
3794
  const perpMarket = this.driftClient.getPerpMarketAccount(
3722
3795
  perpPosition.marketIndex
3723
3796
  );
3797
+
3724
3798
  const oraclePriceData = this.driftClient.getOracleDataForPerpMarket(
3725
3799
  perpMarket.marketIndex
3726
3800
  );
3727
- const oraclePrice = oraclePriceData.price;
3728
- const {
3729
- worstCaseBaseAssetAmount: worstCaseBaseAmount,
3730
- worstCaseLiabilityValue,
3731
- } = calculateWorstCasePerpLiabilityValue(
3732
- settledLpPosition,
3733
- perpMarket,
3734
- oraclePrice
3801
+
3802
+ const quoteOraclePriceData = this.driftClient.getOracleDataForSpotMarket(
3803
+ QUOTE_SPOT_MARKET_INDEX
3735
3804
  );
3736
3805
 
3737
- const marginRatio = new BN(
3738
- calculateMarketMarginRatio(
3739
- perpMarket,
3740
- worstCaseBaseAmount.abs(),
3806
+ healthComponents.perpPositions.push(
3807
+ this.getPerpPositionHealth({
3741
3808
  marginCategory,
3742
- this.getUserAccount().maxMarginRatio
3743
- )
3809
+ perpPosition,
3810
+ oraclePriceData,
3811
+ quoteOraclePriceData,
3812
+ })
3744
3813
  );
3745
3814
 
3746
3815
  const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
3747
3816
  perpMarket.quoteSpotMarketIndex
3748
3817
  );
3749
- const quoteOraclePriceData = this.driftClient.getOracleDataForSpotMarket(
3750
- QUOTE_SPOT_MARKET_INDEX
3751
- );
3752
-
3753
- let marginRequirement = worstCaseLiabilityValue
3754
- .mul(quoteOraclePriceData.price)
3755
- .div(PRICE_PRECISION)
3756
- .mul(marginRatio)
3757
- .div(MARGIN_PRECISION);
3758
-
3759
- marginRequirement = marginRequirement.add(
3760
- new BN(perpPosition.openOrders).mul(OPEN_ORDER_MARGIN_REQUIREMENT)
3761
- );
3762
-
3763
- if (perpPosition.lpShares.gt(ZERO)) {
3764
- marginRequirement = marginRequirement.add(
3765
- BN.max(
3766
- QUOTE_PRECISION,
3767
- oraclePrice
3768
- .mul(perpMarket.amm.orderStepSize)
3769
- .mul(QUOTE_PRECISION)
3770
- .div(AMM_RESERVE_PRECISION)
3771
- .div(PRICE_PRECISION)
3772
- )
3773
- );
3774
- }
3775
-
3776
- healthComponents.perpPositions.push({
3777
- marketIndex: perpMarket.marketIndex,
3778
- size: worstCaseBaseAmount,
3779
- value: worstCaseLiabilityValue,
3780
- weight: marginRatio,
3781
- weightedValue: marginRequirement,
3782
- });
3783
3818
 
3784
3819
  const settledPerpPosition = this.getPerpPositionWithLPSettle(
3785
3820
  perpPosition.marketIndex,