@drift-labs/sdk 2.97.0-beta.15 → 2.97.0-beta.17

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.97.0-beta.15
1
+ 2.97.0-beta.17
@@ -296,6 +296,15 @@ function decodeUser(buffer) {
296
296
  offset += 1;
297
297
  const hasOpenAuction = buffer.readUInt8(offset) === 1;
298
298
  offset += 1;
299
+ let marginMode;
300
+ const marginModeNum = buffer.readUInt8(offset);
301
+ if (marginModeNum === 0) {
302
+ marginMode = __1.MarginMode.DEFAULT;
303
+ }
304
+ else {
305
+ marginMode = __1.MarginMode.HIGH_LEVERAGE;
306
+ }
307
+ offset += 1;
299
308
  // @ts-ignore
300
309
  return {
301
310
  authority,
@@ -324,6 +333,7 @@ function decodeUser(buffer) {
324
333
  hasOpenOrder,
325
334
  openAuctions,
326
335
  hasOpenAuction,
336
+ marginMode,
327
337
  };
328
338
  }
329
339
  exports.decodeUser = decodeUser;
@@ -21,19 +21,19 @@ export declare function calculateWorstCasePerpLiabilityValue(perpPosition: PerpP
21
21
  worstCaseBaseAssetAmount: BN;
22
22
  worstCaseLiabilityValue: BN;
23
23
  };
24
- export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, oraclePrice: BN, isPredictionMarket: boolean): BN;
24
+ export declare function calculatePerpLiabilityValue(baseAssetAmount: BN, price: BN, isPredictionMarket: boolean): BN;
25
25
  /**
26
26
  * Calculates the margin required to open a trade, in quote amount. Only accounts for the trade size as a scalar value, does not account for the trade direction or current open positions and whether the trade would _actually_ be risk-increasing and use any extra collateral.
27
27
  * @param targetMarketIndex
28
28
  * @param baseSize
29
29
  * @returns
30
30
  */
31
- export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
31
+ export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, userMaxMarginRatio?: number, userHighLeverageMode?: boolean, entryPrice?: BN): BN;
32
32
  /**
33
33
  * Similar to calculatetMarginUSDCRequiredForTrade, but calculates how much of a given collateral is required to cover the margin requirements for a given trade. Basically does the same thing as getMarginUSDCRequiredForTrade but also accounts for asset weight of the selected collateral.
34
34
  *
35
35
  * Returns collateral required in the precision of the target collateral market.
36
36
  */
37
- export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean): BN;
37
+ export declare function calculateCollateralDepositRequiredForTrade(driftClient: DriftClient, targetMarketIndex: number, baseSize: BN, collateralIndex: number, userMaxMarginRatio?: number, userHighLeverageMode?: boolean, estEntryPrice?: BN): BN;
38
38
  export declare function calculateCollateralValueOfDeposit(driftClient: DriftClient, collateralIndex: number, baseSize: BN): BN;
39
39
  export declare function calculateLiquidationPrice(freeCollateral: BN, freeCollateralDelta: BN, oraclePrice: BN): BN;
@@ -100,20 +100,20 @@ function calculateWorstCasePerpLiabilityValue(perpPosition, perpMarket, oraclePr
100
100
  }
101
101
  }
102
102
  exports.calculateWorstCasePerpLiabilityValue = calculateWorstCasePerpLiabilityValue;
103
- function calculatePerpLiabilityValue(baseAssetAmount, oraclePrice, isPredictionMarket) {
103
+ function calculatePerpLiabilityValue(baseAssetAmount, price, isPredictionMarket) {
104
104
  if (isPredictionMarket) {
105
105
  if (baseAssetAmount.gt(numericConstants_1.ZERO)) {
106
- return baseAssetAmount.mul(oraclePrice).div(numericConstants_1.BASE_PRECISION);
106
+ return baseAssetAmount.mul(price).div(numericConstants_1.BASE_PRECISION);
107
107
  }
108
108
  else {
109
109
  return baseAssetAmount
110
110
  .abs()
111
- .mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(oraclePrice))
111
+ .mul(numericConstants_1.MAX_PREDICTION_PRICE.sub(price))
112
112
  .div(numericConstants_1.BASE_PRECISION);
113
113
  }
114
114
  }
115
115
  else {
116
- return baseAssetAmount.abs().mul(oraclePrice).div(numericConstants_1.BASE_PRECISION);
116
+ return baseAssetAmount.abs().mul(price).div(numericConstants_1.BASE_PRECISION);
117
117
  }
118
118
  }
119
119
  exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
@@ -123,10 +123,10 @@ exports.calculatePerpLiabilityValue = calculatePerpLiabilityValue;
123
123
  * @param baseSize
