@lifi/perps-sdk 20.1.0 → 21.0.0

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 (320) hide show
  1. package/README.md +81 -6
  2. package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
  3. package/dist/cjs/client/PerpsClient.js +4 -1
  4. package/dist/cjs/client/PerpsClient.js.map +1 -1
  5. package/dist/cjs/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
  6. package/dist/cjs/decimal/big.d.ts.map +1 -0
  7. package/dist/cjs/{utils/decimal.js → decimal/big.js} +1 -1
  8. package/dist/cjs/decimal/big.js.map +1 -0
  9. package/dist/cjs/decimal/convert.d.ts +11 -0
  10. package/dist/cjs/decimal/convert.d.ts.map +1 -0
  11. package/dist/cjs/decimal/convert.js +70 -0
  12. package/dist/cjs/decimal/convert.js.map +1 -0
  13. package/dist/cjs/decimal/format.d.ts.map +1 -0
  14. package/dist/cjs/decimal/format.js.map +1 -0
  15. package/dist/cjs/decimal/parse.d.ts +6 -0
  16. package/dist/cjs/decimal/parse.d.ts.map +1 -0
  17. package/dist/cjs/{utils → decimal}/parse.js +5 -4
  18. package/dist/cjs/decimal/parse.js.map +1 -0
  19. package/dist/cjs/index.d.ts +17 -15
  20. package/dist/cjs/index.d.ts.map +1 -1
  21. package/dist/cjs/index.js +75 -61
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/math/fills.d.ts +5 -0
  24. package/dist/cjs/math/fills.d.ts.map +1 -0
  25. package/dist/cjs/{utils/fillClassification.js → math/fills.js} +16 -1
  26. package/dist/cjs/math/fills.js.map +1 -0
  27. package/dist/cjs/math/order.d.ts +45 -0
  28. package/dist/cjs/math/order.d.ts.map +1 -0
  29. package/dist/cjs/math/order.js +256 -0
  30. package/dist/cjs/math/order.js.map +1 -0
  31. package/dist/cjs/math/position.d.ts +56 -0
  32. package/dist/cjs/math/position.d.ts.map +1 -0
  33. package/dist/cjs/math/position.js +160 -0
  34. package/dist/cjs/math/position.js.map +1 -0
  35. package/dist/cjs/services/resolveQuote.d.ts.map +1 -1
  36. package/dist/cjs/services/resolveQuote.js +2 -2
  37. package/dist/cjs/services/resolveQuote.js.map +1 -1
  38. package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
  39. package/dist/cjs/services/resolveSubscribeQuote.js +2 -2
  40. package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
  41. package/dist/cjs/types/provider.d.ts +3 -3
  42. package/dist/cjs/types/provider.d.ts.map +1 -1
  43. package/dist/cjs/types/withdrawal.d.ts +7 -5
  44. package/dist/cjs/types/withdrawal.d.ts.map +1 -1
  45. package/dist/cjs/utils/orderClassification.d.ts +1 -3
  46. package/dist/cjs/utils/orderClassification.d.ts.map +1 -1
  47. package/dist/cjs/utils/orderClassification.js +1 -15
  48. package/dist/cjs/utils/orderClassification.js.map +1 -1
  49. package/dist/cjs/version.d.ts +1 -1
  50. package/dist/cjs/version.js +1 -1
  51. package/dist/cjs/wire/orderAmounts.d.ts +18 -0
  52. package/dist/cjs/wire/orderAmounts.d.ts.map +1 -0
  53. package/dist/cjs/wire/orderAmounts.js +79 -0
  54. package/dist/cjs/wire/orderAmounts.js.map +1 -0
  55. package/dist/cjs/wire/refuel.d.ts +8 -0
  56. package/dist/cjs/wire/refuel.d.ts.map +1 -0
  57. package/dist/cjs/wire/refuel.js +23 -0
  58. package/dist/cjs/wire/refuel.js.map +1 -0
  59. package/dist/cjs/wire/requireDecimal.d.ts +4 -0
  60. package/dist/cjs/wire/requireDecimal.d.ts.map +1 -0
  61. package/dist/cjs/wire/requireDecimal.js +16 -0
  62. package/dist/cjs/wire/requireDecimal.js.map +1 -0
  63. package/dist/cjs/wire/snap.d.ts +5 -0
  64. package/dist/cjs/wire/snap.d.ts.map +1 -0
  65. package/dist/cjs/wire/snap.js +12 -0
  66. package/dist/cjs/wire/snap.js.map +1 -0
  67. package/dist/cjs/wire/transferable.d.ts +3 -0
  68. package/dist/cjs/wire/transferable.d.ts.map +1 -0
  69. package/dist/cjs/wire/transferable.js +13 -0
  70. package/dist/cjs/wire/transferable.js.map +1 -0
  71. package/dist/cjs/wire/withdraw.d.ts +4 -0
  72. package/dist/cjs/wire/withdraw.d.ts.map +1 -0
  73. package/dist/cjs/wire/withdraw.js +13 -0
  74. package/dist/cjs/wire/withdraw.js.map +1 -0
  75. package/dist/esm/client/PerpsClient.d.ts.map +1 -1
  76. package/dist/esm/client/PerpsClient.js +4 -1
  77. package/dist/esm/client/PerpsClient.js.map +1 -1
  78. package/dist/esm/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
  79. package/dist/esm/decimal/big.d.ts.map +1 -0
  80. package/dist/esm/{utils/decimal.js → decimal/big.js} +1 -1
  81. package/dist/esm/decimal/big.js.map +1 -0
  82. package/dist/esm/decimal/convert.d.ts +80 -0
  83. package/dist/esm/decimal/convert.d.ts.map +1 -0
  84. package/dist/esm/decimal/convert.js +118 -0
  85. package/dist/esm/decimal/convert.js.map +1 -0
  86. package/dist/esm/decimal/format.d.ts.map +1 -0
  87. package/dist/esm/decimal/format.js.map +1 -0
  88. package/dist/esm/decimal/parse.d.ts +29 -0
  89. package/dist/esm/decimal/parse.d.ts.map +1 -0
  90. package/dist/esm/{utils → decimal}/parse.js +9 -10
  91. package/dist/esm/decimal/parse.js.map +1 -0
  92. package/dist/esm/index.d.ts +17 -15
  93. package/dist/esm/index.d.ts.map +1 -1
  94. package/dist/esm/index.js +12 -12
  95. package/dist/esm/index.js.map +1 -1
  96. package/dist/esm/math/fills.d.ts +26 -0
  97. package/dist/esm/math/fills.d.ts.map +1 -0
  98. package/dist/esm/{utils/fillClassification.js → math/fills.js} +26 -2
  99. package/dist/esm/math/fills.js.map +1 -0
  100. package/dist/esm/{utils/calculations.d.ts → math/order.d.ts} +70 -75
  101. package/dist/esm/math/order.d.ts.map +1 -0
  102. package/dist/esm/{utils/calculations.js → math/order.js} +155 -111
  103. package/dist/esm/math/order.js.map +1 -0
  104. package/dist/esm/math/position.d.ts +211 -0
  105. package/dist/esm/math/position.d.ts.map +1 -0
  106. package/dist/esm/math/position.js +299 -0
  107. package/dist/esm/math/position.js.map +1 -0
  108. package/dist/esm/services/resolveQuote.d.ts.map +1 -1
  109. package/dist/esm/services/resolveQuote.js +1 -1
  110. package/dist/esm/services/resolveQuote.js.map +1 -1
  111. package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
  112. package/dist/esm/services/resolveSubscribeQuote.js +1 -1
  113. package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
  114. package/dist/esm/types/provider.d.ts +23 -11
  115. package/dist/esm/types/provider.d.ts.map +1 -1
  116. package/dist/esm/types/withdrawal.d.ts +17 -5
  117. package/dist/esm/types/withdrawal.d.ts.map +1 -1
  118. package/dist/esm/utils/orderClassification.d.ts +1 -14
  119. package/dist/esm/utils/orderClassification.d.ts.map +1 -1
  120. package/dist/esm/utils/orderClassification.js +1 -25
  121. package/dist/esm/utils/orderClassification.js.map +1 -1
  122. package/dist/esm/version.d.ts +1 -1
  123. package/dist/esm/version.js +1 -1
  124. package/dist/esm/wire/orderAmounts.d.ts +55 -0
  125. package/dist/esm/wire/orderAmounts.d.ts.map +1 -0
  126. package/dist/esm/wire/orderAmounts.js +114 -0
  127. package/dist/esm/wire/orderAmounts.js.map +1 -0
  128. package/dist/esm/wire/refuel.d.ts +28 -0
  129. package/dist/esm/wire/refuel.d.ts.map +1 -0
  130. package/dist/esm/wire/refuel.js +36 -0
  131. package/dist/esm/wire/refuel.js.map +1 -0
  132. package/dist/esm/wire/requireDecimal.d.ts +11 -0
  133. package/dist/esm/wire/requireDecimal.d.ts.map +1 -0
  134. package/dist/esm/wire/requireDecimal.js +17 -0
  135. package/dist/esm/wire/requireDecimal.js.map +1 -0
  136. package/dist/esm/wire/snap.d.ts +24 -0
  137. package/dist/esm/wire/snap.d.ts.map +1 -0
  138. package/dist/esm/wire/snap.js +27 -0
  139. package/dist/esm/wire/snap.js.map +1 -0
  140. package/dist/esm/wire/transferable.d.ts +15 -0
  141. package/dist/esm/wire/transferable.d.ts.map +1 -0
  142. package/dist/esm/wire/transferable.js +22 -0
  143. package/dist/esm/wire/transferable.js.map +1 -0
  144. package/dist/esm/wire/withdraw.d.ts +14 -0
  145. package/dist/esm/wire/withdraw.d.ts.map +1 -0
  146. package/dist/esm/wire/withdraw.js +20 -0
  147. package/dist/esm/wire/withdraw.js.map +1 -0
  148. package/dist/types/client/PerpsClient.d.ts.map +1 -1
  149. package/dist/types/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
  150. package/dist/types/decimal/big.d.ts.map +1 -0
  151. package/dist/types/decimal/convert.d.ts +80 -0
  152. package/dist/types/decimal/convert.d.ts.map +1 -0
  153. package/dist/types/decimal/format.d.ts.map +1 -0
  154. package/dist/types/decimal/parse.d.ts +29 -0
  155. package/dist/types/decimal/parse.d.ts.map +1 -0
  156. package/dist/types/index.d.ts +17 -15
  157. package/dist/types/index.d.ts.map +1 -1
  158. package/dist/types/math/fills.d.ts +26 -0
  159. package/dist/types/math/fills.d.ts.map +1 -0
  160. package/dist/types/{utils/calculations.d.ts → math/order.d.ts} +70 -75
  161. package/dist/types/math/order.d.ts.map +1 -0
  162. package/dist/types/math/position.d.ts +211 -0
  163. package/dist/types/math/position.d.ts.map +1 -0
  164. package/dist/types/services/resolveQuote.d.ts.map +1 -1
  165. package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
  166. package/dist/types/types/provider.d.ts +23 -11
  167. package/dist/types/types/provider.d.ts.map +1 -1
  168. package/dist/types/types/withdrawal.d.ts +17 -5
  169. package/dist/types/types/withdrawal.d.ts.map +1 -1
  170. package/dist/types/utils/orderClassification.d.ts +1 -14
  171. package/dist/types/utils/orderClassification.d.ts.map +1 -1
  172. package/dist/types/version.d.ts +1 -1
  173. package/dist/types/wire/orderAmounts.d.ts +55 -0
  174. package/dist/types/wire/orderAmounts.d.ts.map +1 -0
  175. package/dist/types/wire/refuel.d.ts +28 -0
  176. package/dist/types/wire/refuel.d.ts.map +1 -0
  177. package/dist/types/wire/requireDecimal.d.ts +11 -0
  178. package/dist/types/wire/requireDecimal.d.ts.map +1 -0
  179. package/dist/types/wire/snap.d.ts +24 -0
  180. package/dist/types/wire/snap.d.ts.map +1 -0
  181. package/dist/types/wire/transferable.d.ts +15 -0
  182. package/dist/types/wire/transferable.d.ts.map +1 -0
  183. package/dist/types/wire/withdraw.d.ts +14 -0
  184. package/dist/types/wire/withdraw.d.ts.map +1 -0
  185. package/package.json +2 -2
  186. package/src/client/PerpsClient.ts +4 -1
  187. package/src/decimal/convert.ts +164 -0
  188. package/src/{utils → decimal}/parse.ts +13 -11
  189. package/src/index.ts +80 -57
  190. package/src/{utils/fillClassification.ts → math/fills.ts} +30 -1
  191. package/src/{utils/calculations.ts → math/order.ts} +205 -127
  192. package/src/{utils/positionMath.ts → math/position.ts} +173 -21
  193. package/src/services/resolveQuote.ts +1 -1
  194. package/src/services/resolveSubscribeQuote.ts +1 -1
  195. package/src/types/provider.ts +23 -10
  196. package/src/types/withdrawal.ts +22 -5
  197. package/src/utils/orderClassification.ts +0 -30
  198. package/src/version.ts +1 -1
  199. package/src/wire/orderAmounts.ts +172 -0
  200. package/src/wire/refuel.ts +51 -0
  201. package/src/wire/requireDecimal.ts +24 -0
  202. package/src/wire/snap.ts +38 -0
  203. package/src/wire/transferable.ts +26 -0
  204. package/src/wire/withdraw.ts +29 -0
  205. package/dist/cjs/utils/calculations.d.ts +0 -47
  206. package/dist/cjs/utils/calculations.d.ts.map +0 -1
  207. package/dist/cjs/utils/calculations.js +0 -214
  208. package/dist/cjs/utils/calculations.js.map +0 -1
  209. package/dist/cjs/utils/decimal.d.ts.map +0 -1
  210. package/dist/cjs/utils/decimal.js.map +0 -1
  211. package/dist/cjs/utils/fillClassification.d.ts +0 -3
  212. package/dist/cjs/utils/fillClassification.d.ts.map +0 -1
  213. package/dist/cjs/utils/fillClassification.js.map +0 -1
  214. package/dist/cjs/utils/format.d.ts.map +0 -1
  215. package/dist/cjs/utils/format.js.map +0 -1
  216. package/dist/cjs/utils/math.d.ts +0 -4
  217. package/dist/cjs/utils/math.d.ts.map +0 -1
  218. package/dist/cjs/utils/math.js +0 -8
  219. package/dist/cjs/utils/math.js.map +0 -1
  220. package/dist/cjs/utils/orderEntry.d.ts +0 -6
  221. package/dist/cjs/utils/orderEntry.d.ts.map +0 -1
  222. package/dist/cjs/utils/orderEntry.js +0 -20
  223. package/dist/cjs/utils/orderEntry.js.map +0 -1
  224. package/dist/cjs/utils/orderMath.d.ts +0 -6
  225. package/dist/cjs/utils/orderMath.d.ts.map +0 -1
  226. package/dist/cjs/utils/orderMath.js +0 -85
  227. package/dist/cjs/utils/orderMath.js.map +0 -1
  228. package/dist/cjs/utils/parse.d.ts +0 -3
  229. package/dist/cjs/utils/parse.d.ts.map +0 -1
  230. package/dist/cjs/utils/parse.js.map +0 -1
  231. package/dist/cjs/utils/positionMath.d.ts +0 -37
  232. package/dist/cjs/utils/positionMath.d.ts.map +0 -1
  233. package/dist/cjs/utils/positionMath.js +0 -99
  234. package/dist/cjs/utils/positionMath.js.map +0 -1
  235. package/dist/cjs/utils/scaleToInteger.d.ts +0 -3
  236. package/dist/cjs/utils/scaleToInteger.d.ts.map +0 -1
  237. package/dist/cjs/utils/scaleToInteger.js +0 -30
  238. package/dist/cjs/utils/scaleToInteger.js.map +0 -1
  239. package/dist/cjs/utils/units.d.ts +0 -3
  240. package/dist/cjs/utils/units.d.ts.map +0 -1
  241. package/dist/cjs/utils/units.js +0 -18
  242. package/dist/cjs/utils/units.js.map +0 -1
  243. package/dist/cjs/utils/validation.d.ts +0 -2
  244. package/dist/cjs/utils/validation.d.ts.map +0 -1
  245. package/dist/cjs/utils/validation.js +0 -16
  246. package/dist/cjs/utils/validation.js.map +0 -1
  247. package/dist/esm/utils/calculations.d.ts.map +0 -1
  248. package/dist/esm/utils/calculations.js.map +0 -1
  249. package/dist/esm/utils/decimal.d.ts.map +0 -1
  250. package/dist/esm/utils/decimal.js.map +0 -1
  251. package/dist/esm/utils/fillClassification.d.ts +0 -13
  252. package/dist/esm/utils/fillClassification.d.ts.map +0 -1
  253. package/dist/esm/utils/fillClassification.js.map +0 -1
  254. package/dist/esm/utils/format.d.ts.map +0 -1
  255. package/dist/esm/utils/format.js.map +0 -1
  256. package/dist/esm/utils/math.d.ts +0 -6
  257. package/dist/esm/utils/math.d.ts.map +0 -1
  258. package/dist/esm/utils/math.js +0 -5
  259. package/dist/esm/utils/math.js.map +0 -1
  260. package/dist/esm/utils/orderEntry.d.ts +0 -26
  261. package/dist/esm/utils/orderEntry.d.ts.map +0 -1
  262. package/dist/esm/utils/orderEntry.js +0 -34
  263. package/dist/esm/utils/orderEntry.js.map +0 -1
  264. package/dist/esm/utils/orderMath.d.ts +0 -37
  265. package/dist/esm/utils/orderMath.d.ts.map +0 -1
  266. package/dist/esm/utils/orderMath.js +0 -113
  267. package/dist/esm/utils/orderMath.js.map +0 -1
  268. package/dist/esm/utils/parse.d.ts +0 -28
  269. package/dist/esm/utils/parse.d.ts.map +0 -1
  270. package/dist/esm/utils/parse.js.map +0 -1
  271. package/dist/esm/utils/positionMath.d.ts +0 -120
  272. package/dist/esm/utils/positionMath.d.ts.map +0 -1
  273. package/dist/esm/utils/positionMath.js +0 -173
  274. package/dist/esm/utils/positionMath.js.map +0 -1
  275. package/dist/esm/utils/scaleToInteger.d.ts +0 -22
  276. package/dist/esm/utils/scaleToInteger.d.ts.map +0 -1
  277. package/dist/esm/utils/scaleToInteger.js +0 -34
  278. package/dist/esm/utils/scaleToInteger.js.map +0 -1
  279. package/dist/esm/utils/units.d.ts +0 -27
  280. package/dist/esm/utils/units.d.ts.map +0 -1
  281. package/dist/esm/utils/units.js +0 -38
  282. package/dist/esm/utils/units.js.map +0 -1
  283. package/dist/esm/utils/validation.d.ts +0 -18
  284. package/dist/esm/utils/validation.d.ts.map +0 -1
  285. package/dist/esm/utils/validation.js +0 -29
  286. package/dist/esm/utils/validation.js.map +0 -1
  287. package/dist/types/utils/calculations.d.ts.map +0 -1
  288. package/dist/types/utils/decimal.d.ts.map +0 -1
  289. package/dist/types/utils/fillClassification.d.ts +0 -13
  290. package/dist/types/utils/fillClassification.d.ts.map +0 -1
  291. package/dist/types/utils/format.d.ts.map +0 -1
  292. package/dist/types/utils/math.d.ts +0 -6
  293. package/dist/types/utils/math.d.ts.map +0 -1
  294. package/dist/types/utils/orderEntry.d.ts +0 -26
  295. package/dist/types/utils/orderEntry.d.ts.map +0 -1
  296. package/dist/types/utils/orderMath.d.ts +0 -37
  297. package/dist/types/utils/orderMath.d.ts.map +0 -1
  298. package/dist/types/utils/parse.d.ts +0 -28
  299. package/dist/types/utils/parse.d.ts.map +0 -1
  300. package/dist/types/utils/positionMath.d.ts +0 -120
  301. package/dist/types/utils/positionMath.d.ts.map +0 -1
  302. package/dist/types/utils/scaleToInteger.d.ts +0 -22
  303. package/dist/types/utils/scaleToInteger.d.ts.map +0 -1
  304. package/dist/types/utils/units.d.ts +0 -27
  305. package/dist/types/utils/units.d.ts.map +0 -1
  306. package/dist/types/utils/validation.d.ts +0 -18
  307. package/dist/types/utils/validation.d.ts.map +0 -1
  308. package/src/utils/math.ts +0 -7
  309. package/src/utils/orderEntry.ts +0 -38
  310. package/src/utils/orderMath.ts +0 -152
  311. package/src/utils/scaleToInteger.ts +0 -56
  312. package/src/utils/units.ts +0 -39
  313. package/src/utils/validation.ts +0 -38
  314. /package/dist/cjs/{utils → decimal}/format.d.ts +0 -0
  315. /package/dist/cjs/{utils → decimal}/format.js +0 -0
  316. /package/dist/esm/{utils → decimal}/format.d.ts +0 -0
  317. /package/dist/esm/{utils → decimal}/format.js +0 -0
  318. /package/dist/types/{utils → decimal}/format.d.ts +0 -0
  319. /package/src/{utils/decimal.ts → decimal/big.ts} +0 -0
  320. /package/src/{utils → decimal}/format.ts +0 -0
