@lifi/perps-sdk 23.1.0 → 25.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 (305) hide show
  1. package/README.md +102 -56
  2. package/dist/cjs/client/PerpsClient.d.ts +3 -2
  3. package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
  4. package/dist/cjs/client/PerpsClient.js +16 -9
  5. package/dist/cjs/client/PerpsClient.js.map +1 -1
  6. package/dist/cjs/decimal/arithmetic.d.ts +15 -0
  7. package/dist/cjs/decimal/arithmetic.d.ts.map +1 -0
  8. package/dist/cjs/decimal/arithmetic.js +53 -0
  9. package/dist/cjs/decimal/arithmetic.js.map +1 -0
  10. package/dist/cjs/decimal/big.d.ts +0 -1
  11. package/dist/cjs/decimal/big.d.ts.map +1 -1
  12. package/dist/cjs/decimal/big.js +0 -4
  13. package/dist/cjs/decimal/big.js.map +1 -1
  14. package/dist/cjs/decimal/compare.d.ts +6 -2
  15. package/dist/cjs/decimal/compare.d.ts.map +1 -1
  16. package/dist/cjs/decimal/compare.js +15 -2
  17. package/dist/cjs/decimal/compare.js.map +1 -1
  18. package/dist/cjs/decimal/convert.d.ts +10 -6
  19. package/dist/cjs/decimal/convert.d.ts.map +1 -1
  20. package/dist/cjs/decimal/convert.js +34 -21
  21. package/dist/cjs/decimal/convert.js.map +1 -1
  22. package/dist/cjs/decimal/decimalStringToBig.d.ts +5 -0
  23. package/dist/cjs/decimal/decimalStringToBig.d.ts.map +1 -0
  24. package/dist/cjs/decimal/decimalStringToBig.js +25 -0
  25. package/dist/cjs/decimal/decimalStringToBig.js.map +1 -0
  26. package/dist/cjs/decimal/format.d.ts.map +1 -1
  27. package/dist/cjs/decimal/format.js +70 -52
  28. package/dist/cjs/decimal/format.js.map +1 -1
  29. package/dist/cjs/decimal/parse.d.ts +5 -4
  30. package/dist/cjs/decimal/parse.d.ts.map +1 -1
  31. package/dist/cjs/decimal/parse.js +43 -25
  32. package/dist/cjs/decimal/parse.js.map +1 -1
  33. package/dist/cjs/errors/invalidInput.d.ts +3 -0
  34. package/dist/cjs/errors/invalidInput.d.ts.map +1 -0
  35. package/dist/cjs/errors/invalidInput.js +9 -0
  36. package/dist/cjs/errors/invalidInput.js.map +1 -0
  37. package/dist/cjs/index.d.ts +15 -12
  38. package/dist/cjs/index.d.ts.map +1 -1
  39. package/dist/cjs/index.js +69 -9
  40. package/dist/cjs/index.js.map +1 -1
  41. package/dist/cjs/math/fills.d.ts +1 -1
  42. package/dist/cjs/math/fills.d.ts.map +1 -1
  43. package/dist/cjs/math/fills.js +28 -13
  44. package/dist/cjs/math/fills.js.map +1 -1
  45. package/dist/cjs/math/order.d.ts +27 -16
  46. package/dist/cjs/math/order.d.ts.map +1 -1
  47. package/dist/cjs/math/order.js +131 -134
  48. package/dist/cjs/math/order.js.map +1 -1
  49. package/dist/cjs/math/position.d.ts +85 -35
  50. package/dist/cjs/math/position.d.ts.map +1 -1
  51. package/dist/cjs/math/position.js +93 -94
  52. package/dist/cjs/math/position.js.map +1 -1
  53. package/dist/cjs/services/getGasRecommendation.js +1 -1
  54. package/dist/cjs/services/getGasRecommendation.js.map +1 -1
  55. package/dist/cjs/services/getQuote.d.ts +1 -1
  56. package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
  57. package/dist/cjs/services/resolveSubscribeQuote.js +3 -2
  58. package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
  59. package/dist/cjs/transport/request.d.ts +1 -0
  60. package/dist/cjs/transport/request.d.ts.map +1 -1
  61. package/dist/cjs/transport/request.js +2 -2
  62. package/dist/cjs/transport/request.js.map +1 -1
  63. package/dist/cjs/types/api.d.ts +4 -0
  64. package/dist/cjs/types/api.d.ts.map +1 -1
  65. package/dist/cjs/types/provider.d.ts +9 -5
  66. package/dist/cjs/types/provider.d.ts.map +1 -1
  67. package/dist/cjs/types/withdrawal.d.ts +12 -4
  68. package/dist/cjs/types/withdrawal.d.ts.map +1 -1
  69. package/dist/cjs/utils/createSafeFunction.d.ts +2 -0
  70. package/dist/cjs/utils/createSafeFunction.d.ts.map +1 -0
  71. package/dist/cjs/utils/createSafeFunction.js +15 -0
  72. package/dist/cjs/utils/createSafeFunction.js.map +1 -0
  73. package/dist/cjs/utils/timestampToIsoString.d.ts +3 -0
  74. package/dist/cjs/utils/timestampToIsoString.d.ts.map +1 -0
  75. package/dist/cjs/utils/timestampToIsoString.js +16 -0
  76. package/dist/cjs/utils/timestampToIsoString.js.map +1 -0
  77. package/dist/cjs/utils/warnOnce.d.ts +6 -0
  78. package/dist/cjs/utils/warnOnce.d.ts.map +1 -1
  79. package/dist/cjs/utils/warnOnce.js +19 -1
  80. package/dist/cjs/utils/warnOnce.js.map +1 -1
  81. package/dist/cjs/version.d.ts +1 -1
  82. package/dist/cjs/version.js +1 -1
  83. package/dist/cjs/websocket/PerpsWsClient.d.ts +2 -0
  84. package/dist/cjs/websocket/PerpsWsClient.d.ts.map +1 -1
  85. package/dist/cjs/websocket/PerpsWsClient.js +14 -1
  86. package/dist/cjs/websocket/PerpsWsClient.js.map +1 -1
  87. package/dist/cjs/websocket/wsLog.d.ts +2 -0
  88. package/dist/cjs/websocket/wsLog.d.ts.map +1 -1
  89. package/dist/cjs/websocket/wsLog.js +9 -0
  90. package/dist/cjs/websocket/wsLog.js.map +1 -1
  91. package/dist/cjs/wire/orderAmounts.d.ts +3 -2
  92. package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
  93. package/dist/cjs/wire/orderAmounts.js +22 -21
  94. package/dist/cjs/wire/orderAmounts.js.map +1 -1
  95. package/dist/cjs/wire/refuel.d.ts +4 -1
  96. package/dist/cjs/wire/refuel.d.ts.map +1 -1
  97. package/dist/cjs/wire/refuel.js +23 -6
  98. package/dist/cjs/wire/refuel.js.map +1 -1
  99. package/dist/cjs/wire/transferable.js +3 -3
  100. package/dist/cjs/wire/transferable.js.map +1 -1
  101. package/dist/cjs/wire/withdraw.d.ts.map +1 -1
  102. package/dist/cjs/wire/withdraw.js +3 -3
  103. package/dist/cjs/wire/withdraw.js.map +1 -1
  104. package/dist/esm/client/PerpsClient.d.ts +17 -4
  105. package/dist/esm/client/PerpsClient.d.ts.map +1 -1
  106. package/dist/esm/client/PerpsClient.js +30 -8
  107. package/dist/esm/client/PerpsClient.js.map +1 -1
  108. package/dist/esm/decimal/arithmetic.d.ts +98 -0
  109. package/dist/esm/decimal/arithmetic.d.ts.map +1 -0
  110. package/dist/esm/decimal/arithmetic.js +126 -0
  111. package/dist/esm/decimal/arithmetic.js.map +1 -0
  112. package/dist/esm/decimal/big.d.ts +0 -2
  113. package/dist/esm/decimal/big.d.ts.map +1 -1
  114. package/dist/esm/decimal/big.js +0 -4
  115. package/dist/esm/decimal/big.js.map +1 -1
  116. package/dist/esm/decimal/compare.d.ts +40 -6
  117. package/dist/esm/decimal/compare.d.ts.map +1 -1
  118. package/dist/esm/decimal/compare.js +46 -6
  119. package/dist/esm/decimal/compare.js.map +1 -1
  120. package/dist/esm/decimal/convert.d.ts +50 -36
  121. package/dist/esm/decimal/convert.d.ts.map +1 -1
  122. package/dist/esm/decimal/convert.js +64 -42
  123. package/dist/esm/decimal/convert.js.map +1 -1
  124. package/dist/esm/decimal/decimalStringToBig.d.ts +25 -0
  125. package/dist/esm/decimal/decimalStringToBig.d.ts.map +1 -0
  126. package/dist/esm/decimal/decimalStringToBig.js +37 -0
  127. package/dist/esm/decimal/decimalStringToBig.js.map +1 -0
  128. package/dist/esm/decimal/format.d.ts +4 -1
  129. package/dist/esm/decimal/format.d.ts.map +1 -1
  130. package/dist/esm/decimal/format.js +71 -70
  131. package/dist/esm/decimal/format.js.map +1 -1
  132. package/dist/esm/decimal/parse.d.ts +29 -13
  133. package/dist/esm/decimal/parse.d.ts.map +1 -1
  134. package/dist/esm/decimal/parse.js +61 -44
  135. package/dist/esm/decimal/parse.js.map +1 -1
  136. package/dist/esm/errors/invalidInput.d.ts +4 -0
  137. package/dist/esm/errors/invalidInput.d.ts.map +1 -0
  138. package/dist/esm/errors/invalidInput.js +7 -0
  139. package/dist/esm/errors/invalidInput.js.map +1 -0
  140. package/dist/esm/index.d.ts +15 -12
  141. package/dist/esm/index.d.ts.map +1 -1
  142. package/dist/esm/index.js +11 -8
  143. package/dist/esm/index.js.map +1 -1
  144. package/dist/esm/math/fills.d.ts +6 -3
  145. package/dist/esm/math/fills.d.ts.map +1 -1
  146. package/dist/esm/math/fills.js +31 -16
  147. package/dist/esm/math/fills.js.map +1 -1
  148. package/dist/esm/math/order.d.ts +84 -66
  149. package/dist/esm/math/order.d.ts.map +1 -1
  150. package/dist/esm/math/order.js +178 -175
  151. package/dist/esm/math/order.js.map +1 -1
  152. package/dist/esm/math/position.d.ts +155 -98
  153. package/dist/esm/math/position.d.ts.map +1 -1
  154. package/dist/esm/math/position.js +160 -156
  155. package/dist/esm/math/position.js.map +1 -1
  156. package/dist/esm/services/getGasRecommendation.js +1 -1
  157. package/dist/esm/services/getGasRecommendation.js.map +1 -1
  158. package/dist/esm/services/getQuote.d.ts +2 -2
  159. package/dist/esm/services/getQuote.js +1 -1
  160. package/dist/esm/services/resolveSubscribeQuote.d.ts +1 -1
  161. package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
  162. package/dist/esm/services/resolveSubscribeQuote.js +4 -3
  163. package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
  164. package/dist/esm/transport/request.d.ts +5 -0
  165. package/dist/esm/transport/request.d.ts.map +1 -1
  166. package/dist/esm/transport/request.js +2 -2
  167. package/dist/esm/transport/request.js.map +1 -1
  168. package/dist/esm/types/api.d.ts +9 -0
  169. package/dist/esm/types/api.d.ts.map +1 -1
  170. package/dist/esm/types/provider.d.ts +30 -16
  171. package/dist/esm/types/provider.d.ts.map +1 -1
  172. package/dist/esm/types/withdrawal.d.ts +32 -7
  173. package/dist/esm/types/withdrawal.d.ts.map +1 -1
  174. package/dist/esm/utils/createSafeFunction.d.ts +9 -0
  175. package/dist/esm/utils/createSafeFunction.d.ts.map +1 -0
  176. package/dist/esm/utils/createSafeFunction.js +19 -0
  177. package/dist/esm/utils/createSafeFunction.js.map +1 -0
  178. package/dist/esm/utils/timestampToIsoString.d.ts +15 -0
  179. package/dist/esm/utils/timestampToIsoString.d.ts.map +1 -0
  180. package/dist/esm/utils/timestampToIsoString.js +24 -0
  181. package/dist/esm/utils/timestampToIsoString.js.map +1 -0
  182. package/dist/esm/utils/warnOnce.d.ts +27 -0
  183. package/dist/esm/utils/warnOnce.d.ts.map +1 -1
  184. package/dist/esm/utils/warnOnce.js +36 -0
  185. package/dist/esm/utils/warnOnce.js.map +1 -1
  186. package/dist/esm/version.d.ts +1 -1
  187. package/dist/esm/version.js +1 -1
  188. package/dist/esm/websocket/PerpsWsClient.d.ts +7 -0
  189. package/dist/esm/websocket/PerpsWsClient.d.ts.map +1 -1
  190. package/dist/esm/websocket/PerpsWsClient.js +20 -2
  191. package/dist/esm/websocket/PerpsWsClient.js.map +1 -1
  192. package/dist/esm/websocket/wsLog.d.ts +12 -0
  193. package/dist/esm/websocket/wsLog.d.ts.map +1 -1
  194. package/dist/esm/websocket/wsLog.js +19 -0
  195. package/dist/esm/websocket/wsLog.js.map +1 -1
  196. package/dist/esm/wire/orderAmounts.d.ts +9 -7
  197. package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
  198. package/dist/esm/wire/orderAmounts.js +24 -20
  199. package/dist/esm/wire/orderAmounts.js.map +1 -1
  200. package/dist/esm/wire/refuel.d.ts +24 -14
  201. package/dist/esm/wire/refuel.d.ts.map +1 -1
  202. package/dist/esm/wire/refuel.js +38 -17
  203. package/dist/esm/wire/refuel.js.map +1 -1
  204. package/dist/esm/wire/transferable.d.ts +2 -2
  205. package/dist/esm/wire/transferable.js +5 -5
  206. package/dist/esm/wire/transferable.js.map +1 -1
  207. package/dist/esm/wire/withdraw.d.ts.map +1 -1
  208. package/dist/esm/wire/withdraw.js +3 -3
  209. package/dist/esm/wire/withdraw.js.map +1 -1
  210. package/dist/types/client/PerpsClient.d.ts +17 -4
  211. package/dist/types/client/PerpsClient.d.ts.map +1 -1
  212. package/dist/types/decimal/arithmetic.d.ts +98 -0
  213. package/dist/types/decimal/arithmetic.d.ts.map +1 -0
  214. package/dist/types/decimal/big.d.ts +0 -2
  215. package/dist/types/decimal/big.d.ts.map +1 -1
  216. package/dist/types/decimal/compare.d.ts +40 -6
  217. package/dist/types/decimal/compare.d.ts.map +1 -1
  218. package/dist/types/decimal/convert.d.ts +50 -36
  219. package/dist/types/decimal/convert.d.ts.map +1 -1
  220. package/dist/types/decimal/decimalStringToBig.d.ts +25 -0
  221. package/dist/types/decimal/decimalStringToBig.d.ts.map +1 -0
  222. package/dist/types/decimal/format.d.ts +4 -1
  223. package/dist/types/decimal/format.d.ts.map +1 -1
  224. package/dist/types/decimal/parse.d.ts +29 -13
  225. package/dist/types/decimal/parse.d.ts.map +1 -1
  226. package/dist/types/errors/invalidInput.d.ts +4 -0
  227. package/dist/types/errors/invalidInput.d.ts.map +1 -0
  228. package/dist/types/index.d.ts +15 -12
  229. package/dist/types/index.d.ts.map +1 -1
  230. package/dist/types/math/fills.d.ts +6 -3
  231. package/dist/types/math/fills.d.ts.map +1 -1
  232. package/dist/types/math/order.d.ts +84 -66
  233. package/dist/types/math/order.d.ts.map +1 -1
  234. package/dist/types/math/position.d.ts +155 -98
  235. package/dist/types/math/position.d.ts.map +1 -1
  236. package/dist/types/services/getQuote.d.ts +2 -2
  237. package/dist/types/services/resolveSubscribeQuote.d.ts +1 -1
  238. package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
  239. package/dist/types/transport/request.d.ts +5 -0
  240. package/dist/types/transport/request.d.ts.map +1 -1
  241. package/dist/types/types/api.d.ts +9 -0
  242. package/dist/types/types/api.d.ts.map +1 -1
  243. package/dist/types/types/provider.d.ts +30 -16
  244. package/dist/types/types/provider.d.ts.map +1 -1
  245. package/dist/types/types/withdrawal.d.ts +32 -7
  246. package/dist/types/types/withdrawal.d.ts.map +1 -1
  247. package/dist/types/utils/createSafeFunction.d.ts +9 -0
  248. package/dist/types/utils/createSafeFunction.d.ts.map +1 -0
  249. package/dist/types/utils/timestampToIsoString.d.ts +15 -0
  250. package/dist/types/utils/timestampToIsoString.d.ts.map +1 -0
  251. package/dist/types/utils/warnOnce.d.ts +27 -0
  252. package/dist/types/utils/warnOnce.d.ts.map +1 -1
  253. package/dist/types/version.d.ts +1 -1
  254. package/dist/types/websocket/PerpsWsClient.d.ts +7 -0
  255. package/dist/types/websocket/PerpsWsClient.d.ts.map +1 -1
  256. package/dist/types/websocket/wsLog.d.ts +12 -0
  257. package/dist/types/websocket/wsLog.d.ts.map +1 -1
  258. package/dist/types/wire/orderAmounts.d.ts +9 -7
  259. package/dist/types/wire/orderAmounts.d.ts.map +1 -1
  260. package/dist/types/wire/refuel.d.ts +24 -14
  261. package/dist/types/wire/refuel.d.ts.map +1 -1
  262. package/dist/types/wire/transferable.d.ts +2 -2
  263. package/dist/types/wire/withdraw.d.ts.map +1 -1
  264. package/package.json +2 -2
  265. package/src/client/PerpsClient.ts +48 -10
  266. package/src/decimal/arithmetic.ts +170 -0
  267. package/src/decimal/big.ts +0 -5
  268. package/src/decimal/compare.ts +61 -11
  269. package/src/decimal/convert.ts +105 -63
  270. package/src/decimal/decimalStringToBig.ts +42 -0
  271. package/src/decimal/format.ts +81 -72
  272. package/src/decimal/parse.ts +78 -57
  273. package/src/errors/invalidInput.ts +10 -0
  274. package/src/index.ts +80 -12
  275. package/src/math/fills.ts +32 -17
  276. package/src/math/order.ts +279 -226
  277. package/src/math/position.ts +280 -196
  278. package/src/services/getGasRecommendation.ts +1 -1
  279. package/src/services/getQuote.ts +2 -2
  280. package/src/services/resolveSubscribeQuote.ts +4 -3
  281. package/src/transport/request.ts +7 -2
  282. package/src/types/api.ts +10 -0
  283. package/src/types/provider.ts +39 -16
  284. package/src/types/withdrawal.ts +35 -8
  285. package/src/utils/createSafeFunction.ts +20 -0
  286. package/src/utils/timestampToIsoString.ts +31 -0
  287. package/src/utils/warnOnce.ts +60 -0
  288. package/src/version.ts +1 -1
  289. package/src/websocket/PerpsWsClient.ts +25 -2
  290. package/src/websocket/wsLog.ts +44 -0
  291. package/src/wire/orderAmounts.ts +45 -26
  292. package/src/wire/refuel.ts +56 -20
  293. package/src/wire/transferable.ts +5 -5
  294. package/src/wire/withdraw.ts +3 -5
  295. package/dist/cjs/decimal/requireDecimal.d.ts +0 -4
  296. package/dist/cjs/decimal/requireDecimal.d.ts.map +0 -1
  297. package/dist/cjs/decimal/requireDecimal.js +0 -12
  298. package/dist/cjs/decimal/requireDecimal.js.map +0 -1
  299. package/dist/esm/decimal/requireDecimal.d.ts +0 -5
  300. package/dist/esm/decimal/requireDecimal.d.ts.map +0 -1
  301. package/dist/esm/decimal/requireDecimal.js +0 -7
  302. package/dist/esm/decimal/requireDecimal.js.map +0 -1
  303. package/dist/types/decimal/requireDecimal.d.ts +0 -5
  304. package/dist/types/decimal/requireDecimal.d.ts.map +0 -1
  305. package/src/decimal/requireDecimal.ts +0 -8
