@drift-labs/sdk 2.60.0-beta.10 → 2.60.0-beta.12

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.60.0-beta.10
1
+ 2.60.0-beta.12
@@ -8463,6 +8463,9 @@
8463
8463
  },
8464
8464
  {
8465
8465
  "name": "OrderFilledWithLPJit"
8466
+ },
8467
+ {
8468
+ "name": "DeriskLp"
8466
8469
  }
8467
8470
  ]
8468
8471
  }
@@ -8480,6 +8483,9 @@
8480
8483
  },
8481
8484
  {
8482
8485
  "name": "SettleLiquidity"
8486
+ },
8487
+ {
8488
+ "name": "RemoveLiquidityDerisk"
8483
8489
  }
8484
8490
  ]
8485
8491
  }
@@ -8621,10 +8627,6 @@
8621
8627
  {
8622
8628
  "name": "Liquidation",
8623
8629
  "fields": [
8624
- {
8625
- "name": "margin_buffer",
8626
- "type": "u128"
8627
- },
8628
8630
  {
8629
8631
  "name": "market_to_track_margin_requirement",
8630
8632
  "type": {
package/lib/types.d.ts CHANGED
@@ -252,6 +252,9 @@ export declare class OrderActionExplanation {
252
252
  static readonly REDUCE_ONLY_ORDER_INCREASED_POSITION: {
253
253
  reduceOnlyOrderIncreasedPosition: {};
254
254
  };
255
+ static readonly DERISK_LP: {
256
+ deriskLp: {};
257
+ };
255
258
  }
256
259
  export declare class OrderTriggerCondition {
257
260
  static readonly ABOVE: {
@@ -447,6 +450,9 @@ export declare class LPAction {
447
450
  static readonly SETTLE_LIQUIDITY: {
448
451
  settleLiquidity: {};
449
452
  };
453
+ static readonly REMOVE_LIQUIDITY_DERISK: {
454
+ removeLiquidityDerisk: {};
455
+ };
450
456
  }
451
457
  export type FundingRateRecord = {
452
458
  ts: BN;
package/lib/types.js CHANGED
@@ -175,6 +175,9 @@ OrderActionExplanation.ORDER_FILLED_WITH_PHOENIX = {
175
175
  OrderActionExplanation.REDUCE_ONLY_ORDER_INCREASED_POSITION = {
176
176
  reduceOnlyOrderIncreasedPosition: {},
177
177
  };
178
+ OrderActionExplanation.DERISK_LP = {
179
+ deriskLp: {},
180
+ };
178
181
  class OrderTriggerCondition {
179
182
  }
180
183
  exports.OrderTriggerCondition = OrderTriggerCondition;
@@ -243,6 +246,7 @@ exports.LPAction = LPAction;
243
246
  LPAction.ADD_LIQUIDITY = { addLiquidity: {} };
244
247
  LPAction.REMOVE_LIQUIDITY = { removeLiquidity: {} };
245
248
  LPAction.SETTLE_LIQUIDITY = { settleLiquidity: {} };
249
+ LPAction.REMOVE_LIQUIDITY_DERISK = { removeLiquidityDerisk: {} };
246
250
  class LiquidationType {
247
251
  }
248
252
  exports.LiquidationType = LiquidationType;
package/lib/user.d.ts CHANGED
@@ -111,7 +111,7 @@ export declare class User {
111
111
  /**
112
112
  * @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
113
113
  */
114
- getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean): BN;
114
+ getMarginRequirement(marginCategory: MarginCategory, liquidationBuffer?: BN, strict?: boolean, includeOpenOrders?: boolean): BN;
115
115
  /**
116
116
  * @returns The initial margin requirement in USDC. : QUOTE_PRECISION
117
117
  */
@@ -246,12 +246,13 @@ export declare class User {
246
246
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
247
247
  * @param marketIndex
248
248
  * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
249
+ * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
249
250
  * @returns Precision : PRICE_PRECISION
250
251
  */
251
- liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN): BN;
252
+ liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory): BN;
252
253
  calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN): BN;
253
- calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN): BN | undefined;
254
- calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN): BN;
254
+ calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, marginCategory?: MarginCategory): BN | undefined;
255
+ calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN, marginCategory?: MarginCategory): BN;
255
256
  /**
256
257
  * Calculates the estimated liquidation price for a position after closing a quote amount of the position.
257
258
  * @param positionMarketIndex
package/lib/user.js CHANGED
@@ -432,8 +432,8 @@ class User {
432
432
  /**
433
433
  * @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
434
434
  */
435
- getMarginRequirement(marginCategory, liquidationBuffer, strict = false) {
436
- return this.getTotalPerpPositionValue(marginCategory, liquidationBuffer, true, strict).add(this.getSpotMarketLiabilityValue(undefined, marginCategory, liquidationBuffer, true, strict));
435
+ getMarginRequirement(marginCategory, liquidationBuffer, strict = false, includeOpenOrders = true) {
436
+ return this.getTotalPerpPositionValue(marginCategory, liquidationBuffer, includeOpenOrders, strict).add(this.getSpotMarketLiabilityValue(undefined, marginCategory, liquidationBuffer, includeOpenOrders, strict));
437
437
  }
438
438
  /**
439
439
  * @returns The initial margin requirement in USDC. : QUOTE_PRECISION
@@ -1080,12 +1080,13 @@ class User {
1080
1080
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
1081
1081
  * @param marketIndex
1082
1082
  * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
1083
+ * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
1083
1084
  * @returns Precision : PRICE_PRECISION
1084
1085
  */
1085
- liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO) {
1086
- const totalCollateral = this.getTotalCollateral('Maintenance');
1087
- const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
1088
- let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(maintenanceMarginRequirement));
1086
+ liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance') {
1087
+ const totalCollateral = this.getTotalCollateral(marginCategory);
1088
+ const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false);
1089
+ let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement));
1089
1090
  const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
