@metamask-previews/perps-controller 11.0.0-preview-9efd0b754 → 12.0.0-preview-4e64b66e8

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 (134) hide show
  1. package/CHANGELOG.md +30 -1
  2. package/dist/PerpsController-method-action-types.cjs.map +1 -1
  3. package/dist/PerpsController-method-action-types.d.cts +36 -1
  4. package/dist/PerpsController-method-action-types.d.cts.map +1 -1
  5. package/dist/PerpsController-method-action-types.d.mts +36 -1
  6. package/dist/PerpsController-method-action-types.d.mts.map +1 -1
  7. package/dist/PerpsController-method-action-types.mjs.map +1 -1
  8. package/dist/PerpsController.cjs +600 -119
  9. package/dist/PerpsController.cjs.map +1 -1
  10. package/dist/PerpsController.d.cts +35 -4
  11. package/dist/PerpsController.d.cts.map +1 -1
  12. package/dist/PerpsController.d.mts +35 -4
  13. package/dist/PerpsController.d.mts.map +1 -1
  14. package/dist/PerpsController.mjs +603 -122
  15. package/dist/PerpsController.mjs.map +1 -1
  16. package/dist/constants/eventNames.cjs +3 -0
  17. package/dist/constants/eventNames.cjs.map +1 -1
  18. package/dist/constants/eventNames.d.cts +1 -0
  19. package/dist/constants/eventNames.d.cts.map +1 -1
  20. package/dist/constants/eventNames.d.mts +1 -0
  21. package/dist/constants/eventNames.d.mts.map +1 -1
  22. package/dist/constants/eventNames.mjs +3 -0
  23. package/dist/constants/eventNames.mjs.map +1 -1
  24. package/dist/constants/hyperLiquidConfig.cjs +14 -2
  25. package/dist/constants/hyperLiquidConfig.cjs.map +1 -1
  26. package/dist/constants/hyperLiquidConfig.d.cts +7 -0
  27. package/dist/constants/hyperLiquidConfig.d.cts.map +1 -1
  28. package/dist/constants/hyperLiquidConfig.d.mts +7 -0
  29. package/dist/constants/hyperLiquidConfig.d.mts.map +1 -1
  30. package/dist/constants/hyperLiquidConfig.mjs +11 -0
  31. package/dist/constants/hyperLiquidConfig.mjs.map +1 -1
  32. package/dist/constants/perpsConfig.cjs +19 -2
  33. package/dist/constants/perpsConfig.cjs.map +1 -1
  34. package/dist/constants/perpsConfig.d.cts +39 -7
  35. package/dist/constants/perpsConfig.d.cts.map +1 -1
  36. package/dist/constants/perpsConfig.d.mts +39 -7
  37. package/dist/constants/perpsConfig.d.mts.map +1 -1
  38. package/dist/constants/perpsConfig.mjs +18 -1
  39. package/dist/constants/perpsConfig.mjs.map +1 -1
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +3 -3
  42. package/dist/index.d.cts.map +1 -1
  43. package/dist/index.d.mts +3 -3
  44. package/dist/index.d.mts.map +1 -1
  45. package/dist/index.mjs.map +1 -1
  46. package/dist/providers/AggregatedPerpsProvider.cjs +16 -0
  47. package/dist/providers/AggregatedPerpsProvider.cjs.map +1 -1
  48. package/dist/providers/AggregatedPerpsProvider.d.cts +3 -1
  49. package/dist/providers/AggregatedPerpsProvider.d.cts.map +1 -1
  50. package/dist/providers/AggregatedPerpsProvider.d.mts +3 -1
  51. package/dist/providers/AggregatedPerpsProvider.d.mts.map +1 -1
  52. package/dist/providers/AggregatedPerpsProvider.mjs +16 -0
  53. package/dist/providers/AggregatedPerpsProvider.mjs.map +1 -1
  54. package/dist/providers/HyperLiquidProvider.cjs +325 -59
  55. package/dist/providers/HyperLiquidProvider.cjs.map +1 -1
  56. package/dist/providers/HyperLiquidProvider.d.cts +27 -1
  57. package/dist/providers/HyperLiquidProvider.d.cts.map +1 -1
  58. package/dist/providers/HyperLiquidProvider.d.mts +27 -1
  59. package/dist/providers/HyperLiquidProvider.d.mts.map +1 -1
  60. package/dist/providers/HyperLiquidProvider.mjs +326 -60
  61. package/dist/providers/HyperLiquidProvider.mjs.map +1 -1
  62. package/dist/services/HyperLiquidClientService.cjs +74 -25
  63. package/dist/services/HyperLiquidClientService.cjs.map +1 -1
  64. package/dist/services/HyperLiquidClientService.d.cts.map +1 -1
  65. package/dist/services/HyperLiquidClientService.d.mts.map +1 -1
  66. package/dist/services/HyperLiquidClientService.mjs +74 -25
  67. package/dist/services/HyperLiquidClientService.mjs.map +1 -1
  68. package/dist/services/HyperLiquidSubscriptionService.cjs +16 -5
  69. package/dist/services/HyperLiquidSubscriptionService.cjs.map +1 -1
  70. package/dist/services/HyperLiquidSubscriptionService.d.cts +1 -1
  71. package/dist/services/HyperLiquidSubscriptionService.d.cts.map +1 -1
  72. package/dist/services/HyperLiquidSubscriptionService.d.mts +1 -1
  73. package/dist/services/HyperLiquidSubscriptionService.d.mts.map +1 -1
  74. package/dist/services/HyperLiquidSubscriptionService.mjs +16 -5
  75. package/dist/services/HyperLiquidSubscriptionService.mjs.map +1 -1
  76. package/dist/services/MarketDataService.cjs +49 -5
  77. package/dist/services/MarketDataService.cjs.map +1 -1
  78. package/dist/services/MarketDataService.d.cts +2 -2
  79. package/dist/services/MarketDataService.d.cts.map +1 -1
  80. package/dist/services/MarketDataService.d.mts +2 -2
  81. package/dist/services/MarketDataService.d.mts.map +1 -1
  82. package/dist/services/MarketDataService.mjs +49 -5
  83. package/dist/services/MarketDataService.mjs.map +1 -1
  84. package/dist/services/RewardsIntegrationService.cjs +330 -68
  85. package/dist/services/RewardsIntegrationService.cjs.map +1 -1
  86. package/dist/services/RewardsIntegrationService.d.cts +61 -5
  87. package/dist/services/RewardsIntegrationService.d.cts.map +1 -1
  88. package/dist/services/RewardsIntegrationService.d.mts +61 -5
  89. package/dist/services/RewardsIntegrationService.d.mts.map +1 -1
  90. package/dist/services/RewardsIntegrationService.mjs +330 -68
  91. package/dist/services/RewardsIntegrationService.mjs.map +1 -1
  92. package/dist/services/ServiceContext.cjs.map +1 -1
  93. package/dist/services/ServiceContext.d.cts +17 -1
  94. package/dist/services/ServiceContext.d.cts.map +1 -1
  95. package/dist/services/ServiceContext.d.mts +17 -1
  96. package/dist/services/ServiceContext.d.mts.map +1 -1
  97. package/dist/services/ServiceContext.mjs.map +1 -1
  98. package/dist/services/TerminalMarketService.cjs +422 -7
  99. package/dist/services/TerminalMarketService.cjs.map +1 -1
  100. package/dist/services/TerminalMarketService.d.cts +10 -1
  101. package/dist/services/TerminalMarketService.d.cts.map +1 -1
  102. package/dist/services/TerminalMarketService.d.mts +10 -1
  103. package/dist/services/TerminalMarketService.d.mts.map +1 -1
  104. package/dist/services/TerminalMarketService.mjs +423 -8
  105. package/dist/services/TerminalMarketService.mjs.map +1 -1
  106. package/dist/services/TradingService.cjs +52 -28
  107. package/dist/services/TradingService.cjs.map +1 -1
  108. package/dist/services/TradingService.d.cts.map +1 -1
  109. package/dist/services/TradingService.d.mts.map +1 -1
  110. package/dist/services/TradingService.mjs +52 -28
  111. package/dist/services/TradingService.mjs.map +1 -1
  112. package/dist/types/index.cjs.map +1 -1
  113. package/dist/types/index.d.cts +156 -7
  114. package/dist/types/index.d.cts.map +1 -1
  115. package/dist/types/index.d.mts +156 -7
  116. package/dist/types/index.d.mts.map +1 -1
  117. package/dist/types/index.mjs.map +1 -1
  118. package/dist/utils/marketUtils.cjs +16 -1
  119. package/dist/utils/marketUtils.cjs.map +1 -1
  120. package/dist/utils/marketUtils.d.cts +1 -0
  121. package/dist/utils/marketUtils.d.cts.map +1 -1
  122. package/dist/utils/marketUtils.d.mts +1 -0
  123. package/dist/utils/marketUtils.d.mts.map +1 -1
  124. package/dist/utils/marketUtils.mjs +14 -0
  125. package/dist/utils/marketUtils.mjs.map +1 -1
  126. package/dist/utils/perpsDiskPersistence.cjs +19 -12
  127. package/dist/utils/perpsDiskPersistence.cjs.map +1 -1
  128. package/dist/utils/perpsDiskPersistence.d.cts +5 -1
  129. package/dist/utils/perpsDiskPersistence.d.cts.map +1 -1
  130. package/dist/utils/perpsDiskPersistence.d.mts +5 -1
  131. package/dist/utils/perpsDiskPersistence.d.mts.map +1 -1
  132. package/dist/utils/perpsDiskPersistence.mjs +19 -12
  133. package/dist/utils/perpsDiskPersistence.mjs.map +1 -1
  134. package/package.json +1 -1
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _TradingService_instances, _TradingService_deps, _TradingService_controllerDeps, _TradingService_getErrorContext, _TradingService_buildAttributionProperties, _TradingService_trackSubmitted, _TradingService_trackOrderResult, _TradingService_handleOrderSuccess, _TradingService_withFeeDiscount, _TradingService_loadPositionData, _TradingService_calculateCloseMetrics, _TradingService_buildCloseEventProperties, _TradingService_trackPositionCloseResult, _TradingService_handleDataLakeReporting, _TradingService_calculateFeeDiscountWithMeasurement;
13
+ var _TradingService_instances, _TradingService_deps, _TradingService_controllerDeps, _TradingService_feeContextTail, _TradingService_getErrorContext, _TradingService_buildAttributionProperties, _TradingService_trackSubmitted, _TradingService_trackOrderResult, _TradingService_handleOrderSuccess, _TradingService_withFeeDiscount, _TradingService_loadPositionData, _TradingService_calculateCloseMetrics, _TradingService_buildCloseEventProperties, _TradingService_trackPositionCloseResult, _TradingService_handleDataLakeReporting, _TradingService_calculateFeeDiscountWithMeasurement;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.TradingService = void 0;
16
16
  const bignumber_js_1 = require("bignumber.js");