@@ -1,41 +1,47 @@
1
1
  /**
2
- * Display-tier order formulas. Every function takes and gives `number`;
3
- * exact decimal arithmetic happens internally with `DivBig`.
2
+ * Display-tier order formulas. Every function takes and gives decimal
3
+ * strings; arithmetic is exact Big.js, with division to 40 decimal places.
4
+ * Each formula throws `ValidationError` on an input that does not match the
5
+ * decimal pattern and has a `safe*` pair that gives `undefined` in place of the throw.
4
6
  */
5
7
  import { type FeeTier, type Market, type MarketContext, type Order, type OrderbookLevel, type Position, type Quote, type QuoteSide, type TradeType } from '@lifi/perps-types';
6
8
  /**
7
- * Calculate position size in asset units from margin.
9
+ * Position size in asset units from margin: `marginUsd × leverage ÷ price`.
8
10
  *
9
- * @param marginUsd - Margin amount in USD
10
- * @param leverage - Position leverage
11
- * @param price - Current asset price
12
- * @returns Position size in asset units
13
11
  * @example
14
12
  * ```ts
15
- * calculateSize(100, 10, 2000) // 0.5 (ETH at $2000)
13
+ * calculateSize('100', '10', '2000') // '0.5' (ETH at $2000)
16
14
  * ```
15
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern
16
+ * or `price` is zero.
17
17
  * @public
18
18
  */
