@metamask-previews/perps-controller 10.0.0-preview-a42e8d0d2 → 10.0.0-preview-d2f661012

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/providers/HyperLiquidProvider.cjs +219 -30
  3. package/dist/providers/HyperLiquidProvider.cjs.map +1 -1
  4. package/dist/providers/HyperLiquidProvider.d.cts.map +1 -1
  5. package/dist/providers/HyperLiquidProvider.d.mts.map +1 -1
  6. package/dist/providers/HyperLiquidProvider.mjs +220 -31
  7. package/dist/providers/HyperLiquidProvider.mjs.map +1 -1
  8. package/dist/services/HyperLiquidSubscriptionService.cjs +23 -0
  9. package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
  10. package/dist/services/HyperLiquidSubscriptionService.d.cts +21 -0
  11. package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
  12. package/dist/services/HyperLiquidSubscriptionService.d.mts +21 -0
  13. package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
  14. package/dist/services/HyperLiquidSubscriptionService.mjs +23 -0
  15. package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
  16. package/dist/types/index.cjs.map +1 -1
  17. package/dist/types/index.d.cts +18 -2
  18. package/dist/types/index.d.cts.map +1 -1
  19. package/dist/types/index.d.mts +18 -2
  20. package/dist/types/index.d.mts.map +1 -1
  21. package/dist/types/index.mjs.map +1 -1
  22. package/dist/utils/orderCalculations.cjs +126 -21
  23. package/dist/utils/orderCalculations.cjs.map +1 -1
  24. package/dist/utils/orderCalculations.d.cts +24 -0
  25. package/dist/utils/orderCalculations.d.cts.map +1 -1
  26. package/dist/utils/orderCalculations.d.mts +24 -0
  27. package/dist/utils/orderCalculations.d.mts.map +1 -1
  28. package/dist/utils/orderCalculations.mjs +124 -20
  29. package/dist/utils/orderCalculations.mjs.map +1 -1
  30. package/package.json +5 -5
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildOrdersArray = exports.calculateOrderPriceAndSize = exports.calculateFinalPositionSize = exports.getMaxAllowedAmount = exports.calculateMarginRequired = exports.calculatePositionSize = void 0;
3
+ exports.buildOrdersArray = exports.calculateOrderPriceAndSize = exports.calculateFinalPositionSize = exports.floorToSizeDecimals = exports.getMaxAllowedAmount = exports.calculateMarginRequired = exports.calculatePositionSize = void 0;
4
4
  const hyperLiquidConfig_js_1 = require("../constants/hyperLiquidConfig.cjs");
5
5
  const perpsConfig_js_1 = require("../constants/perpsConfig.cjs");
6
6
  const perpsErrorCodes_js_1 = require("../perpsErrorCodes.cjs");
7
7
  const hyperLiquidAdapter_js_1 = require("./hyperLiquidAdapter.cjs");
8
+ /**
9
+ * Tolerance used when deciding whether a scaled size is already on the size
10
+ * grid, guarding against floating-point representation error.
11
+ */
12
+ const FLOAT_TOLERANCE = 1e-6;
8
13
  /**
9
14
  * Calculate position size based on USD amount and asset price
10
15
  *
@@ -83,6 +88,60 @@ function getMaxAllowedAmount(params) {
83
88
  return Math.max(0, Math.floor(bufferedMax));
84
89
  }
85
90
  exports.getMaxAllowedAmount = getMaxAllowedAmount;
91
+ /**
92
+ * Round a size down onto the asset's size grid.
93
+ *
94
+ * Used for reduce-only orders, where rounding up would push the size past the
95
+ * live position size. Values already on the grid are snapped rather than
96
+ * truncated, because floating-point math can leave them just below a grid
97
+ * point (0.0123 * 10000 === 122.99999999999999) and truncating would drop a
98
+ * whole increment.
99
+ *
100
+ * The result is never greater than `size`, for negative sizes as well as
101
+ * positive: the snap only ever recovers a grid point the input already
102
+ * represents, so a value genuinely below a grid point is stepped down even when
103
+ * the tolerance would have reached the point above it.
104
+ *
105
+ * A size whose scaled form reaches `2^53` is returned unchanged: doubles cannot
106
+ * represent consecutive integers there, so the grid is finer than the spacing
107
+ * between representable values and there is nothing to round down to.
108
+ *
109
+ * @param size - Size to round down.
110
+ * @param szDecimals - The asset's size decimal precision.
111
+ * @returns The size rounded down onto the size grid, never exceeding `size`.
112
+ */
113
+ function floorToSizeDecimals(size, szDecimals) {
114
+ const multiplier = Math.pow(10, szDecimals);
115
+ const scaled = size * multiplier;
116
+ // Past 2^53 a double cannot represent consecutive integers, so `units -= 1`
117
+ // below would be a no-op and the step-down loop would never terminate. The
118
+ // size grid is finer than the spacing between representable values at that
119
+ // magnitude, so there is no increment to shave: return the input unchanged.
120
+ if (!Number.isFinite(scaled) || Math.abs(scaled) >= Number.MAX_SAFE_INTEGER) {
121
+ return size;
122
+ }
123
+ const nearest = Math.round(scaled);
124
+ // The tolerance scales with the magnitude, because double-precision error
125
+ // does too: a fixed epsilon would stop absorbing representation error for
126
+ // sizes that scale past ~1e10 and would then shave off a whole increment.
127
+ const tolerance = Math.max(FLOAT_TOLERANCE, Math.abs(scaled) * Number.EPSILON * 8);
128
+ let units = Math.abs(scaled - nearest) < tolerance ? nearest : Math.floor(scaled);
129
+ // Step down until the result no longer exceeds the input. One pass is not
130
+ // enough: a tolerance wide enough to absorb representation error at large
131
+ // magnitudes also reaches the next grid point, and for an input less than half
132
+ // an ulp below a grid point `size * multiplier` evaluates to exactly that grid
133
+ // integer, so flooring the scaled value returns the same too-large result.
134
+ // The comparison alone is the whole termination condition: for a non-negative
135
+ // size the loop stops at or before zero, and for a negative size it stops once
136
+ // the value is no longer above the input. Guarding on `units` instead would
137
+ // skip a negative size below the tolerance, which snaps to `-0` — and
138
+ // `-0 !== 0` is false. The 2^53 bail-out above keeps this bounded.
139
+ while (units / multiplier > size) {
140
+ units -= 1;
141
+ }
142
+ return units / multiplier;
143
+ }
144
+ exports.floorToSizeDecimals = floorToSizeDecimals;
86
145
  /**
87
146
  * Calculates final position size using USD as source of truth with price validation
88
147
  *
@@ -93,34 +152,63 @@ exports.getMaxAllowedAmount = getMaxAllowedAmount;
93
152
  * @returns Final position size as a number
94
153
  */
