@metamask-previews/perps-controller 0.0.0-preview-e4aa1532 → 0.0.0-preview-6dfa4aeba

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 (498) hide show
  1. package/CHANGELOG.md +1 -11
  2. package/dist/PerpsController.cjs +66 -2224
  3. package/dist/PerpsController.cjs.map +1 -1
  4. package/dist/PerpsController.d.cts +79 -933
  5. package/dist/PerpsController.d.cts.map +1 -1
  6. package/dist/PerpsController.d.mts +79 -933
  7. package/dist/PerpsController.d.mts.map +1 -1
  8. package/dist/PerpsController.mjs +64 -2221
  9. package/dist/PerpsController.mjs.map +1 -1
  10. package/dist/index.cjs +1 -32
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +2 -6
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +2 -6
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +1 -9
  17. package/dist/index.mjs.map +1 -1
  18. package/package.json +4 -19
  19. package/dist/__mocks__/hyperliquidMock.cjs +0 -68
  20. package/dist/__mocks__/hyperliquidMock.cjs.map +0 -1
  21. package/dist/__mocks__/hyperliquidMock.d.cts +0 -15
  22. package/dist/__mocks__/hyperliquidMock.d.cts.map +0 -1
  23. package/dist/__mocks__/hyperliquidMock.d.mts +0 -15
  24. package/dist/__mocks__/hyperliquidMock.d.mts.map +0 -1
  25. package/dist/__mocks__/hyperliquidMock.mjs +0 -65
  26. package/dist/__mocks__/hyperliquidMock.mjs.map +0 -1
  27. package/dist/__mocks__/providerMocks.cjs +0 -144
  28. package/dist/__mocks__/providerMocks.cjs.map +0 -1
  29. package/dist/__mocks__/providerMocks.d.cts +0 -44
  30. package/dist/__mocks__/providerMocks.d.cts.map +0 -1
  31. package/dist/__mocks__/providerMocks.d.mts +0 -44
  32. package/dist/__mocks__/providerMocks.d.mts.map +0 -1
  33. package/dist/__mocks__/providerMocks.mjs +0 -136
  34. package/dist/__mocks__/providerMocks.mjs.map +0 -1
  35. package/dist/__mocks__/serviceMocks.cjs +0 -187
  36. package/dist/__mocks__/serviceMocks.cjs.map +0 -1
  37. package/dist/__mocks__/serviceMocks.d.cts +0 -58
  38. package/dist/__mocks__/serviceMocks.d.cts.map +0 -1
  39. package/dist/__mocks__/serviceMocks.d.mts +0 -58
  40. package/dist/__mocks__/serviceMocks.d.mts.map +0 -1
  41. package/dist/__mocks__/serviceMocks.mjs +0 -180
  42. package/dist/__mocks__/serviceMocks.mjs.map +0 -1
  43. package/dist/aggregation/SubscriptionMultiplexer.cjs +0 -459
  44. package/dist/aggregation/SubscriptionMultiplexer.cjs.map +0 -1
  45. package/dist/aggregation/SubscriptionMultiplexer.d.cts +0 -222
  46. package/dist/aggregation/SubscriptionMultiplexer.d.cts.map +0 -1
  47. package/dist/aggregation/SubscriptionMultiplexer.d.mts +0 -222
  48. package/dist/aggregation/SubscriptionMultiplexer.d.mts.map +0 -1
  49. package/dist/aggregation/SubscriptionMultiplexer.mjs +0 -455
  50. package/dist/aggregation/SubscriptionMultiplexer.mjs.map +0 -1
  51. package/dist/aggregation/index.cjs +0 -9
  52. package/dist/aggregation/index.cjs.map +0 -1
  53. package/dist/aggregation/index.d.cts +0 -6
  54. package/dist/aggregation/index.d.cts.map +0 -1
  55. package/dist/aggregation/index.d.mts +0 -6
  56. package/dist/aggregation/index.d.mts.map +0 -1
  57. package/dist/aggregation/index.mjs +0 -5
  58. package/dist/aggregation/index.mjs.map +0 -1
  59. package/dist/constants/eventNames.cjs +0 -117
  60. package/dist/constants/eventNames.cjs.map +0 -1
  61. package/dist/constants/eventNames.d.cts +0 -106
  62. package/dist/constants/eventNames.d.cts.map +0 -1
  63. package/dist/constants/eventNames.d.mts +0 -106
  64. package/dist/constants/eventNames.d.mts.map +0 -1
  65. package/dist/constants/eventNames.mjs +0 -114
  66. package/dist/constants/eventNames.mjs.map +0 -1
  67. package/dist/constants/hyperLiquidConfig.cjs +0 -366
  68. package/dist/constants/hyperLiquidConfig.cjs.map +0 -1
  69. package/dist/constants/hyperLiquidConfig.d.cts +0 -276
  70. package/dist/constants/hyperLiquidConfig.d.cts.map +0 -1
  71. package/dist/constants/hyperLiquidConfig.d.mts +0 -276
  72. package/dist/constants/hyperLiquidConfig.d.mts.map +0 -1
  73. package/dist/constants/hyperLiquidConfig.mjs +0 -357
  74. package/dist/constants/hyperLiquidConfig.mjs.map +0 -1
  75. package/dist/constants/index.cjs +0 -23
  76. package/dist/constants/index.cjs.map +0 -1
  77. package/dist/constants/index.d.cts +0 -7
  78. package/dist/constants/index.d.cts.map +0 -1
  79. package/dist/constants/index.d.mts +0 -7
  80. package/dist/constants/index.d.mts.map +0 -1
  81. package/dist/constants/index.mjs +0 -7
  82. package/dist/constants/index.mjs.map +0 -1
  83. package/dist/constants/orderTypes.cjs +0 -31
  84. package/dist/constants/orderTypes.cjs.map +0 -1
  85. package/dist/constants/orderTypes.d.cts +0 -19
  86. package/dist/constants/orderTypes.d.cts.map +0 -1
  87. package/dist/constants/orderTypes.d.mts +0 -19
  88. package/dist/constants/orderTypes.d.mts.map +0 -1
  89. package/dist/constants/orderTypes.mjs +0 -27
  90. package/dist/constants/orderTypes.mjs.map +0 -1
  91. package/dist/constants/performanceMetrics.cjs +0 -61
  92. package/dist/constants/performanceMetrics.cjs.map +0 -1
  93. package/dist/constants/performanceMetrics.d.cts +0 -44
  94. package/dist/constants/performanceMetrics.d.cts.map +0 -1
  95. package/dist/constants/performanceMetrics.d.mts +0 -44
  96. package/dist/constants/performanceMetrics.d.mts.map +0 -1
  97. package/dist/constants/performanceMetrics.mjs +0 -58
  98. package/dist/constants/performanceMetrics.mjs.map +0 -1
  99. package/dist/constants/perpsConfig.cjs +0 -461
  100. package/dist/constants/perpsConfig.cjs.map +0 -1
  101. package/dist/constants/perpsConfig.d.cts +0 -343
  102. package/dist/constants/perpsConfig.d.cts.map +0 -1
  103. package/dist/constants/perpsConfig.d.mts +0 -343
  104. package/dist/constants/perpsConfig.d.mts.map +0 -1
  105. package/dist/constants/perpsConfig.mjs +0 -458
  106. package/dist/constants/perpsConfig.mjs.map +0 -1
  107. package/dist/constants/perpsErrorCodes.cjs +0 -79
  108. package/dist/constants/perpsErrorCodes.cjs.map +0 -1
  109. package/dist/constants/perpsErrorCodes.d.cts +0 -64
  110. package/dist/constants/perpsErrorCodes.d.cts.map +0 -1
  111. package/dist/constants/perpsErrorCodes.d.mts +0 -64
  112. package/dist/constants/perpsErrorCodes.d.mts.map +0 -1
  113. package/dist/constants/perpsErrorCodes.mjs +0 -76
  114. package/dist/constants/perpsErrorCodes.mjs.map +0 -1
  115. package/dist/platform-services/HyperLiquidClientService.cjs +0 -857
  116. package/dist/platform-services/HyperLiquidClientService.cjs.map +0 -1
  117. package/dist/platform-services/HyperLiquidClientService.d.cts +0 -263
  118. package/dist/platform-services/HyperLiquidClientService.d.cts.map +0 -1
  119. package/dist/platform-services/HyperLiquidClientService.d.mts +0 -263
  120. package/dist/platform-services/HyperLiquidClientService.d.mts.map +0 -1
  121. package/dist/platform-services/HyperLiquidClientService.mjs +0 -853
  122. package/dist/platform-services/HyperLiquidClientService.mjs.map +0 -1
  123. package/dist/platform-services/HyperLiquidSubscriptionService.cjs +0 -2170
  124. package/dist/platform-services/HyperLiquidSubscriptionService.cjs.map +0 -1
  125. package/dist/platform-services/HyperLiquidSubscriptionService.d.cts +0 -424
  126. package/dist/platform-services/HyperLiquidSubscriptionService.d.cts.map +0 -1
  127. package/dist/platform-services/HyperLiquidSubscriptionService.d.mts +0 -424
  128. package/dist/platform-services/HyperLiquidSubscriptionService.d.mts.map +0 -1
  129. package/dist/platform-services/HyperLiquidSubscriptionService.mjs +0 -2166
  130. package/dist/platform-services/HyperLiquidSubscriptionService.mjs.map +0 -1
  131. package/dist/platform-services/HyperLiquidWalletService.cjs +0 -140
  132. package/dist/platform-services/HyperLiquidWalletService.cjs.map +0 -1
  133. package/dist/platform-services/HyperLiquidWalletService.d.cts +0 -78
  134. package/dist/platform-services/HyperLiquidWalletService.d.cts.map +0 -1
  135. package/dist/platform-services/HyperLiquidWalletService.d.mts +0 -78
  136. package/dist/platform-services/HyperLiquidWalletService.d.mts.map +0 -1
  137. package/dist/platform-services/HyperLiquidWalletService.mjs +0 -136
  138. package/dist/platform-services/HyperLiquidWalletService.mjs.map +0 -1
  139. package/dist/providers/AggregatedPerpsProvider.cjs +0 -777
  140. package/dist/providers/AggregatedPerpsProvider.cjs.map +0 -1
  141. package/dist/providers/AggregatedPerpsProvider.d.cts +0 -466
  142. package/dist/providers/AggregatedPerpsProvider.d.cts.map +0 -1
  143. package/dist/providers/AggregatedPerpsProvider.d.mts +0 -466
  144. package/dist/providers/AggregatedPerpsProvider.d.mts.map +0 -1
  145. package/dist/providers/AggregatedPerpsProvider.mjs +0 -773
  146. package/dist/providers/AggregatedPerpsProvider.mjs.map +0 -1
  147. package/dist/providers/HyperLiquidProvider.cjs +0 -5172
  148. package/dist/providers/HyperLiquidProvider.cjs.map +0 -1
  149. package/dist/providers/HyperLiquidProvider.d.cts +0 -881
  150. package/dist/providers/HyperLiquidProvider.d.cts.map +0 -1
  151. package/dist/providers/HyperLiquidProvider.d.mts +0 -881
  152. package/dist/providers/HyperLiquidProvider.d.mts.map +0 -1
  153. package/dist/providers/HyperLiquidProvider.mjs +0 -5168
  154. package/dist/providers/HyperLiquidProvider.mjs.map +0 -1
  155. package/dist/providers/index.cjs +0 -11
  156. package/dist/providers/index.cjs.map +0 -1
  157. package/dist/providers/index.d.cts +0 -6
  158. package/dist/providers/index.d.cts.map +0 -1
  159. package/dist/providers/index.d.mts +0 -6
  160. package/dist/providers/index.d.mts.map +0 -1
  161. package/dist/providers/index.mjs +0 -6
  162. package/dist/providers/index.mjs.map +0 -1
  163. package/dist/routing/ProviderRouter.cjs +0 -148
  164. package/dist/routing/ProviderRouter.cjs.map +0 -1
  165. package/dist/routing/ProviderRouter.d.cts +0 -124
  166. package/dist/routing/ProviderRouter.d.cts.map +0 -1
  167. package/dist/routing/ProviderRouter.d.mts +0 -124
  168. package/dist/routing/ProviderRouter.d.mts.map +0 -1
  169. package/dist/routing/ProviderRouter.mjs +0 -144
  170. package/dist/routing/ProviderRouter.mjs.map +0 -1
  171. package/dist/routing/index.cjs +0 -9
  172. package/dist/routing/index.cjs.map +0 -1
  173. package/dist/routing/index.d.cts +0 -6
  174. package/dist/routing/index.d.cts.map +0 -1
  175. package/dist/routing/index.d.mts +0 -6
  176. package/dist/routing/index.d.mts.map +0 -1
  177. package/dist/routing/index.mjs +0 -5
  178. package/dist/routing/index.mjs.map +0 -1
  179. package/dist/selectors.cjs +0 -161
  180. package/dist/selectors.cjs.map +0 -1
  181. package/dist/selectors.d.cts +0 -345
  182. package/dist/selectors.d.cts.map +0 -1
  183. package/dist/selectors.d.mts +0 -345
  184. package/dist/selectors.d.mts.map +0 -1
  185. package/dist/selectors.mjs +0 -153
  186. package/dist/selectors.mjs.map +0 -1
  187. package/dist/services/AccountService.cjs +0 -313
  188. package/dist/services/AccountService.cjs.map +0 -1
  189. package/dist/services/AccountService.d.cts +0 -53
  190. package/dist/services/AccountService.d.cts.map +0 -1
  191. package/dist/services/AccountService.d.mts +0 -53
  192. package/dist/services/AccountService.d.mts.map +0 -1
  193. package/dist/services/AccountService.mjs +0 -309
  194. package/dist/services/AccountService.mjs.map +0 -1
  195. package/dist/services/DataLakeService.cjs +0 -219
  196. package/dist/services/DataLakeService.cjs.map +0 -1
  197. package/dist/services/DataLakeService.d.cts +0 -49
  198. package/dist/services/DataLakeService.d.cts.map +0 -1
  199. package/dist/services/DataLakeService.d.mts +0 -49
  200. package/dist/services/DataLakeService.d.mts.map +0 -1
  201. package/dist/services/DataLakeService.mjs +0 -215
  202. package/dist/services/DataLakeService.mjs.map +0 -1
  203. package/dist/services/DepositService.cjs +0 -96
  204. package/dist/services/DepositService.cjs.map +0 -1
  205. package/dist/services/DepositService.d.cts +0 -37
  206. package/dist/services/DepositService.d.cts.map +0 -1
  207. package/dist/services/DepositService.d.mts +0 -37
  208. package/dist/services/DepositService.d.mts.map +0 -1
  209. package/dist/services/DepositService.mjs +0 -92
  210. package/dist/services/DepositService.mjs.map +0 -1
  211. package/dist/services/EligibilityService.cjs +0 -156
  212. package/dist/services/EligibilityService.cjs.map +0 -1
  213. package/dist/services/EligibilityService.d.cts +0 -48
  214. package/dist/services/EligibilityService.d.cts.map +0 -1
  215. package/dist/services/EligibilityService.d.mts +0 -48
  216. package/dist/services/EligibilityService.d.mts.map +0 -1
  217. package/dist/services/EligibilityService.mjs +0 -152
  218. package/dist/services/EligibilityService.mjs.map +0 -1
  219. package/dist/services/FeatureFlagConfigurationService.cjs +0 -258
  220. package/dist/services/FeatureFlagConfigurationService.cjs.map +0 -1
  221. package/dist/services/FeatureFlagConfigurationService.d.cts +0 -100
  222. package/dist/services/FeatureFlagConfigurationService.d.cts.map +0 -1
  223. package/dist/services/FeatureFlagConfigurationService.d.mts +0 -100
  224. package/dist/services/FeatureFlagConfigurationService.d.mts.map +0 -1
  225. package/dist/services/FeatureFlagConfigurationService.mjs +0 -254
  226. package/dist/services/FeatureFlagConfigurationService.mjs.map +0 -1
  227. package/dist/services/MarketDataService.cjs +0 -875
  228. package/dist/services/MarketDataService.cjs.map +0 -1
  229. package/dist/services/MarketDataService.d.cts +0 -289
  230. package/dist/services/MarketDataService.d.cts.map +0 -1
  231. package/dist/services/MarketDataService.d.mts +0 -289
  232. package/dist/services/MarketDataService.d.mts.map +0 -1
  233. package/dist/services/MarketDataService.mjs +0 -871
  234. package/dist/services/MarketDataService.mjs.map +0 -1
  235. package/dist/services/RewardsIntegrationService.cjs +0 -94
  236. package/dist/services/RewardsIntegrationService.cjs.map +0 -1
  237. package/dist/services/RewardsIntegrationService.d.cts +0 -30
  238. package/dist/services/RewardsIntegrationService.d.cts.map +0 -1
  239. package/dist/services/RewardsIntegrationService.d.mts +0 -30
  240. package/dist/services/RewardsIntegrationService.d.mts.map +0 -1
  241. package/dist/services/RewardsIntegrationService.mjs +0 -90
  242. package/dist/services/RewardsIntegrationService.mjs.map +0 -1
  243. package/dist/services/ServiceContext.cjs +0 -3
  244. package/dist/services/ServiceContext.cjs.map +0 -1
  245. package/dist/services/ServiceContext.d.cts +0 -110
  246. package/dist/services/ServiceContext.d.cts.map +0 -1
  247. package/dist/services/ServiceContext.d.mts +0 -110
  248. package/dist/services/ServiceContext.d.mts.map +0 -1
  249. package/dist/services/ServiceContext.mjs +0 -2
  250. package/dist/services/ServiceContext.mjs.map +0 -1
  251. package/dist/services/TradingService.cjs +0 -1481
  252. package/dist/services/TradingService.cjs.map +0 -1
  253. package/dist/services/TradingService.d.cts +0 -308
  254. package/dist/services/TradingService.d.cts.map +0 -1
  255. package/dist/services/TradingService.d.mts +0 -308
  256. package/dist/services/TradingService.d.mts.map +0 -1
  257. package/dist/services/TradingService.mjs +0 -1477
  258. package/dist/services/TradingService.mjs.map +0 -1
  259. package/dist/services/index.cjs +0 -26
  260. package/dist/services/index.cjs.map +0 -1
  261. package/dist/services/index.d.cts +0 -11
  262. package/dist/services/index.d.cts.map +0 -1
  263. package/dist/services/index.d.mts +0 -11
  264. package/dist/services/index.d.mts.map +0 -1
  265. package/dist/services/index.mjs +0 -10
  266. package/dist/services/index.mjs.map +0 -1
  267. package/dist/services/utils.cjs +0 -16
  268. package/dist/services/utils.cjs.map +0 -1
  269. package/dist/services/utils.d.cts +0 -8
  270. package/dist/services/utils.d.cts.map +0 -1
  271. package/dist/services/utils.d.mts +0 -8
  272. package/dist/services/utils.d.mts.map +0 -1
  273. package/dist/services/utils.mjs +0 -12
  274. package/dist/services/utils.mjs.map +0 -1
  275. package/dist/types/chart.cjs +0 -42
  276. package/dist/types/chart.cjs.map +0 -1
  277. package/dist/types/chart.d.cts +0 -62
  278. package/dist/types/chart.d.cts.map +0 -1
  279. package/dist/types/chart.d.mts +0 -62
  280. package/dist/types/chart.d.mts.map +0 -1
  281. package/dist/types/chart.mjs +0 -39
  282. package/dist/types/chart.mjs.map +0 -1
  283. package/dist/types/hyperliquid-types.cjs +0 -3
  284. package/dist/types/hyperliquid-types.cjs.map +0 -1
  285. package/dist/types/hyperliquid-types.d.cts +0 -52
  286. package/dist/types/hyperliquid-types.d.cts.map +0 -1
  287. package/dist/types/hyperliquid-types.d.mts +0 -52
  288. package/dist/types/hyperliquid-types.d.mts.map +0 -1
  289. package/dist/types/hyperliquid-types.mjs +0 -2
  290. package/dist/types/hyperliquid-types.mjs.map +0 -1
  291. package/dist/types/index.cjs +0 -96
  292. package/dist/types/index.cjs.map +0 -1
  293. package/dist/types/index.d.cts +0 -1153
  294. package/dist/types/index.d.cts.map +0 -1
  295. package/dist/types/index.d.mts +0 -1153
  296. package/dist/types/index.d.mts.map +0 -1
  297. package/dist/types/index.mjs +0 -79
  298. package/dist/types/index.mjs.map +0 -1
  299. package/dist/types/transactionTypes.cjs +0 -28
  300. package/dist/types/transactionTypes.cjs.map +0 -1
  301. package/dist/types/transactionTypes.d.cts +0 -64
  302. package/dist/types/transactionTypes.d.cts.map +0 -1
  303. package/dist/types/transactionTypes.d.mts +0 -64
  304. package/dist/types/transactionTypes.d.mts.map +0 -1
  305. package/dist/types/transactionTypes.mjs +0 -23
  306. package/dist/types/transactionTypes.mjs.map +0 -1
  307. package/dist/utils/accountUtils.cjs +0 -65
  308. package/dist/utils/accountUtils.cjs.map +0 -1
  309. package/dist/utils/accountUtils.d.cts +0 -26
  310. package/dist/utils/accountUtils.d.cts.map +0 -1
  311. package/dist/utils/accountUtils.d.mts +0 -26
  312. package/dist/utils/accountUtils.d.mts.map +0 -1
  313. package/dist/utils/accountUtils.mjs +0 -61
  314. package/dist/utils/accountUtils.mjs.map +0 -1
  315. package/dist/utils/amountConversion.cjs +0 -61
  316. package/dist/utils/amountConversion.cjs.map +0 -1
  317. package/dist/utils/amountConversion.d.cts +0 -16
  318. package/dist/utils/amountConversion.d.cts.map +0 -1
  319. package/dist/utils/amountConversion.d.mts +0 -16
  320. package/dist/utils/amountConversion.d.mts.map +0 -1
  321. package/dist/utils/amountConversion.mjs +0 -54
  322. package/dist/utils/amountConversion.mjs.map +0 -1
  323. package/dist/utils/chartUtils.cjs +0 -98
  324. package/dist/utils/chartUtils.cjs.map +0 -1
  325. package/dist/utils/chartUtils.d.cts +0 -14
  326. package/dist/utils/chartUtils.d.cts.map +0 -1
  327. package/dist/utils/chartUtils.d.mts +0 -14
  328. package/dist/utils/chartUtils.d.mts.map +0 -1
  329. package/dist/utils/chartUtils.mjs +0 -94
  330. package/dist/utils/chartUtils.mjs.map +0 -1
  331. package/dist/utils/errorUtils.cjs +0 -22
  332. package/dist/utils/errorUtils.cjs.map +0 -1
  333. package/dist/utils/errorUtils.d.cts +0 -13
  334. package/dist/utils/errorUtils.d.cts.map +0 -1
  335. package/dist/utils/errorUtils.d.mts +0 -13
  336. package/dist/utils/errorUtils.d.mts.map +0 -1
  337. package/dist/utils/errorUtils.mjs +0 -18
  338. package/dist/utils/errorUtils.mjs.map +0 -1
  339. package/dist/utils/formatUtils.cjs +0 -987
  340. package/dist/utils/formatUtils.cjs.map +0 -1
  341. package/dist/utils/formatUtils.d.cts +0 -387
  342. package/dist/utils/formatUtils.d.cts.map +0 -1
  343. package/dist/utils/formatUtils.d.mts +0 -387
  344. package/dist/utils/formatUtils.d.mts.map +0 -1
  345. package/dist/utils/formatUtils.mjs +0 -966
  346. package/dist/utils/formatUtils.mjs.map +0 -1
  347. package/dist/utils/hyperLiquidAdapter.cjs +0 -454
  348. package/dist/utils/hyperLiquidAdapter.cjs.map +0 -1
  349. package/dist/utils/hyperLiquidAdapter.d.cts +0 -158
  350. package/dist/utils/hyperLiquidAdapter.d.cts.map +0 -1
  351. package/dist/utils/hyperLiquidAdapter.d.mts +0 -158
  352. package/dist/utils/hyperLiquidAdapter.d.mts.map +0 -1
  353. package/dist/utils/hyperLiquidAdapter.mjs +0 -439
  354. package/dist/utils/hyperLiquidAdapter.mjs.map +0 -1
  355. package/dist/utils/hyperLiquidOrderBookProcessor.cjs +0 -89
  356. package/dist/utils/hyperLiquidOrderBookProcessor.cjs.map +0 -1
  357. package/dist/utils/hyperLiquidOrderBookProcessor.d.cts +0 -58
  358. package/dist/utils/hyperLiquidOrderBookProcessor.d.cts.map +0 -1
  359. package/dist/utils/hyperLiquidOrderBookProcessor.d.mts +0 -58
  360. package/dist/utils/hyperLiquidOrderBookProcessor.d.mts.map +0 -1
  361. package/dist/utils/hyperLiquidOrderBookProcessor.mjs +0 -84
  362. package/dist/utils/hyperLiquidOrderBookProcessor.mjs.map +0 -1
  363. package/dist/utils/hyperLiquidValidation.cjs +0 -426
  364. package/dist/utils/hyperLiquidValidation.cjs.map +0 -1
  365. package/dist/utils/hyperLiquidValidation.d.cts +0 -143
  366. package/dist/utils/hyperLiquidValidation.d.cts.map +0 -1
  367. package/dist/utils/hyperLiquidValidation.d.mts +0 -143
  368. package/dist/utils/hyperLiquidValidation.d.mts.map +0 -1
  369. package/dist/utils/hyperLiquidValidation.mjs +0 -413
  370. package/dist/utils/hyperLiquidValidation.mjs.map +0 -1
  371. package/dist/utils/idUtils.cjs +0 -50
  372. package/dist/utils/idUtils.cjs.map +0 -1
  373. package/dist/utils/idUtils.d.cts +0 -38
  374. package/dist/utils/idUtils.d.cts.map +0 -1
  375. package/dist/utils/idUtils.d.mts +0 -38
  376. package/dist/utils/idUtils.d.mts.map +0 -1
  377. package/dist/utils/idUtils.mjs +0 -42
  378. package/dist/utils/idUtils.mjs.map +0 -1
  379. package/dist/utils/marginUtils.cjs +0 -137
  380. package/dist/utils/marginUtils.cjs.map +0 -1
  381. package/dist/utils/marginUtils.d.cts +0 -64
  382. package/dist/utils/marginUtils.d.cts.map +0 -1
  383. package/dist/utils/marginUtils.d.mts +0 -64
  384. package/dist/utils/marginUtils.d.mts.map +0 -1
  385. package/dist/utils/marginUtils.mjs +0 -131
  386. package/dist/utils/marginUtils.mjs.map +0 -1
  387. package/dist/utils/marketDataTransform.cjs +0 -227
  388. package/dist/utils/marketDataTransform.cjs.map +0 -1
  389. package/dist/utils/marketDataTransform.d.cts +0 -45
  390. package/dist/utils/marketDataTransform.d.cts.map +0 -1
  391. package/dist/utils/marketDataTransform.d.mts +0 -45
  392. package/dist/utils/marketDataTransform.d.mts.map +0 -1
  393. package/dist/utils/marketDataTransform.mjs +0 -220
  394. package/dist/utils/marketDataTransform.mjs.map +0 -1
  395. package/dist/utils/marketUtils.cjs +0 -420
  396. package/dist/utils/marketUtils.cjs.map +0 -1
  397. package/dist/utils/marketUtils.d.cts +0 -227
  398. package/dist/utils/marketUtils.d.cts.map +0 -1
  399. package/dist/utils/marketUtils.d.mts +0 -227
  400. package/dist/utils/marketUtils.d.mts.map +0 -1
  401. package/dist/utils/marketUtils.mjs +0 -404
  402. package/dist/utils/marketUtils.mjs.map +0 -1
  403. package/dist/utils/orderBookGrouping.cjs +0 -151
  404. package/dist/utils/orderBookGrouping.cjs.map +0 -1
  405. package/dist/utils/orderBookGrouping.d.cts +0 -76
  406. package/dist/utils/orderBookGrouping.d.cts.map +0 -1
  407. package/dist/utils/orderBookGrouping.d.mts +0 -76
  408. package/dist/utils/orderBookGrouping.d.mts.map +0 -1
  409. package/dist/utils/orderBookGrouping.mjs +0 -143
  410. package/dist/utils/orderBookGrouping.mjs.map +0 -1
  411. package/dist/utils/orderCalculations.cjs +0 -283
  412. package/dist/utils/orderCalculations.cjs.map +0 -1
  413. package/dist/utils/orderCalculations.d.cts +0 -107
  414. package/dist/utils/orderCalculations.d.cts.map +0 -1
  415. package/dist/utils/orderCalculations.d.mts +0 -107
  416. package/dist/utils/orderCalculations.d.mts.map +0 -1
  417. package/dist/utils/orderCalculations.mjs +0 -274
  418. package/dist/utils/orderCalculations.mjs.map +0 -1
  419. package/dist/utils/orderUtils.cjs +0 -159
  420. package/dist/utils/orderUtils.cjs.map +0 -1
  421. package/dist/utils/orderUtils.d.cts +0 -66
  422. package/dist/utils/orderUtils.d.cts.map +0 -1
  423. package/dist/utils/orderUtils.d.mts +0 -66
  424. package/dist/utils/orderUtils.d.mts.map +0 -1
  425. package/dist/utils/orderUtils.mjs +0 -151
  426. package/dist/utils/orderUtils.mjs.map +0 -1
  427. package/dist/utils/pnlCalculations.cjs +0 -107
  428. package/dist/utils/pnlCalculations.cjs.map +0 -1
  429. package/dist/utils/pnlCalculations.d.cts +0 -79
  430. package/dist/utils/pnlCalculations.d.cts.map +0 -1
  431. package/dist/utils/pnlCalculations.d.mts +0 -79
  432. package/dist/utils/pnlCalculations.d.mts.map +0 -1
  433. package/dist/utils/pnlCalculations.mjs +0 -97
  434. package/dist/utils/pnlCalculations.mjs.map +0 -1
  435. package/dist/utils/positionCalculations.cjs +0 -176
  436. package/dist/utils/positionCalculations.cjs.map +0 -1
  437. package/dist/utils/positionCalculations.d.cts +0 -85
  438. package/dist/utils/positionCalculations.d.cts.map +0 -1
  439. package/dist/utils/positionCalculations.d.mts +0 -85
  440. package/dist/utils/positionCalculations.d.mts.map +0 -1
  441. package/dist/utils/positionCalculations.mjs +0 -165
  442. package/dist/utils/positionCalculations.mjs.map +0 -1
  443. package/dist/utils/sortMarkets.cjs +0 -88
  444. package/dist/utils/sortMarkets.cjs.map +0 -1
  445. package/dist/utils/sortMarkets.d.cts +0 -21
  446. package/dist/utils/sortMarkets.d.cts.map +0 -1
  447. package/dist/utils/sortMarkets.d.mts +0 -21
  448. package/dist/utils/sortMarkets.d.mts.map +0 -1
  449. package/dist/utils/sortMarkets.mjs +0 -84
  450. package/dist/utils/sortMarkets.mjs.map +0 -1
  451. package/dist/utils/standaloneInfoClient.cjs +0 -34
  452. package/dist/utils/standaloneInfoClient.cjs.map +0 -1
  453. package/dist/utils/standaloneInfoClient.d.cts +0 -31
  454. package/dist/utils/standaloneInfoClient.d.cts.map +0 -1
  455. package/dist/utils/standaloneInfoClient.d.mts +0 -31
  456. package/dist/utils/standaloneInfoClient.d.mts.map +0 -1
  457. package/dist/utils/standaloneInfoClient.mjs +0 -30
  458. package/dist/utils/standaloneInfoClient.mjs.map +0 -1
  459. package/dist/utils/stringParseUtils.cjs +0 -49
  460. package/dist/utils/stringParseUtils.cjs.map +0 -1
  461. package/dist/utils/stringParseUtils.d.cts +0 -35
  462. package/dist/utils/stringParseUtils.d.cts.map +0 -1
  463. package/dist/utils/stringParseUtils.d.mts +0 -35
  464. package/dist/utils/stringParseUtils.d.mts.map +0 -1
  465. package/dist/utils/stringParseUtils.mjs +0 -44
  466. package/dist/utils/stringParseUtils.mjs.map +0 -1
  467. package/dist/utils/time.cjs +0 -37
  468. package/dist/utils/time.cjs.map +0 -1
  469. package/dist/utils/time.d.cts +0 -17
  470. package/dist/utils/time.d.cts.map +0 -1
  471. package/dist/utils/time.d.mts +0 -17
  472. package/dist/utils/time.d.mts.map +0 -1
  473. package/dist/utils/time.mjs +0 -33
  474. package/dist/utils/time.mjs.map +0 -1
  475. package/dist/utils/tpslValidation.cjs +0 -505
  476. package/dist/utils/tpslValidation.cjs.map +0 -1
  477. package/dist/utils/tpslValidation.d.cts +0 -182
  478. package/dist/utils/tpslValidation.d.cts.map +0 -1
  479. package/dist/utils/tpslValidation.d.mts +0 -182
  480. package/dist/utils/tpslValidation.d.mts.map +0 -1
  481. package/dist/utils/tpslValidation.mjs +0 -482
  482. package/dist/utils/tpslValidation.mjs.map +0 -1
  483. package/dist/utils/transactionUtils.cjs +0 -36
  484. package/dist/utils/transactionUtils.cjs.map +0 -1
  485. package/dist/utils/transactionUtils.d.cts +0 -17
  486. package/dist/utils/transactionUtils.d.cts.map +0 -1
  487. package/dist/utils/transactionUtils.d.mts +0 -17
  488. package/dist/utils/transactionUtils.d.mts.map +0 -1
  489. package/dist/utils/transactionUtils.mjs +0 -32
  490. package/dist/utils/transactionUtils.mjs.map +0 -1
  491. package/dist/utils/wait.cjs +0 -12
  492. package/dist/utils/wait.cjs.map +0 -1
  493. package/dist/utils/wait.d.cts +0 -8
  494. package/dist/utils/wait.d.cts.map +0 -1
  495. package/dist/utils/wait.d.mts +0 -8
  496. package/dist/utils/wait.d.mts.map +0 -1
  497. package/dist/utils/wait.mjs +0 -8
  498. package/dist/utils/wait.mjs.map +0 -1