19
- export declare function calculateSize(marginUsd: number, leverage: number, price: number): number;
19
+ export declare function calculateSize(marginUsd: string, leverage: string, price: string): string;
20
+ /** @public */
21
+ export declare const safeCalculateSize: (marginUsd: string, leverage: string, price: string) => string | undefined;
20
22
  /**
21
- * Estimate trading fees.
23
+ * Estimated trading fee in USD: `sizeUsd × feeRate`.
22
24
  *
23
- * @param sizeUsd - Position size in USD (notional value)
24
- * @param feeRate - Fee rate as decimal (e.g., 0.00035 for 0.035%)
25
- * @returns Estimated fee in USD
25
+ * @param feeRate - Fee rate as a fraction (`'0.00035'` for 0.035%).
26
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
26
27
  * @public
27
28
  */
28
- export declare function estimateFees(sizeUsd: number, feeRate: number): number;
29
+ export declare function estimateFees(sizeUsd: string, feeRate: string): string;
30
+ /** @public */
31
+ export declare const safeEstimateFees: (sizeUsd: string, feeRate: string) => string | undefined;
29
32
  /**
30
- * Apply slippage to a price for order execution.
33
+ * Apply slippage to an order-entry price with exact decimal math: a buy
34
+ * multiplies by `1 + slippagePercent / 100`, a sell divides by it. The result
35
+ * is not rounded; snap it to the market tick before it goes to a venue.
31
36
  *
32
- * @param price - Base price
33
- * @param slippagePercent - Slippage tolerance as percentage (e.g., 0.5 for 0.5%)
34
- * @param isBuy - True if buying (price goes up), false if selling (price goes down)
35
- * @returns Price adjusted for slippage
37
+ * @param slippagePercent - Slippage tolerance as a percentage (`'0.5'` is 0.5%).
38
+ * @throws {PerpsError} `ValidationError` when an input does not match the
39
+ * decimal pattern, or `slippagePercent` is -100 or less.
36
40
  * @public
37
41
  */