1090
1091
  const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
1091
1092
  const market = this.driftClient.getPerpMarketAccount(marketIndex);
@@ -1094,7 +1095,7 @@ class User {
1094
1095
  positionBaseSizeChange = (0, _1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
1095
1096
  const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice);
1096
1097
  freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
1097
- let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange);
1098
+ let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, marginCategory);
1098
1099
  if (!freeCollateralDelta) {
1099
1100
  return new _1.BN(-1);
1100
1101
  }
@@ -1105,7 +1106,7 @@ class User {
1105
1106
  const spotPosition = this.getSpotPosition(spotMarketWithSameOracle.marketIndex);
1106
1107
  if (spotPosition) {
1107
1108
  const signedTokenAmount = (0, _1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketWithSameOracle, spotPosition.balanceType), spotPosition.balanceType);
1108
- const spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount);
1109
+ const spotFreeCollateralDelta = this.calculateFreeCollateralDeltaForSpot(spotMarketWithSameOracle, signedTokenAmount, marginCategory);
1109
1110
  freeCollateralDelta = freeCollateralDelta.add(spotFreeCollateralDelta || numericConstants_1.ZERO);
1110
1111
  }
1111
1112
  }
@@ -1162,13 +1163,13 @@ class User {
1162
1163
  .div(numericConstants_1.MARGIN_PRECISION));
1163
1164
  return freeCollateralChange;
1164
1165
  }
1165
- calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange) {
1166
+ calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, marginCategory = 'Maintenance') {
1166
1167
  const currentBaseAssetAmount = perpPosition.baseAssetAmount;
1167
1168
  const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
1168
1169
  const orderBaseAssetAmount = worstCaseBaseAssetAmount.sub(currentBaseAssetAmount);
1169
1170
  const proposedBaseAssetAmount = currentBaseAssetAmount.add(positionBaseSizeChange);
1170
1171
  const proposedWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
1171
- const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(), 'Maintenance');
1172
+ const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
1172
1173
  const marginRatioQuotePrecision = new _1.BN(marginRatio)
1173
1174
  .mul(numericConstants_1.QUOTE_PRECISION)
1174
1175
  .div(numericConstants_1.MARGIN_PRECISION);
@@ -1194,17 +1195,17 @@ class User {
1194
1195
  }
1195
1196
  return freeCollateralDelta;
1196
1197
  }
