@drift-labs/sdk 2.114.0-beta.4 → 2.114.0-beta.5

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.114.0-beta.4
1
+ 2.114.0-beta.5
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN } from '@coral-xyz/anchor';
3
3
  import { OraclePriceData } from '../oracles/types';
4
- import { DriftClient, PerpMarketAccount, PerpPosition } from '..';
4
+ import { DriftClient, PerpMarketAccount, PerpPosition, PositionDirection, PublicKey, UserAccount } from '..';
5
5
  export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
6
6
  imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
7
7
  export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
@@ -37,3 +37,7 @@ export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftCl
37
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;
40
+ export declare function calculateUserMaxPerpOrderSize(driftClient: DriftClient, userAccountKey: PublicKey, userAccount: UserAccount, targetMarketIndex: number, tradeSide: PositionDirection): {
41
+ tradeSize: BN;
42
+ oppositeSideTradeSize: BN;
43
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
3
+ exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const anchor_1 = require("@coral-xyz/anchor");
@@ -182,3 +182,17 @@ function calculateLiquidationPrice(freeCollateral, freeCollateralDelta, oraclePr
182
182
  return liqPrice;
183
183
  }
184
184
  exports.calculateLiquidationPrice = calculateLiquidationPrice;
185
+ function calculateUserMaxPerpOrderSize(driftClient, userAccountKey, userAccount, targetMarketIndex, tradeSide) {
186
+ const userAccountSubscriber = new __1.OneShotUserAccountSubscriber(driftClient.program, userAccountKey, userAccount);
187
+ const user = new __1.User({
188
+ driftClient,
189
+ userAccountPublicKey: userAccountKey,
190
+ accountSubscription: {
191
+ type: 'custom',
192
+ userAccountSubscriber: userAccountSubscriber,
193
+ },
194
+ });
195
+ user.isSubscribed = true;
196
+ return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
197
+ }
198
+ exports.calculateUserMaxPerpOrderSize = calculateUserMaxPerpOrderSize;
@@ -284,22 +284,9 @@ export declare class User {
284
284
  getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN, estEntryPrice?: BN): BN;
285
285
  getCollateralDepositRequiredForTrade(targetMarketIndex: number, baseSize: BN, collateralIndex: number): BN;
286
286
  /**
287
- * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
288
- *
289
- * To Calculate Max Quote Available:
290
- *
291
- * Case 1: SameSide
292
- * => Remaining quote to get to maxLeverage
293
- *
294
- * Case 2: NOT SameSide && currentLeverage <= maxLeverage
295
- * => Current opposite position x2 + remaining to get to maxLeverage
296
- *
297
- * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
298
- * => strictly reduce current position size
299
- *
300
- * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
301
- * => current position + remaining to get to maxLeverage
302
- *
287
+ * Separates the max trade size into two parts:
288
+ * - tradeSize: The maximum trade size for target direction
289
+ * - oppositeSideTradeSize: the trade size for closing the opposite direction
303
290
  * @param targetMarketIndex
304
291
  * @param tradeSide
305
292
  * @param isLp
@@ -1390,22 +1390,9 @@ class User {
1390
1390
  );
1391
1391
  }
1392
1392
  /**
1393
- * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
1394
- *
1395
- * To Calculate Max Quote Available:
1396
- *
1397
- * Case 1: SameSide
1398
- * => Remaining quote to get to maxLeverage
1399
- *
1400
- * Case 2: NOT SameSide && currentLeverage <= maxLeverage
1401
- * => Current opposite position x2 + remaining to get to maxLeverage
1402
- *
1403
- * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
1404
- * => strictly reduce current position size
1405
- *
1406
- * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
1407
- * => current position + remaining to get to maxLeverage
1408
- *
1393
+ * Separates the max trade size into two parts:
1394
+ * - tradeSize: The maximum trade size for target direction
1395
+ * - oppositeSideTradeSize: the trade size for closing the opposite direction
1409
1396
  * @param targetMarketIndex
1410
1397
  * @param tradeSide
1411
1398
  * @param isLp
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN } from '@coral-xyz/anchor';
3
3
  import { OraclePriceData } from '../oracles/types';
4
- import { DriftClient, PerpMarketAccount, PerpPosition } from '..';
4
+ import { DriftClient, PerpMarketAccount, PerpPosition, PositionDirection, PublicKey, UserAccount } from '..';
5
5
  export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
6
6
  imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
7
7
  export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
@@ -37,3 +37,7 @@ export declare function calculateMarginUSDCRequiredForTrade(driftClient: DriftCl
37
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;
40
+ export declare function calculateUserMaxPerpOrderSize(driftClient: DriftClient, userAccountKey: PublicKey, userAccount: UserAccount, targetMarketIndex: number, tradeSide: PositionDirection): {
41
+ tradeSize: BN;
42
+ oppositeSideTradeSize: BN;
43
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
3
+ exports.calculateUserMaxPerpOrderSize = exports.calculateLiquidationPrice = exports.calculateCollateralValueOfDeposit = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const anchor_1 = require("@coral-xyz/anchor");
@@ -182,3 +182,17 @@ function calculateLiquidationPrice(freeCollateral, freeCollateralDelta, oraclePr
182
182
  return liqPrice;
183
183
  }
184
184
  exports.calculateLiquidationPrice = calculateLiquidationPrice;
185
+ function calculateUserMaxPerpOrderSize(driftClient, userAccountKey, userAccount, targetMarketIndex, tradeSide) {
186
+ const userAccountSubscriber = new __1.OneShotUserAccountSubscriber(driftClient.program, userAccountKey, userAccount);
187
+ const user = new __1.User({
188
+ driftClient,
189
+ userAccountPublicKey: userAccountKey,
190
+ accountSubscription: {
191
+ type: 'custom',
192
+ userAccountSubscriber: userAccountSubscriber,
193
+ },
194
+ });
195
+ user.isSubscribed = true;
196
+ return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
197
+ }
198
+ exports.calculateUserMaxPerpOrderSize = calculateUserMaxPerpOrderSize;
@@ -284,22 +284,9 @@ export declare class User {
284
284
  getMarginUSDCRequiredForTrade(targetMarketIndex: number, baseSize: BN, estEntryPrice?: BN): BN;
285
285
  getCollateralDepositRequiredForTrade(targetMarketIndex: number, baseSize: BN, collateralIndex: number): BN;
286
286
  /**
287
- * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
288
- *
289
- * To Calculate Max Quote Available:
290
- *
291
- * Case 1: SameSide
292
- * => Remaining quote to get to maxLeverage
293
- *
294
- * Case 2: NOT SameSide && currentLeverage <= maxLeverage
295
- * => Current opposite position x2 + remaining to get to maxLeverage
296
- *
297
- * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
298
- * => strictly reduce current position size
299
- *
300
- * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
301
- * => current position + remaining to get to maxLeverage
302
- *
287
+ * Separates the max trade size into two parts:
288
+ * - tradeSize: The maximum trade size for target direction
289
+ * - oppositeSideTradeSize: the trade size for closing the opposite direction
303
290
  * @param targetMarketIndex
304
291
  * @param tradeSide
305
292
  * @param isLp
package/lib/node/user.js CHANGED
@@ -1390,22 +1390,9 @@ class User {
1390
1390
  );
1391
1391
  }
1392
1392
  /**
1393
- * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
1394
- *
1395
- * To Calculate Max Quote Available:
1396
- *
1397
- * Case 1: SameSide
1398
- * => Remaining quote to get to maxLeverage
1399
- *
1400
- * Case 2: NOT SameSide && currentLeverage <= maxLeverage
1401
- * => Current opposite position x2 + remaining to get to maxLeverage
1402
- *
1403
- * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
1404
- * => strictly reduce current position size
1405
- *
1406
- * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
1407
- * => current position + remaining to get to maxLeverage
1408
- *
1393
+ * Separates the max trade size into two parts:
1394
+ * - tradeSize: The maximum trade size for target direction
1395
+ * - oppositeSideTradeSize: the trade size for closing the opposite direction
1409
1396
  * @param targetMarketIndex
1410
1397
  * @param tradeSide
1411
1398
  * @param isLp
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.114.0-beta.4",
3
+ "version": "2.114.0-beta.5",
4
4
  "main": "lib/node/index.js",
5
5
  "types": "lib/node/index.d.ts",
6
6
  "browser": "./lib/browser/index.js",
@@ -17,8 +17,13 @@ import {
17
17
  calculateMarketMarginRatio,
18
18
  calculateScaledInitialAssetWeight,
19
19
  DriftClient,
20
+ OneShotUserAccountSubscriber,
20
21
  PerpMarketAccount,
21
22
  PerpPosition,
23
+ PositionDirection,
24
+ PublicKey,
25
+ User,
26
+ UserAccount,
22
27
  } from '..';
23
28
  import { isVariant } from '../types';
24
29
  import { assert } from '../assert/assert';
@@ -338,3 +343,30 @@ export function calculateLiquidationPrice(
338
343
 
339
344
  return liqPrice;
340
345
  }
346
+
347
+ export function calculateUserMaxPerpOrderSize(
348
+ driftClient: DriftClient,
349
+ userAccountKey: PublicKey,
350
+ userAccount: UserAccount,
351
+ targetMarketIndex: number,
352
+ tradeSide: PositionDirection
353
+ ): { tradeSize: BN; oppositeSideTradeSize: BN } {
354
+ const userAccountSubscriber = new OneShotUserAccountSubscriber(
355
+ driftClient.program,
356
+ userAccountKey,
357
+ userAccount
358
+ );
359
+
360
+ const user = new User({
361
+ driftClient,
362
+ userAccountPublicKey: userAccountKey,
363
+ accountSubscription: {
364
+ type: 'custom',
365
+ userAccountSubscriber: userAccountSubscriber,
366
+ },
367
+ });
368
+
369
+ user.isSubscribed = true;
370
+
371
+ return user.getMaxTradeSizeUSDCForPerp(targetMarketIndex, tradeSide);
372
+ }
package/src/user.ts CHANGED
@@ -2671,22 +2671,9 @@ export class User {
2671
2671
  }
2672
2672
 
2673
2673
  /**
2674
- * Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
2675
- *
2676
- * To Calculate Max Quote Available:
2677
- *
2678
- * Case 1: SameSide
2679
- * => Remaining quote to get to maxLeverage
2680
- *
2681
- * Case 2: NOT SameSide && currentLeverage <= maxLeverage
2682
- * => Current opposite position x2 + remaining to get to maxLeverage
2683
- *
2684
- * Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
2685
- * => strictly reduce current position size
2686
- *
2687
- * Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
2688
- * => current position + remaining to get to maxLeverage
2689
- *
2674
+ * Separates the max trade size into two parts:
2675
+ * - tradeSize: The maximum trade size for target direction
2676
+ * - oppositeSideTradeSize: the trade size for closing the opposite direction
2690
2677
  * @param targetMarketIndex
2691
2678
  * @param tradeSide
2692
2679
  * @param isLp