package/src/index.ts CHANGED
@@ -17,8 +17,78 @@ export {
17
17
  } from './client/createPerpsClient.js'
18
18
  export { PerpsClient } from './client/PerpsClient.js'
19
19
  export { requireProvider } from './client/requireProvider.js'
20
+ export type {
21
+ BaseUnitsRounding,
22
+ ScaleToIntegerPolicy,
23
+ } from './decimal/convert.js'
24
+ export {
25
+ baseUnitsToDecimal,
26
+ decimalToBaseUnits,
27
+ fromBaseUnits,
28
+ fromBaseUnitsNumber,
29
+ numberToDecimalString,
30
+ scaleToInteger,
31
+ truncateDecimal,
32
+ } from './decimal/convert.js'
33
+ export type { FormatOptions, RoundingMode } from './decimal/format.js'
34
+ export {
35
+ formatCompactUsd,
36
+ formatNumber,
37
+ formatPrice,
38
+ formatSignedPercent,
39
+ formatSignedUsd,
40
+ formatUsd,
41
+ } from './decimal/format.js'
42
+ export { parseDecimal, stringToFloat } from './decimal/parse.js'
20
43
  export { PerpsErrorMessage } from './errors/constants.js'
21
44
  export { PerpsError } from './errors/PerpsError.js'