124
124
  * @returns
125
125
  */
126
- function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode) {
126
+ function calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode, entryPrice) {
127
127
  const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
128
- const oracleData = driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex);
129
- const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, oracleData.price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
128
+ const price = entryPrice !== null && entryPrice !== void 0 ? entryPrice : driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
129
+ const perpLiabilityValue = calculatePerpLiabilityValue(baseSize, price, (0, types_1.isVariant)(targetMarket.contractType, 'prediction'));
130
130
  const marginRequired = new anchor_1.BN((0, __1.calculateMarketMarginRatio)(targetMarket, baseSize.abs(), 'Initial', userMaxMarginRatio, userHighLeverageMode))
131
131
  .mul(perpLiabilityValue)
132
132
  .div(numericConstants_1.MARGIN_PRECISION);
@@ -138,8 +138,8 @@ exports.calculateMarginUSDCRequiredForTrade = calculateMarginUSDCRequiredForTrad
138
138
  *
139
139
  * Returns collateral required in the precision of the target collateral market.
140
140
  */
141
- function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode) {
142
- const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode);
141
+ function calculateCollateralDepositRequiredForTrade(driftClient, targetMarketIndex, baseSize, collateralIndex, userMaxMarginRatio, userHighLeverageMode, estEntryPrice) {
142
+ const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(driftClient, targetMarketIndex, baseSize, userMaxMarginRatio, userHighLeverageMode, estEntryPrice);
143
143
  const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
144
144
  const collateralOracleData = driftClient.getOracleDataForSpotMarket(collateralIndex);
145
145
  const scaledAssetWeight = (0, __1.calculateScaledInitialAssetWeight)(collateralMarket, collateralOracleData.price);
package/lib/user.d.ts CHANGED
@@ -280,7 +280,7 @@ export declare class User {
280
280
  * @returns : Precision PRICE_PRECISION
281
281
  */
282
282
  liquidationPriceAfterClose(positionMarketIndex: number, closeQuoteAmount: BN, estimatedEntryPrice?: BN): BN;
283
- getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN): BN;
283
+ getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN, estEntryPrice?: BN): BN;
284
284
  getCollateralDepositRequiredForTrade(targetMarketIndex: number, baseSize: BN, collateralIndex: number): BN;