38
- export declare function applySlippage(price: number, slippagePercent: number, isBuy: boolean): number;
42
+ export declare function applySlippageToPrice(price: string, slippagePercent: string, isBuy: boolean): string;
43
+ /** @public */
44
+ export declare const safeApplySlippageToPrice: (price: string, slippagePercent: string, isBuy: boolean) => string | undefined;
39
45
  /**
40
46
  * Signed expected PnL for a trigger price — see {@link calculateExpectedPnl}.
41
47
  *
@@ -43,61 +49,63 @@ export declare function applySlippage(price: number, slippagePercent: number, is
43
49
  */
44
50
  export interface ExpectedPnl {
45
51
  /** Signed expected profit/loss in USD. */
46
- amount: number;
47
- /** Signed expected return as a percentage (10 means 10%). */
48
- percent: number;
52
+ amount: string;
53
+ /** Signed expected return as a percentage (`'10'` means 10%). */
54
+ percent: string;
49
55
  }
50
56
  /**
51
- * Calculate expected gain/loss for a TP or SL trigger price.
52
- * Returns signed values — positive means profit, negative means loss.
57
+ * Expected gain or loss for a TP or SL trigger price. Positive is a profit,
58
+ * negative a loss.
53
59
  *
54
- * @param triggerPrice - The TP or SL target price
55
- * @param entryPrice - Position entry / current market price
56
- * @param leverage - Position leverage multiplier
57
- * @param isLong - True for long positions, false for short
58
- * @param margin - Margin amount in USD
60
+ * @param entryPrice - Position entry or current market price.
61
+ * @param margin - Margin amount in USD.
62
+ * @throws {PerpsError} `ValidationError` when an input does not match the
63
+ * decimal pattern, or `triggerPrice`, `entryPrice` or `margin` is zero.
59
64
  * @public
60
65
  */