45
+ export {
46
+ classifyFill,
47
+ classifyFillFromPosition,
48
+ FillClassification,
49
+ } from './math/fills.js'
50
+ export type { ExpectedPnl } from './math/order.js'
51
+ export {
52
+ applySlippage,
53
+ buildQuote,
54
+ calculateExpectedPnl,
55
+ calculatePositionSize,
56
+ calculateRealizedPnlPercent,
57
+ calculateSize,
58
+ calculateTriggerPercent,
59
+ calculateTriggerPrice,
60
+ estimateFees,
61
+ estimateRealizedPnl,
62
+ expectedRealizedPnlForOpenOrder,
63
+ expectedRealizedPnlForTriggerOrder,
64
+ findMatchingPosition,
65
+ percentFromPrice,
66
+ priceFromPercent,
67
+ resolveCloseSize,
68
+ walkOrderbook,
69
+ } from './math/order.js'
70
+ export {
71
+ calculateEffectiveLeverage,
72
+ calculateLiquidationDistance,
73
+ calculateNotionalValue,
74
+ calculateRealizedPnl,
75
+ calculateRequiredMargin,
76
+ calculateRoe,
77
+ calculateUnrealizedPnl,
78
+ directionSign,
79
+ effectiveLeverage,
80
+ estimateAverageEntryPrice,
81
+ estimateIsolatedLiquidationPrice,
82
+ estimateLiquidationPrice,
83
+ estimateNewLeverage,
84
+ estimateUnrealizedPnl,
85
+ liquidationDistancePercent,
86
+ predictAverageEntryPrice,
87
+ predictNewLeverage,
88
+ predictUnrealizedPnl,
89
+ realizedPnlOnClose,
90
+ wouldImmediatelyLiquidate,
91
+ } from './math/position.js'
22
92
  // Registries