95
154
  function calculateFinalPositionSize(params) {
96
- const { usdAmount, size, currentPrice, priceAtCalculation, maxSlippageBps, szDecimals, leverage, debugLogger, } = params;
155
+ const { usdAmount, size, currentPrice, priceAtCalculation, maxSlippageBps, szDecimals, leverage, reduceOnly, debugLogger, } = params;
97
156
  let finalPositionSize;
157
+ // Validate price staleness whenever the caller supplied a calculation-time
158
+ // price. This runs before the sizing branches on purpose: a full close submits
159
+ // the exact live position size rather than a USD-derived one, and it must still
160
+ // be rejected when the price has moved past the caller's tolerance.
161
+ if (priceAtCalculation) {
162
+ const priceDeltaBps = Math.abs(((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000);
163
+ const maxSlippageBpsValue = maxSlippageBps ?? perpsConfig_js_1.ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;
164
+ if (priceDeltaBps > maxSlippageBpsValue) {
165
+ throw new Error(`Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +
166
+ `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`);
167
+ }
168
+ debugLogger?.log('Price validation passed:', {
169
+ priceAtCalculation,
170
+ currentPrice,
171
+ deltaBps: priceDeltaBps.toFixed(2),
172
+ maxSlippageBps: maxSlippageBpsValue,
173
+ });
174
+ }
98
175
  if (usdAmount && parseFloat(usdAmount) > 0) {
99
176
  // USD amount provided - use it as source of truth
100
177
  const usdValue = parseFloat(usdAmount);
101
- // 1. Validate price staleness if priceAtCalculation provided
102
- if (priceAtCalculation) {
103
- const priceDeltaBps = Math.abs(((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000);
104
- const maxSlippageBpsValue = maxSlippageBps ?? perpsConfig_js_1.ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;
105
- if (priceDeltaBps > maxSlippageBpsValue) {
106
- throw new Error(`Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +
107
- `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`);
178
+ // Recalculate position size with fresh price
179
+ finalPositionSize = usdValue / currentPrice;
180
+ // A reduce-only order may never exceed the size the caller asked to close:
181
+ // that size is already clamped to the live position, while the USD amount was
182
+ // computed against an older price and can imply a larger size after an
183
+ // adverse move. Capping here keeps USD accuracy in the common case and makes
184
+ // the caller's clamp binding.
185
+ if (reduceOnly && size) {
186
+ const requestedSize = parseFloat(size);
187
+ // A supplied size must be positive, or the cap below would submit a
188
+ // zero/negative order. Reject it rather than silently falling back to the
189
+ // USD-derived size, matching how closePosition treats the same input.
190
+ if (!Number.isFinite(requestedSize) || requestedSize <= 0) {
191
+ throw new Error(perpsErrorCodes_js_1.PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
108
192
  }
109
- debugLogger?.log('Price validation passed:', {
110
- priceAtCalculation,
111
- currentPrice,
112
- deltaBps: priceDeltaBps.toFixed(2),
113
- maxSlippageBps: maxSlippageBpsValue,
114
- });
193
+ finalPositionSize = Math.min(finalPositionSize, requestedSize);
115
194
  }
116
- // 2. Recalculate position size with fresh price
117
- finalPositionSize = usdValue / currentPrice;
118
- // 3. Apply size decimals rounding
195
+ // 3. Apply size decimals rounding (reduce-only never rounds up)
119
196
  const multiplier = Math.pow(10, szDecimals);
120
- finalPositionSize = Math.round(finalPositionSize * multiplier) / multiplier;
121
- // 4. Ensure rounded size meets requested USD (fix validation gap)
197
+ const sizeBeforeRounding = finalPositionSize;
198
+ finalPositionSize = reduceOnly
199
+ ? floorToSizeDecimals(finalPositionSize, szDecimals)
200
+ : Math.round(finalPositionSize * multiplier) / multiplier;
201
+ // Rounding down can zero out a reduce-only order whose USD value is worth
202
+ // less than one size increment. Fail with a clear error instead of
203
+ // submitting a size of "0" the exchange will reject.
204
+ if (reduceOnly && finalPositionSize <= 0 && sizeBeforeRounding > 0) {
205
+ throw new Error(perpsErrorCodes_js_1.PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
206
+ }
207
+ // 4. Ensure rounded size meets requested USD (fix validation gap).
208
+ // Skipped for reduce-only orders: adding an increment there would submit
209
+ // more than the position holds and HyperLiquid rejects the order.
122
210
  let actualNotionalValue = finalPositionSize * currentPrice;
123
- if (actualNotionalValue < usdValue) {
211
+ if (!reduceOnly && actualNotionalValue < usdValue) {
124
212
  // Add 1 minimum increment to meet requested USD
125
213
  finalPositionSize += 1 / multiplier;
126
214
  actualNotionalValue = finalPositionSize * currentPrice;
@@ -155,6 +243,23 @@ function calculateFinalPositionSize(params) {
155
243
  else {
156
244
  // Legacy: Use provided size (backward compatibility)
157
245
  finalPositionSize = parseFloat(size ?? '0');
246
+ // Reduce-only sizes are formatted with toFixed() further down, which rounds
247
+ // up; truncate onto the size grid first so a close can never exceed the
248
+ // position it is closing.
249
+ if (reduceOnly) {
250
+ // A supplied size must be positive, or formatHyperLiquidSize would render
251
+ // a zero or negative order size. The USD branch above rejects the same
252
+ // input.
253
+ if (size && !(finalPositionSize > 0)) {
254
+ throw new Error(perpsErrorCodes_js_1.PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
255
+ }
256
+ const sizeBeforeFlooring = finalPositionSize;
257
+ finalPositionSize = floorToSizeDecimals(finalPositionSize, szDecimals);
258
+ // A positive size that floors to zero is worth less than one increment
259
+ if (finalPositionSize <= 0 && sizeBeforeFlooring > 0) {
260
+ throw new Error(perpsErrorCodes_js_1.PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
261
+ }
262
+ }
158
263
  debugLogger?.log('Using legacy size calculation (no USD amount provided):', {
159
264
  providedSize: size,
160
265
  finalSize: finalPositionSize,
@@ -1 +1 @@
1
- {"version":3,"file":"orderCalculations.cjs","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":";;;AAEA,6EAAyE;AACzE,iEAGqC;AACrC,+DAA0D;AAG1D,oEAGiC;AAgFjC;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,MAA0B;IAC9D,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IAE7C,+BAA+B;I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I,aAAa,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAEzE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC;AAC7C,CAAC;AAjGD,4CAiGC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport { BASIS_POINTS_DIVISOR } from '../constants/hyperLiquidConfig.js';\nimport {\n MAX_ORDER_MARGIN_BUFFER,\n ORDER_SLIPPAGE_CONFIG,\n} from '../constants/perpsConfig.js';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes.js';\nimport type { SDKOrderParams } from '../types/hyperliquid-types.js';\nimport type { PerpsDebugLogger } from '../types/index.js';\nimport {\n formatHyperLiquidPrice,\n formatHyperLiquidSize,\n} from './hyperLiquidAdapter.js';\n\n/**\n * Optional debug logger for order calculation functions.\n * When provided, enables detailed logging for debugging.\n */\nexport type OrderCalculationsDebugLogger = PerpsDebugLogger | undefined;\n\ntype PositionSizeParams = {\n amount: string;\n price: number;\n szDecimals: number;\n};\n\ntype MarginRequiredParams = {\n amount: string;\n leverage: number;\n};\n\ntype MaxAllowedAmountParams = {\n spendableBalance: number;\n assetPrice: number;\n assetSzDecimals: number;\n leverage: number;\n};\n\n// Advanced order calculation interfaces\nexport type CalculateFinalPositionSizeParams = {\n usdAmount?: string;\n size?: string;\n currentPrice: number;\n priceAtCalculation?: number;\n maxSlippageBps?: number;\n szDecimals: number;\n leverage?: number;\n debugLogger?: OrderCalculationsDebugLogger;\n};\n\nexport type CalculateFinalPositionSizeResult = {\n finalPositionSize: number;\n};\n\nexport type CalculateOrderPriceAndSizeParams = {\n orderType: 'market' | 'limit';\n isBuy: boolean;\n finalPositionSize: number;\n currentPrice: number;\n limitPrice?: string;\n // Max slippage in basis points (e.g. 300 = 3%). Only applied to market orders;\n // limit orders use limitPrice directly. Falls back to ORDER_SLIPPAGE_CONFIG\n // .DefaultMarketSlippageBps when omitted on a market order.\n maxSlippageBps?: number;\n szDecimals: number;\n};\n\nexport type CalculateOrderPriceAndSizeResult = {\n orderPrice: number;\n formattedSize: string;\n formattedPrice: string;\n};\n\nexport type BuildOrdersArrayParams = {\n assetId: number;\n isBuy: boolean;\n formattedPrice: string;\n formattedSize: string;\n reduceOnly: boolean;\n orderType: 'market' | 'limit';\n clientOrderId?: string;\n takeProfitPrice?: string;\n stopLossPrice?: string;\n szDecimals: number;\n grouping?: 'na' | 'normalTpsl' | 'positionTpsl';\n};\n\nexport type BuildOrdersArrayResult = {\n orders: SDKOrderParams[];\n grouping: 'na' | 'normalTpsl' | 'positionTpsl';\n};\n\n/**\n * Calculate position size based on USD amount and asset price\n *\n * @param params - Amount in USD, current asset price, and required decimal precision\n * @returns Position size formatted to the asset's decimal precision\n */\nexport function calculatePositionSize(params: PositionSizeParams): string {\n const { amount, price, szDecimals } = params;\n\n // Validate required parameters\n if (szDecimals === undefined || szDecimals === null) {\n throw new Error('szDecimals is required for position size calculation');\n }\n if (szDecimals < 0) {\n throw new Error(`szDecimals must be >= 0, got: ${szDecimals}`);\n }\n\n const amountNum = parseFloat(amount || '0');\n\n if (isNaN(amountNum) || isNaN(price) || amountNum === 0 || price === 0) {\n return (0).toFixed(szDecimals);\n }\n\n const positionSize = amountNum / price;\n const multiplier = Math.pow(10, szDecimals);\n let rounded = Math.round(positionSize * multiplier) / multiplier;\n\n // Ensure rounded size meets requested USD (fix validation gap)\n const actualUsd = rounded * price;\n if (actualUsd < amountNum) {\n rounded += 1 / multiplier;\n }\n\n return rounded.toFixed(szDecimals);\n}\n\n/**\n * Calculate margin required for a position\n *\n * @param params - Position amount and leverage\n * @returns Margin required formatted to 2 decimal places\n */\nexport function calculateMarginRequired(params: MarginRequiredParams): string {\n const { amount, leverage } = params;\n const amountNum = parseFloat(amount || '0');\n\n if (\n isNaN(amountNum) ||\n isNaN(leverage) ||\n amountNum === 0 ||\n leverage === 0\n ) {\n return '0.00';\n }\n\n return (amountNum / leverage).toFixed(2);\n}\n\nexport function getMaxAllowedAmount(params: MaxAllowedAmountParams): number {\n const { spendableBalance, assetPrice, assetSzDecimals, leverage } = params;\n if (spendableBalance === 0 || !assetPrice || assetSzDecimals === undefined) {\n return 0;\n }\n\n // The theoretical maximum is simply spendableBalance * leverage\n const theoreticalMax = spendableBalance * leverage;\n\n // But we need to account for position size rounding\n // Find the largest whole dollar amount that fits within this limit\n let maxAmount = Math.floor(theoreticalMax);\n\n // Verify this amount doesn't exceed available balance after rounding\n const testPositionSize = calculatePositionSize({\n amount: maxAmount.toString(),\n price: assetPrice,\n szDecimals: assetSzDecimals,\n });\n\n const actualNotionalValue = parseFloat(testPositionSize) * assetPrice;\n const requiredMargin = actualNotionalValue / leverage;\n\n // If rounding caused us to exceed available balance, step down by one position increment\n if (requiredMargin > spendableBalance) {\n const minPositionSizeIncrement = 1 / Math.pow(10, assetSzDecimals);\n const positionSizeIncrementUsd = Math.ceil(\n minPositionSizeIncrement * assetPrice,\n );\n maxAmount -= positionSizeIncrementUsd;\n }\n\n // Apply margin buffer to reduce \"Insufficient margin\" rejections from the exchange\n // (fees, rounding, and exchange-side checks can make 100% theoretical max fail)\n const bufferedMax = maxAmount * (1 - MAX_ORDER_MARGIN_BUFFER);\n\n return Math.max(0, Math.floor(bufferedMax));\n}\n\n/**\n * Calculates final position size using USD as source of truth with price validation\n *\n * This function implements the hybrid approach where USD is the source of truth,\n * but includes price staleness validation and proper rounding to prevent precision loss.\n *\n * @param params - USD amount, size, prices, and configuration\n * @returns Final position size as a number\n */\nexport function calculateFinalPositionSize(\n params: CalculateFinalPositionSizeParams,\n): CalculateFinalPositionSizeResult {\n const {\n usdAmount,\n size,\n currentPrice,\n priceAtCalculation,\n maxSlippageBps,\n szDecimals,\n leverage,\n debugLogger,\n } = params;\n\n let finalPositionSize: number;\n\n if (usdAmount && parseFloat(usdAmount) > 0) {\n // USD amount provided - use it as source of truth\n const usdValue = parseFloat(usdAmount);\n\n // 1. Validate price staleness if priceAtCalculation provided\n if (priceAtCalculation) {\n const priceDeltaBps = Math.abs(\n ((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000,\n );\n const maxSlippageBpsValue =\n maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;\n\n if (priceDeltaBps > maxSlippageBpsValue) {\n throw new Error(\n `Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +\n `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`,\n );\n }\n\n debugLogger?.log('Price validation passed:', {\n priceAtCalculation,\n currentPrice,\n deltaBps: priceDeltaBps.toFixed(2),\n maxSlippageBps: maxSlippageBpsValue,\n });\n }\n\n // 2. Recalculate position size with fresh price\n finalPositionSize = usdValue / currentPrice;\n\n // 3. Apply size decimals rounding\n const multiplier = Math.pow(10, szDecimals);\n finalPositionSize = Math.round(finalPositionSize * multiplier) / multiplier;\n\n // 4. Ensure rounded size meets requested USD (fix validation gap)\n let actualNotionalValue = finalPositionSize * currentPrice;\n if (actualNotionalValue < usdValue) {\n // Add 1 minimum increment to meet requested USD\n finalPositionSize += 1 / multiplier;\n actualNotionalValue = finalPositionSize * currentPrice;\n\n debugLogger?.log('Position size adjusted to meet USD minimum:', {\n requestedUsd: usdValue,\n beforeAdjustment: finalPositionSize - 1 / multiplier,\n afterAdjustment: finalPositionSize,\n actualUsd: actualNotionalValue,\n });\n }\n\n const requiredMargin = actualNotionalValue / (leverage ?? 1);\n\n // Log if rounding caused significant difference\n const usdDifference = Math.abs(actualNotionalValue - usdValue);\n if (usdDifference > 0.01) {\n debugLogger?.log(\n 'Position size rounding caused USD difference (acceptable):',\n {\n requestedUsd: usdValue,\n actualUsd: actualNotionalValue,\n difference: usdDifference,\n positionSize: finalPositionSize,\n },\n );\n }\n\n debugLogger?.log('Recalculated position size with fresh price:', {\n usdAmount: usdValue,\n priceAtCalculation,\n currentPrice,\n originalSize: size,\n recalculatedSize: finalPositionSize,\n requiredMargin,\n minIncrement: 1 / multiplier,\n });\n } else {\n // Legacy: Use provided size (backward compatibility)\n finalPositionSize = parseFloat(size ?? '0');\n\n debugLogger?.log(\n 'Using legacy size calculation (no USD amount provided):',\n {\n providedSize: size,\n finalSize: finalPositionSize,\n },\n );\n }\n\n return { finalPositionSize };\n}\n\n/**\n * Calculates order price and formatted size based on order type\n *\n * @param params - Order parameters including type, direction, size, and prices\n * @returns Formatted order price, size, and price string\n */\nexport function calculateOrderPriceAndSize(\n params: CalculateOrderPriceAndSizeParams,\n): CalculateOrderPriceAndSizeResult {\n const {\n orderType,\n isBuy,\n finalPositionSize,\n currentPrice,\n limitPrice,\n maxSlippageBps,\n szDecimals,\n } = params;\n\n let orderPrice: number;\n let formattedSize: string;\n\n if (orderType === 'market') {\n // Market orders: apply slippage buffer to the live price so HyperLiquid\n // receives a worst-case acceptable limit price. Falls back to the\n // documented default if the caller does not provide one.\n const effectiveBps =\n maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;\n const slippageValue = effectiveBps / BASIS_POINTS_DIVISOR;\n orderPrice = isBuy\n ? currentPrice * (1 + slippageValue)\n : currentPrice * (1 - slippageValue);\n formattedSize = formatHyperLiquidSize({\n size: finalPositionSize,\n szDecimals,\n });\n } else {\n // Limit orders: use provided price (no slippage applied)\n if (!limitPrice) {\n throw new Error(PERPS_ERROR_CODES.ORDER_LIMIT_PRICE_REQUIRED);\n }\n orderPrice = parseFloat(limitPrice);\n formattedSize = formatHyperLiquidSize({\n size: finalPositionSize,\n szDecimals,\n });\n }\n\n const formattedPrice = formatHyperLiquidPrice({\n price: orderPrice,\n szDecimals,\n });\n\n return { orderPrice, formattedSize, formattedPrice };\n}\n\n/**\n * Builds orders array including main order and optional TP/SL orders\n *\n * @param params - Order construction parameters\n * @returns Array of SDK order params and grouping type\n */\nexport function buildOrdersArray(\n params: BuildOrdersArrayParams,\n): BuildOrdersArrayResult {\n const {\n assetId,\n isBuy,\n formattedPrice,\n formattedSize,\n reduceOnly,\n orderType,\n clientOrderId,\n takeProfitPrice,\n stopLossPrice,\n szDecimals,\n grouping,\n } = params;\n\n const orders: SDKOrderParams[] = [];\n\n // 1. Main order\n const mainOrder: SDKOrderParams = {\n a: assetId,\n b: isBuy,\n p: formattedPrice,\n s: formattedSize,\n r: reduceOnly || false,\n t:\n orderType === 'limit'\n ? { limit: { tif: 'Gtc' } }\n : { limit: { tif: 'FrontendMarket' } },\n c: clientOrderId ? (clientOrderId as Hex) : undefined,\n };\n orders.push(mainOrder);\n\n // 2. Take Profit order\n if (takeProfitPrice) {\n const tpOrder: SDKOrderParams = {\n a: assetId,\n b: !isBuy,\n p: formatHyperLiquidPrice({\n price: parseFloat(takeProfitPrice),\n szDecimals,\n }),\n s: formattedSize,\n r: true,\n t: {\n trigger: {\n isMarket: false,\n triggerPx: formatHyperLiquidPrice({\n price: parseFloat(takeProfitPrice),\n szDecimals,\n }),\n tpsl: 'tp',\n },\n },\n };\n orders.push(tpOrder);\n }\n\n // 3. Stop Loss order\n if (stopLossPrice) {\n // Apply 10% slippage to SL limit price (executes as market order when triggered)\n // HyperLiquid recommended: 10% for TP/SL orders\n const stopLossPriceNum = parseFloat(stopLossPrice);\n const slippageValue = ORDER_SLIPPAGE_CONFIG.DefaultTpslSlippageBps / 10000;\n const limitPriceWithSlippage = isBuy\n ? stopLossPriceNum * (1 - slippageValue) // Selling to close long: willing to accept LESS (slippage protection)\n : stopLossPriceNum * (1 + slippageValue); // Buying to close short: willing to pay MORE (slippage protection)\n\n const slOrder: SDKOrderParams = {\n a: assetId,\n b: !isBuy,\n p: formatHyperLiquidPrice({\n price: limitPriceWithSlippage,\n szDecimals,\n }),\n s: formattedSize,\n r: true,\n t: {\n trigger: {\n isMarket: true,\n triggerPx: formatHyperLiquidPrice({\n price: stopLossPriceNum,\n szDecimals,\n }),\n tpsl: 'sl',\n },\n },\n };\n orders.push(slOrder);\n }\n\n // Determine grouping\n const finalGrouping: 'na' | 'normalTpsl' | 'positionTpsl' =\n grouping ?? ((takeProfitPrice ?? stopLossPrice) ? 'normalTpsl' : 'na');\n\n return { orders, grouping: finalGrouping };\n}\n"]}
1
+ {"version":3,"file":"orderCalculations.cjs","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":";;;AAEA,6EAAyE;AACzE,iEAGqC;AACrC,+DAA0D;AAG1D,oEAGiC;AAQjC;;;GAGG;AACH,MAAM,eAAe,GAAG,IAAI,CAAC;AA8E7B;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,MAA0B;IAC9D,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IAE7C,+BAA+B;IAC/B,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;QACpD,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,iCAAiC,UAAU,EAAE,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,SAAS,GAAG,UAAU,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC;IAE5C,IAAI,KAAK,CAAC,SAAS,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,SAAS,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACvE,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;IAED,MAAM,YAAY,GAAG,SAAS,GAAG,KAAK,CAAC;IACvC,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;IAC5C,IAAI,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,UAAU,CAAC,GAAG,UAAU,CAAC;IAEjE,+DAA+D;IAC/D,MAAM,SAAS,GAAG,OAAO,GAAG,KAAK,CAAC;IAClC,IAAI,SAAS,GAA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AG,aAAa,CAAC,CAAC,CAAC,mEAAmE;QAE/G,MAAM,OAAO,GAAmB;YAC9B,CAAC,EAAE,OAAO;YACV,CAAC,EAAE,CAAC,KAAK;YACT,CAAC,EAAE,IAAA,8CAAsB,EAAC;gBACxB,KAAK,EAAE,sBAAsB;gBAC7B,UAAU;aACX,CAAC;YACF,CAAC,EAAE,aAAa;YAChB,CAAC,EAAE,IAAI;YACP,CAAC,EAAE;gBACD,OAAO,EAAE;oBACP,QAAQ,EAAE,IAAI;oBACd,SAAS,EAAE,IAAA,8CAAsB,EAAC;wBAChC,KAAK,EAAE,gBAAgB;wBACvB,UAAU;qBACX,CAAC;oBACF,IAAI,EAAE,IAAI;iBACX;aACF;SACF,CAAC;QACF,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvB,CAAC;IAED,qBAAqB;IACrB,MAAM,aAAa,GACjB,QAAQ,IAAI,CAAC,CAAC,eAAe,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAEzE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC;AAC7C,CAAC;AAjGD,4CAiGC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport { BASIS_POINTS_DIVISOR } from '../constants/hyperLiquidConfig.js';\nimport {\n MAX_ORDER_MARGIN_BUFFER,\n ORDER_SLIPPAGE_CONFIG,\n} from '../constants/perpsConfig.js';\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes.js';\nimport type { SDKOrderParams } from '../types/hyperliquid-types.js';\nimport type { PerpsDebugLogger } from '../types/index.js';\nimport {\n formatHyperLiquidPrice,\n formatHyperLiquidSize,\n} from './hyperLiquidAdapter.js';\n\n/**\n * Optional debug logger for order calculation functions.\n * When provided, enables detailed logging for debugging.\n */\nexport type OrderCalculationsDebugLogger = PerpsDebugLogger | undefined;\n\n/**\n * Tolerance used when deciding whether a scaled size is already on the size\n * grid, guarding against floating-point representation error.\n */\nconst FLOAT_TOLERANCE = 1e-6;\n\ntype PositionSizeParams = {\n amount: string;\n price: number;\n szDecimals: number;\n};\n\ntype MarginRequiredParams = {\n amount: string;\n leverage: number;\n};\n\ntype MaxAllowedAmountParams = {\n spendableBalance: number;\n assetPrice: number;\n assetSzDecimals: number;\n leverage: number;\n};\n\n// Advanced order calculation interfaces\nexport type CalculateFinalPositionSizeParams = {\n usdAmount?: string;\n size?: string;\n currentPrice: number;\n priceAtCalculation?: number;\n maxSlippageBps?: number;\n szDecimals: number;\n leverage?: number;\n // Reduce-only orders (position closes) may never round up: HyperLiquid\n // rejects a reduce-only order whose size exceeds the live position with\n // \"Reduce only order would increase position\".\n reduceOnly?: boolean;\n debugLogger?: OrderCalculationsDebugLogger;\n};\n\nexport type CalculateFinalPositionSizeResult = {\n finalPositionSize: number;\n};\n\nexport type CalculateOrderPriceAndSizeParams = {\n orderType: 'market' | 'limit';\n isBuy: boolean;\n finalPositionSize: number;\n currentPrice: number;\n limitPrice?: string;\n // Max slippage in basis points (e.g. 300 = 3%). Only applied to market orders;\n // limit orders use limitPrice directly. Falls back to ORDER_SLIPPAGE_CONFIG\n // .DefaultMarketSlippageBps when omitted on a market order.\n maxSlippageBps?: number;\n szDecimals: number;\n};\n\nexport type CalculateOrderPriceAndSizeResult = {\n orderPrice: number;\n formattedSize: string;\n formattedPrice: string;\n};\n\nexport type BuildOrdersArrayParams = {\n assetId: number;\n isBuy: boolean;\n formattedPrice: string;\n formattedSize: string;\n reduceOnly: boolean;\n orderType: 'market' | 'limit';\n clientOrderId?: string;\n takeProfitPrice?: string;\n stopLossPrice?: string;\n szDecimals: number;\n grouping?: 'na' | 'normalTpsl' | 'positionTpsl';\n};\n\nexport type BuildOrdersArrayResult = {\n orders: SDKOrderParams[];\n grouping: 'na' | 'normalTpsl' | 'positionTpsl';\n};\n\n/**\n * Calculate position size based on USD amount and asset price\n *\n * @param params - Amount in USD, current asset price, and required decimal precision\n * @returns Position size formatted to the asset's decimal precision\n */\nexport function calculatePositionSize(params: PositionSizeParams): string {\n const { amount, price, szDecimals } = params;\n\n // Validate required parameters\n if (szDecimals === undefined || szDecimals === null) {\n throw new Error('szDecimals is required for position size calculation');\n }\n if (szDecimals < 0) {\n throw new Error(`szDecimals must be >= 0, got: ${szDecimals}`);\n }\n\n const amountNum = parseFloat(amount || '0');\n\n if (isNaN(amountNum) || isNaN(price) || amountNum === 0 || price === 0) {\n return (0).toFixed(szDecimals);\n }\n\n const positionSize = amountNum / price;\n const multiplier = Math.pow(10, szDecimals);\n let rounded = Math.round(positionSize * multiplier) / multiplier;\n\n // Ensure rounded size meets requested USD (fix validation gap)\n const actualUsd = rounded * price;\n if (actualUsd < amountNum) {\n rounded += 1 / multiplier;\n }\n\n return rounded.toFixed(szDecimals);\n}\n\n/**\n * Calculate margin required for a position\n *\n * @param params - Position amount and leverage\n * @returns Margin required formatted to 2 decimal places\n */\nexport function calculateMarginRequired(params: MarginRequiredParams): string {\n const { amount, leverage } = params;\n const amountNum = parseFloat(amount || '0');\n\n if (\n isNaN(amountNum) ||\n isNaN(leverage) ||\n amountNum === 0 ||\n leverage === 0\n ) {\n return '0.00';\n }\n\n return (amountNum / leverage).toFixed(2);\n}\n\nexport function getMaxAllowedAmount(params: MaxAllowedAmountParams): number {\n const { spendableBalance, assetPrice, assetSzDecimals, leverage } = params;\n if (spendableBalance === 0 || !assetPrice || assetSzDecimals === undefined) {\n return 0;\n }\n\n // The theoretical maximum is simply spendableBalance * leverage\n const theoreticalMax = spendableBalance * leverage;\n\n // But we need to account for position size rounding\n // Find the largest whole dollar amount that fits within this limit\n let maxAmount = Math.floor(theoreticalMax);\n\n // Verify this amount doesn't exceed available balance after rounding\n const testPositionSize = calculatePositionSize({\n amount: maxAmount.toString(),\n price: assetPrice,\n szDecimals: assetSzDecimals,\n });\n\n const actualNotionalValue = parseFloat(testPositionSize) * assetPrice;\n const requiredMargin = actualNotionalValue / leverage;\n\n // If rounding caused us to exceed available balance, step down by one position increment\n if (requiredMargin > spendableBalance) {\n const minPositionSizeIncrement = 1 / Math.pow(10, assetSzDecimals);\n const positionSizeIncrementUsd = Math.ceil(\n minPositionSizeIncrement * assetPrice,\n );\n maxAmount -= positionSizeIncrementUsd;\n }\n\n // Apply margin buffer to reduce \"Insufficient margin\" rejections from the exchange\n // (fees, rounding, and exchange-side checks can make 100% theoretical max fail)\n const bufferedMax = maxAmount * (1 - MAX_ORDER_MARGIN_BUFFER);\n\n return Math.max(0, Math.floor(bufferedMax));\n}\n\n/**\n * Round a size down onto the asset's size grid.\n *\n * Used for reduce-only orders, where rounding up would push the size past the\n * live position size. Values already on the grid are snapped rather than\n * truncated, because floating-point math can leave them just below a grid\n * point (0.0123 * 10000 === 122.99999999999999) and truncating would drop a\n * whole increment.\n *\n * The result is never greater than `size`, for negative sizes as well as\n * positive: the snap only ever recovers a grid point the input already\n * represents, so a value genuinely below a grid point is stepped down even when\n * the tolerance would have reached the point above it.\n *\n * A size whose scaled form reaches `2^53` is returned unchanged: doubles cannot\n * represent consecutive integers there, so the grid is finer than the spacing\n * between representable values and there is nothing to round down to.\n *\n * @param size - Size to round down.\n * @param szDecimals - The asset's size decimal precision.\n * @returns The size rounded down onto the size grid, never exceeding `size`.\n */\nexport function floorToSizeDecimals(size: number, szDecimals: number): number {\n const multiplier = Math.pow(10, szDecimals);\n const scaled = size * multiplier;\n\n // Past 2^53 a double cannot represent consecutive integers, so `units -= 1`\n // below would be a no-op and the step-down loop would never terminate. The\n // size grid is finer than the spacing between representable values at that\n // magnitude, so there is no increment to shave: return the input unchanged.\n if (!Number.isFinite(scaled) || Math.abs(scaled) >= Number.MAX_SAFE_INTEGER) {\n return size;\n }\n\n const nearest = Math.round(scaled);\n // The tolerance scales with the magnitude, because double-precision error\n // does too: a fixed epsilon would stop absorbing representation error for\n // sizes that scale past ~1e10 and would then shave off a whole increment.\n const tolerance = Math.max(\n FLOAT_TOLERANCE,\n Math.abs(scaled) * Number.EPSILON * 8,\n );\n let units =\n Math.abs(scaled - nearest) < tolerance ? nearest : Math.floor(scaled);\n\n // Step down until the result no longer exceeds the input. One pass is not\n // enough: a tolerance wide enough to absorb representation error at large\n // magnitudes also reaches the next grid point, and for an input less than half\n // an ulp below a grid point `size * multiplier` evaluates to exactly that grid\n // integer, so flooring the scaled value returns the same too-large result.\n // The comparison alone is the whole termination condition: for a non-negative\n // size the loop stops at or before zero, and for a negative size it stops once\n // the value is no longer above the input. Guarding on `units` instead would\n // skip a negative size below the tolerance, which snaps to `-0` — and\n // `-0 !== 0` is false. The 2^53 bail-out above keeps this bounded.\n while (units / multiplier > size) {\n units -= 1;\n }\n\n return units / multiplier;\n}\n\n/**\n * Calculates final position size using USD as source of truth with price validation\n *\n * This function implements the hybrid approach where USD is the source of truth,\n * but includes price staleness validation and proper rounding to prevent precision loss.\n *\n * @param params - USD amount, size, prices, and configuration\n * @returns Final position size as a number\n */\nexport function calculateFinalPositionSize(\n params: CalculateFinalPositionSizeParams,\n): CalculateFinalPositionSizeResult {\n const {\n usdAmount,\n size,\n currentPrice,\n priceAtCalculation,\n maxSlippageBps,\n szDecimals,\n leverage,\n reduceOnly,\n debugLogger,\n } = params;\n\n let finalPositionSize: number;\n\n // Validate price staleness whenever the caller supplied a calculation-time\n // price. This runs before the sizing branches on purpose: a full close submits\n // the exact live position size rather than a USD-derived one, and it must still\n // be rejected when the price has moved past the caller's tolerance.\n if (priceAtCalculation) {\n const priceDeltaBps = Math.abs(\n ((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000,\n );\n const maxSlippageBpsValue =\n maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;\n\n if (priceDeltaBps > maxSlippageBpsValue) {\n throw new Error(\n `Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +\n `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`,\n );\n }\n\n debugLogger?.log('Price validation passed:', {\n priceAtCalculation,\n currentPrice,\n deltaBps: priceDeltaBps.toFixed(2),\n maxSlippageBps: maxSlippageBpsValue,\n });\n }\n\n if (usdAmount && parseFloat(usdAmount) > 0) {\n // USD amount provided - use it as source of truth\n const usdValue = parseFloat(usdAmount);\n\n // Recalculate position size with fresh price\n finalPositionSize = usdValue / currentPrice;\n\n // A reduce-only order may never exceed the size the caller asked to close:\n // that size is already clamped to the live position, while the USD amount was\n // computed against an older price and can imply a larger size after an\n // adverse move. Capping here keeps USD accuracy in the common case and makes\n // the caller's clamp binding.\n if (reduceOnly && size) {\n const requestedSize = parseFloat(size);\n\n // A supplied size must be positive, or the cap below would submit a\n // zero/negative order. Reject it rather than silently falling back to the\n // USD-derived size, matching how closePosition treats the same input.\n if (!Number.isFinite(requestedSize) || requestedSize <= 0) {\n throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);\n }\n\n finalPositionSize = Math.min(finalPositionSize, requestedSize);\n }\n\n // 3. Apply size decimals rounding (reduce-only never rounds up)\n const multiplier = Math.pow(10, szDecimals);\n const sizeBeforeRounding = finalPositionSize;\n finalPositionSize = reduceOnly\n ? floorToSizeDecimals(finalPositionSize, szDecimals)\n : Math.round(finalPositionSize * multiplier) / multiplier;\n\n // Rounding down can zero out a reduce-only order whose USD value is worth\n // less than one size increment. Fail with a clear error instead of\n // submitting a size of \"0\" the exchange will reject.\n if (reduceOnly && finalPositionSize <= 0 && sizeBeforeRounding > 0) {\n throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);\n }\n\n // 4. Ensure rounded size meets requested USD (fix validation gap).\n // Skipped for reduce-only orders: adding an increment there would submit\n // more than the position holds and HyperLiquid rejects the order.\n let actualNotionalValue = finalPositionSize * currentPrice;\n if (!reduceOnly && actualNotionalValue < usdValue) {\n // Add 1 minimum increment to meet requested USD\n finalPositionSize += 1 / multiplier;\n actualNotionalValue = finalPositionSize * currentPrice;\n\n debugLogger?.log('Position size adjusted to meet USD minimum:', {\n requestedUsd: usdValue,\n beforeAdjustment: finalPositionSize - 1 / multiplier,\n afterAdjustment: finalPositionSize,\n actualUsd: actualNotionalValue,\n });\n }\n\n const requiredMargin = actualNotionalValue / (leverage ?? 1);\n\n // Log if rounding caused significant difference\n const usdDifference = Math.abs(actualNotionalValue - usdValue);\n if (usdDifference > 0.01) {\n debugLogger?.log(\n 'Position size rounding caused USD difference (acceptable):',\n {\n requestedUsd: usdValue,\n actualUsd: actualNotionalValue,\n difference: usdDifference,\n positionSize: finalPositionSize,\n },\n );\n }\n\n debugLogger?.log('Recalculated position size with fresh price:', {\n usdAmount: usdValue,\n priceAtCalculation,\n currentPrice,\n originalSize: size,\n recalculatedSize: finalPositionSize,\n requiredMargin,\n minIncrement: 1 / multiplier,\n });\n } else {\n // Legacy: Use provided size (backward compatibility)\n finalPositionSize = parseFloat(size ?? '0');\n\n // Reduce-only sizes are formatted with toFixed() further down, which rounds\n // up; truncate onto the size grid first so a close can never exceed the\n // position it is closing.\n if (reduceOnly) {\n // A supplied size must be positive, or formatHyperLiquidSize would render\n // a zero or negative order size. The USD branch above rejects the same\n // input.\n if (size && !(finalPositionSize > 0)) {\n throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);\n }\n\n const sizeBeforeFlooring = finalPositionSize;\n finalPositionSize = floorToSizeDecimals(finalPositionSize, szDecimals);\n\n // A positive size that floors to zero is worth less than one increment\n if (finalPositionSize <= 0 && sizeBeforeFlooring > 0) {\n throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);\n }\n }\n\n debugLogger?.log(\n 'Using legacy size calculation (no USD amount provided):',\n {\n providedSize: size,\n finalSize: finalPositionSize,\n },\n );\n }\n\n return { finalPositionSize };\n}\n\n/**\n * Calculates order price and formatted size based on order type\n *\n * @param params - Order parameters including type, direction, size, and prices\n * @returns Formatted order price, size, and price string\n */\nexport function calculateOrderPriceAndSize(\n params: CalculateOrderPriceAndSizeParams,\n): CalculateOrderPriceAndSizeResult {\n const {\n orderType,\n isBuy,\n finalPositionSize,\n currentPrice,\n limitPrice,\n maxSlippageBps,\n szDecimals,\n } = params;\n\n let orderPrice: number;\n let formattedSize: string;\n\n if (orderType === 'market') {\n // Market orders: apply slippage buffer to the live price so HyperLiquid\n // receives a worst-case acceptable limit price. Falls back to the\n // documented default if the caller does not provide one.\n const effectiveBps =\n maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;\n const slippageValue = effectiveBps / BASIS_POINTS_DIVISOR;\n orderPrice = isBuy\n ? currentPrice * (1 + slippageValue)\n : currentPrice * (1 - slippageValue);\n formattedSize = formatHyperLiquidSize({\n size: finalPositionSize,\n szDecimals,\n });\n } else {\n // Limit orders: use provided price (no slippage applied)\n if (!limitPrice) {\n throw new Error(PERPS_ERROR_CODES.ORDER_LIMIT_PRICE_REQUIRED);\n }\n orderPrice = parseFloat(limitPrice);\n formattedSize = formatHyperLiquidSize({\n size: finalPositionSize,\n szDecimals,\n });\n }\n\n const formattedPrice = formatHyperLiquidPrice({\n price: orderPrice,\n szDecimals,\n });\n\n return { orderPrice, formattedSize, formattedPrice };\n}\n\n/**\n * Builds orders array including main order and optional TP/SL orders\n *\n * @param params - Order construction parameters\n * @returns Array of SDK order params and grouping type\n */\nexport function buildOrdersArray(\n params: BuildOrdersArrayParams,\n): BuildOrdersArrayResult {\n const {\n assetId,\n isBuy,\n formattedPrice,\n formattedSize,\n reduceOnly,\n orderType,\n clientOrderId,\n takeProfitPrice,\n stopLossPrice,\n szDecimals,\n grouping,\n } = params;\n\n const orders: SDKOrderParams[] = [];\n\n // 1. Main order\n const mainOrder: SDKOrderParams = {\n a: assetId,\n b: isBuy,\n p: formattedPrice,\n s: formattedSize,\n r: reduceOnly || false,\n t:\n orderType === 'limit'\n ? { limit: { tif: 'Gtc' } }\n : { limit: { tif: 'FrontendMarket' } },\n c: clientOrderId ? (clientOrderId as Hex) : undefined,\n };\n orders.push(mainOrder);\n\n // 2. Take Profit order\n if (takeProfitPrice) {\n const tpOrder: SDKOrderParams = {\n a: assetId,\n b: !isBuy,\n p: formatHyperLiquidPrice({\n price: parseFloat(takeProfitPrice),\n szDecimals,\n }),\n s: formattedSize,\n r: true,\n t: {\n trigger: {\n isMarket: false,\n triggerPx: formatHyperLiquidPrice({\n price: parseFloat(takeProfitPrice),\n szDecimals,\n }),\n tpsl: 'tp',\n },\n },\n };\n orders.push(tpOrder);\n }\n\n // 3. Stop Loss order\n if (stopLossPrice) {\n // Apply 10% slippage to SL limit price (executes as market order when triggered)\n // HyperLiquid recommended: 10% for TP/SL orders\n const stopLossPriceNum = parseFloat(stopLossPrice);\n const slippageValue = ORDER_SLIPPAGE_CONFIG.DefaultTpslSlippageBps / 10000;\n const limitPriceWithSlippage = isBuy\n ? stopLossPriceNum * (1 - slippageValue) // Selling to close long: willing to accept LESS (slippage protection)\n : stopLossPriceNum * (1 + slippageValue); // Buying to close short: willing to pay MORE (slippage protection)\n\n const slOrder: SDKOrderParams = {\n a: assetId,\n b: !isBuy,\n p: formatHyperLiquidPrice({\n price: limitPriceWithSlippage,\n szDecimals,\n }),\n s: formattedSize,\n r: true,\n t: {\n trigger: {\n isMarket: true,\n triggerPx: formatHyperLiquidPrice({\n price: stopLossPriceNum,\n szDecimals,\n }),\n tpsl: 'sl',\n },\n },\n };\n orders.push(slOrder);\n }\n\n // Determine grouping\n const finalGrouping: 'na' | 'normalTpsl' | 'positionTpsl' =\n grouping ?? ((takeProfitPrice ?? stopLossPrice) ? 'normalTpsl' : 'na');\n\n return { orders, grouping: finalGrouping };\n}\n"]}
@@ -28,6 +28,7 @@ export type CalculateFinalPositionSizeParams = {
28
28
  maxSlippageBps?: number;
29
29
  szDecimals: number;
30
30
  leverage?: number;
31
+ reduceOnly?: boolean;
31
32
  debugLogger?: OrderCalculationsDebugLogger;
32
33
  };
33
34
  export type CalculateFinalPositionSizeResult = {
@@ -79,6 +80,29 @@ export declare function calculatePositionSize(params: PositionSizeParams): strin
79
80
  */
80
81
  export declare function calculateMarginRequired(params: MarginRequiredParams): string;
81
82
  export declare function getMaxAllowedAmount(params: MaxAllowedAmountParams): number;
83
+ /**
84
+ * Round a size down onto the asset's size grid.
85
+ *
86
+ * Used for reduce-only orders, where rounding up would push the size past the
87
+ * live position size. Values already on the grid are snapped rather than
88
+ * truncated, because floating-point math can leave them just below a grid
89
+ * point (0.0123 * 10000 === 122.99999999999999) and truncating would drop a
90
+ * whole increment.
91
+ *
92
+ * The result is never greater than `size`, for negative sizes as well as
93
+ * positive: the snap only ever recovers a grid point the input already
94
+ * represents, so a value genuinely below a grid point is stepped down even when
95
+ * the tolerance would have reached the point above it.
96
+ *
97
+ * A size whose scaled form reaches `2^53` is returned unchanged: doubles cannot
98
+ * represent consecutive integers there, so the grid is finer than the spacing
99
+ * between representable values and there is nothing to round down to.
100
+ *
101
+ * @param size - Size to round down.
102
+ * @param szDecimals - The asset's size decimal precision.
103
+ * @returns The size rounded down onto the size grid, never exceeding `size`.
104
+ */
105
+ export declare function floorToSizeDecimals(size: number, szDecimals: number): number;
82
106
  /**
83
107
  * Calculates final position size using USD as source of truth with price validation
84
108
  *
@@ -1 +1 @@
1
- {"version":3,"file":"orderCalculations.d.cts","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,uCAAsC;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA0B;AAM1D;;;GAGG;AACH,MAAM,MAAM,4BAA4B,GAAG,gBAAgB,GAAG,SAAS,CAAC;AAExE,KAAK,kBAAkB,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,KAAK,oBAAoB,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,sBAAsB,GAAG;IAC5B,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,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SAAS,EAAE,QAAQ,GAAG,OAAO,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,IAAI,GAAG,YAAY,GAAG,cAAc,CAAC;CACjD,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,QAAQ,EAAE,IAAI,GAAG,YAAY,GAAG,cAAc,CAAC;CAChD,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM,CA4BxE;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,oBAAoB,GAAG,MAAM,CAc5E;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,sBAAsB,GAAG,MAAM,CAqC1E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAsC5E;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CA4JlC;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CA8ClC;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,sBAAsB,GAC7B,sBAAsB,CA+FxB"}
@@ -28,6 +28,7 @@ export type CalculateFinalPositionSizeParams = {
28
28
  maxSlippageBps?: number;
29
29
  szDecimals: number;
30
30
  leverage?: number;
31
+ reduceOnly?: boolean;
31
32
  debugLogger?: OrderCalculationsDebugLogger;
32
33
  };
33
34
  export type CalculateFinalPositionSizeResult = {
@@ -79,6 +80,29 @@ export declare function calculatePositionSize(params: PositionSizeParams): strin
79
80
  */
80
81
  export declare function calculateMarginRequired(params: MarginRequiredParams): string;
81
82
  export declare function getMaxAllowedAmount(params: MaxAllowedAmountParams): number;
83
+ /**
84
+ * Round a size down onto the asset's size grid.
85
+ *
86
+ * Used for reduce-only orders, where rounding up would push the size past the
87
+ * live position size. Values already on the grid are snapped rather than
88
+ * truncated, because floating-point math can leave them just below a grid
89
+ * point (0.0123 * 10000 === 122.99999999999999) and truncating would drop a
90
+ * whole increment.
91
+ *
92
+ * The result is never greater than `size`, for negative sizes as well as
93
+ * positive: the snap only ever recovers a grid point the input already
94
+ * represents, so a value genuinely below a grid point is stepped down even when
95
+ * the tolerance would have reached the point above it.
96
+ *
97
+ * A size whose scaled form reaches `2^53` is returned unchanged: doubles cannot
98
+ * represent consecutive integers there, so the grid is finer than the spacing
99
+ * between representable values and there is nothing to round down to.
100
+ *
101
+ * @param size - Size to round down.
102
+ * @param szDecimals - The asset's size decimal precision.
103
+ * @returns The size rounded down onto the size grid, never exceeding `size`.
104
+ */
105
+ export declare function floorToSizeDecimals(size: number, szDecimals: number): number;
82
106
  /**
83
107
  * Calculates final position size using USD as source of truth with price validation
84
108
  *
@@ -1 +1 @@
1
- {"version":3,"file":"orderCalculations.d.mts","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,cAAc,EAAE,uCAAsC;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA0B;AAM1D;;;GAGG;AACH,MAAM,MAAM,4BAA4B,GAAG,gBAAgB,GAAG,SAAS,CAAC;AAExE,KAAK,kBAAkB,GAAG;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,KAAK,oBAAoB,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF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
3
3
  import { PERPS_ERROR_CODES } from "../perpsErrorCodes.mjs";
4
4
  import { formatHyperLiquidPrice, formatHyperLiquidSize } from "./hyperLiquidAdapter.mjs";
5
+ /**
6
+ * Tolerance used when deciding whether a scaled size is already on the size
7
+ * grid, guarding against floating-point representation error.
8
+ */
9
+ const FLOAT_TOLERANCE = 1e-6;
5
10
  /**
6
11
  * Calculate position size based on USD amount and asset price
7
12
  *
@@ -77,6 +82,59 @@ export function getMaxAllowedAmount(params) {
77
82
  const bufferedMax = maxAmount * (1 - MAX_ORDER_MARGIN_BUFFER);
78
83
  return Math.max(0, Math.floor(bufferedMax));
79
84
  }
85
+ /**
86
+ * Round a size down onto the asset's size grid.
87
+ *
88
+ * Used for reduce-only orders, where rounding up would push the size past the
89
+ * live position size. Values already on the grid are snapped rather than
90
+ * truncated, because floating-point math can leave them just below a grid
91
+ * point (0.0123 * 10000 === 122.99999999999999) and truncating would drop a
92
+ * whole increment.
93
+ *
94
+ * The result is never greater than `size`, for negative sizes as well as
95
+ * positive: the snap only ever recovers a grid point the input already
96
+ * represents, so a value genuinely below a grid point is stepped down even when
97
+ * the tolerance would have reached the point above it.
98
+ *
99
+ * A size whose scaled form reaches `2^53` is returned unchanged: doubles cannot
100
+ * represent consecutive integers there, so the grid is finer than the spacing
101
+ * between representable values and there is nothing to round down to.
102
+ *
103
+ * @param size - Size to round down.
104
+ * @param szDecimals - The asset's size decimal precision.
105
+ * @returns The size rounded down onto the size grid, never exceeding `size`.
106
+ */
107
+ export function floorToSizeDecimals(size, szDecimals) {
108
+ const multiplier = Math.pow(10, szDecimals);
109
+ const scaled = size * multiplier;
110
+ // Past 2^53 a double cannot represent consecutive integers, so `units -= 1`
111
+ // below would be a no-op and the step-down loop would never terminate. The
112
+ // size grid is finer than the spacing between representable values at that
113
+ // magnitude, so there is no increment to shave: return the input unchanged.
114
+ if (!Number.isFinite(scaled) || Math.abs(scaled) >= Number.MAX_SAFE_INTEGER) {
115
+ return size;
116
+ }
117
+ const nearest = Math.round(scaled);
118
+ // The tolerance scales with the magnitude, because double-precision error
119
+ // does too: a fixed epsilon would stop absorbing representation error for
120
+ // sizes that scale past ~1e10 and would then shave off a whole increment.
121
+ const tolerance = Math.max(FLOAT_TOLERANCE, Math.abs(scaled) * Number.EPSILON * 8);
122
+ let units = Math.abs(scaled - nearest) < tolerance ? nearest : Math.floor(scaled);
123
+ // Step down until the result no longer exceeds the input. One pass is not
124
+ // enough: a tolerance wide enough to absorb representation error at large
125
+ // magnitudes also reaches the next grid point, and for an input less than half
126
+ // an ulp below a grid point `size * multiplier` evaluates to exactly that grid
127
+ // integer, so flooring the scaled value returns the same too-large result.
128
+ // The comparison alone is the whole termination condition: for a non-negative
129
+ // size the loop stops at or before zero, and for a negative size it stops once
130
+ // the value is no longer above the input. Guarding on `units` instead would
131
+ // skip a negative size below the tolerance, which snaps to `-0` — and
132
+ // `-0 !== 0` is false. The 2^53 bail-out above keeps this bounded.
133
+ while (units / multiplier > size) {
134
+ units -= 1;
135
+ }
136
+ return units / multiplier;
137
+ }
80
138
  /**
81
139
  * Calculates final position size using USD as source of truth with price validation
82
140
  *
@@ -87,34 +145,63 @@ export function getMaxAllowedAmount(params) {
87
145
  * @returns Final position size as a number
88
146
  */
89
147
  export function calculateFinalPositionSize(params) {
90
- const { usdAmount, size, currentPrice, priceAtCalculation, maxSlippageBps, szDecimals, leverage, debugLogger, } = params;
148
+ const { usdAmount, size, currentPrice, priceAtCalculation, maxSlippageBps, szDecimals, leverage, reduceOnly, debugLogger, } = params;
91
149
  let finalPositionSize;
150
+ // Validate price staleness whenever the caller supplied a calculation-time
151
+ // price. This runs before the sizing branches on purpose: a full close submits
152
+ // the exact live position size rather than a USD-derived one, and it must still
153
+ // be rejected when the price has moved past the caller's tolerance.
154
+ if (priceAtCalculation) {
155
+ const priceDeltaBps = Math.abs(((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000);
156
+ const maxSlippageBpsValue = maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;
157
+ if (priceDeltaBps > maxSlippageBpsValue) {
158
+ throw new Error(`Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +
159
+ `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`);
160
+ }
161
+ debugLogger?.log('Price validation passed:', {
162
+ priceAtCalculation,
163
+ currentPrice,
164
+ deltaBps: priceDeltaBps.toFixed(2),
165
+ maxSlippageBps: maxSlippageBpsValue,
166
+ });
167
+ }
92
168
  if (usdAmount && parseFloat(usdAmount) > 0) {
93
169
  // USD amount provided - use it as source of truth
94
170
  const usdValue = parseFloat(usdAmount);
95
- // 1. Validate price staleness if priceAtCalculation provided
96
- if (priceAtCalculation) {
97
- const priceDeltaBps = Math.abs(((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000);
98
- const maxSlippageBpsValue = maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;
99
- if (priceDeltaBps > maxSlippageBpsValue) {
100
- throw new Error(`Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +
101
- `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`);
171
+ // Recalculate position size with fresh price
172
+ finalPositionSize = usdValue / currentPrice;
173
+ // A reduce-only order may never exceed the size the caller asked to close:
174
+ // that size is already clamped to the live position, while the USD amount was
175
+ // computed against an older price and can imply a larger size after an
176
+ // adverse move. Capping here keeps USD accuracy in the common case and makes
177
+ // the caller's clamp binding.
178
+ if (reduceOnly && size) {
179
+ const requestedSize = parseFloat(size);
180
+ // A supplied size must be positive, or the cap below would submit a
181
+ // zero/negative order. Reject it rather than silently falling back to the
182
+ // USD-derived size, matching how closePosition treats the same input.
183
+ if (!Number.isFinite(requestedSize) || requestedSize <= 0) {
184
+ throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
102
185
  }
103
- debugLogger?.log('Price validation passed:', {
104
- priceAtCalculation,
105
- currentPrice,
106
- deltaBps: priceDeltaBps.toFixed(2),
107
- maxSlippageBps: maxSlippageBpsValue,
108
- });
186
+ finalPositionSize = Math.min(finalPositionSize, requestedSize);
109
187
  }
110
- // 2. Recalculate position size with fresh price
111
- finalPositionSize = usdValue / currentPrice;
112
- // 3. Apply size decimals rounding
188
+ // 3. Apply size decimals rounding (reduce-only never rounds up)
113
189
  const multiplier = Math.pow(10, szDecimals);
114
- finalPositionSize = Math.round(finalPositionSize * multiplier) / multiplier;
115
- // 4. Ensure rounded size meets requested USD (fix validation gap)
190
+ const sizeBeforeRounding = finalPositionSize;
191
+ finalPositionSize = reduceOnly
192
+ ? floorToSizeDecimals(finalPositionSize, szDecimals)
193
+ : Math.round(finalPositionSize * multiplier) / multiplier;
194
+ // Rounding down can zero out a reduce-only order whose USD value is worth
195
+ // less than one size increment. Fail with a clear error instead of
196
+ // submitting a size of "0" the exchange will reject.
197
+ if (reduceOnly && finalPositionSize <= 0 && sizeBeforeRounding > 0) {
198
+ throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
199
+ }
200
+ // 4. Ensure rounded size meets requested USD (fix validation gap).
201
+ // Skipped for reduce-only orders: adding an increment there would submit
202
+ // more than the position holds and HyperLiquid rejects the order.
116
203
  let actualNotionalValue = finalPositionSize * currentPrice;
117
- if (actualNotionalValue < usdValue) {
204
+ if (!reduceOnly && actualNotionalValue < usdValue) {
118
205
  // Add 1 minimum increment to meet requested USD
119
206
  finalPositionSize += 1 / multiplier;
120
207
  actualNotionalValue = finalPositionSize * currentPrice;
@@ -149,6 +236,23 @@ export function calculateFinalPositionSize(params) {
149
236
  else {
150
237
  // Legacy: Use provided size (backward compatibility)
151
238
  finalPositionSize = parseFloat(size ?? '0');
239
+ // Reduce-only sizes are formatted with toFixed() further down, which rounds
240
+ // up; truncate onto the size grid first so a close can never exceed the
241
+ // position it is closing.
242
+ if (reduceOnly) {
243
+ // A supplied size must be positive, or formatHyperLiquidSize would render
244
+ // a zero or negative order size. The USD branch above rejects the same
245
+ // input.
246
+ if (size && !(finalPositionSize > 0)) {
247
+ throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
248
+ }
249
+ const sizeBeforeFlooring = finalPositionSize;
250
+ finalPositionSize = floorToSizeDecimals(finalPositionSize, szDecimals);
251
+ // A positive size that floors to zero is worth less than one increment
252
+ if (finalPositionSize <= 0 && sizeBeforeFlooring > 0) {
253
+ throw new Error(PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE);
254
+ }
255
+ }
152
256
  debugLogger?.log('Using legacy size calculation (no USD amount provided):', {
153
257
  providedSize: size,
154
258
  finalSize: finalPositionSize,