61
- export declare function calculateExpectedPnl(triggerPrice: number, entryPrice: number, leverage: number, isLong: boolean, margin: number): ExpectedPnl | null;
66
+ export declare function calculateExpectedPnl(triggerPrice: string, entryPrice: string, leverage: string, isLong: boolean, margin: string): ExpectedPnl;
67
+ /** @public */
68
+ export declare const safeCalculateExpectedPnl: (triggerPrice: string, entryPrice: string, leverage: string, isLong: boolean, margin: string) => ExpectedPnl | undefined;
62
69
  /**
63
- * Calculate the trigger price that realises a percentage gain/loss.
70
+ * Trigger price that realises a percentage gain or loss.
64
71
  *
65
- * @param percent - Target gain/loss percentage (positive = profitable direction)
66
- * @param entryPrice - Position entry price
67
- * @param leverage - Position leverage multiplier
68
- * @param isLong - True for long positions, false for short
72
+ * @param percent - Target gain or loss percentage (positive = profitable
73
+ * direction).
74
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
69
75
  * @public
70
76
  */
71
- export declare function calculateTriggerPrice(percent: number, entryPrice: number, leverage: number, isLong: boolean): number;
77
+ export declare function calculateTriggerPrice(percent: string, entryPrice: string, leverage: string, isLong: boolean): string;
78
+ /** @public */
79
+ export declare const safeCalculateTriggerPrice: (percent: string, entryPrice: string, leverage: string, isLong: boolean) => string | undefined;
72
80
  /**
73
- * Calculate the percentage gain/loss a trigger price realises.
81
+ * Percentage gain or loss that a trigger price realises.
74
82
  *
75
- * @param price - Target price
76
- * @param entryPrice - Position entry price
77
- * @param leverage - Position leverage multiplier
78
- * @param isLong - True for long positions, false for short
83
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
79
84
  * @public
80
85
  */
81
- export declare function calculateTriggerPercent(price: number, entryPrice: number, leverage: number, isLong: boolean): number;
86
+ export declare function calculateTriggerPercent(price: string, entryPrice: string, leverage: string, isLong: boolean): string;
87
+ /** @public */
88
+ export declare const safeCalculateTriggerPercent: (price: string, entryPrice: string, leverage: string, isLong: boolean) => string | undefined;
82
89
  /**
83
- * Calculate realized PnL as a percentage of position value at close.
90
+ * Realized PnL as a percentage of the position value at close:
91
+ * `realizedPnl ÷ (|size| × price) × 100`.
84
92
  *
85
- * @param realizedPnl - The realized profit/loss in USD
86
- * @param size - Position size in asset units at close
87
- * @param price - Price at close
88
- * @returns PnL as a percentage of position value
93
+ * @throws {PerpsError} `ValidationError` when an input does not match the
94
+ * decimal pattern or `size × price` is zero.
89
95
  * @public
90
96
  */
91
- export declare function calculateRealizedPnlPercent(realizedPnl: number, size: number, price: number): number;
97
+ export declare function calculateRealizedPnlPercent(realizedPnl: string, size: string, price: string): string;
98
+ /** @public */
99
+ export declare const safeCalculateRealizedPnlPercent: (realizedPnl: string, size: string, price: string) => string | undefined;
92
100
  /** Result of {@link walkOrderbook} — the fill obtained for a USD-notional walk. */
93
101
  interface BookWalk {
94
102
  /** Base amount filled. */
95
- baseSize: number;
103
+ baseSize: string;
96
104
  /** Notional actually filled in USD (equals the requested size unless the book ran dry). */
97
- filledNotional: number;
98
- /** Volume-weighted average fill price, or 0 when the book is empty. */
99
- vwap: number;
100
- /** True when the levels could not absorb the requested notional, and for a non-finite notional. */
105
+ filledNotional: string;
106
+ /** Volume-weighted average fill price, or `'0'` when the book is empty. */
107
+ vwap: string;
108
+ /** True when the levels could not absorb the requested notional. */
101
109
  insufficientLiquidity: boolean;
102
110
  }
103
111
  /**
@@ -106,21 +114,22 @@ interface BookWalk {
106
114
  * in array order — the caller passes asks for a buy and bids for a sell, each
107
115
  * already ordered best-price-first. When the book cannot absorb the full
108
116
  * notional, the walk stops at the last level and flags `insufficientLiquidity`,
109
- * returning the best obtainable fill. A non-finite `sizeUsd` gives NaN fill
110
- * fields with `insufficientLiquidity` set.
117
+ * returning the best obtainable fill.
111
118
  *
112
- * @throws {PerpsError} `ValidationError` when a level's `price` or `size`
113
- * does not parse to a finite number.
119
+ * @throws {PerpsError} `ValidationError` when `sizeUsd` or a level's `price`
120
+ * or `size` does not match the decimal pattern.
114
121
  * @public
115
122
  */
116
- export declare function walkOrderbook(levels: OrderbookLevel[], sizeUsd: number): BookWalk;
123
+ export declare function walkOrderbook(levels: OrderbookLevel[], sizeUsd: string): BookWalk;
124
+ /** @public */
125
+ export declare const safeWalkOrderbook: (levels: OrderbookLevel[], sizeUsd: string) => BookWalk | undefined;
117
126
  /** Inputs to {@link buildQuote} — the resolved market, its live price, its book, and the trade ask. */