@@ -49,6 +49,8 @@ class TradingService {
49
49
  * Set via setControllerDependencies() after construction.
50
50
  */
51
51
  _TradingService_controllerDeps.set(this, null);
52
+ /** Serializes provider fee context so concurrent orders cannot share it. */
53
+ _TradingService_feeContextTail.set(this, Promise.resolve());
52
54
  __classPrivateFieldSet(this, _TradingService_deps, deps, "f");
53
55
  }
54
56
  /**
@@ -112,10 +114,11 @@ class TradingService {
112
114
  },
113
115
  });
114
116
  // Calculate fee discount at execution time (fresh, secure)
115
- const feeDiscountBips = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
116
- __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee discount calculated', {
117
- feeDiscountBips,
118
- hasDiscount: feeDiscountBips !== undefined,
117
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
118
+ __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee resolution calculated', {
119
+ feeDiscountBips: feeResolution?.discountBips,
120
+ feeSource: feeResolution?.source,
121
+ hasDiscount: feeResolution?.discountBips !== undefined,
119
122
  });
120
123
  __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Submitting order to provider', {
121
124
  symbol: params.symbol,
@@ -165,7 +168,7 @@ class TradingService {
165
168
  }, perpsConfig_js_1.PERPS_CONSTANTS.PlaceOrderTimeoutMs);
166
169
  const result = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
167
170
  provider,
168
- feeDiscountBips,
171
+ feeResolution,
169
172
  operation: () => provider.placeOrder(params),
170
173
  });
171
174
  if (orderSubmissionThresholdTimeoutId !== undefined) {
@@ -292,11 +295,11 @@ class TradingService {
292
295
  },
293
296
  });
294
297
  // Calculate fee discount only if required dependencies are available
295
- const feeDiscountBips = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
298
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
296
299
  // Execute order edit with fee discount management
297
300
  const result = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
298
301
  provider,
299
- feeDiscountBips,
302
+ feeResolution,
300
303
  operation: () => provider.editOrder(params),
301
304
  });
302
305
  const completionDuration = __classPrivateFieldGet(this, _TradingService_deps, "f").performance.now() - startTime;
@@ -686,11 +689,11 @@ class TradingService {
686
689
  }),
687
690
  });
688
691
  // Calculate fee discount with measurement
689
- const feeDiscountBips = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
692
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
690
693
  // Execute position close with fee discount management
691
694
  result = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
692
695
  provider,
693
- feeDiscountBips,
696
+ feeResolution,
694
697
  operation: () => provider.closePosition(params),
695
698
  });
696
699
  const completionDuration = __classPrivateFieldGet(this, _TradingService_deps, "f").performance.now() - startTime;
@@ -807,10 +810,10 @@ class TradingService {
807
810
  });
808
811
  // Use batch close if provider supports it (provider handles filtering)
809
812
  if (provider.closePositions) {
810
- const feeDiscountBips = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
813
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
811
814
  operationResult = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
812
815
  provider,
813
- feeDiscountBips,
816
+ feeResolution,
814
817
  operation: async () => {
815
818
  if (!provider.closePositions) {
816
819
  throw new Error('closePositions method not available');
@@ -970,11 +973,11 @@ class TradingService {
970
973
  ...__classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_buildAttributionProperties).call(this, params.trackingData),
971
974
  });
972
975
  // Get fee discount from rewards
973
- const feeDiscountBips = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
976
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
974
977
  // Execute with fee discount management
975
978
  result = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
976
979
  provider,
977
- feeDiscountBips,
980
+ feeResolution,
978
981
  operation: () => provider.updatePositionTPSL(params),
979
982
  });
980
983
  if (result.success) {
@@ -1221,8 +1224,13 @@ class TradingService {
1221
1224
  [eventNames_js_1.PERPS_EVENT_PROPERTY.ACTION]: flipAction,
1222
1225
  ...__classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_buildAttributionProperties).call(this, trackingData),
1223
1226
  });
1227
+ const feeResolution = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_calculateFeeDiscountWithMeasurement).call(this);
1224
1228
  // Place flip order (HyperLiquid handles margin transfer automatically)
1225
- const result = await provider.placeOrder(orderParams);
1229
+ const result = await __classPrivateFieldGet(this, _TradingService_instances, "m", _TradingService_withFeeDiscount).call(this, {
1230
+ provider,
1231
+ feeResolution,
1232
+ operation: () => provider.placeOrder(orderParams),
1233
+ });
1226
1234
  const completionDuration = __classPrivateFieldGet(this, _TradingService_deps, "f").performance.now() - startTime;
1227
1235
  const executedPrice = parseFloat(result.averagePrice ?? position.entryPrice);
1228
1236
  if (result.success) {
@@ -1319,7 +1327,7 @@ class TradingService {
1319
1327
  }
1320
1328
  }
1321
1329
  exports.TradingService = TradingService;
1322
- _TradingService_deps = new WeakMap(), _TradingService_controllerDeps = new WeakMap(), _TradingService_instances = new WeakSet(), _TradingService_getErrorContext = function _TradingService_getErrorContext(method, additionalContext) {
1330
+ _TradingService_deps = new WeakMap(), _TradingService_controllerDeps = new WeakMap(), _TradingService_feeContextTail = new WeakMap(), _TradingService_instances = new WeakSet(), _TradingService_getErrorContext = function _TradingService_getErrorContext(method, additionalContext) {
1323
1331
  return {
1324
1332
  controller: 'TradingService',
1325
1333
  method,
@@ -1554,18 +1562,29 @@ async function _TradingService_handleOrderSuccess(options) {
1554
1562
  *
1555
1563
  * @param options - The configuration options.
1556
1564
  * @param options.provider - The perps provider instance.
1557
- * @param options.feeDiscountBips - The fee discount bips value.
1565
+ * @param options.feeResolution - The resolved fee and attribution source.
1558
1566
  * @param options.operation - The operation value.
1559
1567
  * @returns The result of the operation.
1560
1568
  */
