@drift-labs/sdk 2.37.1-beta.1 → 2.37.1-beta.3

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/lib/user.js CHANGED
@@ -660,64 +660,81 @@ class User {
660
660
  }
661
661
  return health;
662
662
  }
663
+ calculateWeightedPerpPositionValue(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
664
+ const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
665
+ if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
666
+ // is an lp, clone so we dont mutate the position
667
+ perpPosition = this.getPerpPositionWithLPSettle(market.marketIndex, this.getClonedPosition(perpPosition), !!marginCategory)[0];
668
+ }
669
+ let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
670
+ if ((0, types_1.isVariant)(market.status, 'settlement')) {
671
+ valuationPrice = market.expiryPrice;
672
+ }
673
+ const baseAssetAmount = includeOpenOrders
674
+ ? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
675
+ : perpPosition.baseAssetAmount;
676
+ let baseAssetValue = baseAssetAmount
677
+ .abs()
678
+ .mul(valuationPrice)
679
+ .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
680
+ if (marginCategory) {
681
+ let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory));
682
+ if (marginCategory === 'Initial') {
683
+ marginRatio = _1.BN.max(marginRatio, new _1.BN(this.getUserAccount().maxMarginRatio));
684
+ }
685
+ if (liquidationBuffer !== undefined) {
686
+ marginRatio = marginRatio.add(liquidationBuffer);
687
+ }
688
+ if ((0, types_1.isVariant)(market.status, 'settlement')) {
689
+ marginRatio = numericConstants_1.ZERO;
690
+ }
691
+ const quoteSpotMarket = this.driftClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
692
+ const quoteOraclePriceData = this.driftClient.getOraclePriceDataAndSlot(quoteSpotMarket.oracle).data;
693
+ let quotePrice;
694
+ if (strict) {
695
+ quotePrice = _1.BN.max(quoteOraclePriceData.price, quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min);
696
+ }
697
+ else {
698
+ quotePrice = quoteOraclePriceData.price;
699
+ }
700
+ baseAssetValue = baseAssetValue
701
+ .mul(quotePrice)
702
+ .div(numericConstants_1.PRICE_PRECISION)
703
+ .mul(marginRatio)
704
+ .div(numericConstants_1.MARGIN_PRECISION);
705
+ if (includeOpenOrders) {
706
+ baseAssetValue = baseAssetValue.add(new _1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
707
+ if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
708
+ baseAssetValue = baseAssetValue.add(_1.BN.max(numericConstants_1.QUOTE_PRECISION, valuationPrice
709
+ .mul(market.amm.orderStepSize)
710
+ .mul(numericConstants_1.QUOTE_PRECISION)
711
+ .div(numericConstants_1.AMM_RESERVE_PRECISION)
712
+ .div(numericConstants_1.PRICE_PRECISION)));
713
+ }
714
+ }
715
+ }
716
+ return baseAssetValue;
717
+ }
718
+ /**
719
+ * calculates position value of a single perp market in margin system
720
+ * @returns : Precision QUOTE_PRECISION
721
+ */
722
+ getPerpMarketLiabilityValue(marketIndex, marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
723
+ const perpPosition = this.getPerpPosition(marketIndex);
724
+ if (!perpPosition) {
725
+ return numericConstants_1.ZERO;
726
+ }
727
+ else {
728
+ return this.calculateWeightedPerpPositionValue(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
729
+ }
730
+ }
663
731
  /**
664
732
  * calculates sum of position value across all positions in margin system
665
733
  * @returns : Precision QUOTE_PRECISION
666
734
  */
667
735
  getTotalPerpPositionValue(marginCategory, liquidationBuffer, includeOpenOrders, strict = false) {
668
736
  return this.getActivePerpPositions().reduce((totalPerpValue, perpPosition) => {
669
- const market = this.driftClient.getPerpMarketAccount(perpPosition.marketIndex);
670
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
671
- // is an lp, clone so we dont mutate the position
672
- perpPosition = this.getPerpPositionWithLPSettle(market.marketIndex, this.getClonedPosition(perpPosition), !!marginCategory)[0];
673
- }
674
- let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
675
- if ((0, types_1.isVariant)(market.status, 'settlement')) {
676
- valuationPrice = market.expiryPrice;
677
- }
678
- const baseAssetAmount = includeOpenOrders
679
- ? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
680
- : perpPosition.baseAssetAmount;
681
- let baseAssetValue = baseAssetAmount
682
- .abs()
683
- .mul(valuationPrice)
684
- .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
685
- if (marginCategory) {
686
- let marginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, baseAssetAmount.abs(), marginCategory));
687
- if (marginCategory === 'Initial') {
688
- marginRatio = _1.BN.max(marginRatio, new _1.BN(this.getUserAccount().maxMarginRatio));
689
- }
690
- if (liquidationBuffer !== undefined) {
691
- marginRatio = marginRatio.add(liquidationBuffer);
692
- }
693
- if ((0, types_1.isVariant)(market.status, 'settlement')) {
694
- marginRatio = numericConstants_1.ZERO;
695
- }
696
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(market.quoteSpotMarketIndex);
697
- const quoteOraclePriceData = this.driftClient.getOraclePriceDataAndSlot(quoteSpotMarket.oracle).data;
698
- let quotePrice;
699
- if (strict) {
700
- quotePrice = _1.BN.max(quoteOraclePriceData.price, quoteSpotMarket.historicalOracleData.lastOraclePriceTwap5Min);
701
- }
702
- else {
703
- quotePrice = quoteOraclePriceData.price;
704
- }
705
- baseAssetValue = baseAssetValue
706
- .mul(quotePrice)
707
- .div(numericConstants_1.PRICE_PRECISION)
708
- .mul(marginRatio)
709
- .div(numericConstants_1.MARGIN_PRECISION);
710
- if (includeOpenOrders) {
711
- baseAssetValue = baseAssetValue.add(new _1.BN(perpPosition.openOrders).mul(numericConstants_1.OPEN_ORDER_MARGIN_REQUIREMENT));
712
- if (perpPosition.lpShares.gt(numericConstants_1.ZERO)) {
713
- baseAssetValue = baseAssetValue.add(_1.BN.max(numericConstants_1.QUOTE_PRECISION, valuationPrice
714
- .mul(market.amm.orderStepSize)
715
- .mul(numericConstants_1.QUOTE_PRECISION)
716
- .div(numericConstants_1.AMM_RESERVE_PRECISION)
717
- .div(numericConstants_1.PRICE_PRECISION)));
718
- }
719
- }
720
- }
737
+ const baseAssetValue = this.calculateWeightedPerpPositionValue(perpPosition, marginCategory, liquidationBuffer, includeOpenOrders, strict);
721
738
  return totalPerpValue.add(baseAssetValue);
722
739
  }, numericConstants_1.ZERO);
723
740
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.37.1-beta.1",
3
+ "version": "2.37.1-beta.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -55,7 +55,7 @@
55
55
  "eslint-plugin-prettier": "^3.4.0",
56
56
  "lodash": "^4.17.21",
57
57
  "mocha": "^10.0.0",
58
- "prettier": "^2.4.1",
58
+ "prettier": "^3.0.1",
59
59
  "ts-node": "^10.8.0",
60
60
  "typescript": "^4.9.5"
61
61
  },
package/src/index.ts CHANGED
@@ -61,6 +61,7 @@ export * from './serum/serumSubscriber';
61
61
  export * from './serum/serumFulfillmentConfigMap';
62
62
  export * from './phoenix/phoenixSubscriber';
63
63
  export * from './phoenix/phoenixFulfillmentConfigMap';
64
+ export * from './tx/fastSingleTxSender';
64
65
  export * from './tx/retryTxSender';
65
66
  export * from './tx/types';
66
67
  export * from './util/computeUnits';