@metamask-previews/perps-controller 25.0.0-preview-4046c2f69 → 26.0.0-preview-203750488

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 (156) hide show
  1. package/CHANGELOG.md +105 -9
  2. package/dist/PerpsController-method-action-types.d.ts +149 -3
  3. package/dist/PerpsController-method-action-types.d.ts.map +1 -1
  4. package/dist/PerpsController-method-action-types.js.map +1 -1
  5. package/dist/PerpsController.d.ts +125 -3
  6. package/dist/PerpsController.d.ts.map +1 -1
  7. package/dist/PerpsController.js +787 -31
  8. package/dist/PerpsController.js.map +1 -1
  9. package/dist/constants/eventNames.d.ts +69 -9
  10. package/dist/constants/eventNames.d.ts.map +1 -1
  11. package/dist/constants/eventNames.js +82 -11
  12. package/dist/constants/eventNames.js.map +1 -1
  13. package/dist/constants/hyperLiquidConfig.d.ts +15 -0
  14. package/dist/constants/hyperLiquidConfig.d.ts.map +1 -1
  15. package/dist/constants/hyperLiquidConfig.js +14 -0
  16. package/dist/constants/hyperLiquidConfig.js.map +1 -1
  17. package/dist/constants/perpsConfig.d.ts +10 -2
  18. package/dist/constants/perpsConfig.d.ts.map +1 -1
  19. package/dist/constants/perpsConfig.js +11 -3
  20. package/dist/constants/perpsConfig.js.map +1 -1
  21. package/dist/index.d.ts +10 -8
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -2
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal/accountReturnOnEquity.d.ts +12 -0
  26. package/dist/internal/accountReturnOnEquity.d.ts.map +1 -0
  27. package/dist/internal/accountReturnOnEquity.js +18 -0
  28. package/dist/internal/accountReturnOnEquity.js.map +1 -0
  29. package/dist/internal/hyperLiquidHip3Rebalance.d.ts +14 -0
  30. package/dist/internal/hyperLiquidHip3Rebalance.d.ts.map +1 -0
  31. package/dist/internal/hyperLiquidHip3Rebalance.js +18 -0
  32. package/dist/internal/hyperLiquidHip3Rebalance.js.map +1 -0
  33. package/dist/internal/marketNumericValues.d.ts +10 -0
  34. package/dist/internal/marketNumericValues.d.ts.map +1 -0
  35. package/dist/internal/marketNumericValues.js +9 -0
  36. package/dist/internal/marketNumericValues.js.map +1 -0
  37. package/dist/internal/perpsDisplaySymbol.d.ts +12 -0
  38. package/dist/internal/perpsDisplaySymbol.d.ts.map +1 -0
  39. package/dist/internal/perpsDisplaySymbol.js +21 -0
  40. package/dist/internal/perpsDisplaySymbol.js.map +1 -0
  41. package/dist/perpsErrorCodes.d.ts +1 -0
  42. package/dist/perpsErrorCodes.d.ts.map +1 -1
  43. package/dist/perpsErrorCodes.js +1 -0
  44. package/dist/perpsErrorCodes.js.map +1 -1
  45. package/dist/providers/AggregatedPerpsProvider.d.ts +47 -1
  46. package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
  47. package/dist/providers/AggregatedPerpsProvider.js +140 -0
  48. package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
  49. package/dist/providers/HyperLiquidProvider.d.ts +72 -4
  50. package/dist/providers/HyperLiquidProvider.d.ts.map +1 -1
  51. package/dist/providers/HyperLiquidProvider.js +751 -159
  52. package/dist/providers/HyperLiquidProvider.js.map +1 -1
  53. package/dist/providers/LighterProvider.d.ts +2 -1
  54. package/dist/providers/LighterProvider.d.ts.map +1 -1
  55. package/dist/providers/LighterProvider.js +11 -6
  56. package/dist/providers/LighterProvider.js.map +1 -1
  57. package/dist/selectors.d.ts +15 -5
  58. package/dist/selectors.d.ts.map +1 -1
  59. package/dist/selectors.js +23 -5
  60. package/dist/selectors.js.map +1 -1
  61. package/dist/services/AggregatedOrderBookConnection.d.ts.map +1 -1
  62. package/dist/services/AggregatedOrderBookConnection.js +8 -10
  63. package/dist/services/AggregatedOrderBookConnection.js.map +1 -1
  64. package/dist/services/HyperLiquidClientService.d.ts +8 -0
  65. package/dist/services/HyperLiquidClientService.d.ts.map +1 -1
  66. package/dist/services/HyperLiquidClientService.js +65 -0
  67. package/dist/services/HyperLiquidClientService.js.map +1 -1
  68. package/dist/services/HyperLiquidSubscriptionService.d.ts +30 -1
  69. package/dist/services/HyperLiquidSubscriptionService.d.ts.map +1 -1
  70. package/dist/services/HyperLiquidSubscriptionService.js +117 -30
  71. package/dist/services/HyperLiquidSubscriptionService.js.map +1 -1
  72. package/dist/services/MarketDataService.d.ts +20 -3
  73. package/dist/services/MarketDataService.d.ts.map +1 -1
  74. package/dist/services/MarketDataService.js +55 -3
  75. package/dist/services/MarketDataService.js.map +1 -1
  76. package/dist/services/StreamChannelManager.d.ts +139 -0
  77. package/dist/services/StreamChannelManager.d.ts.map +1 -0
  78. package/dist/services/StreamChannelManager.js +475 -0
  79. package/dist/services/StreamChannelManager.js.map +1 -0
  80. package/dist/services/TerminalMarketService.d.ts.map +1 -1
  81. package/dist/services/TerminalMarketService.js +7 -0
  82. package/dist/services/TerminalMarketService.js.map +1 -1
  83. package/dist/services/agentSigner.d.ts +8 -0
  84. package/dist/services/agentSigner.d.ts.map +1 -1
  85. package/dist/services/agentSigner.js +13 -0
  86. package/dist/services/agentSigner.js.map +1 -1
  87. package/dist/types/hyperliquid-types.d.ts +9 -1
  88. package/dist/types/hyperliquid-types.d.ts.map +1 -1
  89. package/dist/types/hyperliquid-types.js +10 -0
  90. package/dist/types/hyperliquid-types.js.map +1 -1
  91. package/dist/types/index.d.ts +434 -17
  92. package/dist/types/index.d.ts.map +1 -1
  93. package/dist/types/index.js.map +1 -1
  94. package/dist/utils/accountAggregation.d.ts +17 -0
  95. package/dist/utils/accountAggregation.d.ts.map +1 -1
  96. package/dist/utils/accountAggregation.js +21 -5
  97. package/dist/utils/accountAggregation.js.map +1 -1
  98. package/dist/utils/accountUtils.d.ts +31 -10
  99. package/dist/utils/accountUtils.d.ts.map +1 -1
  100. package/dist/utils/accountUtils.js +68 -28
  101. package/dist/utils/accountUtils.js.map +1 -1
  102. package/dist/utils/closeCalculations.d.ts +17 -4
  103. package/dist/utils/closeCalculations.d.ts.map +1 -1
  104. package/dist/utils/closeCalculations.js +23 -7
  105. package/dist/utils/closeCalculations.js.map +1 -1
  106. package/dist/utils/hyperLiquidAdapter.d.ts +10 -1
  107. package/dist/utils/hyperLiquidAdapter.d.ts.map +1 -1
  108. package/dist/utils/hyperLiquidAdapter.js +17 -1
  109. package/dist/utils/hyperLiquidAdapter.js.map +1 -1
  110. package/dist/utils/hyperLiquidPositionPreview.d.ts +133 -9
  111. package/dist/utils/hyperLiquidPositionPreview.d.ts.map +1 -1
  112. package/dist/utils/hyperLiquidPositionPreview.js +264 -29
  113. package/dist/utils/hyperLiquidPositionPreview.js.map +1 -1
  114. package/dist/utils/index.d.ts +4 -2
  115. package/dist/utils/index.d.ts.map +1 -1
  116. package/dist/utils/index.js +1 -0
  117. package/dist/utils/index.js.map +1 -1
  118. package/dist/utils/lighterAdapter.d.ts.map +1 -1
  119. package/dist/utils/lighterAdapter.js +46 -9
  120. package/dist/utils/lighterAdapter.js.map +1 -1
  121. package/dist/utils/marketDataTransform.d.ts.map +1 -1
  122. package/dist/utils/marketDataTransform.js +13 -8
  123. package/dist/utils/marketDataTransform.js.map +1 -1
  124. package/dist/utils/marketUtils.d.ts +1 -2
  125. package/dist/utils/marketUtils.d.ts.map +1 -1
  126. package/dist/utils/marketUtils.js +7 -17
  127. package/dist/utils/marketUtils.js.map +1 -1
  128. package/dist/utils/orderBookGrouping.d.ts +35 -5
  129. package/dist/utils/orderBookGrouping.d.ts.map +1 -1
  130. package/dist/utils/orderBookGrouping.js +62 -6
  131. package/dist/utils/orderBookGrouping.js.map +1 -1
  132. package/dist/utils/perpsClientCopy.d.ts.map +1 -1
  133. package/dist/utils/perpsClientCopy.js +4 -0
  134. package/dist/utils/perpsClientCopy.js.map +1 -1
  135. package/dist/utils/perpsDiskPersistence.d.ts.map +1 -1
  136. package/dist/utils/perpsDiskPersistence.js +10 -0
  137. package/dist/utils/perpsDiskPersistence.js.map +1 -1
  138. package/dist/utils/perpsFormatters.d.ts +31 -0
  139. package/dist/utils/perpsFormatters.d.ts.map +1 -1
  140. package/dist/utils/perpsFormatters.js +26 -0
  141. package/dist/utils/perpsFormatters.js.map +1 -1
  142. package/dist/utils/priceDeviation.d.ts +9 -6
  143. package/dist/utils/priceDeviation.d.ts.map +1 -1
  144. package/dist/utils/priceDeviation.js +17 -26
  145. package/dist/utils/priceDeviation.js.map +1 -1
  146. package/dist/utils/sortMarkets.d.ts +13 -5
  147. package/dist/utils/sortMarkets.d.ts.map +1 -1
  148. package/dist/utils/sortMarkets.js +60 -38
  149. package/dist/utils/sortMarkets.js.map +1 -1
  150. package/docs/architecture.md +54 -24
  151. package/docs/client-contract.md +52 -27
  152. package/docs/contracts/events.md +38 -32
  153. package/docs/contracts/feature-flags.md +4 -4
  154. package/docs/formatting.md +2 -0
  155. package/docs/venues/lighter/capabilities.md +18 -15
  156. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import type { MarketInfo, PerpsProviderType, Position, PositionModifyPreviewParams, PositionModifyPreviewResult } from '../types/index.js';