285
285
  /**
286
286
  * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
package/lib/user.js CHANGED
@@ -1134,10 +1134,7 @@ class User {
1134
1134
  return (this.getUserAccount().status & types_1.UserStatus.BANKRUPT) > 0;
1135
1135
  }
1136
1136
  isHighLeverageMode() {
1137
- if (this.getUserAccount().marginMode) {
1138
- return (0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverage');
1139
- }
1140
- return false;
1137
+ return (0, types_1.isVariant)(this.getUserAccount().marginMode, 'highLeverage');
1141
1138
  }
1142
1139
  /**
1143
1140
  * Checks if any user position cumulative funding differs from respective market cumulative funding
@@ -1374,8 +1371,8 @@ class User {
1374
1371
  .neg();
1375
1372
  return this.liquidationPrice(positionMarketIndex, closeBaseAmount, estimatedEntryPrice);
1376
1373
  }
1377
- getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize) {
1378
- return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio);
1374
+ getMarginUSDCRequiredForTrade(targetMarketIndex, baseSize, estEntryPrice) {
1375
+ return (0, margin_1.calculateMarginUSDCRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, this.getUserAccount().maxMarginRatio, undefined, estEntryPrice);
1379
1376
  }
1380
1377
  getCollateralDepositRequiredForTrade(targetMarketIndex, baseSize, collateralIndex) {
1381
1378
  return (0, margin_1.calculateCollateralDepositRequiredForTrade)(this.driftClient, targetMarketIndex, baseSize, collateralIndex, this.getUserAccount().maxMarginRatio, false // assume user cant be high leverage if they havent created user account ?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.97.0-beta.15",
3
+ "version": "2.97.0-beta.17",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -11,7 +11,7 @@ import {
11
11
  UserAccount,
12
12
  } from '../types';
13
13
  import { PublicKey } from '@solana/web3.js';
14
- import { BN } from '../';
14
+ import { BN, MarginMode } from '../';
15
15
  import { ZERO } from '../';
16
16
 
17
17
  function readUnsignedBigInt64LE(buffer: Buffer, offset: number): BN {
@@ -326,6 +326,15 @@ export function decodeUser(buffer: Buffer): UserAccount {
326
326
  const hasOpenAuction = buffer.readUInt8(offset) === 1;
327
327
  offset += 1;
328
328
 
329
+ let marginMode: MarginMode;
330
+ const marginModeNum = buffer.readUInt8(offset);
331
+ if (marginModeNum === 0) {
332
+ marginMode = MarginMode.DEFAULT;
333
+ } else {
334
+ marginMode = MarginMode.HIGH_LEVERAGE;
335
+ }
336
+ offset += 1;
337
+
329
338
  // @ts-ignore
330
339
  return {
331
340
  authority,
@@ -354,5 +363,6 @@ export function decodeUser(buffer: Buffer): UserAccount {
354
363
  hasOpenOrder,
355
364
  openAuctions,
356
365
  hasOpenAuction,
366
+ marginMode,
357
367
  };
358
368
  }
@@ -187,20 +187,20 @@ export function calculateWorstCasePerpLiabilityValue(
187
187
 
188
188
  export function calculatePerpLiabilityValue(
189
189
  baseAssetAmount: BN,
190
- oraclePrice: BN,
190
+ price: BN,
191
191
  isPredictionMarket: boolean
192
192
  ): BN {
193
193
  if (isPredictionMarket) {
194
194
  if (baseAssetAmount.gt(ZERO)) {
195
- return baseAssetAmount.mul(oraclePrice).div(BASE_PRECISION);
195
+ return baseAssetAmount.mul(price).div(BASE_PRECISION);
196
196
  } else {
197
197
  return baseAssetAmount
198
198
  .abs()
199
- .mul(MAX_PREDICTION_PRICE.sub(oraclePrice))
199
+ .mul(MAX_PREDICTION_PRICE.sub(price))
200
200
  .div(BASE_PRECISION);
201
201
  }
202
202
  } else {
203
- return baseAssetAmount.abs().mul(oraclePrice).div(BASE_PRECISION);
203
+ return baseAssetAmount.abs().mul(price).div(BASE_PRECISION);
204
204
  }
205
205
  }
206
206
 
@@ -215,16 +215,18 @@ export function calculateMarginUSDCRequiredForTrade(
215
215
  targetMarketIndex: number,
216
216
  baseSize: BN,
217
217
  userMaxMarginRatio?: number,
218
- userHighLeverageMode?: boolean
218
+ userHighLeverageMode?: boolean,
219
+ entryPrice?: BN
219
220
  ): BN {
220
221
  const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
221
- const oracleData = driftClient.getOracleDataForPerpMarket(
222
- targetMarket.marketIndex
223
- );
222
+
223
+ const price =
224
+ entryPrice ??
225
+ driftClient.getOracleDataForPerpMarket(targetMarket.marketIndex).price;
224
226
 
225
227
  const perpLiabilityValue = calculatePerpLiabilityValue(
226
228
  baseSize,
227
- oracleData.price,
229
+ price,
228
230
  isVariant(targetMarket.contractType, 'prediction')
229
231
  );
230
232
 
@@ -254,14 +256,16 @@ export function calculateCollateralDepositRequiredForTrade(
254
256
  baseSize: BN,
255
257
  collateralIndex: number,
256
258
  userMaxMarginRatio?: number,
257
- userHighLeverageMode?: boolean
259
+ userHighLeverageMode?: boolean,
260
+ estEntryPrice?: BN
258
261
  ): BN {
259
262
  const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
260
263
  driftClient,
261
264
  targetMarketIndex,
262
265
  baseSize,
263
266
  userMaxMarginRatio,
264
- userHighLeverageMode
267
+ userHighLeverageMode,
268
+ estEntryPrice
265
269
  );
266
270
 
267
271
  const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
package/src/user.ts CHANGED
@@ -2159,10 +2159,7 @@ export class User {
2159
2159
  }
2160
2160
 
2161
2161
  public isHighLeverageMode(): boolean {
2162
- if (this.getUserAccount().marginMode) {
2163
- return isVariant(this.getUserAccount().marginMode, 'highLeverage');
2164
- }
2165
- return false;
2162
+ return isVariant(this.getUserAccount().marginMode, 'highLeverage');
2166
2163
  }
2167
2164
 
2168
2165
  /**
@@ -2616,13 +2613,16 @@ export class User {
2616
2613
 
2617
2614
  public getMarginUSDCRequiredForTrade(
2618
2615
  targetMarketIndex: number,
2619
- baseSize: BN
2616
+ baseSize: BN,
2617
+ estEntryPrice?: BN
2620
2618
  ): BN {
2621
2619
  return calculateMarginUSDCRequiredForTrade(
2622
2620
  this.driftClient,
2623
2621
  targetMarketIndex,
2624
2622
  baseSize,
2625
- this.getUserAccount().maxMarginRatio
2623
+ this.getUserAccount().maxMarginRatio,
2624
+ undefined,
2625
+ estEntryPrice
2626
2626
  );
2627
2627
  }
2628
2628