@drift-labs/sdk 2.74.0-beta.5 → 2.74.0-beta.7

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.74.0-beta.5
1
+ 2.74.0-beta.7
@@ -134,6 +134,11 @@ export declare class DriftClient {
134
134
  subAccountId: number;
135
135
  }[]): Promise<TransactionSignature>;
136
136
  getUpdateAdvancedDlpIx(advancedLp: boolean, subAccountId: number): Promise<anchor.web3.TransactionInstruction>;
137
+ updateUserReduceOnly(updates: {
138
+ reduceOnly: boolean;
139
+ subAccountId: number;
140
+ }[]): Promise<TransactionSignature>;
141
+ getUpdateUserReduceOnlyIx(reduceOnly: boolean, subAccountId: number): Promise<anchor.web3.TransactionInstruction>;
137
142
  fetchAllUserAccounts(includeIdle?: boolean): Promise<ProgramAccount<UserAccount>[]>;
138
143
  getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
139
144
  getUserAccountsAndAddressesForAuthority(authority: PublicKey): Promise<ProgramAccount<UserAccount>[]>;
@@ -627,6 +627,23 @@ class DriftClient {
627
627
  });
628
628
  return ix;
629
629
  }
630
+ async updateUserReduceOnly(updates) {
631
+ const ixs = await Promise.all(updates.map(async ({ reduceOnly, subAccountId }) => {
632
+ return await this.getUpdateUserReduceOnlyIx(reduceOnly, subAccountId);
633
+ }));
634
+ const tx = await this.buildTransaction(ixs, this.txParams);
635
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
636
+ return txSig;
637
+ }
638
+ async getUpdateUserReduceOnlyIx(reduceOnly, subAccountId) {
639
+ const ix = await this.program.instruction.updateUserReduceOnly(subAccountId, reduceOnly, {
640
+ accounts: {
641
+ user: (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId),
642
+ authority: this.wallet.publicKey,
643
+ },
644
+ });
645
+ return ix;
646
+ }
630
647
  async fetchAllUserAccounts(includeIdle = true) {
631
648
  let filters = undefined;
632
649
  if (!includeIdle) {
package/lib/user.d.ts CHANGED
@@ -246,13 +246,15 @@ export declare class User {
246
246
  /**
247
247
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
248
248
  * @param marketIndex
249
- * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
249
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^9
250
+ * @param estimatedEntryPrice
250
251
  * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
252
+ * @param includeOpenOrders
251
253
  * @returns Precision : PRICE_PRECISION
252
254
  */
253
- liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory): BN;
254
- calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN): BN;
255
- calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, marginCategory?: MarginCategory): BN | undefined;
255
+ liquidationPrice(marketIndex: number, positionBaseSizeChange?: BN, estimatedEntryPrice?: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean): BN;
256
+ calculateEntriesEffectOnFreeCollateral(market: PerpMarketAccount, oraclePrice: BN, perpPosition: PerpPosition, positionBaseSizeChange: BN, estimatedEntryPrice: BN, includeOpenOrders: boolean): BN;
257
+ calculateFreeCollateralDeltaForPerp(market: PerpMarketAccount, perpPosition: PerpPosition, positionBaseSizeChange: BN, marginCategory?: MarginCategory, includeOpenOrders?: boolean): BN | undefined;
256
258
  calculateFreeCollateralDeltaForSpot(market: SpotMarketAccount, signedTokenAmount: BN, marginCategory?: MarginCategory): BN;
257
259
  /**
258
260
  * Calculates the estimated liquidation price for a position after closing a quote amount of the position.
package/lib/user.js CHANGED
@@ -1082,13 +1082,15 @@ class User {
1082
1082
  /**
1083
1083
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
1084
1084
  * @param marketIndex
1085
- * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
1085
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^9
1086
+ * @param estimatedEntryPrice
1086
1087
  * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
1088
+ * @param includeOpenOrders
1087
1089
  * @returns Precision : PRICE_PRECISION
1088
1090
  */
1089
- liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance') {
1091
+ liquidationPrice(marketIndex, positionBaseSizeChange = numericConstants_1.ZERO, estimatedEntryPrice = numericConstants_1.ZERO, marginCategory = 'Maintenance', includeOpenOrders = false) {
1090
1092
  const totalCollateral = this.getTotalCollateral(marginCategory);
1091
- const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false);
1093
+ const marginRequirement = this.getMarginRequirement(marginCategory, undefined, false, includeOpenOrders);
1092
1094
  let freeCollateral = _1.BN.max(numericConstants_1.ZERO, totalCollateral.sub(marginRequirement));
1093
1095
  const oracle = this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
1094
1096
  const oraclePrice = this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