@@ -1,107 +0,0 @@
1
- import type { PerpsDebugLogger } from "../types/index.cjs";
2
- import type { SDKOrderParams } from "../types/hyperliquid-types.cjs";
3
- /**
4
- * Optional debug logger for order calculation functions.
5
- * When provided, enables detailed logging for debugging.
6
- */
7
- export type OrderCalculationsDebugLogger = PerpsDebugLogger | undefined;
8
- type PositionSizeParams = {
9
- amount: string;
10
- price: number;
11
- szDecimals: number;
12
- };
13
- type MarginRequiredParams = {
14
- amount: string;
15
- leverage: number;
16
- };
17
- type MaxAllowedAmountParams = {
18
- availableBalance: number;
19
- assetPrice: number;
20
- assetSzDecimals: number;
21
- leverage: number;
22
- };
23
- export type CalculateFinalPositionSizeParams = {
24
- usdAmount?: string;
25
- size?: string;
26
- currentPrice: number;
27
- priceAtCalculation?: number;
28
- maxSlippageBps?: number;
29
- szDecimals: number;
30
- leverage?: number;
31
- debugLogger?: OrderCalculationsDebugLogger;
32
- };
33
- export type CalculateFinalPositionSizeResult = {
34
- finalPositionSize: number;
35
- };
36
- export type CalculateOrderPriceAndSizeParams = {
37
- orderType: 'market' | 'limit';
38
- isBuy: boolean;
39
- finalPositionSize: number;
40
- currentPrice: number;
41
- limitPrice?: string;
42
- slippage?: number;
43
- szDecimals: number;
44
- };
45
- export type CalculateOrderPriceAndSizeResult = {
46
- orderPrice: number;
47
- formattedSize: string;
48
- formattedPrice: string;
49
- };
50
- export type BuildOrdersArrayParams = {
51
- assetId: number;
52
- isBuy: boolean;
53
- formattedPrice: string;
54
- formattedSize: string;
55
- reduceOnly: boolean;
56
- orderType: 'market' | 'limit';
57
- clientOrderId?: string;
58
- takeProfitPrice?: string;
59
- stopLossPrice?: string;
60
- szDecimals: number;
61
- grouping?: 'na' | 'normalTpsl' | 'positionTpsl';
62
- };
63
- export type BuildOrdersArrayResult = {
64
- orders: SDKOrderParams[];
65
- grouping: 'na' | 'normalTpsl' | 'positionTpsl';
66
- };
67
- /**
68
- * Calculate position size based on USD amount and asset price
69
- *
70
- * @param params - Amount in USD, current asset price, and required decimal precision
71
- * @returns Position size formatted to the asset's decimal precision
72
- */
73
- export declare function calculatePositionSize(params: PositionSizeParams): string;
74
- /**
75
- * Calculate margin required for a position
76
- *
77
- * @param params - Position amount and leverage
78
- * @returns Margin required formatted to 2 decimal places
79
- */
80
- export declare function calculateMarginRequired(params: MarginRequiredParams): string;
81
- export declare function getMaxAllowedAmount(params: MaxAllowedAmountParams): number;
82
- /**
83
- * Calculates final position size using USD as source of truth with price validation
84
- *
85
- * This function implements the hybrid approach where USD is the source of truth,
86
- * but includes price staleness validation and proper rounding to prevent precision loss.
87
- *
88
- * @param params - USD amount, size, prices, and configuration
89
- * @returns Final position size as a number
90
- */
91
- export declare function calculateFinalPositionSize(params: CalculateFinalPositionSizeParams): CalculateFinalPositionSizeResult;
92
- /**
93
- * Calculates order price and formatted size based on order type
94
- *
95
- * @param params - Order parameters including type, direction, size, and prices
96
- * @returns Formatted order price, size, and price string
97
- */
98
- export declare function calculateOrderPriceAndSize(params: CalculateOrderPriceAndSizeParams): CalculateOrderPriceAndSizeResult;
99
- /**
100
- * Builds orders array including main order and optional TP/SL orders
101
- *
102
- * @param params - Order construction parameters
103
- * @returns Array of SDK order params and grouping type
104
- */
105
- export declare function buildOrdersArray(params: BuildOrdersArrayParams): BuildOrdersArrayResult;
106
- export {};
107
- //# sourceMappingURL=orderCalculations.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"orderCalculations.d.cts","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAAiB;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,uCAAmC;AAEjE;;;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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AM,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,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,CAiC1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CAsGlC;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CA2ClC;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,sBAAsB,GAC7B,sBAAsB,CA+FxB"}
@@ -1,107 +0,0 @@
1
- import type { PerpsDebugLogger } from "../types/index.mjs";
2
- import type { SDKOrderParams } from "../types/hyperliquid-types.mjs";
3
- /**
4
- * Optional debug logger for order calculation functions.
5
- * When provided, enables detailed logging for debugging.
6
- */
7
- export type OrderCalculationsDebugLogger = PerpsDebugLogger | undefined;
8
- type PositionSizeParams = {
9
- amount: string;
10
- price: number;
11
- szDecimals: number;
12
- };
13
- type MarginRequiredParams = {
14
- amount: string;
15
- leverage: number;
16
- };
17
- type MaxAllowedAmountParams = {
18
- availableBalance: number;
19
- assetPrice: number;
20
- assetSzDecimals: number;
21
- leverage: number;
22
- };
23
- export type CalculateFinalPositionSizeParams = {
24
- usdAmount?: string;
25
- size?: string;
26
- currentPrice: number;
27
- priceAtCalculation?: number;
28
- maxSlippageBps?: number;
29
- szDecimals: number;
30
- leverage?: number;
31
- debugLogger?: OrderCalculationsDebugLogger;
32
- };
33
- export type CalculateFinalPositionSizeResult = {
34
- finalPositionSize: number;
35
- };
36
- export type CalculateOrderPriceAndSizeParams = {
37
- orderType: 'market' | 'limit';
38
- isBuy: boolean;
39
- finalPositionSize: number;
40
- currentPrice: number;
41
- limitPrice?: string;
42
- slippage?: number;
43
- szDecimals: number;
44
- };
45
- export type CalculateOrderPriceAndSizeResult = {
46
- orderPrice: number;
47
- formattedSize: string;
48
- formattedPrice: string;
49
- };
50
- export type BuildOrdersArrayParams = {
51
- assetId: number;
52
- isBuy: boolean;
53
- formattedPrice: string;
54
- formattedSize: string;
55
- reduceOnly: boolean;
56
- orderType: 'market' | 'limit';
57
- clientOrderId?: string;
58
- takeProfitPrice?: string;
59
- stopLossPrice?: string;
60
- szDecimals: number;
61
- grouping?: 'na' | 'normalTpsl' | 'positionTpsl';
62
- };
63
- export type BuildOrdersArrayResult = {
64
- orders: SDKOrderParams[];
65
- grouping: 'na' | 'normalTpsl' | 'positionTpsl';
66
- };
67
- /**
68
- * Calculate position size based on USD amount and asset price
69
- *
70
- * @param params - Amount in USD, current asset price, and required decimal precision
71
- * @returns Position size formatted to the asset's decimal precision
72
- */
73
- export declare function calculatePositionSize(params: PositionSizeParams): string;
74
- /**
75
- * Calculate margin required for a position
76
- *
77
- * @param params - Position amount and leverage
78
- * @returns Margin required formatted to 2 decimal places
79
- */
80
- export declare function calculateMarginRequired(params: MarginRequiredParams): string;
81
- export declare function getMaxAllowedAmount(params: MaxAllowedAmountParams): number;
82
- /**
83
- * Calculates final position size using USD as source of truth with price validation
84
- *
85
- * This function implements the hybrid approach where USD is the source of truth,
86
- * but includes price staleness validation and proper rounding to prevent precision loss.
87
- *
88
- * @param params - USD amount, size, prices, and configuration
89
- * @returns Final position size as a number
90
- */
91
- export declare function calculateFinalPositionSize(params: CalculateFinalPositionSizeParams): CalculateFinalPositionSizeResult;
92
- /**
93
- * Calculates order price and formatted size based on order type
94
- *
95
- * @param params - Order parameters including type, direction, size, and prices
96
- * @returns Formatted order price, size, and price string
97
- */
98
- export declare function calculateOrderPriceAndSize(params: CalculateOrderPriceAndSizeParams): CalculateOrderPriceAndSizeResult;
99
- /**
100
- * Builds orders array including main order and optional TP/SL orders
101
- *
102
- * @param params - Order construction parameters
103
- * @returns Array of SDK order params and grouping type
104
- */
105
- export declare function buildOrdersArray(params: BuildOrdersArrayParams): BuildOrdersArrayResult;
106
- export {};
107
- //# sourceMappingURL=orderCalculations.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"orderCalculations.d.mts","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAAiB;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,uCAAmC;AAEjE;;;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,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAGF,MAAM,MAAM,gCAAgC,GAAG;I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AC;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,CAiC1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CAsGlC;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,gCAAgC,GACvC,gCAAgC,CA2ClC;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,sBAAsB,GAC7B,sBAAsB,CA+FxB"}
@@ -1,274 +0,0 @@
1
- import { formatHyperLiquidPrice, formatHyperLiquidSize } from "./hyperLiquidAdapter.mjs";
2
- import { ORDER_SLIPPAGE_CONFIG } from "../constants/perpsConfig.mjs";
3
- import { PERPS_ERROR_CODES } from "../constants/perpsErrorCodes.mjs";
4
- /**
5
- * Calculate position size based on USD amount and asset price
6
- *
7
- * @param params - Amount in USD, current asset price, and required decimal precision
8
- * @returns Position size formatted to the asset's decimal precision
9
- */
10
- export function calculatePositionSize(params) {
11
- const { amount, price, szDecimals } = params;
12
- // Validate required parameters
13
- if (szDecimals === undefined || szDecimals === null) {
14
- throw new Error('szDecimals is required for position size calculation');
15
- }
16
- if (szDecimals < 0) {
17
- throw new Error(`szDecimals must be >= 0, got: ${szDecimals}`);
18
- }
19
- const amountNum = parseFloat(amount || '0');
20
- if (isNaN(amountNum) || isNaN(price) || amountNum === 0 || price === 0) {
21
- return (0).toFixed(szDecimals);
22
- }
23
- const positionSize = amountNum / price;
24
- const multiplier = Math.pow(10, szDecimals);
25
- let rounded = Math.round(positionSize * multiplier) / multiplier;
26
- // Ensure rounded size meets requested USD (fix validation gap)
27
- const actualUsd = rounded * price;
28
- if (actualUsd < amountNum) {
29
- rounded += 1 / multiplier;
30
- }
31
- return rounded.toFixed(szDecimals);
32
- }
33
- /**
34
- * Calculate margin required for a position
35
- *
36
- * @param params - Position amount and leverage
37
- * @returns Margin required formatted to 2 decimal places
38
- */
39
- export function calculateMarginRequired(params) {
40
- const { amount, leverage } = params;
41
- const amountNum = parseFloat(amount || '0');
42
- if (isNaN(amountNum) ||
43
- isNaN(leverage) ||
44
- amountNum === 0 ||
45
- leverage === 0) {
46
- return '0.00';
47
- }
48
- return (amountNum / leverage).toFixed(2);
49
- }
50
- export function getMaxAllowedAmount(params) {
51
- const { availableBalance, assetPrice, assetSzDecimals, leverage } = params;
52
- if (availableBalance === 0 || !assetPrice || assetSzDecimals === undefined) {
53
- return 0;
54
- }
55
- // The theoretical maximum is simply availableBalance * leverage
56
- const theoreticalMax = availableBalance * leverage;
57
- // But we need to account for position size rounding
58
- // Find the largest whole dollar amount that fits within this limit
59
- let maxAmount = Math.floor(theoreticalMax);
60
- // Verify this amount doesn't exceed available balance after rounding
61
- const testPositionSize = calculatePositionSize({
62
- amount: maxAmount.toString(),
63
- price: assetPrice,
64
- szDecimals: assetSzDecimals,
65
- });
66
- const actualNotionalValue = parseFloat(testPositionSize) * assetPrice;
67
- const requiredMargin = actualNotionalValue / leverage;
68
- // If rounding caused us to exceed available balance, step down by one position increment
69
- if (requiredMargin > availableBalance) {
70
- const minPositionSizeIncrement = 1 / Math.pow(10, assetSzDecimals);
71
- const positionSizeIncrementUsd = Math.ceil(minPositionSizeIncrement * assetPrice);
72
- maxAmount -= positionSizeIncrementUsd;
73
- }
74
- return Math.max(0, maxAmount);
75
- }
76
- /**
77
- * Calculates final position size using USD as source of truth with price validation
78
- *
79
- * This function implements the hybrid approach where USD is the source of truth,
80
- * but includes price staleness validation and proper rounding to prevent precision loss.
81
- *
82
- * @param params - USD amount, size, prices, and configuration
83
- * @returns Final position size as a number
84
- */
85
- export function calculateFinalPositionSize(params) {
86
- const { usdAmount, size, currentPrice, priceAtCalculation, maxSlippageBps, szDecimals, leverage, debugLogger, } = params;
87
- let finalPositionSize;
88
- if (usdAmount && parseFloat(usdAmount) > 0) {
89
- // USD amount provided - use it as source of truth
90
- const usdValue = parseFloat(usdAmount);
91
- // 1. Validate price staleness if priceAtCalculation provided
92
- if (priceAtCalculation) {
93
- const priceDeltaBps = Math.abs(((currentPrice - priceAtCalculation) / priceAtCalculation) * 10000);
94
- const maxSlippageBpsValue = maxSlippageBps ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps;
95
- if (priceDeltaBps > maxSlippageBpsValue) {
96
- throw new Error(`Price moved too much: ${priceDeltaBps.toFixed(0)} bps (max: ${maxSlippageBpsValue} bps). ` +
97
- `Expected: ${priceAtCalculation.toFixed(2)}, Current: ${currentPrice.toFixed(2)}`);
98
- }
99
- debugLogger?.log('Price validation passed:', {
100
- priceAtCalculation,
101
- currentPrice,
102
- deltaBps: priceDeltaBps.toFixed(2),
103
- maxSlippageBps: maxSlippageBpsValue,
104
- });
105
- }
106
- // 2. Recalculate position size with fresh price
107
- finalPositionSize = usdValue / currentPrice;
108
- // 3. Apply size decimals rounding
109
- const multiplier = Math.pow(10, szDecimals);
110
- finalPositionSize = Math.round(finalPositionSize * multiplier) / multiplier;
111
- // 4. Ensure rounded size meets requested USD (fix validation gap)
112
- let actualNotionalValue = finalPositionSize * currentPrice;
113
- if (actualNotionalValue < usdValue) {
114
- // Add 1 minimum increment to meet requested USD
115
- finalPositionSize += 1 / multiplier;
116
- actualNotionalValue = finalPositionSize * currentPrice;
117
- debugLogger?.log('Position size adjusted to meet USD minimum:', {
118
- requestedUsd: usdValue,
119
- beforeAdjustment: finalPositionSize - 1 / multiplier,
120
- afterAdjustment: finalPositionSize,
121
- actualUsd: actualNotionalValue,
122
- });
123
- }
124
- const requiredMargin = actualNotionalValue / (leverage ?? 1);
125
- // Log if rounding caused significant difference
126
- const usdDifference = Math.abs(actualNotionalValue - usdValue);
127
- if (usdDifference > 0.01) {
128
- debugLogger?.log('Position size rounding caused USD difference (acceptable):', {
129
- requestedUsd: usdValue,
130
- actualUsd: actualNotionalValue,
131
- difference: usdDifference,
132
- positionSize: finalPositionSize,
133
- });
134
- }
135
- debugLogger?.log('Recalculated position size with fresh price:', {
136
- usdAmount: usdValue,
137
- priceAtCalculation,
138
- currentPrice,
139
- originalSize: size,
140
- recalculatedSize: finalPositionSize,
141
- requiredMargin,
142
- minIncrement: 1 / multiplier,
143
- });
144
- }
145
- else {
146
- // Legacy: Use provided size (backward compatibility)
147
- finalPositionSize = parseFloat(size ?? '0');
148
- debugLogger?.log('Using legacy size calculation (no USD amount provided):', {
149
- providedSize: size,
150
- finalSize: finalPositionSize,
151
- });
152
- }
153
- return { finalPositionSize };
154
- }
155
- /**
156
- * Calculates order price and formatted size based on order type
157
- *
158
- * @param params - Order parameters including type, direction, size, and prices
159
- * @returns Formatted order price, size, and price string
160
- */
161
- export function calculateOrderPriceAndSize(params) {
162
- const { orderType, isBuy, finalPositionSize, currentPrice, limitPrice, slippage, szDecimals, } = params;
163
- let orderPrice;
164
- let formattedSize;
165
- if (orderType === 'market') {
166
- // Market orders: add slippage (3% conservative default)
167
- const slippageValue = slippage ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps / 10000;
168
- orderPrice = isBuy
169
- ? currentPrice * (1 + slippageValue)
170
- : currentPrice * (1 - slippageValue);
171
- formattedSize = formatHyperLiquidSize({
172
- size: finalPositionSize,
173
- szDecimals,
174
- });
175
- }
176
- else {
177
- // Limit orders: use provided price (no slippage applied)
178
- if (!limitPrice) {
179
- throw new Error(PERPS_ERROR_CODES.ORDER_LIMIT_PRICE_REQUIRED);
180
- }
181
- orderPrice = parseFloat(limitPrice);
182
- formattedSize = formatHyperLiquidSize({
183
- size: finalPositionSize,
184
- szDecimals,
185
- });
186
- }
187
- const formattedPrice = formatHyperLiquidPrice({
188
- price: orderPrice,
189
- szDecimals,
190
- });
191
- return { orderPrice, formattedSize, formattedPrice };
192
- }
193
- /**
194
- * Builds orders array including main order and optional TP/SL orders
195
- *
196
- * @param params - Order construction parameters
197
- * @returns Array of SDK order params and grouping type
198
- */
199
- export function buildOrdersArray(params) {
200
- const { assetId, isBuy, formattedPrice, formattedSize, reduceOnly, orderType, clientOrderId, takeProfitPrice, stopLossPrice, szDecimals, grouping, } = params;
201
- const orders = [];
202
- // 1. Main order
203
- const mainOrder = {
204
- a: assetId,
205
- b: isBuy,
206
- p: formattedPrice,
207
- s: formattedSize,
208
- r: reduceOnly || false,
209
- t: orderType === 'limit'
210
- ? { limit: { tif: 'Gtc' } }
211
- : { limit: { tif: 'FrontendMarket' } },
212
- c: clientOrderId ? clientOrderId : undefined,
213
- };
214
- orders.push(mainOrder);
215
- // 2. Take Profit order
216
- if (takeProfitPrice) {
217
- const tpOrder = {
218
- a: assetId,
219
- b: !isBuy,
220
- p: formatHyperLiquidPrice({
221
- price: parseFloat(takeProfitPrice),
222
- szDecimals,
223
- }),
224
- s: formattedSize,
225
- r: true,
226
- t: {
227
- trigger: {
228
- isMarket: false,
229
- triggerPx: formatHyperLiquidPrice({
230
- price: parseFloat(takeProfitPrice),
231
- szDecimals,
232
- }),
233
- tpsl: 'tp',
234
- },
235
- },
236
- };
237
- orders.push(tpOrder);
238
- }
239
- // 3. Stop Loss order
240
- if (stopLossPrice) {
241
- // Apply 10% slippage to SL limit price (executes as market order when triggered)
242
- // HyperLiquid recommended: 10% for TP/SL orders
243
- const stopLossPriceNum = parseFloat(stopLossPrice);
244
- const slippageValue = ORDER_SLIPPAGE_CONFIG.DefaultTpslSlippageBps / 10000;
245
- const limitPriceWithSlippage = isBuy
246
- ? stopLossPriceNum * (1 - slippageValue) // Selling to close long: willing to accept LESS (slippage protection)
247
- : stopLossPriceNum * (1 + slippageValue); // Buying to close short: willing to pay MORE (slippage protection)
248
- const slOrder = {
249
- a: assetId,
250
- b: !isBuy,
251
- p: formatHyperLiquidPrice({
252
- price: limitPriceWithSlippage,
253
- szDecimals,
254
- }),
255
- s: formattedSize,
256
- r: true,
257
- t: {
258
- trigger: {
259
- isMarket: true,
260
- triggerPx: formatHyperLiquidPrice({
261
- price: stopLossPriceNum,
262
- szDecimals,
263
- }),
264
- tpsl: 'sl',
265
- },
266
- },
267
- };
268
- orders.push(slOrder);
269
- }
270
- // Determine grouping
271
- const finalGrouping = grouping ?? ((takeProfitPrice ?? stopLossPrice) ? 'normalTpsl' : 'na');
272
- return { orders, grouping: finalGrouping };
273
- }
274
- //# sourceMappingURL=orderCalculations.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"orderCalculations.mjs","sourceRoot":"","sources":["../../src/utils/orderCalculations.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACtB,iCAA6B;AAC9B,OAAO,EAAE,qBAAqB,EAAE,qCAAiC;AACjE,OAAO,EAAE,iBAAiB,EAAE,yCAAqC;AA+EjE;;;;;GAKG;AACH,MAAM,UAAU,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,CA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AmE;QAE/G,MAAM,OAAO,GAAmB;YAC9B,CAAC,EAAE,OAAO;YACV,CAAC,EAAE,CAAC,KAAK;YACT,CAAC,EAAE,sBAAsB,CAAC;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,sBAAsB,CAAC;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","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport {\n formatHyperLiquidPrice,\n formatHyperLiquidSize,\n} from './hyperLiquidAdapter';\nimport { ORDER_SLIPPAGE_CONFIG } from '../constants/perpsConfig';\nimport { PERPS_ERROR_CODES } from '../constants/perpsErrorCodes';\nimport type { PerpsDebugLogger } from '../types';\nimport type { SDKOrderParams } from '../types/hyperliquid-types';\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 availableBalance: 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 slippage?: 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 { availableBalance, assetPrice, assetSzDecimals, leverage } = params;\n if (availableBalance === 0 || !assetPrice || assetSzDecimals === undefined) {\n return 0;\n }\n\n // The theoretical maximum is simply availableBalance * leverage\n const theoreticalMax = availableBalance * 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 > availableBalance) {\n const minPositionSizeIncrement = 1 / Math.pow(10, assetSzDecimals);\n const positionSizeIncrementUsd = Math.ceil(\n minPositionSizeIncrement * assetPrice,\n );\n maxAmount -= positionSizeIncrementUsd;\n }\n\n return Math.max(0, maxAmount);\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 slippage,\n szDecimals,\n } = params;\n\n let orderPrice: number;\n let formattedSize: string;\n\n if (orderType === 'market') {\n // Market orders: add slippage (3% conservative default)\n const slippageValue =\n slippage ?? ORDER_SLIPPAGE_CONFIG.DefaultMarketSlippageBps / 10000;\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"]}