@drift-labs/common 1.0.20 → 1.0.21

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.
@@ -1,6 +1,6 @@
1
1
  import { BN, DriftClient, User, PositionDirection } from '@drift-labs/sdk';
2
2
  import { PublicKey, TransactionInstruction } from '@solana/web3.js';
3
- export declare const ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 300;
3
+ export declare const ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 20;
4
4
  export interface ComputeIsolatedPositionDepositParams {
5
5
  driftClient: DriftClient;
6
6
  user: User;
@@ -25,10 +25,14 @@ export interface ComputeIsolatedPositionDepositParams {
25
25
  * If greater than 0, we will consider the trade as entering high leverage mode.
26
26
  */
27
27
  numOfOpenHighLeverageSpots?: number;
28
+ /**
29
+ * Whether to use the buffer for the isolated position deposit.
30
+ */
31
+ bufferBpsDenominator?: number;
28
32
  }
29
33
  /**
30
34
  * Computes the isolated position deposit required for opening an isolated perp position.
31
35
  * Returns a BN in QUOTE_PRECISION (USDC).
32
36
  */
33
- export declare function computeIsolatedPositionDepositForTrade({ driftClient, user, marketIndex, baseAssetAmount, direction, marginRatio, entryPrice, numOfOpenHighLeverageSpots, }: ComputeIsolatedPositionDepositParams): BN | null;
37
+ export declare function computeIsolatedPositionDepositForTrade({ driftClient, user, marketIndex, baseAssetAmount, direction, marginRatio, entryPrice, numOfOpenHighLeverageSpots, bufferBpsDenominator, }: ComputeIsolatedPositionDepositParams): BN | null;
34
38
  export declare function getIsolatedPositionDepositIxIfNeeded(driftClient: DriftClient, user: User, marketIndex: number, isolatedPositionDeposit?: BN, signingAuthority?: PublicKey): Promise<TransactionInstruction | undefined>;
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getIsolatedPositionDepositIxIfNeeded = exports.computeIsolatedPositionDepositForTrade = exports.ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = void 0;
4
4
  const sdk_1 = require("@drift-labs/sdk");
5
- exports.ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 300;
5
+ exports.ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 20;
6
6
  /**
7
7
  * Computes the isolated position deposit required for opening an isolated perp position.
8
8
  * Returns a BN in QUOTE_PRECISION (USDC).
9
9
  */
10
- function computeIsolatedPositionDepositForTrade({ driftClient, user, marketIndex, baseAssetAmount, direction, marginRatio, entryPrice, numOfOpenHighLeverageSpots, }) {
10
+ function computeIsolatedPositionDepositForTrade({ driftClient, user, marketIndex, baseAssetAmount, direction, marginRatio, entryPrice, numOfOpenHighLeverageSpots, bufferBpsDenominator, }) {
11
11
  var _a;
12
12
  // Only require isolated deposit if the order will increase the position (when direction is provided)
13
13
  if (direction !== undefined) {
@@ -28,7 +28,7 @@ function computeIsolatedPositionDepositForTrade({ driftClient, user, marketIndex
28
28
  const hasOpenHighLeverageSpots = numOfOpenHighLeverageSpots !== undefined && numOfOpenHighLeverageSpots > 0;
29
29
  const enteringHighLeverageMode = userIsInHighLeverageMode || hasOpenHighLeverageSpots;
30
30
  const marginRequired = (0, sdk_1.calculateMarginUSDCRequiredForTrade)(driftClient, marketIndex, baseAssetAmount, marginRatio, enteringHighLeverageMode, entryPrice);
31
- return marginRequired.add(marginRequired.div(new sdk_1.BN(exports.ISOLATED_POSITION_DEPOSIT_BUFFER_BPS))); // buffer in basis points
31
+ return marginRequired.add(marginRequired.div(new sdk_1.BN(bufferBpsDenominator !== null && bufferBpsDenominator !== void 0 ? bufferBpsDenominator : exports.ISOLATED_POSITION_DEPOSIT_BUFFER_BPS))); // buffer in basis points
32
32
  }
33
33
  exports.computeIsolatedPositionDepositForTrade = computeIsolatedPositionDepositForTrade;
34
34
  async function getIsolatedPositionDepositIxIfNeeded(driftClient, user, marketIndex, isolatedPositionDeposit, signingAuthority) {
@@ -1 +1 @@
1
- {"version":3,"file":"isolatedPositionDeposit.js","sourceRoot":"","sources":["../../../../../../src/drift/base/actions/trade/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,WAAmB,EACnB,uBAA4B,EAC5B,gBAA4B;IAE5B,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC9B,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,IAAI,uBAAuB,CAAC,MAAM,EAAE,EAAE,CAAC;QACtC,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,OAAO,WAAW,CAAC,wCAAwC,CAC1D,uBAAuB,EACvB,WAAW,EACX,IAAI,CAAC,cAAc,EAAE,CAAC,YAAY,EAClC,SAAS,EAAE,iBAAiB;IAC5B,gBAAgB,CAChB,CAAC;AACH,CAAC;AArBD,oFAqBC","sourcesContent":["import {\n\tBN,\n\tDriftClient,\n\tUser,\n\tcalculateMarginUSDCRequiredForTrade,\n\tOptionalOrderParams,\n\tPositionDirection,\n\tMarketType,\n\tOrderType,\n} from '@drift-labs/sdk';\nimport { PublicKey, TransactionInstruction } from '@solana/web3.js';\n\nexport const ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 300;\n\nexport interface ComputeIsolatedPositionDepositParams {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tmarketIndex: number;\n\tbaseAssetAmount: BN;\n\t/**\n\t * Optional direction of the order.\n\t * If provided, we will check if the order will increase the position.\n\t * If the order will not increase the position, we will return 0.\n\t */\n\tdirection?: PositionDirection;\n\t/**\n\t * Margin ratio to use for the position (e.g. 2000 for 5x leverage).\n\t */\n\tmarginRatio: number;\n\t/**\n\t * Optional estimated entry price to use for the margin calculation.\n\t */\n\tentryPrice?: BN;\n\t/**\n\t * Number of open high leverage spots available to the user (if any).\n\t * If greater than 0, we will consider the trade as entering high leverage mode.\n\t */\n\tnumOfOpenHighLeverageSpots?: number;\n}\n\n/**\n * Computes the isolated position deposit required for opening an isolated perp position.\n * Returns a BN in QUOTE_PRECISION (USDC).\n */\nexport function computeIsolatedPositionDepositForTrade({\n\tdriftClient,\n\tuser,\n\tmarketIndex,\n\tbaseAssetAmount,\n\tdirection,\n\tmarginRatio,\n\tentryPrice,\n\tnumOfOpenHighLeverageSpots,\n}: ComputeIsolatedPositionDepositParams): BN | null {\n\t// Only require isolated deposit if the order will increase the position (when direction is provided)\n\tif (direction !== undefined) {\n\t\tconst maybeOrderParams: OptionalOrderParams = {\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\torderType: OrderType.MARKET,\n\t\t\tdirection,\n\t\t\tbaseAssetAmount,\n\t\t};\n\t\tconst subAccountId = user.getUserAccount().subAccountId;\n\t\tconst isIncreasing = driftClient.isOrderIncreasingPosition(\n\t\t\tmaybeOrderParams,\n\t\t\tsubAccountId\n\t\t);\n\t\tif (!isIncreasing) {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tconst userIsInHighLeverageMode = user.isHighLeverageMode('Initial') ?? false;\n\tconst hasOpenHighLeverageSpots =\n\t\tnumOfOpenHighLeverageSpots !== undefined && numOfOpenHighLeverageSpots > 0;\n\tconst enteringHighLeverageMode =\n\t\tuserIsInHighLeverageMode || hasOpenHighLeverageSpots;\n\n\tconst marginRequired = calculateMarginUSDCRequiredForTrade(\n\t\tdriftClient,\n\t\tmarketIndex,\n\t\tbaseAssetAmount,\n\t\tmarginRatio,\n\t\tenteringHighLeverageMode,\n\t\tentryPrice\n\t);\n\n\treturn marginRequired.add(\n\t\tmarginRequired.div(new BN(ISOLATED_POSITION_DEPOSIT_BUFFER_BPS))\n\t); // buffer in basis points\n}\n\nexport async function getIsolatedPositionDepositIxIfNeeded(\n\tdriftClient: DriftClient,\n\tuser: User,\n\tmarketIndex: number,\n\tisolatedPositionDeposit?: BN,\n\tsigningAuthority?: PublicKey\n): Promise<TransactionInstruction | undefined> {\n\tif (!isolatedPositionDeposit) {\n\t\treturn undefined;\n\t}\n\tif (isolatedPositionDeposit.isZero()) {\n\t\treturn undefined;\n\t}\n\n\treturn driftClient.getTransferIsolatedPerpPositionDepositIx(\n\t\tisolatedPositionDeposit,\n\t\tmarketIndex,\n\t\tuser.getUserAccount().subAccountId,\n\t\tundefined, // noAmountBuffer\n\t\tsigningAuthority\n\t);\n}\n"]}
1
+ {"version":3,"file":"isolatedPositionDeposit.js","sourceRoot":"","sources":["../../../../../../src/drift/base/actions/trade/openPerpOrder/isolatedPositionDeposit.ts"],"names":[],"mappings":";;;AAAA,yCASyB;AAGZ,QAAA,oCAAoC,GAAG,EAAE,CAAC;AAgCvD;;;GAGG;AACH,SAAgB,sCAAsC,CAAC,EACtD,WAAW,EACX,IAAI,EACJ,WAAW,EACX,eAAe,EACf,SAAS,EACT,WAAW,EACX,UAAU,EACV,0BAA0B,EAC1B,oBAAoB,GACkB;;IACtC,qGAAqG;IACrG,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,gBAAgB,GAAwB;YAC7C,WAAW;YACX,UAAU,EAAE,gBAAU,CAAC,IAAI;YAC3B,SAAS,EAAE,eAAS,CAAC,MAAM;YAC3B,SAAS;YACT,eAAe;SACf,CAAC;QACF,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC,YAAY,CAAC;QACxD,MAAM,YAAY,GAAG,WAAW,CAAC,yBAAyB,CACzD,gBAAgB,EAChB,YAAY,CACZ,CAAC;QACF,IAAI,CAAC,YAAY,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,MAAM,wBAAwB,GAAG,MAAA,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,mCAAI,KAAK,CAAC;IAC7E,MAAM,wBAAwB,GAC7B,0BAA0B,KAAK,SAAS,IAAI,0BAA0B,GAAG,CAAC,CAAC;IAC5E,MAAM,wBAAwB,GAC7B,wBAAwB,IAAI,wBAAwB,CAAC;IAEtD,MAAM,cAAc,GAAG,IAAA,yCAAmC,EACzD,WAAW,EACX,WAAW,EACX,eAAe,EACf,WAAW,EACX,wBAAwB,EACxB,UAAU,CACV,CAAC;IAEF,OAAO,cAAc,CAAC,GAAG,CACxB,cAAc,CAAC,GAAG,CACjB,IAAI,QAAE,CAAC,oBAAoB,aAApB,oBAAoB,cAApB,oBAAoB,GAAI,4CAAoC,CAAC,CACpE,CACD,CAAC,CAAC,yBAAyB;AAC7B,CAAC;AAlDD,wFAkDC;AAEM,KAAK,UAAU,oCAAoC,CACzD,WAAwB,EACxB,IAAU,EACV,WAAmB,EACnB,uBAA4B,EAC5B,gBAA4B;IAE5B,IAAI,CAAC,uBAAuB,EAAE,CAAC;QAC9B,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,IAAI,uBAAuB,CAAC,MAAM,EAAE,EAAE,CAAC;QACtC,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,OAAO,WAAW,CAAC,wCAAwC,CAC1D,uBAAuB,EACvB,WAAW,EACX,IAAI,CAAC,cAAc,EAAE,CAAC,YAAY,EAClC,SAAS,EAAE,iBAAiB;IAC5B,gBAAgB,CAChB,CAAC;AACH,CAAC;AArBD,oFAqBC","sourcesContent":["import {\n\tBN,\n\tDriftClient,\n\tUser,\n\tcalculateMarginUSDCRequiredForTrade,\n\tOptionalOrderParams,\n\tPositionDirection,\n\tMarketType,\n\tOrderType,\n} from '@drift-labs/sdk';\nimport { PublicKey, TransactionInstruction } from '@solana/web3.js';\n\nexport const ISOLATED_POSITION_DEPOSIT_BUFFER_BPS = 20;\n\nexport interface ComputeIsolatedPositionDepositParams {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tmarketIndex: number;\n\tbaseAssetAmount: BN;\n\t/**\n\t * Optional direction of the order.\n\t * If provided, we will check if the order will increase the position.\n\t * If the order will not increase the position, we will return 0.\n\t */\n\tdirection?: PositionDirection;\n\t/**\n\t * Margin ratio to use for the position (e.g. 2000 for 5x leverage).\n\t */\n\tmarginRatio: number;\n\t/**\n\t * Optional estimated entry price to use for the margin calculation.\n\t */\n\tentryPrice?: BN;\n\t/**\n\t * Number of open high leverage spots available to the user (if any).\n\t * If greater than 0, we will consider the trade as entering high leverage mode.\n\t */\n\tnumOfOpenHighLeverageSpots?: number;\n\t/**\n\t * Whether to use the buffer for the isolated position deposit.\n\t */\n\tbufferBpsDenominator?: number;\n}\n\n/**\n * Computes the isolated position deposit required for opening an isolated perp position.\n * Returns a BN in QUOTE_PRECISION (USDC).\n */\nexport function computeIsolatedPositionDepositForTrade({\n\tdriftClient,\n\tuser,\n\tmarketIndex,\n\tbaseAssetAmount,\n\tdirection,\n\tmarginRatio,\n\tentryPrice,\n\tnumOfOpenHighLeverageSpots,\n\tbufferBpsDenominator,\n}: ComputeIsolatedPositionDepositParams): BN | null {\n\t// Only require isolated deposit if the order will increase the position (when direction is provided)\n\tif (direction !== undefined) {\n\t\tconst maybeOrderParams: OptionalOrderParams = {\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\torderType: OrderType.MARKET,\n\t\t\tdirection,\n\t\t\tbaseAssetAmount,\n\t\t};\n\t\tconst subAccountId = user.getUserAccount().subAccountId;\n\t\tconst isIncreasing = driftClient.isOrderIncreasingPosition(\n\t\t\tmaybeOrderParams,\n\t\t\tsubAccountId\n\t\t);\n\t\tif (!isIncreasing) {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tconst userIsInHighLeverageMode = user.isHighLeverageMode('Initial') ?? false;\n\tconst hasOpenHighLeverageSpots =\n\t\tnumOfOpenHighLeverageSpots !== undefined && numOfOpenHighLeverageSpots > 0;\n\tconst enteringHighLeverageMode =\n\t\tuserIsInHighLeverageMode || hasOpenHighLeverageSpots;\n\n\tconst marginRequired = calculateMarginUSDCRequiredForTrade(\n\t\tdriftClient,\n\t\tmarketIndex,\n\t\tbaseAssetAmount,\n\t\tmarginRatio,\n\t\tenteringHighLeverageMode,\n\t\tentryPrice\n\t);\n\n\treturn marginRequired.add(\n\t\tmarginRequired.div(\n\t\t\tnew BN(bufferBpsDenominator ?? ISOLATED_POSITION_DEPOSIT_BUFFER_BPS)\n\t\t)\n\t); // buffer in basis points\n}\n\nexport async function getIsolatedPositionDepositIxIfNeeded(\n\tdriftClient: DriftClient,\n\tuser: User,\n\tmarketIndex: number,\n\tisolatedPositionDeposit?: BN,\n\tsigningAuthority?: PublicKey\n): Promise<TransactionInstruction | undefined> {\n\tif (!isolatedPositionDeposit) {\n\t\treturn undefined;\n\t}\n\tif (isolatedPositionDeposit.isZero()) {\n\t\treturn undefined;\n\t}\n\n\treturn driftClient.getTransferIsolatedPerpPositionDepositIx(\n\t\tisolatedPositionDeposit,\n\t\tmarketIndex,\n\t\tuser.getUserAccount().subAccountId,\n\t\tundefined, // noAmountBuffer\n\t\tsigningAuthority\n\t);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/common",
3
- "version": "1.0.20",
3
+ "version": "1.0.21",
4
4
  "description": "Common functions for Drift",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",