@@ -1096,9 +1098,9 @@ class User {
1096
1098
  const currentPerpPosition = this.getPerpPositionWithLPSettle(marketIndex, undefined, true)[0] ||
1097
1099
  this.getEmptyPosition(marketIndex);
1098
1100
  positionBaseSizeChange = (0, _1.standardizeBaseAssetAmount)(positionBaseSizeChange, market.amm.orderStepSize);
1099
- const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice);
1101
+ const freeCollateralChangeFromNewPosition = this.calculateEntriesEffectOnFreeCollateral(market, oraclePrice, currentPerpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders);
1100
1102
  freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
1101
- let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, marginCategory);
1103
+ let freeCollateralDelta = this.calculateFreeCollateralDeltaForPerp(market, currentPerpPosition, positionBaseSizeChange, marginCategory, includeOpenOrders);
1102
1104
  if (!freeCollateralDelta) {
1103
1105
  return new _1.BN(-1);
1104
1106
  }
@@ -1125,7 +1127,7 @@ class User {
1125
1127
  }
1126
1128
  return liqPrice;
1127
1129
  }
1128
- calculateEntriesEffectOnFreeCollateral(market, oraclePrice, perpPosition, positionBaseSizeChange, estimatedEntryPrice) {
1130
+ calculateEntriesEffectOnFreeCollateral(market, oraclePrice, perpPosition, positionBaseSizeChange, estimatedEntryPrice, includeOpenOrders) {
1129
1131
  let freeCollateralChange = numericConstants_1.ZERO;
1130
1132
  // update free collateral to account for change in pnl from new position
1131
1133
  if (!estimatedEntryPrice.eq(numericConstants_1.ZERO) && !positionBaseSizeChange.eq(numericConstants_1.ZERO)) {
@@ -1139,8 +1141,6 @@ class User {
1139
1141
  freeCollateralChange = costBasis.sub(newPositionValue);
1140
1142
  }
1141
1143
  else {
1142
- console.log('newPositionValue', newPositionValue.toString());
1143
- console.log('costBasis', costBasis.toString());
1144
1144
  freeCollateralChange = newPositionValue.sub(costBasis);
1145
1145
  }
1146
1146
  // assume worst fee tier
@@ -1150,30 +1150,33 @@ class User {
1150
1150
  .divn(takerFeeTier.feeDenominator);
1151
1151
  freeCollateralChange = freeCollateralChange.sub(takerFee);
1152
1152
  }
1153
- const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
1154
- const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
1155
- const newMarginRatio = (0, _1.calculateMarketMarginRatio)(market, newWorstCaseBaseAssetAmount.abs(), 'Maintenance');
1153
+ const baseAssetAmount = includeOpenOrders
1154
+ ? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
1155
+ : perpPosition.baseAssetAmount;
1156
+ const newBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
1157
+ const newMarginRatio = (0, _1.calculateMarketMarginRatio)(market, newBaseAssetAmount.abs(), 'Maintenance');
1156
1158
  // update free collateral to account for new margin requirement from position change
1157
- freeCollateralChange = freeCollateralChange.sub(newWorstCaseBaseAssetAmount
1159
+ freeCollateralChange = freeCollateralChange.sub(newBaseAssetAmount
1158
1160
  .abs()
1159
- .sub(worstCaseBaseAssetAmount.abs())
1161
+ .sub(baseAssetAmount.abs())
1160
1162
  .mul(oraclePrice)
1161
1163
  .div(numericConstants_1.BASE_PRECISION)
1162
1164
  .mul(new _1.BN(newMarginRatio))
1163
1165
  .div(numericConstants_1.MARGIN_PRECISION));
1164
1166
  return freeCollateralChange;
1165
1167
  }
1166
- calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, marginCategory = 'Maintenance') {
1167
- const currentBaseAssetAmount = perpPosition.baseAssetAmount;
1168
- const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition);
1169
- const orderBaseAssetAmount = worstCaseBaseAssetAmount.sub(currentBaseAssetAmount);
1170
- const proposedBaseAssetAmount = currentBaseAssetAmount.add(positionBaseSizeChange);
1171
- const proposedWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(positionBaseSizeChange);
1172
- const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedWorstCaseBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
1168
+ calculateFreeCollateralDeltaForPerp(market, perpPosition, positionBaseSizeChange, marginCategory = 'Maintenance', includeOpenOrders = false) {
1169
+ const baseAssetAmount = includeOpenOrders
1170
+ ? (0, margin_1.calculateWorstCaseBaseAssetAmount)(perpPosition)
1171
+ : perpPosition.baseAssetAmount;
1172
+ // zero if include orders == false
1173
+ const orderBaseAssetAmount = baseAssetAmount.sub(perpPosition.baseAssetAmount);
1174
+ const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
1175
+ const marginRatio = (0, _1.calculateMarketMarginRatio)(market, proposedBaseAssetAmount.abs(), marginCategory, this.getUserAccount().maxMarginRatio);
1173
1176
  const marginRatioQuotePrecision = new _1.BN(marginRatio)
1174
1177
  .mul(numericConstants_1.QUOTE_PRECISION)
1175
1178
  .div(numericConstants_1.MARGIN_PRECISION);
1176
- if (proposedWorstCaseBaseAssetAmount.eq(numericConstants_1.ZERO)) {
1179
+ if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO)) {
1177
1180
  return undefined;
1178
1181
  }
1179
1182
  let freeCollateralDelta = numericConstants_1.ZERO;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.74.0-beta.5",
3
+ "version": "2.74.0-beta.7",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1074,6 +1074,43 @@ export class DriftClient {
1074
1074
  return ix;
1075
1075
  }
1076
1076
 
1077
+ public async updateUserReduceOnly(
1078
+ updates: { reduceOnly: boolean; subAccountId: number }[]
1079
+ ): Promise<TransactionSignature> {
1080
+ const ixs = await Promise.all(
1081
+ updates.map(async ({ reduceOnly, subAccountId }) => {
1082
+ return await this.getUpdateUserReduceOnlyIx(reduceOnly, subAccountId);
1083
+ })
1084
+ );
1085
+
1086
+ const tx = await this.buildTransaction(ixs, this.txParams);
1087
+
1088
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
1089
+ return txSig;
1090
+ }
1091
+
1092
+ public async getUpdateUserReduceOnlyIx(
1093
+ reduceOnly: boolean,
1094
+ subAccountId: number
1095
+ ) {
1096
+ const ix = await this.program.instruction.updateUserReduceOnly(
1097
+ subAccountId,
1098
+ reduceOnly,
1099
+ {
1100
+ accounts: {
1101
+ user: getUserAccountPublicKeySync(
1102
+ this.program.programId,
1103
+ this.wallet.publicKey,
1104
+ subAccountId
1105
+ ),
1106
+ authority: this.wallet.publicKey,
1107
+ },
1108
+ }
1109
+ );
1110
+
1111
+ return ix;
1112
+ }
1113
+
1077
1114
  public async fetchAllUserAccounts(
1078
1115
  includeIdle = true
1079
1116
  ): Promise<ProgramAccount<UserAccount>[]> {
package/src/user.ts CHANGED
@@ -1975,21 +1975,25 @@ export class User {
1975
1975
  /**
1976
1976
  * Calculate the liquidation price of a perp position, with optional parameter to calculate the liquidation price after a trade
1977
1977
  * @param marketIndex
1978
- * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^13
1978
+ * @param positionBaseSizeChange // change in position size to calculate liquidation price for : Precision 10^9
1979
+ * @param estimatedEntryPrice
1979
1980
  * @param marginCategory // allow Initial to be passed in if we are trying to calculate price for DLP de-risking
1981
+ * @param includeOpenOrders
1980
1982
  * @returns Precision : PRICE_PRECISION
1981
1983
  */
1982
1984
  public liquidationPrice(
1983
1985
  marketIndex: number,
1984
1986
  positionBaseSizeChange: BN = ZERO,
1985
1987
  estimatedEntryPrice: BN = ZERO,
1986
- marginCategory: MarginCategory = 'Maintenance'
1988
+ marginCategory: MarginCategory = 'Maintenance',
1989
+ includeOpenOrders = false
1987
1990
  ): BN {
1988
1991
  const totalCollateral = this.getTotalCollateral(marginCategory);
1989
1992
  const marginRequirement = this.getMarginRequirement(
1990
1993
  marginCategory,
1991
1994
  undefined,
1992
- false
1995
+ false,
1996
+ includeOpenOrders
1993
1997
  );
1994
1998
  let freeCollateral = BN.max(ZERO, totalCollateral.sub(marginRequirement));
1995
1999
 
@@ -2015,7 +2019,8 @@ export class User {
2015
2019
  oraclePrice,
2016
2020
  currentPerpPosition,
2017
2021
  positionBaseSizeChange,
2018
- estimatedEntryPrice
2022
+ estimatedEntryPrice,
2023
+ includeOpenOrders
2019
2024
  );
2020
2025
 
2021
2026
  freeCollateral = freeCollateral.add(freeCollateralChangeFromNewPosition);
@@ -2024,7 +2029,8 @@ export class User {
2024
2029
  market,
2025
2030
  currentPerpPosition,
2026
2031
  positionBaseSizeChange,
2027
- marginCategory
2032
+ marginCategory,
2033
+ includeOpenOrders
2028
2034
  );
2029
2035
 
2030
2036
  if (!freeCollateralDelta) {
@@ -2082,7 +2088,8 @@ export class User {
2082
2088
  oraclePrice: BN,
2083
2089
  perpPosition: PerpPosition,
2084
2090
  positionBaseSizeChange: BN,
2085
- estimatedEntryPrice: BN
2091
+ estimatedEntryPrice: BN,
2092
+ includeOpenOrders: boolean
2086
2093
  ): BN {
2087
2094
  let freeCollateralChange = ZERO;
2088
2095
 
@@ -2097,8 +2104,6 @@ export class User {
2097
2104
  if (positionBaseSizeChange.gt(ZERO)) {
2098
2105
  freeCollateralChange = costBasis.sub(newPositionValue);
2099
2106
  } else {
2100
- console.log('newPositionValue', newPositionValue.toString());
2101
- console.log('costBasis', costBasis.toString());
2102
2107
  freeCollateralChange = newPositionValue.sub(costBasis);
2103
2108
  }
2104
2109
 
@@ -2111,24 +2116,23 @@ export class User {
2111
2116
  freeCollateralChange = freeCollateralChange.sub(takerFee);
2112
2117
  }
2113
2118
 
2114
- const worstCaseBaseAssetAmount =
2115
- calculateWorstCaseBaseAssetAmount(perpPosition);
2119
+ const baseAssetAmount = includeOpenOrders
2120
+ ? calculateWorstCaseBaseAssetAmount(perpPosition)
2121
+ : perpPosition.baseAssetAmount;
2116
2122
 
2117
- const newWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(
2118
- positionBaseSizeChange
2119
- );
2123
+ const newBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
2120
2124
 
2121
2125
  const newMarginRatio = calculateMarketMarginRatio(
2122
2126
  market,
2123
- newWorstCaseBaseAssetAmount.abs(),
2127
+ newBaseAssetAmount.abs(),
2124
2128
  'Maintenance'
2125
2129
  );
2126
2130
 
2127
2131
  // update free collateral to account for new margin requirement from position change
2128
2132
  freeCollateralChange = freeCollateralChange.sub(
2129
- newWorstCaseBaseAssetAmount
2133
+ newBaseAssetAmount
2130
2134
  .abs()
2131
- .sub(worstCaseBaseAssetAmount.abs())
2135
+ .sub(baseAssetAmount.abs())
2132
2136
  .mul(oraclePrice)
2133
2137
  .div(BASE_PRECISION)
2134
2138
  .mul(new BN(newMarginRatio))
@@ -2142,25 +2146,23 @@ export class User {
2142
2146
  market: PerpMarketAccount,
2143
2147
  perpPosition: PerpPosition,
2144
2148
  positionBaseSizeChange: BN,
2145
- marginCategory: MarginCategory = 'Maintenance'
2149
+ marginCategory: MarginCategory = 'Maintenance',
2150
+ includeOpenOrders = false
2146
2151
  ): BN | undefined {
2147
- const currentBaseAssetAmount = perpPosition.baseAssetAmount;
2152
+ const baseAssetAmount = includeOpenOrders
2153
+ ? calculateWorstCaseBaseAssetAmount(perpPosition)
2154
+ : perpPosition.baseAssetAmount;
2148
2155
 
2149
- const worstCaseBaseAssetAmount =
2150
- calculateWorstCaseBaseAssetAmount(perpPosition);
2151
- const orderBaseAssetAmount = worstCaseBaseAssetAmount.sub(
2152
- currentBaseAssetAmount
2153
- );
2154
- const proposedBaseAssetAmount = currentBaseAssetAmount.add(
2155
- positionBaseSizeChange
2156
- );
2157
- const proposedWorstCaseBaseAssetAmount = worstCaseBaseAssetAmount.add(
2158
- positionBaseSizeChange
2156
+ // zero if include orders == false
2157
+ const orderBaseAssetAmount = baseAssetAmount.sub(
2158
+ perpPosition.baseAssetAmount
2159
2159
  );
2160
2160
 
2161
+ const proposedBaseAssetAmount = baseAssetAmount.add(positionBaseSizeChange);
2162
+
2161
2163
  const marginRatio = calculateMarketMarginRatio(
2162
2164
  market,
2163
- proposedWorstCaseBaseAssetAmount.abs(),
2165
+ proposedBaseAssetAmount.abs(),
2164
2166
  marginCategory,
2165
2167
  this.getUserAccount().maxMarginRatio
2166
2168
  );
@@ -2168,7 +2170,7 @@ export class User {
2168
2170
  .mul(QUOTE_PRECISION)
2169
2171
  .div(MARGIN_PRECISION);
2170
2172
 
2171
- if (proposedWorstCaseBaseAssetAmount.eq(ZERO)) {
2173
+ if (proposedBaseAssetAmount.eq(ZERO)) {
2172
2174
  return undefined;
2173
2175
  }
2174
2176