1
+ import type { MarketInfo, PerpsProviderType, Position, PositionModifyPreviewKind, PositionModifyPreviewParams, PositionModifyPreviewResult } from '../types/index.js';
2
2
  export type HyperLiquidMarginTier = {
3
3
  /** Inclusive notional lower bound in USD. */
4
4
  lowerBound: number;
@@ -162,6 +162,47 @@ export type CrossMarginPosition = {
162
162
  * or tiers are unknown.
163
163
  */
164
164
  export declare function estimateCrossMaintenanceMargin(positions: readonly CrossMarginPosition[]): number | null;
165
+ /** Inputs to {@link estimateCrossLiquidationPrice}. */
166
+ export type CrossLiquidationPriceParams = {
167
+ isLong: boolean;
168
+ markPrice: number;
169
+ positionSize: number;
170
+ accountValue: number;
171
+ maintenanceMargin: number;
172
+ marginTiers: HyperLiquidMarginTier[] | null | undefined;
173
+ };
174
+ /**
175
+ * A Cross liquidation price, classified: a positive `price`,
176
+ * `covered_by_account` for a long that no positive price liquidates (the
177
+ * collateral covers its loss down to a zero price plus the other positions'
178
+ * maintenance), or `missing_inputs` when the inputs are unknown or produce
179
+ * no price.
180
+ */
181
+ export type CrossLiquidationClassification = {
182
+ status: 'price';
183
+ price: number;
184
+ } | {
185
+ status: 'covered_by_account';
186
+ } | {
187
+ status: 'missing_inputs';
188
+ };
189
+ /**
190
+ * Classifies the Cross liquidation price of one position, holding the
191
+ * account's other positions at their current value: the price at which
192
+ * account value falls to the maintenance margin. Within one tier this is
193
+ * HyperLiquid's documented
194
+ * `mark - side * (accountValue - maintenanceMargin) / size / (1 - mmr * side)`;
195
+ * the tier the position's notional reaches at that price is used when it
196
+ * differs from the current one. The one Cross
197
+ * liquidation formula: {@link estimateCrossLiquidationPrice}, the
198
+ * provider's estimates and the Cross preview all read it.
199
+ *
200
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/trading/liquidations
201
+ * @param params - Position geometry and the cross account's figures, as
202
+ * {@link estimateCrossLiquidationPrice} takes them.
203
+ * @returns The classified price.
204
+ */
205
+ export declare function classifyCrossLiquidationPrice(params: CrossLiquidationPriceParams): CrossLiquidationClassification;
165
206
  /**
166
207
  * Cross-margin liquidation price of one position, holding the account's
167
208
  * other positions at their current value: the price at which account value
@@ -183,14 +224,16 @@ export declare function estimateCrossMaintenanceMargin(positions: readonly Cross
183
224
  * positive one (a long with enough collateral to never be liquidated, or
184
225
  * unknown inputs).
185
226
  */
186
- export declare function estimateCrossLiquidationPrice(params: {
187
- isLong: boolean;
188
- markPrice: number;
189
- positionSize: number;
190
- accountValue: number;
191
- maintenanceMargin: number;
192
- marginTiers: HyperLiquidMarginTier[] | null | undefined;
193
- }): number | null;
227
+ export declare function estimateCrossLiquidationPrice(params: CrossLiquidationPriceParams): number | null;
228
+ /**
229
+ * Whether margin may leave an open isolated position on a market.
230
+ *
231
+ * @param market - The market, or undefined when unknown.
232
+ * @returns Whether removal is allowed, or undefined when the market or its
233
+ * margin mode is unknown, including an isolated-only market without
234
+ * `marginMode`, which may be strict isolated or `noCross`.
235
+ */
236
+ export declare const isMarginRemovalAllowed: (market: Pick<MarketInfo, 'marginMode' | 'onlyIsolated'> | undefined) => boolean | undefined;
194
237
  /**
195
238
  * An isolated position after its leverage changes with no size change: the
196
239
  * margin the change frees, and the margin and liquidation price afterwards.
@@ -248,5 +291,86 @@ export declare function estimateIsolatedLeverageChange({ position, providerId, m
248
291
  * @returns Discriminated preview; margin and liquidation are independently available.
249
292
  */
250
293
  export declare function previewHyperLiquidIsolatedPositionModify(params: PreviewHyperLiquidIsolatedPositionModifyParams): PositionModifyPreviewResult;
294
+ /**
295
+ * The cross account a {@link projectHyperLiquidCrossPositionModify} order
296
+ * draws on.
297
+ */
298
+ export type HyperLiquidCrossAccount = {
299
+ /** Cross collateral, as `getCrossCollateral` returns. */
300
+ accountValue: number;
301
+ /**
302
+ * Maintenance margin of every cross position, the modified one included at
303
+ * its current notional.
304
+ */
305
+ maintenanceMargin: number;
306
+ /**
307
+ * Standard account on a HIP-3 DEX only. Placement moves collateral into the
308
+ * DEX when its withdrawable balance is below the order's required margin,
309
+ * then returns the excess the order leaves (`calculateHip3RebalanceAmount`)
310
+ * to the source DEX. Omit on a Unified account and on the main DEX.
311
+ */
312
+ hip3Funding?: {
313
+ /** The DEX's withdrawable balance before the order. */
314
+ withdrawable: number;
315
+ /** Margin placement requires on the DEX before the order. */
316
+ requiredMargin: number;
317
+ };
318
+ };
319
+ export type ProjectHyperLiquidCrossPositionModifyParams = PositionModifyPreviewParams & {
320
+ /** This market's maintenance tiers. */
321
+ marginTiers?: HyperLiquidMarginTier[] | null;
322
+ /** `null` when unknown: margin is projected, liquidation is not. */
323
+ crossAccount: HyperLiquidCrossAccount | null;
324
+ };
325
+ /** A Cross position after an order, from {@link projectHyperLiquidCrossPositionModify}. */
326
+ export type HyperLiquidCrossPositionModifyProjection = {
327
+ status: 'none';
328
+ } | {
329
+ status: 'full_close';
330
+ resultingDirection: 'long' | 'short';
331
+ } | {
332
+ status: 'open';
333
+ kind: PositionModifyPreviewKind;
334
+ direction: 'long' | 'short';
335
+ size: number;
336
+ entryPrice: number;
337
+ /** Notional at the projected mark divided by the order leverage. */
338
+ margin: number;
339
+ /** The resulting position's liquidation price, classified. */
340
+ liquidation: CrossLiquidationClassification;
341
+ };
342
+ /**
343
+ * Projects the Cross position, flat or open, that would remain after a
344
+ * proposed order.
345
+ *
346
+ * The venue sets the asset's Cross leverage to the order leverage, so the
347
+ * resulting margin is the notional at the projected mark over that leverage.
348
+ * Liquidation holds the account's other positions at their current value:
349
+ * the account gains the existing size's move from its mark to the fill
350
+ * price, pays the fee on an add or flip, and replaces this position's
351
+ * maintenance with the resulting position's. A reduce ignores the fee, as the
352
+ * isolated preview does. On a Standard HIP-3 DEX that placement funds (see
353
+ * {@link HyperLiquidCrossAccount}), the DEX also receives the shortfall and
354
+ * returns the excess placement would rebalance away. Without the position
355
+ * value the current mark is unknown, so liquidation is `missing_inputs`.
356
+ *
357
+ * @param params - The position (size `'0'` for an opening), the proposed
358
+ * order, the market's tiers and the cross account.
359
+ * @returns The projection; `none` for an order that changes nothing or lacks
360
+ * an input.
361
+ */
362
+ export declare function projectHyperLiquidCrossPositionModify(params: ProjectHyperLiquidCrossPositionModifyParams): HyperLiquidCrossPositionModifyProjection;
363
+ /**
364
+ * Previews the Cross position that would remain after a proposed order, as
365
+ * {@link projectHyperLiquidCrossPositionModify} projects it, in the shape of
366
+ * the isolated preview. An unavailable resulting liquidation price carries
367
+ * `reason`: `covered_by_account` when no positive price liquidates the
368
+ * position, `missing_inputs` otherwise.
369
+ *
370
+ * @param params - The open Cross position, the proposed order, the market's
371
+ * tiers and the cross account.
372
+ * @returns Discriminated preview; `none` for a flat position.
373
+ */
374
+ export declare function previewHyperLiquidCrossPositionModify(params: ProjectHyperLiquidCrossPositionModifyParams): PositionModifyPreviewResult;
251
375
  export {};
252
376
  //# sourceMappingURL=hyperLiquidPositionPreview.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hyperLiquidPositionPreview.d.ts","sourceRoot":"","sources":["../../src/utils/hyperLiquidPositionPreview.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,U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QACZ,uDAAuD;QACvD,YAAY,EAAE,MAAM,CAAC;QACrB,6DAA6D;QAC7D,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,2CAA2C,GACrD,2BAA2B,GAAG;IAC5B,uCAAuC;IACvC,WAAW,CAAC,EAAE,qBAAqB,EAAE,GAAG,IAAI,CAAC;IAC7C,oEAAoE;IACpE,YAAY,EAAE,uBAAuB,GAAG,IAAI,CAAC;CAC9C,CAAC;AAEJ,2FAA2F;AAC3F,MAAM,MAAM,wCAAwC,GAChD;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAClB;IAAE,MAAM,EAAE,YAAY,CAAC;IAAC,kBAAkB,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAC9D;IACE,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,yBAAyB,CAAC;IAChC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,MAAM,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,WAAW,EAAE,8BAA8B,CAAC;CAC7C,CAAC;AAEN;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,EAAE,2CAA2C,GAClD,wCAAwC,CAuJ1C;AAeD;;;;;;;;;;GAUG;AACH,wBAAgB,qCAAqC,CACnD,MAAM,EAAE,2CAA2C,GAClD,2BAA2B,CA+B7B"}
@@ -1,4 +1,5 @@
1
1
  import { MARGIN_ADJUSTMENT_CONFIG } from '../constants/perpsConfig.js';
2
+ import { calculateHip3RebalanceAmount } from '../internal/hyperLiquidHip3Rebalance.js';
2
3
  /** Token-size comparison tolerance (floating-point / szDecimals noise). */
3
4
  const SIZE_EPSILON = 1e-10;
4
5
  /**
@@ -241,39 +242,46 @@ export function estimateCrossMaintenanceMargin(positions) {
241
242
  }
242
243
  return maintenanceMargin;
243
244
  }
245
+ const missingInputs = { status: 'missing_inputs' };
244
246
  /**
245
- * Cross-margin liquidation price of one position, holding the account's
246
- * other positions at their current value: the price at which account value
247
- * falls to the maintenance margin. Within one tier this is HyperLiquid's
248
- * documented `mark - side * (accountValue - maintenanceMargin) / size /
249
- * (1 - mmr * side)`; the tier the position's notional reaches at that price
250
- * is used when it differs from the current one.
247
+ * Classifies the Cross liquidation price of one position, holding the
248
+ * account's other positions at their current value: the price at which
249
+ * account value falls to the maintenance margin. Within one tier this is
250
+ * HyperLiquid's documented
251
+ * `mark - side * (accountValue - maintenanceMargin) / size / (1 - mmr * side)`;
252
+ * the tier the position's notional reaches at that price is used when it
253
+ * differs from the current one. The one Cross
254
+ * liquidation formula: {@link estimateCrossLiquidationPrice}, the
255
+ * provider's estimates and the Cross preview all read it.
251
256
  *
252
257
  * @see https://hyperliquid.gitbook.io/hyperliquid-docs/trading/liquidations
253
- * @param params - Position geometry and the cross account's figures.
254
- * @param params.isLong - Whether the position is long.
255
- * @param params.markPrice - Current mark price.
256
- * @param params.positionSize - Absolute position size in base units.
257
- * @param params.accountValue - Cross account value, unrealized PnL included.
258
- * @param params.maintenanceMargin - Maintenance margin of every cross
259
- * position including this one, as {@link estimateCrossMaintenanceMargin}.
260
- * @param params.marginTiers - This market's maintenance tiers.
261
- * @returns Liquidation price, or `null` when the inputs cannot produce a
262
- * positive one (a long with enough collateral to never be liquidated, or
263
- * unknown inputs).
258
+ * @param params - Position geometry and the cross account's figures, as
259
+ * {@link estimateCrossLiquidationPrice} takes them.
260
+ * @returns The classified price.
264
261
  */
265
- export function estimateCrossLiquidationPrice(params) {
262
+ export function classifyCrossLiquidationPrice(params) {
266
263
  const { isLong, markPrice, positionSize, accountValue, maintenanceMargin } = params;
267
264
  if (!isPositiveFinite(markPrice) ||
268
265
  !isPositiveFinite(positionSize) ||
269
266
  !Number.isFinite(accountValue) ||
270
267
  !isNonNegativeFinite(maintenanceMargin)) {
271
- return null;
268
+ return missingInputs;
272
269
  }
270
+ const currentMaintenance = estimateCrossMaintenanceMargin([
271
+ { notional: positionSize * markPrice, marginTiers: params.marginTiers },
272
+ ]);
273
+ if (currentMaintenance === null) {
274
+ return missingInputs;
275
+ }
276
+ const otherMaintenance = maintenanceMargin - currentMaintenance;
273
277
  const schedule = buildMaintenanceSchedule(params.marginTiers ?? []);
274
- const currentTier = findMaintenanceTier(schedule, positionSize * markPrice);
275
- if (!currentTier) {
276
- return null;
278
+ // A long loses at most its notional, and its maintenance falls to 0 with
279
+ // the price. Collateral still above the other positions' maintenance at a
280
+ // zero price leaves no positive liquidation price.
281
+ if (isLong &&
282
+ schedule[0].lowerBound === 0 &&
283
+ accountValue - positionSize * markPrice >= otherMaintenance) {
284
+ return { status: 'covered_by_account' };
277
285
  }
278
286
  const side = isLong ? 1 : -1;
279
287
  for (const tier of schedule) {
@@ -281,11 +289,10 @@ export function estimateCrossLiquidationPrice(params) {
281
289
  if (Math.abs(denominator) < Number.EPSILON) {
282
290
  continue;
283
291
  }
284
- const liquidationPrice = (maintenanceMargin -
292
+ const liquidationPrice = (otherMaintenance -
285
293
  accountValue +
286
- currentTier.maintenanceDeduction -
287
- tier.maintenanceDeduction +
288
- positionSize * markPrice * (side - currentTier.maintenanceMarginRate)) /
294
+ side * positionSize * markPrice -
295
+ tier.maintenanceDeduction) /
289
296
  denominator;
290
297
  if (!isPositiveFinite(liquidationPrice)) {
291
298
  continue;
@@ -293,10 +300,35 @@ export function estimateCrossLiquidationPrice(params) {
293
300
  const notionalAtLiquidation = positionSize * liquidationPrice;
294
301
  if (notionalAtLiquidation >= tier.lowerBound &&
295
302
  notionalAtLiquidation < tier.upperBound) {
296
- return liquidationPrice;
303
+ return { status: 'price', price: liquidationPrice };
297
304
  }
298
305
  }
299
- return null;
306
+ return missingInputs;
307
+ }
308
+ /**
309
+ * Cross-margin liquidation price of one position, holding the account's
310
+ * other positions at their current value: the price at which account value
311
+ * falls to the maintenance margin. Within one tier this is HyperLiquid's
312
+ * documented `mark - side * (accountValue - maintenanceMargin) / size /
313
+ * (1 - mmr * side)`; the tier the position's notional reaches at that price
314
+ * is used when it differs from the current one.
315
+ *
316
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/trading/liquidations
317
+ * @param params - Position geometry and the cross account's figures.
318
+ * @param params.isLong - Whether the position is long.
319
+ * @param params.markPrice - Current mark price.
320
+ * @param params.positionSize - Absolute position size in base units.
321
+ * @param params.accountValue - Cross account value, unrealized PnL included.
322
+ * @param params.maintenanceMargin - Maintenance margin of every cross
323
+ * position including this one, as {@link estimateCrossMaintenanceMargin}.
324
+ * @param params.marginTiers - This market's maintenance tiers.
325
+ * @returns Liquidation price, or `null` when the inputs cannot produce a
326
+ * positive one (a long with enough collateral to never be liquidated, or
327
+ * unknown inputs).
328
+ */
329
+ export function estimateCrossLiquidationPrice(params) {
330
+ const result = classifyCrossLiquidationPrice(params);
331
+ return result.status === 'price' ? result.price : null;
300
332
  }
301
333
  /**
302
334
  * Whether margin may leave an open isolated position on a market.
@@ -306,7 +338,7 @@ export function estimateCrossLiquidationPrice(params) {
306
338
  * margin mode is unknown, including an isolated-only market without
307
339
  * `marginMode`, which may be strict isolated or `noCross`.
308
340
  */
309
- const isMarginRemovalAllowed = (market) => {
341
+ export const isMarginRemovalAllowed = (market) => {
310
342
  if (market === undefined) {
311
343
  return undefined;
312
344
  }
@@ -610,4 +642,207 @@ export function previewHyperLiquidIsolatedPositionModify(params) {
610
642
  resultingDirection: openDirection,
611
643
  };
612
644
  }
645
+ /**
646
+ * Projects the Cross position, flat or open, that would remain after a
647
+ * proposed order.
648
+ *
649
+ * The venue sets the asset's Cross leverage to the order leverage, so the
650
+ * resulting margin is the notional at the projected mark over that leverage.
651
+ * Liquidation holds the account's other positions at their current value:
652
+ * the account gains the existing size's move from its mark to the fill
653
+ * price, pays the fee on an add or flip, and replaces this position's
654
+ * maintenance with the resulting position's. A reduce ignores the fee, as the
655
+ * isolated preview does. On a Standard HIP-3 DEX that placement funds (see
656
+ * {@link HyperLiquidCrossAccount}), the DEX also receives the shortfall and
657
+ * returns the excess placement would rebalance away. Without the position
658
+ * value the current mark is unknown, so liquidation is `missing_inputs`.
659
+ *
660
+ * @param params - The position (size `'0'` for an opening), the proposed
661
+ * order, the market's tiers and the cross account.
662
+ * @returns The projection; `none` for an order that changes nothing or lacks
663
+ * an input.
664
+ */
665
+ export function projectHyperLiquidCrossPositionModify(params) {
666
+ const { position, direction, reduceOnly = false, leverage } = params;
667
+ const { marginTiers, crossAccount } = params;
668
+ const signedSize = parseFiniteNumber(position.size);
669
+ const currentSize = Math.abs(signedSize);
670
+ const currentEntry = parseFiniteNumber(position.entryPrice);
671
+ const currentMargin = parseFiniteNumber(position.marginUsed);
672
+ const orderSize = parseFiniteNumber(params.size);
673
+ const orderPrice = parseFiniteNumber(params.price);
674
+ const fillPrice = isPositiveFinite(orderPrice) ? orderPrice : null;
675
+ const none = { status: 'none' };
676
+ if (!Number.isFinite(signedSize) ||
677
+ (currentSize > 0 &&
678
+ (!isPositiveFinite(currentEntry) ||
679
+ !isNonNegativeFinite(currentMargin))) ||
680
+ !isPositiveFinite(leverage) ||
681
+ !isPositiveFinite(orderSize)) {
682
+ return none;
683
+ }
684
+ const positionValue = parseFiniteNumber(position.positionValue);
685
+ // Without the position value the mark is unknown: margin is projected at
686
+ // entry, liquidation is not.
687
+ const currentMarkKnown = currentSize === 0 || isPositiveFinite(positionValue);
688
+ let currentNotional = 0;
689
+ if (currentSize > 0) {
690
+ currentNotional = isPositiveFinite(positionValue)
691
+ ? positionValue
692
+ : currentSize * currentEntry;
693
+ }
694
+ let openDirection = direction;
695
+ if (signedSize !== 0) {
696
+ openDirection = signedSize > 0 ? 'long' : 'short';
697
+ }
698
+ let resulting;
699
+ if (openDirection === direction) {
700
+ if (reduceOnly || fillPrice === null) {
701
+ return none;
702
+ }
703
+ const size = currentSize + orderSize;
704
+ resulting = {
705
+ kind: 'increase',
706
+ direction,
707
+ size,
708
+ entryPrice: ((currentSize > 0 ? currentSize * currentEntry : 0) +
709
+ orderSize * fillPrice) /
710
+ size,
711
+ markPrice: fillPrice,
712
+ };
713
+ }
714
+ else if (orderSize + SIZE_EPSILON < currentSize) {
715
+ resulting = {
716
+ kind: 'decrease',
717
+ direction: openDirection,
718
+ size: currentSize - orderSize,
719
+ entryPrice: currentEntry,
720
+ markPrice: fillPrice ?? currentNotional / currentSize,
721
+ };
722
+ }
723
+ else if (orderSize - currentSize > SIZE_EPSILON && !reduceOnly) {
724
+ if (fillPrice === null) {
725
+ return none;
726
+ }
727
+ resulting = {
728
+ kind: 'flip',
729
+ direction,
730
+ size: orderSize - currentSize,
731
+ entryPrice: fillPrice,
732
+ markPrice: fillPrice,
733
+ };
734
+ }
735
+ else {
736
+ return { status: 'full_close', resultingDirection: openDirection };
737
+ }
738
+ const margin = (resulting.size * resulting.markPrice) / leverage;
739
+ // An omitted, NaN or negative fee is unknown, never $0.
740
+ let feeAmountUsd = 0;
741
+ if (resulting.kind !== 'decrease') {
742
+ feeAmountUsd = isNonNegativeFinite(params.feeAmountUsd)
743
+ ? params.feeAmountUsd
744
+ : undefined;
745
+ }
746
+ const currentMaintenance = currentSize > 0
747
+ ? estimateCrossMaintenanceMargin([
748
+ { notional: currentNotional, marginTiers },
749
+ ])
750
+ : 0;
751
+ const resultingMaintenance = estimateCrossMaintenanceMargin([
752
+ { notional: resulting.size * resulting.markPrice, marginTiers },
753
+ ]);
754
+ let liquidation = missingInputs;
755
+ if (crossAccount &&
756
+ currentMarkKnown &&
757
+ feeAmountUsd !== undefined &&
758
+ currentMaintenance !== null &&
759
+ resultingMaintenance !== null) {
760
+ const { hip3Funding } = crossAccount;
761
+ const currentMark = currentSize > 0 ? currentNotional / currentSize : resulting.markPrice;
762
+ let accountValue = crossAccount.accountValue -
763
+ feeAmountUsd +
764
+ signedSize * (resulting.markPrice - currentMark);
765
+ if (hip3Funding && hip3Funding.withdrawable < hip3Funding.requiredMargin) {
766
+ // Placement transfers the shortfall in, then returns the excess the
767
+ // order leaves withdrawable, as `calculateHip3RebalanceAmount` decides.
768
+ accountValue += hip3Funding.requiredMargin - hip3Funding.withdrawable;
769
+ const committed = crossAccount.accountValue -
770
+ hip3Funding.withdrawable +
771
+ margin -
772
+ (currentSize > 0 ? currentMargin : 0);
773
+ accountValue -= calculateHip3RebalanceAmount({
774
+ postOrderBalance: accountValue - committed,
775
+ });
776
+ }
777
+ liquidation = classifyCrossLiquidationPrice({
778
+ isLong: resulting.direction === 'long',
779
+ markPrice: resulting.markPrice,
780
+ positionSize: resulting.size,
781
+ accountValue,
782
+ maintenanceMargin: crossAccount.maintenanceMargin -
783
+ currentMaintenance +
784
+ resultingMaintenance,
785
+ marginTiers,
786
+ });
787
+ }
788
+ return {
789
+ status: 'open',
790
+ kind: resulting.kind,
791
+ direction: resulting.direction,
792
+ size: resulting.size,
793
+ entryPrice: resulting.entryPrice,
794
+ margin,
795
+ liquidation,
796
+ };
797
+ }
798
+ /**
799
+ * A Cross liquidation estimate as a preview value.
800
+ *
801
+ * @param liquidation - As {@link classifyCrossLiquidationPrice} returns.
802
+ * @returns The price, or unavailable with the classification as `reason`.
803
+ */
804
+ const crossLiquidationPreview = (liquidation) => liquidation.status === 'price'
805
+ ? available(liquidation.price)
806
+ : { available: false, reason: liquidation.status };
807
+ /**
808
+ * Previews the Cross position that would remain after a proposed order, as
809
+ * {@link projectHyperLiquidCrossPositionModify} projects it, in the shape of
810
+ * the isolated preview. An unavailable resulting liquidation price carries
811
+ * `reason`: `covered_by_account` when no positive price liquidates the
812
+ * position, `missing_inputs` otherwise.
813
+ *
814
+ * @param params - The open Cross position, the proposed order, the market's
815
+ * tiers and the cross account.
816
+ * @returns Discriminated preview; `none` for a flat position.
817
+ */
818
+ export function previewHyperLiquidCrossPositionModify(params) {
819
+ const signedSize = parseFiniteNumber(params.position.size);
820
+ if (!Number.isFinite(signedSize) || signedSize === 0) {
821
+ return { status: 'none' };
822
+ }
823
+ const projection = projectHyperLiquidCrossPositionModify(params);
824
+ if (projection.status === 'none') {
825
+ return projection;
826
+ }
827
+ const current = currentFromPosition({
828
+ currentMargin: parseFiniteNumber(params.position.marginUsed),
829
+ currentLiquidationPrice: parseFiniteNumber(params.position.liquidationPrice),
830
+ });
831
+ if (projection.status === 'full_close') {
832
+ return { ...projection, current };
833
+ }
834
+ return {
835
+ status: 'open',
836
+ kind: projection.kind,
837
+ current,
838
+ resulting: {
839
+ direction: projection.direction,
840
+ size: projection.size,
841
+ entryPrice: projection.entryPrice,
842
+ leverage: params.leverage,
843
+ margin: available(projection.margin),
844
+ liquidationPrice: crossLiquidationPreview(projection.liquidation),
845
+ },
846
+ };
847
+ }
613
848
  //# sourceMappingURL=hyperLiquidPositionPreview.js.map