118
127
  interface BuildQuoteInput {
119
128
  provider: string;
120
129
  symbol: string;
121
130
  type: TradeType;
122
131
  side: QuoteSide;
123
- sizeUsd: number;
132
+ sizeUsd: string;
124
133
  market: Market;
125
134
  price: MarketContext;
126
135
  bids: OrderbookLevel[];
@@ -136,12 +145,13 @@ interface BuildQuoteInput {
136
145
  * versus mark, applies the base taker fee on the filled notional, and carries
137
146
  * the market's `funding` (`null` for spot, which has none).
138
147
  *
139
- * @throws {PerpsError} `ValidationError` when a book level does not parse to
140
- * a finite number — see {@link walkOrderbook} — or when a quoted figure is
141
- * not finite (a non-finite `sizeUsd`, mark price or taker fee).
148
+ * @throws {PerpsError} `ValidationError` when `sizeUsd`, the mark price, the
149
+ * taker fee or a book level does not match the decimal pattern.
142
150
  * @public
143
151
  */
144
152
  export declare function buildQuote(input: BuildQuoteInput): Quote;
153
+ /** @public */
154
+ export declare const safeBuildQuote: (input: BuildQuoteInput) => Quote | undefined;
145
155
  /**
146
156
  * Pick the matching open position for an order's market, if any.
147
157
  *
@@ -158,9 +168,13 @@ export declare function findMatchingPosition(marketId: string, positions: readon
158
168
  * position can only close what's open.
159
169
  *
160
170
  * Inputs are non-negative magnitudes.
171
+ *
172
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
161
173
  * @public
162
174
  */
163
- export declare function resolveCloseSize(orderSize: number, positionSize: number): number;
175
+ export declare function resolveCloseSize(orderSize: string, positionSize: string): string;
176
+ /** @public */
177
+ export declare const safeResolveCloseSize: (orderSize: string, positionSize: string) => string | undefined;
164
178
  /**
165
179
  * Expected rPnL an unfilled order would realise against a matching position:
166
180
  * a resting limit order prices off its limit, an armed trigger off its trigger
@@ -168,9 +182,13 @@ export declare function resolveCloseSize(orderSize: number, positionSize: number
168
182
  * nothing.
169
183
  *
170
184
  * @returns Realised PnL if the order would reduce the position, otherwise
171
- * `null`.
185
+ * `undefined`.
186
+ * @throws {PerpsError} `ValidationError` when a price or size on the order or
187
+ * the position does not match the decimal pattern.
172
188
  * @public
173
189
  */
174
- export declare function estimateRealizedPnl(order: Order, position: Position | undefined): number | null;
190
+ export declare function estimateRealizedPnl(order: Order, position: Position | undefined): string | undefined;
191
+ /** @public */
192
+ export declare const safeEstimateRealizedPnl: (order: Order, position: Position | undefined) => string | undefined;
175
193
  export {};
176
194
  //# sourceMappingURL=order.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"order.d.ts","sourceRoot":"","sources":["../../../src/math/order.ts"],"names":[],"mappings":"AAAA;;;GAGG;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,EAAE,cAAc,EAAE,CAAA;IACtB,IAAI,EAAE,cAAc,EAAE,CAAA;IACtB,kFAAkF;IAClF,OAAO,EAAE,OAAO,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,eAAe,GAAG,KAAK,CA8BxD;AAED,cAAc;AACd,eAAO,MAAM,cAAc,+CAA+C,CAAA;AAE1E;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,SAAS,QAAQ,EAAE,GAC7B,QAAQ,GAAG,SAAS,CAEtB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,GACnB,MAAM,CAOR;AAED,cAAc;AACd,eAAO,MAAM,oBAAoB,iEAGhC,CAAA;AAkGD;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,QAAQ,GAAG,SAAS,GAC7B,MAAM,GAAG,SAAS,CAQpB;AAED,cAAc;AACd,eAAO,MAAM,uBAAuB,sEAGnC,CAAA"}
@@ -1,181 +1,238 @@
1
1
  /**
2
2
  * Display-tier position formulas and margin-adjustment predicates. Every
3
- * formula takes and gives `number`; exact decimal arithmetic happens
4
- * internally with `DivBig`.
3
+ * formula takes and gives decimal strings; arithmetic is exact Big.js, with
4
+ * division to 40 decimal places. Each formula throws `ValidationError` on an
5
+ * input that does not match the decimal pattern and has a `safe*` pair that gives
6
+ * `undefined` in place of the throw.
5
7
  *
6
8
  * Sign convention: long = +1, short = -1. Sizes passed to the `estimate*`
7
9
  * helpers are always non-negative magnitudes; direction is carried by
8
10
  * `isLong`.
9
11
  */
10
- import { type Position } from '@lifi/perps-types';
12
+ import { type PerpsMarket, type Position } from '@lifi/perps-types';
13
+ import type { LiquidationEstimateParams } from '../types/provider.js';
11
14
  /**
12
- * Calculate notional value of a position.
15
+ * Notional value of a position: `|size| × price`.
13
16
  *
14
- * @param size - Position size in asset units
15
- * @param price - Current asset price
16
- * @returns Notional value in USD
17
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
17
18
  * @public
18
19
  */
19
- export declare function calculateNotionalValue(size: number, price: number): number;
20
+ export declare function calculateNotionalValue(size: string, price: string): string;
21
+ /** @public */
22
+ export declare const safeCalculateNotionalValue: (size: string, price: string) => string | undefined;
20
23
  /**
21
- * Calculate unrealized PnL.
24
+ * Unrealized PnL: `(currentPrice - entryPrice) × size`, where `size` is
25
+ * positive for a long and negative for a short.
22
26
  *
23
- * @param entryPrice - Position entry price
24
- * @param currentPrice - Current market price
25
- * @param size - Position size (positive for long, negative for short)
26
- * @returns Unrealized PnL in USD
27
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
27
28
  * @public
28
29
  */
29
- export declare function calculateUnrealizedPnl(entryPrice: number, currentPrice: number, size: number): number;
30
+ export declare function calculateUnrealizedPnl(entryPrice: string, currentPrice: string, size: string): string;
31
+ /** @public */
32
+ export declare const safeCalculateUnrealizedPnl: (entryPrice: string, currentPrice: string, size: string) => string | undefined;
30
33
  /**
31
- * Calculate return on equity (ROE) percentage.
34
+ * Return on equity as a percentage (`'10'` for 10%): `pnl ÷ margin × 100`.
32
35
  *
33
- * @param pnl - Profit/loss in USD
34
- * @param margin - Initial margin in USD
35
- * @returns ROE as percentage (e.g., 10 for 10%)
36
+ * @throws {PerpsError} `ValidationError` when an input does not match the
37
+ * decimal pattern or `margin` is zero.
36
38
  * @public
37
39
  */
38
- export declare function calculateRoe(pnl: number, margin: number): number;
40
+ export declare function calculateRoe(pnl: string, margin: string): string;
41
+ /** @public */
42
+ export declare const safeCalculateRoe: (pnl: string, margin: string) => string | undefined;
39
43
  /**
40
- * Calculate required margin for a position.
44
+ * Required margin: `notionalValue ÷ leverage`.
41
45
  *
42
- * @param notionalValue - Position notional value in USD
43
- * @param leverage - Position leverage
44
- * @returns Required margin in USD
46
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern
47
+ * or `leverage` is zero.
45
48
  * @public
46
49
  */
47
- export declare function calculateRequiredMargin(notionalValue: number, leverage: number): number;
50
+ export declare function calculateRequiredMargin(notionalValue: string, leverage: string): string;
51
+ /** @public */
52
+ export declare const safeCalculateRequiredMargin: (notionalValue: string, leverage: string) => string | undefined;
48
53
  /**
49
- * Distance from the current price to the liquidation price, as a percentage of
50
- * the current price.
54
+ * Distance from the current price to the liquidation price, as an absolute
55
+ * percentage of the current price.
51
56
  *
52
- * @param liquidationPrice - The position's liquidation price
53
- * @param currentPrice - Current market price
54
- * @returns Absolute distance as a percentage, or 0 when current price is zero
57
+ * @throws {PerpsError} `ValidationError` when an input does not match the
58
+ * decimal pattern or `currentPrice` is zero.
55
59
  * @public
56
60
  */
57
61
  export declare function calculateLiquidationDistance(params: {
58
- liquidationPrice: number;
59
- currentPrice: number;
60
- }): number;
62
+ liquidationPrice: string;
63
+ currentPrice: string;
64
+ }): string;
65
+ /** @public */
66
+ export declare const safeCalculateLiquidationDistance: (params: {
67
+ liquidationPrice: string;
68
+ currentPrice: string;
69
+ }) => string | undefined;
61
70
  /**
62
- * Effective leverage of an open position.
71
+ * Effective leverage of an open position: `positionValueUsd ÷ marginUsd`.
63
72
  *
64
- * leverage = positionValueUsd / marginUsd
65
- *
66
- * @param positionValueUsd - Position notional value in USD
67
- * @param marginUsd - Margin backing the position in USD
68
- * @returns Effective leverage, or 0 when margin is zero
73
+ * @throws {PerpsError} `ValidationError` when an input does not match the
74
+ * decimal pattern or `marginUsd` is zero.
69
75
  * @public
70
76
  */
71
77
  export declare function calculateEffectiveLeverage(params: {
72
- positionValueUsd: number;
73
- marginUsd: number;
74
- }): number;
78
+ positionValueUsd: string;
79
+ marginUsd: string;
80
+ }): string;
81
+ /** @public */
82
+ export declare const safeCalculateEffectiveLeverage: (params: {
83
+ positionValueUsd: string;
84
+ marginUsd: string;
85
+ }) => string | undefined;
75
86
  /**
76
87
  * Estimated liquidation price for an isolated-margin position, parameterised
77
88
  * by the venue's maintenance margin rate.
78
89
  *
79
90
  * Standard isolated-margin model (new position, no existing exposure):
80
- * margin_per_unit = entryPrice / leverage
81
- * maintenance_per_unit = entryPrice * mmr
82
- * margin_available = margin_per_unit - maintenance_per_unit
83
- * liq_price = entryPrice - side * margin_available / (1 - mmr * side)
91
+ * margin_available = entryPrice × (1 / leverage - mmr)
92
+ * liq_price = entryPrice - side × margin_available / (1 - mmr × side)
84
93
  *
85
94
  * For existing positions, prefer `Position.liquidationPrice` from the venue.
86
95
  *
87
96
  * @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
88
- * (e.g. 0.01 = 1%).
89
- * @returns Estimated liquidation price, or undefined if the inputs cannot
90
- * produce one (zero leverage, degenerate denominator).
97
+ * (`'0.01'` = 1%).
98
+ * @throws {PerpsError} `ValidationError` when an input does not match the
99
+ * decimal pattern, `leverage` is zero, or `maintenanceMarginRate` gives a
100
+ * zero denominator (`1` for a long, `-1` for a short).
91
101
  * @public
92
102
  */
93
103
  export declare function estimateLiquidationPrice(params: {
94
- entryPrice: number;
95
- leverage: number;
104
+ entryPrice: string;
105
+ leverage: string;
106
+ isLong: boolean;
107
+ maintenanceMarginRate: string;
108
+ }): string;
109
+ /** @public */
110
+ export declare const safeEstimateLiquidationPrice: (params: {
111
+ entryPrice: string;
112
+ leverage: string;
96
113
  isLong: boolean;
97
- maintenanceMarginRate: number;
98
- }): number | undefined;
114
+ maintenanceMarginRate: string;
115
+ }) => string | undefined;
116
+ /**
117
+ * Estimate the liquidation price of a new isolated position from the flat
118
+ * `market.maintenanceMarginRate`.
119
+ *
120
+ * @returns The estimate, or `undefined` when the market carries no
121
+ * `maintenanceMarginRate`.
122
+ * @throws {PerpsError} `ValidationError` on an input that
123
+ * {@link estimateLiquidationPrice} rejects.
124
+ * @public
125
+ */
126
+ export declare function estimateLiquidationPriceAtMarketRate(market: PerpsMarket, params: LiquidationEstimateParams): string | undefined;
127
+ /** @public */
128
+ export declare const safeEstimateLiquidationPriceAtMarketRate: (market: PerpsMarket, params: LiquidationEstimateParams) => string | undefined;
99
129
  /**
100
130
  * Whether an isolated position with this liquidation price is already past
101
131
  * liquidation at `currentPrice`: a long liquidates when the price falls to its
102
132
  * liquidation level, a short when it rises to it. A non-positive price on
103
133
  * either side counts as unknown, so the result is `false`.
104
134
  *
135
+ * @throws {PerpsError} `ValidationError` when a price does not match the decimal pattern.
105
136
  * @public
106
137
  */
107
138
  export declare function wouldImmediatelyLiquidate(params: {
108
- liquidationPrice: number;
109
- currentPrice: number;
139
+ liquidationPrice: string;
140
+ currentPrice: string;
110
141
  isLong: boolean;
111
142
  }): boolean;
143
+ /** @public */
144
+ export declare const safeWouldImmediatelyLiquidate: (params: {
145
+ liquidationPrice: string;
146
+ currentPrice: string;
147
+ isLong: boolean;
148
+ }) => boolean | undefined;
112
149
  /**
113
- * Estimated average entry price after adding to an existing position.
114
- *
115
- * Weighted average of the current entry and the new fill price, weighted by
116
- * the size of each leg in coin units. Both legs assumed in the same direction
117
- * (this helper is for adding to, not flipping, a position).
118
- *
119
- * @param currentSize - Existing position size in coin units (>= 0).
120
- * @param currentEntry - Existing position's average entry price.
121
- * @param addSize - Size being added in coin units (>= 0).
122
- * @param fillPrice - Price the new size is expected to fill at (mid for
123
- * market, limitPrice for limit orders).
124
- * @returns The new weighted-average entry price, or undefined if the inputs
125
- * cannot produce a valid average (zero combined size, non-finite values).
150
+ * Estimated average entry price after adding to an existing position: the
151
+ * size-weighted average of the current entry and the fill price. Both legs
152
+ * are in the same direction (an add, not a flip).
153
+ *
154
+ * @param fillPrice - Expected fill price (mid for market, limit price for
155
+ * limit orders).
156
+ * @throws {PerpsError} `ValidationError` when an input does not match the
157
+ * decimal pattern or `currentSize + addSize` is not positive.
126
158
  * @public
127
159
  */
128
160
  export declare function estimateAverageEntryPrice(params: {
129
- currentSize: number;
130
- currentEntry: number;
131
- addSize: number;
132
- fillPrice: number;
133
- }): number | undefined;
161
+ currentSize: string;
162
+ currentEntry: string;
163
+ addSize: string;
164
+ fillPrice: string;
165
+ }): string;
166
+ /** @public */
167
+ export declare const safeEstimateAverageEntryPrice: (params: {
168
+ currentSize: string;
169
+ currentEntry: string;
170
+ addSize: string;
171
+ fillPrice: string;
172
+ }) => string | undefined;
134
173
  /**
135
- * Estimated effective leverage after adding margin and notional.
174
+ * Estimated effective leverage after adding margin and notional:
175
+ * `(currentNotional + addNotional) ÷ (currentMargin + addMargin)`.
136
176
  *
137
- * leverage = totalNotional / totalMargin. The caller computes notional from
138
- * size and price (`calculateNotionalValue`) and supplies the additional
139
- * margin the user is about to put up.
140
- *
141
- * @returns The new effective leverage, or undefined if total margin is
142
- * non-positive.
177
+ * @throws {PerpsError} `ValidationError` when an input does not match the
178
+ * decimal pattern or `currentMargin + addMargin` is not positive.
143
179
  * @public
144
180
  */
145
181
  export declare function estimateNewLeverage(params: {
146
- currentNotional: number;
147
- currentMargin: number;
148
- addNotional: number;
149
- addMargin: number;
150
- }): number | undefined;
182
+ currentNotional: string;
183
+ currentMargin: string;
184
+ addNotional: string;
185
+ addMargin: string;
186
+ }): string;
187
+ /** @public */
188
+ export declare const safeEstimateNewLeverage: (params: {
189
+ currentNotional: string;
190
+ currentMargin: string;
191
+ addNotional: string;
192
+ addMargin: string;
193
+ }) => string | undefined;
151
194
  /**
152
- * Estimated unrealised PnL at the current mark price.
153
- *
154
- * `pnl = (markPrice - entryPrice) * size * (isLong ? 1 : -1)`
195
+ * Estimated unrealised PnL at the mark price:
196
+ * `(markPrice - entryPrice) × size × (isLong ? 1 : -1)`.
155
197
  *
156
198
  * @param size - Position size as a non-negative magnitude.
199
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
157
200
  * @public
158
201
  */
159
202
  export declare function estimateUnrealizedPnl(params: {
160
- entryPrice: number;
161
- markPrice: number;
162
- size: number;
203
+ entryPrice: string;
204
+ markPrice: string;
205
+ size: string;
163
206
  isLong: boolean;
164
- }): number;
207
+ }): string;
208
+ /** @public */
209
+ export declare const safeEstimateUnrealizedPnl: (params: {
210
+ entryPrice: string;
211
+ markPrice: string;
212
+ size: string;
213
+ isLong: boolean;
214
+ }) => string | undefined;
165
215
  /**
166
- * Realised PnL on the portion of a position being closed.
167
- *
168
- * `rPnl = (closePrice - entryPrice) * closeSize * (isLong ? 1 : -1)`
216
+ * Realised PnL on the portion of a position being closed:
217
+ * `(closePrice - entryPrice) × closeSize × (isLong ? 1 : -1)`.
169
218
  *
170
219
  * @param closeSize - Size being closed as a non-negative magnitude.
220
+ * @throws {PerpsError} `ValidationError` when an input does not match the decimal pattern.
171
221
  * @public
172
222
  */
173
223
  export declare function calculateRealizedPnl(params: {
174
- entryPrice: number;
175
- closePrice: number;
176
- closeSize: number;
224
+ entryPrice: string;
225
+ closePrice: string;
226
+ closeSize: string;
227
+ isLong: boolean;
228
+ }): string;
229
+ /** @public */
230
+ export declare const safeCalculateRealizedPnl: (params: {
231
+ entryPrice: string;
232
+ closePrice: string;
233
+ closeSize: string;
177
234
  isLong: boolean;
178
- }): number;
235
+ }) => string | undefined;
179
236
  /**
180
237
  * Whether this position can take a margin adjustment at all: it holds margin of
181
238
  * its own and its market exposes individual position margin.
@@ -1 +1 @@
1
- {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../../src/math/position.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,mBAAmB,CAAA;AAG1B;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAK1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,MAAM,GACX,MAAM,CAKR;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,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AAU,EAAE,MAAM,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,OAAO,CAAA;CAChB,GAAG,MAAM,CAOT;AAED,cAAc;AACd,eAAO,MAAM,wBAAwB;gBAdvB,MAAM;gBACN,MAAM;eACP,MAAM;YACT,OAAO;wBAchB,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAK5E;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAMzE"}
@@ -13,7 +13,7 @@ export interface GetQuoteParams {
13
13
  /** Buy consumes asks; sell consumes bids. */
14
14
  side: QuoteSide;
15
15
  /** USD notional to fill. */
16
- size: number;
16
+ size: string;
17
17
  /** Product family used to distinguish spot and perpetual markets. */
18
18
  type: TradeType;
19
19
  }
@@ -33,7 +33,7 @@ export interface GetQuoteParams {
33
33
  * provider: 'hyperliquid',
34
34
  * symbol: 'BTC',
35
35
  * side: 'buy',
36
- * size: 10_000,
36
+ * size: '10000',
37
37
  * type: 'perps',
38
38
  * })
39
39
  * console.log(quote.expectedFillPrice, quote.priceImpactBps)
@@ -29,7 +29,7 @@ export declare const QUOTE_THROTTLE_MS = 100;
29
29
  * price, a throwing `onQuote`) is logged via `wsLog.listenerFailure` and the
30
30
  * emission is skipped, on both the immediate and the trailing-timer path.
31
31
  *
32
- * @throws {PerpsError} `ValidationError` when `params.size` is not finite.
32
+ * @throws {PerpsError} `ValidationError` when `params.size` does not match the decimal pattern.
33
33
  * @throws {PerpsError} `MarketNotFound` when no market matches symbol+type.
34
34
  * @internal
35
35
  */