1197
- calculateFreeCollateralDeltaForSpot(market, signedTokenAmount) {
1198
+ calculateFreeCollateralDeltaForSpot(market, signedTokenAmount, marginCategory = 'Maintenance') {
1198
1199
  const tokenPrecision = new _1.BN(Math.pow(10, market.decimals));
1199
1200
  if (signedTokenAmount.gt(numericConstants_1.ZERO)) {
1200
- const assetWeight = (0, spotBalance_1.calculateAssetWeight)(signedTokenAmount, this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price, market, 'Maintenance');
1201
+ const assetWeight = (0, spotBalance_1.calculateAssetWeight)(signedTokenAmount, this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price, market, marginCategory);
1201
1202
  return numericConstants_1.QUOTE_PRECISION.mul(assetWeight)
1202
1203
  .div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
1203
1204
  .mul(signedTokenAmount)
1204
1205
  .div(tokenPrecision);
1205
1206
  }
1206
1207
  else {
1207
- const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(signedTokenAmount.abs(), market, 'Maintenance');
1208
+ const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(signedTokenAmount.abs(), market, marginCategory);
1208
1209
  return numericConstants_1.QUOTE_PRECISION.neg()
1209
1210
  .mul(liabilityWeight)
1210
1211
  .div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.60.0-beta.10",
3
+ "version": "2.60.0-beta.12",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -8463,6 +8463,9 @@
8463
8463
  },
8464
8464
  {
8465
8465
  "name": "OrderFilledWithLPJit"
8466
+ },
8467
+ {
8468
+ "name": "DeriskLp"
8466
8469
  }
8467
8470
  ]
8468
8471
  }
@@ -8480,6 +8483,9 @@
8480
8483
  },
8481
8484
  {
8482
8485
  "name": "SettleLiquidity"
8486
+ },
8487
+ {
8488
+ "name": "RemoveLiquidityDerisk"
8483
8489
  }
8484
8490
  ]
8485
8491
  }