23
93
  export {
24
94
  AssetRegistry,
@@ -179,24 +249,6 @@ export type {
179
249
  WithdrawFlowSetupRequired,
180
250
  } from './types/withdrawal.js'
181
251
  export { paginateActivity } from './utils/activityPaging.js'
182
- export type { ExpectedPnl } from './utils/calculations.js'
183
- export {
184
- applySlippage,
185
- buildQuote,
186
- calculateExpectedPnl,
187
- calculateNotionalValue,
188
- calculatePositionSize,
189
- calculateRealizedPnlPercent,
190
- calculateRequiredMargin,
191
- calculateRoe,
192
- calculateUnrealizedPnl,
193
- effectiveLeverage,
194
- estimateFees,
195
- liquidationDistancePercent,
196
- percentFromPrice,
197
- priceFromPercent,
198
- walkOrderbook,
199
- } from './utils/calculations.js'
200
252
  export {
201
253
  ETHEREUM_NATIVE_GAS,
202
254
  ETHEREUM_USDC,
@@ -210,21 +262,8 @@ export {
210
262
  explorerTxUrl,
211
263
  explorerTxUrlFromBase,
212
264
  } from './utils/explorer.js'
213
- export { classifyFillFromPosition } from './utils/fillClassification.js'
214
- export type { FormatOptions, RoundingMode } from './utils/format.js'
215
- export {
216
- formatCompactUsd,
217
- formatNumber,
218
- formatPrice,
219
- formatSignedPercent,
220
- formatSignedUsd,
221
- formatUsd,
222
- } from './utils/format.js'
223
- export { maxOf, minOf } from './utils/math.js'
224
265
  export {
225
266
  ACTIVE_ORDER_STATUSES,
226
- classifyFill,
227
- FillClassification,
228
267
  isActiveOrderStatus,
229
268
  isRegularOrder,
230
269
  isStopLossOrder,
@@ -234,30 +273,6 @@ export {
234
273
  isTwapOrder,
235
274
  triggerConditionFor,
236
275
  } from './utils/orderClassification.js'
237
- export {
238
- marginFromNotional,
239
- marginFromSize,
240
- sizeFromMargin,
241
- sizeFromNotional,
242
- } from './utils/orderEntry.js'
243
- export {
244
- expectedRealizedPnlForOpenOrder,
245
- expectedRealizedPnlForTriggerOrder,
246
- findMatchingPosition,
247
- resolveCloseSize,
248
- } from './utils/orderMath.js'
249
- export { parseDecimal, stringToFloat } from './utils/parse.js'
250
- export {
251
- directionSign,
252
- estimateIsolatedLiquidationPrice,
253
- predictAverageEntryPrice,
254
- predictNewLeverage,
255
- predictUnrealizedPnl,
256
- realizedPnlOnClose,
257
- wouldImmediatelyLiquidate,
258
- } from './utils/positionMath.js'
259
- export type { ScaleToIntegerPolicy } from './utils/scaleToInteger.js'
260
- export { scaleToInteger } from './utils/scaleToInteger.js'
261
276
  export {
262
277
  isSetupOptionFor,
263
278
  selectUserSetupActions,
@@ -266,8 +281,6 @@ export {
266
281
  signTypedData,
267
282
  signTypedDataWithSigner,
268
283
  } from './utils/signTypedData.js'
269
- export { fromBaseUnits, fromBaseUnitsNumber } from './utils/units.js'
270
- export { validateMargin } from './utils/validation.js'
271
284
  export { createWarnOnce } from './utils/warnOnce.js'
272
285
  // Version
273
286
  export { name, version } from './version.js'
@@ -294,3 +307,13 @@ export {
294
307
  WsProviderBase,
295
308
  } from './websocket/WsProviderBase.js'
296
309
  export { wsLog } from './websocket/wsLog.js'
310
+ export type {
311
+ OrderAmounts,
312
+ OrderAmountsInput,
313
+ } from './wire/orderAmounts.js'
314
+ export { calculateOrderAmounts } from './wire/orderAmounts.js'
315
+ export type { RefuelAmountInput } from './wire/refuel.js'
316
+ export { calculateRefuelAmount } from './wire/refuel.js'
317
+ export { snapOrderPrice, snapOrderSize } from './wire/snap.js'
318
+ export { calculateTransferable } from './wire/transferable.js'
319
+ export { calculateWithdrawMax } from './wire/withdraw.js'
@@ -1,4 +1,12 @@
1
- import { FillClassification } from '@lifi/perps-types'
1
+ import { FillClassification, OrderSide } from '@lifi/perps-types'
2
+ import { parseDecimal } from '../decimal/parse.js'
3
+
4
+ /**
5
+ * Re-exported fill taxonomy used by {@link classifyFillFromPosition}.
6
+ *
7
+ * @public
8
+ */
9
+ export { FillClassification }
2
10
 
3
11
  /**
4
12
  * Classify a perpetual fill into the Open/Close/Increase/Reduce/Switch
@@ -49,3 +57,24 @@ export function classifyFillFromPosition(
49
57
  }
50
58
  return FillClassification.REDUCED_SHORT
51
59
  }
60
+
61
+ /**
62
+ * Classify a fill as open or close based on realizedPnl.
63
+ * @deprecated Use `Fill.classification` instead — it uses startPosition
64
+ * for accurate open/increase/reduce/close/reverse classification.
65
+ * @public
66
+ */
67
+ export function classifyFill(
68
+ side: OrderSide,
69
+ realizedPnl: string | null | undefined
70
+ ): FillClassification {
71
+ const isClose = realizedPnl != null && parseDecimal(realizedPnl) !== 0
72
+ if (side === OrderSide.BUY) {
73
+ return isClose
74
+ ? FillClassification.CLOSED_SHORT
75
+ : FillClassification.OPENED_LONG
76
+ }
77
+ return isClose
78
+ ? FillClassification.CLOSED_LONG
79
+ : FillClassification.OPENED_SHORT
80
+ }
@@ -1,24 +1,34 @@
1
1
  /**
2
- * Universal perpetual futures calculation utilities.
3
- *
4
- * Pure functions for computing position-related values that any consumer
5
- * of the perps SDK would need. All parameters are required — no default
6
- * values for critical financial parameters.
2
+ * Display-tier order formulas. Every function takes and gives `number`;
3
+ * exact decimal arithmetic happens internally with `DivBig`.
7
4
  */
8
5
 
9
6
  import {
10
7
  type FeeTier,
11
8
  type Market,
12
9
  type MarketContext,
10
+ type Order,
13
11
  type OrderbookLevel,
12
+ OrderSide,
13
+ OrderStatus,
14
14
  PerpsErrorCode,
15
+ type Position,
16
+ PositionSide,
15
17
  type Quote,
16
18
  type QuoteSide,
19
+ type RegularOrder,
17
20
  type TradeType,
21
+ type TriggerOrder,
18
22
  } from '@lifi/perps-types'
19
23
  import type Big from 'big.js'
24
+ import { areFinite, DivBig } from '../decimal/big.js'
20
25
  import { PerpsError } from '../errors/PerpsError.js'
21
- import { areFinite, DivBig } from './decimal.js'
26
+ import {
27
+ isActiveOrderStatus,
28
+ isRegularOrder,
29
+ isTriggerOrder,
30
+ } from '../utils/orderClassification.js'
31
+ import { calculateRealizedPnl } from './position.js'
22
32
 
23
33
  /**
24
34
  * Calculate position size in asset units from margin.
@@ -29,11 +39,11 @@ import { areFinite, DivBig } from './decimal.js'
29
39
  * @returns Position size in asset units
30
40
  * @example
31
41
  * ```ts
32
- * calculatePositionSize(100, 10, 2000) // 0.5 (ETH at $2000)
42
+ * calculateSize(100, 10, 2000) // 0.5 (ETH at $2000)
33
43
  * ```
34
44
  * @public
35
45
  */
36
- export function calculatePositionSize(
46
+ export function calculateSize(
37
47
  marginUsd: number,
38
48
  leverage: number,
39
49
  price: number
@@ -48,78 +58,9 @@ export function calculatePositionSize(
48
58
  }
49
59
 
50
60
  /**
51
- * Calculate notional value of a position.
52
- *
53
- * @param size - Position size in asset units
54
- * @param price - Current asset price
55
- * @returns Notional value in USD
56
- * @public
61
+ * @deprecated Use `calculateSize`. Removed in the next major.
57
62
  */
58
- export function calculateNotionalValue(size: number, price: number): number {
59
- if (!areFinite(size, price)) {
60
- return Number.NaN
61
- }
62
- return new DivBig(size).abs().times(price).toNumber()
63
- }
64
-
65
- /**
66
- * Calculate unrealized PnL.
67
- *
68
- * @param entryPrice - Position entry price
69
- * @param currentPrice - Current market price
70
- * @param size - Position size (positive for long, negative for short)
71
- * @returns Unrealized PnL in USD
72
- * @public
73
- */
74
- export function calculateUnrealizedPnl(
75
- entryPrice: number,
76
- currentPrice: number,
77
- size: number
78
- ): number {
79
- if (!areFinite(entryPrice, currentPrice, size)) {
80
- return Number.NaN
81
- }
82
- return new DivBig(currentPrice).minus(entryPrice).times(size).toNumber()
83
- }
84
-
85
- /**
86
- * Calculate return on equity (ROE) percentage.
87
- *
88
- * @param pnl - Profit/loss in USD
89
- * @param margin - Initial margin in USD
90
- * @returns ROE as percentage (e.g., 10 for 10%)
91
- * @public
92
- */
93
- export function calculateRoe(pnl: number, margin: number): number {
94
- if (margin === 0) {
95
- return 0
96
- }
97
- if (!areFinite(pnl, margin)) {
98
- return Number.NaN
99
- }
100
- return new DivBig(pnl).div(margin).times(100).toNumber()
101
- }
102
-
103
- /**
104
- * Calculate required margin for a position.
105
- *
106
- * @param notionalValue - Position notional value in USD
107
- * @param leverage - Position leverage
108
- * @returns Required margin in USD
109
- * @public
110
- */
111
- export function calculateRequiredMargin(
112
- notionalValue: number,
113
- leverage: number
114
- ): number {
115
- if (leverage === 0) {
116
- return notionalValue / leverage
117
- }
118
- if (!areFinite(notionalValue, leverage)) {
119
- return Number.NaN
120
- }
121
- return new DivBig(notionalValue).div(leverage).toNumber()
122
- }
63
+ export const calculatePositionSize = calculateSize
123
64
 
124
65
  /**
125
66
  * Estimate trading fees.
@@ -163,50 +104,6 @@ export function applySlippage(
163
104
  return new DivBig(price).div(multiplier).toNumber()
164
105
  }
165
106
 
166
- /**
167
- * Distance from the current price to the liquidation price, as a percentage of
168
- * the current price.
169
- *
170
- * @param liquidationPrice - The position's liquidation price
171
- * @param currentPrice - Current market price
172
- * @returns Absolute distance as a percentage, or 0 when current price is zero
173
- * @public
174
- */
175
- export function liquidationDistancePercent(params: {
176
- liquidationPrice: number
177
- currentPrice: number
178
- }): number {
179
- const { liquidationPrice, currentPrice } = params
180
- if (currentPrice === 0) {
181
- return 0
182
- }
183
- return Math.abs((liquidationPrice - currentPrice) / currentPrice) * 100
184
- }
185
-
186
- /**
187
- * Effective leverage of an open position.
188
- *
189
- * leverage = positionValueUsd / marginUsd
190
- *
191
- * @param positionValueUsd - Position notional value in USD
192
- * @param marginUsd - Margin backing the position in USD
193
- * @returns Effective leverage, or 0 when margin is zero
194
- * @public
195
- */
196
- export function effectiveLeverage(params: {
197
- positionValueUsd: number
198
- marginUsd: number
199
- }): number {
200
- const { positionValueUsd, marginUsd } = params
201
- if (marginUsd === 0) {
202
- return 0
203
- }
204
- if (!areFinite(positionValueUsd, marginUsd)) {
205
- return Number.NaN
206
- }
207
- return new DivBig(positionValueUsd).div(marginUsd).toNumber()
208
- }
209
-
210
107
  /**
211
108
  * Signed expected PnL for a trigger price — see {@link calculateExpectedPnl}.
212
109
  *
@@ -252,7 +149,7 @@ export function calculateExpectedPnl(
252
149
  }
253
150
 
254
151
  /**
255
- * Convert a percentage gain/loss to a target price.
152
+ * Calculate the trigger price that realises a percentage gain/loss.
256
153
  *
257
154
  * @param percent - Target gain/loss percentage (positive = profitable direction)
258
155
  * @param entryPrice - Position entry price
@@ -260,7 +157,7 @@ export function calculateExpectedPnl(
260
157
  * @param isLong - True for long positions, false for short
261
158
  * @public
262
159
  */
263
- export function priceFromPercent(
160
+ export function calculateTriggerPrice(
264
161
  percent: number,
265
162
  entryPrice: number,
266
163
  leverage: number,
@@ -280,7 +177,12 @@ export function priceFromPercent(
280
177
  }
281
178
 
282
179
  /**
283
- * Convert a target price to a percentage gain/loss.
180
+ * @deprecated Use `calculateTriggerPrice`. Removed in the next major.
181
+ */
182
+ export const priceFromPercent = calculateTriggerPrice
183
+
184
+ /**
185
+ * Calculate the percentage gain/loss a trigger price realises.
284
186
  *
285
187
  * @param price - Target price
286
188
  * @param entryPrice - Position entry price
@@ -288,7 +190,7 @@ export function priceFromPercent(
288
190
  * @param isLong - True for long positions, false for short
289
191
  * @public
290
192
  */
291
- export function percentFromPrice(
193
+ export function calculateTriggerPercent(
292
194
  price: number,
293
195
  entryPrice: number,
294
196
  leverage: number,
@@ -306,6 +208,11 @@ export function percentFromPrice(
306
208
  return priceDiff.div(entryPrice).times(leverage).times(100).toNumber()
307
209
  }
308
210
 
211
+ /**
212
+ * @deprecated Use `calculateTriggerPercent`. Removed in the next major.
213
+ */
214
+ export const percentFromPrice = calculateTriggerPercent
215
+
309
216
  /**
310
217
  * Calculate realized PnL as a percentage of position value at close.
311
218
  *
@@ -462,3 +369,174 @@ export function buildQuote(input: BuildQuoteInput): Quote {
462
369
  timestamp: input.timestamp,
463
370
  }
464
371
  }
372
+
373
+ /**
374
+ * Pick the matching open position for an order's market, if any.
375
+ *
376
+ * @param marketId - The order's `market.id`.
377
+ * @param positions - Open positions list (any market).
378
+ * @public
379
+ */
380
+ export function findMatchingPosition(
381
+ marketId: string,
382
+ positions: readonly Position[]
383
+ ): Position | undefined {
384
+ return positions.find((p) => p.market.id === marketId)
385
+ }
386
+
387
+ /**
388
+ * Resolve the close-size against a position, applying the spec's cap rules:
389
+ * - `orderSize === 0` is the Hyperliquid convention for "close entire
390
+ * position" → close the full position size.
391
+ * - Otherwise cap at the absolute position size; an order larger than the
392
+ * position can only close what's open.
393
+ *
394
+ * Inputs are non-negative magnitudes.
395
+ * @public
396
+ */
397
+ export function resolveCloseSize(
398
+ orderSize: number,
399
+ positionSize: number
400
+ ): number {
401
+ if (orderSize === 0) {
402
+ return positionSize
403
+ }
404
+ return Math.min(orderSize, positionSize)
405
+ }
406
+
407
+ /**
408
+ * Expected rPnL for a resting limit order against a matching position.
409
+ *
410
+ * Reducing requires opposite sides (long position + SELL, short position +
411
+ * BUY). Same-side orders add to the position and have no rPnL → `null`.
412
+ * Projects `remainingSize`, because an already-filled quantity has realised
413
+ * its PnL at the fill price rather than at this order's limit price. A
414
+ * `remainingSize` of zero leaves nothing to project.
415
+ */
416
+ function regularOrderRealizedPnl(
417
+ order: RegularOrder,
418
+ position: Position | undefined
419
+ ): number | null {
420
+ if (!position || !isActiveOrderStatus(order.status)) {
421
+ return null
422
+ }
423
+
424
+ const isLong = position.side === PositionSide.LONG
425
+ const reducesPosition =
426
+ (isLong && order.side === OrderSide.SELL) ||
427
+ (!isLong && order.side === OrderSide.BUY)
428
+ if (!reducesPosition) {
429
+ return null
430
+ }
431
+
432
+ const limitPrice = Number.parseFloat(order.price ?? '')
433
+ const entryPrice = Number.parseFloat(position.entryPrice)
434
+ const orderSize = Math.abs(Number.parseFloat(order.remainingSize))
435
+ const positionSize = Math.abs(Number.parseFloat(position.size))
436
+ if (
437
+ !Number.isFinite(limitPrice) ||
438
+ !Number.isFinite(entryPrice) ||
439
+ !Number.isFinite(orderSize) ||
440
+ !Number.isFinite(positionSize) ||
441
+ positionSize <= 0
442
+ ) {
443
+ return null
444
+ }
445
+
446
+ // `resolveCloseSize` reads a zero size as "close the whole position", a
447
+ // convention that belongs to an order's submitted size. `remainingSize` is
448
+ // the unfilled quantity, so zero means nothing is left to fill.
449
+ if (orderSize === 0) {
450
+ return null
451
+ }
452
+
453
+ const closeSize = resolveCloseSize(orderSize, positionSize)
454
+ return calculateRealizedPnl({
455
+ entryPrice,
456
+ closePrice: limitPrice,
457
+ closeSize,
458
+ isLong,
459
+ })
460
+ }
461
+
462
+ /** Project the unfilled closing quantity of an active trigger at its trigger price. */
463
+ function triggerOrderRealizedPnl(
464
+ order: TriggerOrder,
465
+ position: Position | undefined
466
+ ): number | null {
467
+ if (
468
+ !position ||
469
+ !isActiveOrderStatus(order.status) ||
470
+ order.status === OrderStatus.PENDING
471
+ ) {
472
+ return null
473
+ }
474
+
475
+ const isLong = position.side === PositionSide.LONG
476
+ if (
477
+ (isLong && order.side !== OrderSide.SELL) ||
478
+ (!isLong && order.side !== OrderSide.BUY)
479
+ ) {
480
+ return null
481
+ }
482
+ const triggerPrice = Number.parseFloat(order.triggerPrice)
483
+ const entryPrice = Number.parseFloat(position.entryPrice)
484
+ const orderSize = Math.abs(Number.parseFloat(order.remainingSize))
485
+ const positionSize = Math.abs(Number.parseFloat(position.size))
486
+ if (
487
+ !Number.isFinite(triggerPrice) ||
488
+ !Number.isFinite(entryPrice) ||
489
+ !Number.isFinite(orderSize) ||
490
+ !Number.isFinite(positionSize) ||
491
+ positionSize <= 0
492
+ ) {
493
+ return null
494
+ }
495
+ if (
496
+ orderSize === 0 &&
497
+ (Number.parseFloat(order.originalSize) !== 0 || !order.reduceOnly)
498
+ ) {
499
+ return null
500
+ }
501
+
502
+ const closeSize = resolveCloseSize(orderSize, positionSize)
503
+ return calculateRealizedPnl({
504
+ entryPrice,
505
+ closePrice: triggerPrice,
506
+ closeSize,
507
+ isLong,
508
+ })
509
+ }
510
+
511
+ /**
512
+ * Expected rPnL an unfilled order would realise against a matching position:
513
+ * a resting limit order prices off its limit, an armed trigger off its trigger
514
+ * price. A TWAP parent carries no single execution price, so it projects
515
+ * nothing.
516
+ *
517
+ * @returns Realised PnL if the order would reduce the position, otherwise
518
+ * `null`.
519
+ * @public
520
+ */
521
+ export function estimateRealizedPnl(
522
+ order: Order,
523
+ position: Position | undefined
524
+ ): number | null {
525
+ if (isTriggerOrder(order)) {
526
+ return triggerOrderRealizedPnl(order, position)
527
+ }
528
+ if (isRegularOrder(order)) {
529
+ return regularOrderRealizedPnl(order, position)
530
+ }
531
+ return null
532
+ }
533
+
534
+ /**
535
+ * @deprecated Use `estimateRealizedPnl`. Removed in the next major.
536
+ */
537
+ export const expectedRealizedPnlForOpenOrder = regularOrderRealizedPnl
538
+
539
+ /**
540
+ * @deprecated Use `estimateRealizedPnl`. Removed in the next major.
541
+ */
542
+ export const expectedRealizedPnlForTriggerOrder = triggerOrderRealizedPnl