1561
1569
  async function _TradingService_withFeeDiscount(options) {
1562
- const { provider, feeDiscountBips, operation } = options;
1570
+ const { provider, feeResolution, operation } = options;
1571
+ const previous = __classPrivateFieldGet(this, _TradingService_feeContextTail, "f");
1572
+ let release = () => undefined;
1573
+ __classPrivateFieldSet(this, _TradingService_feeContextTail, new Promise((resolve) => {
1574
+ release = resolve;
1575
+ }), "f");
1576
+ await previous;
1563
1577
  try {
1564
- // Set discount context in provider for this operation
1565
- if (feeDiscountBips !== undefined && provider.setUserFeeDiscount) {
1566
- provider.setUserFeeDiscount(feeDiscountBips);
1567
- __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee discount set in provider', {
1568
- feeDiscountBips,
1578
+ if (provider.setUserFeeResolution) {
1579
+ provider.setUserFeeResolution(feeResolution);
1580
+ }
1581
+ else if (provider.setUserFeeDiscount) {
1582
+ provider.setUserFeeDiscount(feeResolution?.discountBips);
1583
+ }
1584
+ if (feeResolution) {
1585
+ __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee resolution set in provider', {
1586
+ feeDiscountBips: feeResolution.discountBips,
1587
+ feeSource: feeResolution.source,
1569
1588
  });
1570
1589
  }
1571
1590
  // Execute the operation
@@ -1573,10 +1592,14 @@ async function _TradingService_withFeeDiscount(options) {
1573
1592
  }
1574
1593
  finally {
1575
1594
  // Always clear discount context, even on exception
1576
- if (provider.setUserFeeDiscount) {
1595
+ if (provider.setUserFeeResolution) {
1596
+ provider.setUserFeeResolution(undefined);
1597
+ }
1598
+ else if (provider.setUserFeeDiscount) {
1577
1599
  provider.setUserFeeDiscount(undefined);
1578
- __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee discount cleared from provider');
1579
1600
  }
1601
+ __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee resolution cleared from provider');
1602
+ release();
1580
1603
  }
1581
1604
  }, _TradingService_loadPositionData =
1582
1605
  /**
@@ -1828,14 +1851,15 @@ async function _TradingService_calculateFeeDiscountWithMeasurement() {
1828
1851
  const { rewardsIntegrationService } = __classPrivateFieldGet(this, _TradingService_controllerDeps, "f");
1829
1852
  const orderExecutionFeeDiscountStartTime = __classPrivateFieldGet(this, _TradingService_deps, "f").performance.now();
1830
1853
  // Calculate fee discount using messenger pattern (service handles controller access internally)
1831
- const discountBips = await rewardsIntegrationService.calculateUserFeeDiscount();
1854
+ const resolution = await rewardsIntegrationService.resolveFee();
1832
1855
  const orderExecutionFeeDiscountDuration = __classPrivateFieldGet(this, _TradingService_deps, "f").performance.now() - orderExecutionFeeDiscountStartTime;
1833
1856
  // Record measurement
1834
1857
  __classPrivateFieldGet(this, _TradingService_deps, "f").tracer.setMeasurement(performanceMetrics_js_1.PerpsMeasurementName.PerpsRewardsOrderExecutionFeeDiscountApiCall, orderExecutionFeeDiscountDuration, 'millisecond');
1835
1858
  __classPrivateFieldGet(this, _TradingService_deps, "f").debugLogger.log('TradingService: Fee discount API call completed', {
1836
- discountBips,
1859
+ discountBips: resolution.discountBips,
1860
+ source: resolution.source,
1837
1861
  duration: `${orderExecutionFeeDiscountDuration.toFixed(0)}ms`,
1838
1862
  });
1839
- return discountBips;
1863
+ return resolution;
1840
1864
  };
1841
1865
  //# sourceMappingURL=TradingService.cjs.map