@@ -8621,10 +8627,6 @@
8621
8627
  {
8622
8628
  "name": "Liquidation",
8623
8629
  "fields": [
8624
- {
8625
- "name": "margin_buffer",
8626
- "type": "u128"
8627
- },
8628
8630
  {
8629
8631
  "name": "market_to_track_margin_requirement",
8630
8632
  "type": {
package/src/types.ts CHANGED
@@ -174,6 +174,9 @@ export class OrderActionExplanation {
174
174
  static readonly REDUCE_ONLY_ORDER_INCREASED_POSITION = {
175
175
  reduceOnlyOrderIncreasedPosition: {},
176
176
  };
177
+ static readonly DERISK_LP = {
178
+ deriskLp: {},
179
+ };
177
180
  }
178
181
 
179
182
  export class OrderTriggerCondition {
@@ -357,6 +360,7 @@ export class LPAction {
357
360
  static readonly ADD_LIQUIDITY = { addLiquidity: {} };
358
361
  static readonly REMOVE_LIQUIDITY = { removeLiquidity: {} };
359
362
  static readonly SETTLE_LIQUIDITY = { settleLiquidity: {} };
363
+ static readonly REMOVE_LIQUIDITY_DERISK = { removeLiquidityDerisk: {} };
360
364
  }
361
365
 
362
366
  export type FundingRateRecord = {
package/src/user.ts CHANGED
@@ -674,19 +674,20 @@ export class User {
674
674
  public getMarginRequirement(
675
675
  marginCategory: MarginCategory,
676
676
  liquidationBuffer?: BN,
677
- strict = false
677
+ strict = false,
678
+ includeOpenOrders = true
678
679
  ): BN {
679
680
  return this.getTotalPerpPositionValue(
680
681
  marginCategory,
681
682
  liquidationBuffer,
682
- true,
683
+ includeOpenOrders,
683
684
  strict
684
685
  ).add(
685
686
  this.getSpotMarketLiabilityValue(
686
687
  undefined,
687
688
  marginCategory,
688
689
  liquidationBuffer,
689
- true,
690
+ includeOpenOrders,
690
691
  strict
691
692
  )
692
693
  );
@@ -1970,19 +1971,22 @@ export class User {
1970
1971
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
1971
1972
  * @param marketIndex
1972
1973
  * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
1974
+ * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
1973
1975
  * @returns Precision : PRICE_PRECISION
1974
1976
  */
1975
1977
  public liquidationPrice(
1976
1978
  marketIndex: number,
1977
1979
  positionBaseSizeChange: BN = ZERO,
1978
- estimatedEntryPrice: BN = ZERO
1980
+ estimatedEntryPrice: BN = ZERO,
1981
+ marginCategory: MarginCategory = 'Maintenance'
1979
1982
  ): BN {
1980
- const totalCollateral = this.getTotalCollateral('Maintenance');
1981
- const maintenanceMarginRequirement = this.getMaintenanceMarginRequirement();
1982
- let freeCollateral = BN.max(
1983
- ZERO,
1984
- totalCollateral.sub(maintenanceMarginRequirement)
1983
+ const totalCollateral = this.getTotalCollateral(marginCategory);
1984
+ const marginRequirement = this.getMarginRequirement(
1985
+ marginCategory,
1986
+ undefined,
1987
+ false
1985
1988
  );
1989
+ let freeCollateral = BN.max(ZERO, totalCollateral.sub(marginRequirement));
1986
1990
 
1987
1991
  const oracle =
1988
1992
  this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
@@ -2014,7 +2018,8 @@ export class User {
2014
2018
  let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(
2015
2019
  market,
2016
2020
  currentPerpPosition,
2017
- positionBaseSizeChange
2021
+ positionBaseSizeChange,
2022
+ marginCategory
2018
2023
  );
2019
2024
 
2020
2025
  if (!freeCollateralDelta) {
@@ -2041,7 +2046,8 @@ export class User {
2041
2046
  const spotFreeCollateralDelta =
2042
2047
  this.calculateFreeCollateralDeltaForSpot(
2043
2048
  spotMarketWithSameOracle,
2044
- signedTokenAmount
2049
+ signedTokenAmount,
2050
+ marginCategory
2045
2051
  );
2046
2052
  freeCollateralDelta = freeCollateralDelta.add(
2047
2053
  spotFreeCollateralDelta || ZERO
@@ -2141,7 +2147,8 @@ export class User {
2141
2147
  calculateFreeCollateralDeltaForPerp(
2142
2148
  market: PerpMarketAccount,
2143
2149
  perpPosition: PerpPosition,
2144
- positionBaseSizeChange: BN
2150
+ positionBaseSizeChange: BN,
2151
+ marginCategory: MarginCategory = 'Maintenance'
2145
2152
  ): BN | undefined {
2146
2153
  const currentBaseAssetAmount = perpPosition.baseAssetAmount;
2147
2154
 
@@ -2160,7 +2167,8 @@ export class User {
2160
2167
  const marginRatio = calculateMarketMarginRatio(
2161
2168
  market,
2162
2169
  proposedWorstCaseBaseAssetAmount.abs(),
2163
- 'Maintenance'
2170
+ marginCategory,
2171
+ this.getUserAccount().maxMarginRatio
2164
2172
  );
2165
2173
  const marginRatioQuotePrecision = new BN(marginRatio)
2166
2174
  .mul(QUOTE_PRECISION)
@@ -2195,7 +2203,8 @@ export class User {
2195
2203
 
2196
2204
  calculateFreeCollateralDeltaForSpot(
2197
2205
  market: SpotMarketAccount,
2198
- signedTokenAmount: BN
2206
+ signedTokenAmount: BN,
2207
+ marginCategory: MarginCategory = 'Maintenance'
2199
2208
  ): BN {
2200
2209
  const tokenPrecision = new BN(Math.pow(10, market.decimals));
2201
2210
 
@@ -2204,7 +2213,7 @@ export class User {
2204
2213
  signedTokenAmount,
2205
2214
  this.driftClient.getOraclePriceDataAndSlot(market.oracle).data.price,
2206
2215
  market,
2207
- 'Maintenance'
2216
+ marginCategory
2208
2217
  );
2209
2218
 
2210
2219
  return QUOTE_PRECISION.mul(assetWeight)
@@ -2215,7 +2224,7 @@ export class User {
2215
2224
  const liabilityWeight = calculateLiabilityWeight(
2216
2225
  signedTokenAmount.abs(),
2217
2226
  market,
2218
- 'Maintenance'
2227
+ marginCategory
2219
2228
  );
2220
2229
 
2221
2230
  return QUOTE_PRECISION.neg()