@lifi/perps-sdk 20.1.1 → 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 +2 -0
  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 +12 -0
  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 +12 -0
  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 +12 -0
  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
@@ -1,28 +0,0 @@
1
- /**
2
- * Parse a string to a float, stripping common formatting artefacts.
3
- *
4
- * Handles:
5
- * - Currency prefixes/suffixes ($, USD, etc.)
6
- * - Explicit sign prefixes (+/-)
7
- * - Thousands separators (commas)
8
- * - Percentage suffixes (%)
9
- * - Whitespace
10
- *
11
- * @returns Parsed number; `0` for empty/blank input, `NaN` when a non-empty
12
- * string contains no parseable number
13
- * @example
14
- * ```ts
15
- * stringToFloat('$1,234.50') // 1234.5
16
- * stringToFloat('') // 0
17
- * ```
18
- * @public
19
- */
20
- export declare function stringToFloat(value: string): number;
21
- /**
22
- * Parse an optional decimal field, with the same formatting tolerance as {@link stringToFloat}.
23
- * Gives back `undefined` for a missing, malformed or non-finite value, and `0` for an empty string.
24
- *
25
- * @public
26
- */
27
- export declare function parseDecimal(value: string | null | undefined): number | undefined;
28
- //# sourceMappingURL=parse.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/utils/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CASnD;AAKD;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAC/B,MAAM,GAAG,SAAS,CAYpB"}
@@ -1,120 +0,0 @@
1
- /**
2
- * Pure-function helpers for predicting how a perp position changes after a
3
- * fill.
4
- *
5
- * Sign convention: long = +1, short = -1. Sizes passed to these helpers are
6
- * always non-negative magnitudes; direction is carried by `isLong`.
7
- */
8
- /**
9
- * Direction sign for a position.
10
- *
11
- * @param isLong - True for long positions, false for short.
12
- * @returns +1 for long, -1 for short.
13
- * @public
14
- */
15
- export declare function directionSign(isLong: boolean): 1 | -1;
16
- /**
17
- * Estimated liquidation price for an isolated-margin position, parameterised
18
- * by the venue's maintenance margin rate.
19
- *
20
- * Standard isolated-margin model (new position, no existing exposure):
21
- * margin_per_unit = entryPrice / leverage
22
- * maintenance_per_unit = entryPrice * mmr
23
- * margin_available = margin_per_unit - maintenance_per_unit
24
- * liq_price = entryPrice - side * margin_available / (1 - mmr * side)
25
- *
26
- * For existing positions, prefer `Position.liquidationPrice` from the venue.
27
- *
28
- * @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
29
- * (e.g. 0.01 = 1%).
30
- * @returns Estimated liquidation price, or undefined if the inputs cannot
31
- * produce one (zero leverage, degenerate denominator).
32
- * @public
33
- */
34
- export declare function estimateIsolatedLiquidationPrice(params: {
35
- entryPrice: number;
36
- leverage: number;
37
- isLong: boolean;
38
- maintenanceMarginRate: number;
39
- }): number | undefined;
40
- /**
41
- * Whether an isolated position with this liquidation price is already past
42
- * liquidation at `currentPrice`: a long liquidates when the price falls to its
43
- * liquidation level, a short when it rises to it. A non-positive price on
44
- * either side counts as unknown, so the result is `false`.
45
- *
46
- * @public
47
- */
48
- export declare function wouldImmediatelyLiquidate(params: {
49
- liquidationPrice: number;
50
- currentPrice: number;
51
- isLong: boolean;
52
- }): boolean;
53
- /**
54
- * Predicted average entry price after adding to an existing position.
55
- *
56
- * Weighted average of the current entry and the new fill price, weighted by
57
- * the size of each leg in coin units. Both legs assumed in the same direction
58
- * (this helper is for adding to, not flipping, a position).
59
- *
60
- * @param currentSize - Existing position size in coin units (>= 0).
61
- * @param currentEntry - Existing position's average entry price.
62
- * @param addSize - Size being added in coin units (>= 0).
63
- * @param fillPrice - Price the new size is expected to fill at (mid for
64
- * market, limitPrice for limit orders).
65
- * @returns The new weighted-average entry price, or undefined if the inputs
66
- * cannot produce a valid average (zero combined size, non-finite values).
67
- * @public
68
- */
69
- export declare function predictAverageEntryPrice(params: {
70
- currentSize: number;
71
- currentEntry: number;
72
- addSize: number;
73
- fillPrice: number;
74
- }): number | undefined;
75
- /**
76
- * Predicted effective leverage after adding margin and notional.
77
- *
78
- * leverage = totalNotional / totalMargin. The caller computes notional from
79
- * size and price (`calculateNotionalValue`) and supplies the additional
80
- * margin the user is about to put up.
81
- *
82
- * @returns The new effective leverage, or undefined if total margin is
83
- * non-positive.
84
- * @public
85
- */
86
- export declare function predictNewLeverage(params: {
87
- currentNotional: number;
88
- currentMargin: number;
89
- addNotional: number;
90
- addMargin: number;
91
- }): number | undefined;
92
- /**
93
- * Predicted unrealised PnL at the current mark price.
94
- *
95
- * `pnl = (markPrice - entryPrice) * size * directionSign(isLong)`
96
- *
97
- * @param size - Position size as a non-negative magnitude.
98
- * @public
99
- */
100
- export declare function predictUnrealizedPnl(params: {
101
- entryPrice: number;
102
- markPrice: number;
103
- size: number;
104
- isLong: boolean;
105
- }): number;
106
- /**
107
- * Realised PnL on the portion of a position being closed.
108
- *
109
- * `rPnl = (closePrice - entryPrice) * closeSize * directionSign(isLong)`
110
- *
111
- * @param closeSize - Size being closed as a non-negative magnitude.
112
- * @public
113
- */
114
- export declare function realizedPnlOnClose(params: {
115
- entryPrice: number;
116
- closePrice: number;
117
- closeSize: number;
118
- isLong: boolean;
119
- }): number;
120
- //# sourceMappingURL=positionMath.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"positionMath.d.ts","sourceRoot":"","sources":["../../../src/utils/positionMath.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAErD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gCAAgC,CAAC,MAAM,EAAE;IACvD,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;IACf,qBAAqB,EAAE,MAAM,CAAA;CAC9B,GAAG,MAAM,GAAG,SAAS,CAqBrB;AAED;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,EAAE;IAChD,gBAAgB,EAAE,MAAM,CAAA;IACxB,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,EAAE,OAAO,CAAA;CAChB,GAAG,OAAO,CAQV;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE;IAC/C,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB,GAAG,MAAM,GAAG,SAAS,CAiBrB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IACzC,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,MAAM,CAAA;IACrB,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;CAClB,GAAG,MAAM,GAAG,SAAS,CAarB;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAC3C,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,OAAO,CAAA;CAChB,GAAG,MAAM,CAUT;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IACzC,UAAU,EAAE,MAAM,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,OAAO,CAAA;CAChB,GAAG,MAAM,CAUT"}
@@ -1,22 +0,0 @@
1
- /**
2
- * Off-grid resolution for {@link scaleToInteger}:
3
- * - `truncate` — toward zero, for sizes and collateral amounts. The wire
4
- * value never exceeds the caller's intent.
5
- * - `round` — half away from zero, for prices, which snap to the nearest
6
- * tick.
7
- * @public
8
- */
9
- export type ScaleToIntegerPolicy = 'truncate' | 'round';
10
- /**
11
- * Scale a human-readable decimal string to a scaled integer in exact decimal
12
- * arithmetic — an on-grid input maps to its exact scaled integer with no
13
- * binary float artifacts (`'0.29'` at 2 decimals is 29, never 28).
14
- * Off-grid input resolves per `policy`; there is no silent default.
15
- *
16
- * @throws {PerpsError} `ValidationError` when `value` is not a decimal
17
- * numeric string, `decimals` is not a non-negative integer, or the scaled
18
- * result's magnitude exceeds `Number.MAX_SAFE_INTEGER`.
19
- * @public
20
- */
21
- export declare const scaleToInteger: (value: string, decimals: number, policy: ScaleToIntegerPolicy) => number;
22
- //# sourceMappingURL=scaleToInteger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"scaleToInteger.d.ts","sourceRoot":"","sources":["../../../src/utils/scaleToInteger.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,MAAM,MAAM,oBAAoB,GAAG,UAAU,GAAG,OAAO,CAAA;AAEvD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,cAAc,GACzB,OAAO,MAAM,EACb,UAAU,MAAM,EAChB,QAAQ,oBAAoB,KAC3B,MA0BF,CAAA"}
@@ -1,27 +0,0 @@
1
- /**
2
- * Token unit conversion utilities.
3
- */
4
- /**
5
- * Convert a base-unit amount (bigint string) to a human-readable decimal string.
6
- *
7
- * @param amount - Amount in base units (e.g. "1000000" for 1 USDC)
8
- * @param decimals - Token decimals (e.g. 6 for USDC)
9
- * @returns Decimal string (e.g. "1.0"); `"0"` when `amount` is not a valid
10
- * bigint
11
- * @example
12
- * ```ts
13
- * fromBaseUnits('1000000', 6) // '1'
14
- * ```
15
- * @public
16
- */
17
- export declare function fromBaseUnits(amount: string, decimals: number): string;
18
- /**
19
- * Convert a base-unit amount to a number.
20
- *
21
- * @param amount - Amount in base units
22
- * @param decimals - Token decimals
23
- * @returns Numeric value, or 0 if parsing fails
24
- * @public
25
- */
26
- export declare function fromBaseUnitsNumber(amount: string, decimals: number): number;
27
- //# sourceMappingURL=units.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../../src/utils/units.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMtE;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAG5E"}
@@ -1,18 +0,0 @@
1
- /**
2
- * Order validation utilities.
3
- */
4
- /**
5
- * Validate margin input against available balance and exchange minimums.
6
- *
7
- * @param margin - Margin amount in USD
8
- * @param _leverage - Position leverage (reserved for future use)
9
- * @param availableBalance - Available balance, or null if unknown
10
- * @param _feeRate - Fee rate as decimal, or null if unknown (reserved for future use)
11
- * @param minMarginUsd - Minimum margin requirement in USD (exchange-specific).
12
- * Source this from the selected provider's `minOrderValueUsd` metadata
13
- * (see `@lifi/perps-types` `Provider`) rather than a client-side constant.
14
- * @returns Error code: 'insufficient' | 'below-minimum' | '' (valid)
15
- * @public
16
- */
17
- export declare function validateMargin(margin: number, _leverage: number, availableBalance: number | null, _feeRate: number | null, minMarginUsd: number): string;
18
- //# sourceMappingURL=validation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/utils/validation.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,gBAAgB,EAAE,MAAM,GAAG,IAAI,EAC/B,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,YAAY,EAAE,MAAM,GACnB,MAAM,CAcR"}
package/src/utils/math.ts DELETED
@@ -1,7 +0,0 @@
1
- import type Big from 'big.js'
2
-
3
- /** The larger of two Big values, as `Math.max` for numbers. */
4
- export const maxOf = (a: Big, b: Big): Big => (a.gt(b) ? a : b)
5
-
6
- /** The smaller of two Big values, as `Math.min` for numbers. */
7
- export const minOf = (a: Big, b: Big): Big => (a.lt(b) ? a : b)
@@ -1,38 +0,0 @@
1
- import type Big from 'big.js'
2
- import { TruncBig } from './decimal.js'
3
-
4
- /**
5
- * Base size the given margin buys at `leverage`: size = margin × leverage ÷ price.
6
- * Truncates, so `size × price` never exceeds `margin × leverage`.
7
- * @public
8
- */
9
- export function sizeFromMargin(margin: Big, leverage: number, price: Big): Big {
10
- return new TruncBig(margin).times(leverage).div(price)
11
- }
12
-
13
- /**
14
- * Margin that holds the given base size at `leverage`: margin = size × price ÷ leverage.
15
- * Truncates, so `margin × leverage` never exceeds `size × price`.
16
- * @public
17
- */
18
- export function marginFromSize(size: Big, leverage: number, price: Big): Big {
19
- return new TruncBig(size).times(price).div(leverage)
20
- }
21
-
22
- /**
23
- * Base size for a notional: size = notional ÷ price.
24
- * Truncates, so `size × price` never exceeds the notional.
25
- * @public
26
- */
27
- export function sizeFromNotional(notionalUsd: Big, price: Big): Big {
28
- return new TruncBig(notionalUsd).div(price)
29
- }
30
-
31
- /**
32
- * Margin for a notional at `leverage`: margin = notional ÷ leverage.
33
- * Truncates, so `margin × leverage` never exceeds the notional.
34
- * @public
35
- */
36
- export function marginFromNotional(notionalUsd: Big, leverage: number): Big {
37
- return new TruncBig(notionalUsd).div(leverage)
38
- }
@@ -1,152 +0,0 @@
1
- import {
2
- OrderSide,
3
- OrderStatus,
4
- type Position,
5
- PositionSide,
6
- type RegularOrder,
7
- type TriggerOrder,
8
- } from '@lifi/perps-types'
9
- import { isActiveOrderStatus } from './orderClassification.js'
10
- import { realizedPnlOnClose } from './positionMath.js'
11
-
12
- /**
13
- * Pick the matching open position for an order's market, if any.
14
- *
15
- * @param marketId - The order's `market.id`.
16
- * @param positions - Open positions list (any market).
17
- * @public
18
- */
19
- export function findMatchingPosition(
20
- marketId: string,
21
- positions: readonly Position[]
22
- ): Position | undefined {
23
- return positions.find((p) => p.market.id === marketId)
24
- }
25
-
26
- /**
27
- * Resolve the close-size against a position, applying the spec's cap rules:
28
- * - `orderSize === 0` is the Hyperliquid convention for "close entire
29
- * position" → close the full position size.
30
- * - Otherwise cap at the absolute position size; an order larger than the
31
- * position can only close what's open.
32
- *
33
- * Inputs are non-negative magnitudes.
34
- * @public
35
- */
36
- export function resolveCloseSize(
37
- orderSize: number,
38
- positionSize: number
39
- ): number {
40
- if (orderSize === 0) {
41
- return positionSize
42
- }
43
- return Math.min(orderSize, positionSize)
44
- }
45
-
46
- /**
47
- * Expected rPnL for a resting limit order against a matching position.
48
- *
49
- * Reducing requires opposite sides (long position + SELL, short position +
50
- * BUY). Same-side orders add to the position and have no rPnL → `null`.
51
- * Projects `remainingSize`, because an already-filled quantity has realised
52
- * its PnL at the fill price rather than at this order's limit price. A
53
- * `remainingSize` of zero leaves nothing to project.
54
- *
55
- * @returns Realised PnL if the order would reduce the position, otherwise
56
- * `null`.
57
- * @public
58
- */
59
- export function expectedRealizedPnlForOpenOrder(
60
- order: RegularOrder,
61
- position: Position | undefined
62
- ): number | null {
63
- if (!position || !isActiveOrderStatus(order.status)) {
64
- return null
65
- }
66
-
67
- const isLong = position.side === PositionSide.LONG
68
- const reducesPosition =
69
- (isLong && order.side === OrderSide.SELL) ||
70
- (!isLong && order.side === OrderSide.BUY)
71
- if (!reducesPosition) {
72
- return null
73
- }
74
-
75
- const limitPrice = Number.parseFloat(order.price ?? '')
76
- const entryPrice = Number.parseFloat(position.entryPrice)
77
- const orderSize = Math.abs(Number.parseFloat(order.remainingSize))
78
- const positionSize = Math.abs(Number.parseFloat(position.size))
79
- if (
80
- !Number.isFinite(limitPrice) ||
81
- !Number.isFinite(entryPrice) ||
82
- !Number.isFinite(orderSize) ||
83
- !Number.isFinite(positionSize) ||
84
- positionSize <= 0
85
- ) {
86
- return null
87
- }
88
-
89
- // `resolveCloseSize` reads a zero size as "close the whole position", a
90
- // convention that belongs to an order's submitted size. `remainingSize` is
91
- // the unfilled quantity, so zero means nothing is left to fill.
92
- if (orderSize === 0) {
93
- return null
94
- }
95
-
96
- const closeSize = resolveCloseSize(orderSize, positionSize)
97
- return realizedPnlOnClose({
98
- entryPrice,
99
- closePrice: limitPrice,
100
- closeSize,
101
- isLong,
102
- })
103
- }
104
-
105
- /** Project the unfilled closing quantity of an active trigger at its trigger price. */
106
- export function expectedRealizedPnlForTriggerOrder(
107
- order: TriggerOrder,
108
- position: Position | undefined
109
- ): number | null {
110
- if (
111
- !position ||
112
- !isActiveOrderStatus(order.status) ||
113
- order.status === OrderStatus.PENDING
114
- ) {
115
- return null
116
- }
117
-
118
- const isLong = position.side === PositionSide.LONG
119
- if (
120
- (isLong && order.side !== OrderSide.SELL) ||
121
- (!isLong && order.side !== OrderSide.BUY)
122
- ) {
123
- return null
124
- }
125
- const triggerPrice = Number.parseFloat(order.triggerPrice)
126
- const entryPrice = Number.parseFloat(position.entryPrice)
127
- const orderSize = Math.abs(Number.parseFloat(order.remainingSize))
128
- const positionSize = Math.abs(Number.parseFloat(position.size))
129
- if (
130
- !Number.isFinite(triggerPrice) ||
131
- !Number.isFinite(entryPrice) ||
132
- !Number.isFinite(orderSize) ||
133
- !Number.isFinite(positionSize) ||
134
- positionSize <= 0
135
- ) {
136
- return null
137
- }
138
- if (
139
- orderSize === 0 &&
140
- (Number.parseFloat(order.originalSize) !== 0 || !order.reduceOnly)
141
- ) {
142
- return null
143
- }
144
-
145
- const closeSize = resolveCloseSize(orderSize, positionSize)
146
- return realizedPnlOnClose({
147
- entryPrice,
148
- closePrice: triggerPrice,
149
- closeSize,
150
- isLong,
151
- })
152
- }
@@ -1,56 +0,0 @@
1
- import { PerpsErrorCode } from '@lifi/perps-types'
2
- import Big from 'big.js'
3
- import { PerpsError } from '../errors/PerpsError.js'
4
-
5
- /**
6
- * Off-grid resolution for {@link scaleToInteger}:
7
- * - `truncate` — toward zero, for sizes and collateral amounts. The wire
8
- * value never exceeds the caller's intent.
9
- * - `round` — half away from zero, for prices, which snap to the nearest
10
- * tick.
11
- * @public
12
- */
13
- export type ScaleToIntegerPolicy = 'truncate' | 'round'
14
-
15
- /**
16
- * Scale a human-readable decimal string to a scaled integer in exact decimal
17
- * arithmetic — an on-grid input maps to its exact scaled integer with no
18
- * binary float artifacts (`'0.29'` at 2 decimals is 29, never 28).
19
- * Off-grid input resolves per `policy`; there is no silent default.
20
- *
21
- * @throws {PerpsError} `ValidationError` when `value` is not a decimal
22
- * numeric string, `decimals` is not a non-negative integer, or the scaled
23
- * result's magnitude exceeds `Number.MAX_SAFE_INTEGER`.
24
- * @public
25
- */
26
- export const scaleToInteger = (
27
- value: string,
28
- decimals: number,
29
- policy: ScaleToIntegerPolicy
30
- ): number => {
31
- if (!Number.isInteger(decimals) || decimals < 0) {
32
- throw new PerpsError(
33
- PerpsErrorCode.ValidationError,
34
- `Invalid decimals for integer scaling: ${decimals}`
35
- )
36
- }
37
- let parsed: Big
38
- try {
39
- parsed = new Big(value)
40
- } catch {
41
- throw new PerpsError(
42
- PerpsErrorCode.ValidationError,
43
- `Invalid decimal string for integer scaling: '${value}'`
44
- )
45
- }
46
- const scaled = parsed
47
- .times(new Big(10).pow(decimals))
48
- .round(0, policy === 'truncate' ? Big.roundDown : Big.roundHalfUp)
49
- if (scaled.abs().gt(Number.MAX_SAFE_INTEGER)) {
50
- throw new PerpsError(
51
- PerpsErrorCode.ValidationError,
52
- `Scaled value ${scaled.toFixed()} exceeds Number.MAX_SAFE_INTEGER and cannot be encoded exactly.`
53
- )
54
- }
55
- return scaled.toNumber()
56
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * Token unit conversion utilities.
3
- */
4
-
5
- import { formatUnits } from 'viem'
6
-
7
- /**
8
- * Convert a base-unit amount (bigint string) to a human-readable decimal string.
9
- *
10
- * @param amount - Amount in base units (e.g. "1000000" for 1 USDC)
11
- * @param decimals - Token decimals (e.g. 6 for USDC)
12
- * @returns Decimal string (e.g. "1.0"); `"0"` when `amount` is not a valid
13
- * bigint
14
- * @example
15
- * ```ts
16
- * fromBaseUnits('1000000', 6) // '1'
17
- * ```
18
- * @public
19
- */
20
- export function fromBaseUnits(amount: string, decimals: number): string {
21
- try {
22
- return formatUnits(BigInt(amount), decimals)
23
- } catch {
24
- return '0'
25
- }
26
- }
27
-
28
- /**
29
- * Convert a base-unit amount to a number.
30
- *
31
- * @param amount - Amount in base units
32
- * @param decimals - Token decimals
33
- * @returns Numeric value, or 0 if parsing fails
34
- * @public
35
- */
36
- export function fromBaseUnitsNumber(amount: string, decimals: number): number {
37
- const value = Number(fromBaseUnits(amount, decimals))
38
- return Number.isFinite(value) ? value : 0
39
- }
@@ -1,38 +0,0 @@
1
- /**
2
- * Order validation utilities.
3
- */
4
-
5
- /**
6
- * Validate margin input against available balance and exchange minimums.
7
- *
8
- * @param margin - Margin amount in USD
9
- * @param _leverage - Position leverage (reserved for future use)
10
- * @param availableBalance - Available balance, or null if unknown
11
- * @param _feeRate - Fee rate as decimal, or null if unknown (reserved for future use)
12
- * @param minMarginUsd - Minimum margin requirement in USD (exchange-specific).
13
- * Source this from the selected provider's `minOrderValueUsd` metadata
14
- * (see `@lifi/perps-types` `Provider`) rather than a client-side constant.
15
- * @returns Error code: 'insufficient' | 'below-minimum' | '' (valid)
16
- * @public
17
- */
18
- export function validateMargin(
19
- margin: number,
20
- _leverage: number,
21
- availableBalance: number | null,
22
- _feeRate: number | null,
23
- minMarginUsd: number
24
- ): string {
25
- if (margin <= 0) {
26
- return ''
27
- }
28
-
29
- if (availableBalance !== null && margin > availableBalance) {
30
- return 'insufficient'
31
- }
32
-
33
- if (margin < minMarginUsd) {
34
- return 'below-minimum'
35
- }
36
-
37
- return ''
38
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes