@lifi/perps-sdk 23.1.0 → 24.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.
- package/README.md +85 -45
- package/dist/cjs/client/PerpsClient.d.ts +3 -2
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +16 -9
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/decimal/arithmetic.d.ts +13 -0
- package/dist/cjs/decimal/arithmetic.d.ts.map +1 -0
- package/dist/cjs/decimal/arithmetic.js +48 -0
- package/dist/cjs/decimal/arithmetic.js.map +1 -0
- package/dist/cjs/decimal/big.d.ts +0 -1
- package/dist/cjs/decimal/big.d.ts.map +1 -1
- package/dist/cjs/decimal/big.js +0 -4
- package/dist/cjs/decimal/big.js.map +1 -1
- package/dist/cjs/decimal/compare.d.ts +6 -2
- package/dist/cjs/decimal/compare.d.ts.map +1 -1
- package/dist/cjs/decimal/compare.js +15 -2
- package/dist/cjs/decimal/compare.js.map +1 -1
- package/dist/cjs/decimal/convert.d.ts +12 -6
- package/dist/cjs/decimal/convert.d.ts.map +1 -1
- package/dist/cjs/decimal/convert.js +36 -14
- package/dist/cjs/decimal/convert.js.map +1 -1
- package/dist/cjs/decimal/decimalStringToBig.d.ts +5 -0
- package/dist/cjs/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/cjs/decimal/decimalStringToBig.js +27 -0
- package/dist/cjs/decimal/decimalStringToBig.js.map +1 -0
- package/dist/cjs/decimal/format.d.ts.map +1 -1
- package/dist/cjs/decimal/format.js +70 -52
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +4 -4
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +42 -25
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/index.d.ts +13 -11
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +64 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +1 -1
- package/dist/cjs/math/fills.d.ts.map +1 -1
- package/dist/cjs/math/fills.js +28 -13
- package/dist/cjs/math/fills.js.map +1 -1
- package/dist/cjs/math/order.d.ts +27 -16
- package/dist/cjs/math/order.d.ts.map +1 -1
- package/dist/cjs/math/order.js +106 -122
- package/dist/cjs/math/order.js.map +1 -1
- package/dist/cjs/math/position.d.ts +85 -35
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +88 -91
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/services/getGasRecommendation.js +1 -1
- package/dist/cjs/services/getGasRecommendation.js.map +1 -1
- package/dist/cjs/services/getQuote.d.ts +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.js +3 -2
- package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/cjs/transport/request.d.ts +1 -0
- package/dist/cjs/transport/request.d.ts.map +1 -1
- package/dist/cjs/transport/request.js +2 -2
- package/dist/cjs/transport/request.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -0
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +9 -5
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/types/withdrawal.d.ts +12 -4
- package/dist/cjs/types/withdrawal.d.ts.map +1 -1
- package/dist/cjs/utils/createSafeFunction.d.ts +2 -0
- package/dist/cjs/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/cjs/utils/createSafeFunction.js +15 -0
- package/dist/cjs/utils/createSafeFunction.js.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts +3 -0
- package/dist/cjs/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/cjs/utils/timestampToIsoString.js +16 -0
- package/dist/cjs/utils/timestampToIsoString.js.map +1 -0
- package/dist/cjs/utils/warnOnce.d.ts +2 -0
- package/dist/cjs/utils/warnOnce.d.ts.map +1 -1
- package/dist/cjs/utils/warnOnce.js +13 -1
- package/dist/cjs/utils/warnOnce.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/websocket/PerpsWsClient.d.ts +2 -0
- package/dist/cjs/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/cjs/websocket/PerpsWsClient.js +14 -1
- package/dist/cjs/websocket/PerpsWsClient.js.map +1 -1
- package/dist/cjs/websocket/wsLog.d.ts +2 -0
- package/dist/cjs/websocket/wsLog.d.ts.map +1 -1
- package/dist/cjs/websocket/wsLog.js +8 -0
- package/dist/cjs/websocket/wsLog.js.map +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +6 -9
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/cjs/wire/refuel.d.ts +4 -1
- package/dist/cjs/wire/refuel.d.ts.map +1 -1
- package/dist/cjs/wire/refuel.js +23 -6
- package/dist/cjs/wire/refuel.js.map +1 -1
- package/dist/cjs/wire/transferable.js +3 -3
- package/dist/cjs/wire/transferable.js.map +1 -1
- package/dist/cjs/wire/withdraw.d.ts.map +1 -1
- package/dist/cjs/wire/withdraw.js +3 -3
- package/dist/cjs/wire/withdraw.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +17 -4
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +30 -8
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/decimal/arithmetic.d.ts +84 -0
- package/dist/esm/decimal/arithmetic.d.ts.map +1 -0
- package/dist/esm/decimal/arithmetic.js +110 -0
- package/dist/esm/decimal/arithmetic.js.map +1 -0
- package/dist/esm/decimal/big.d.ts +0 -2
- package/dist/esm/decimal/big.d.ts.map +1 -1
- package/dist/esm/decimal/big.js +0 -4
- package/dist/esm/decimal/big.js.map +1 -1
- package/dist/esm/decimal/compare.d.ts +39 -5
- package/dist/esm/decimal/compare.d.ts.map +1 -1
- package/dist/esm/decimal/compare.js +45 -5
- package/dist/esm/decimal/compare.js.map +1 -1
- package/dist/esm/decimal/convert.d.ts +62 -26
- package/dist/esm/decimal/convert.d.ts.map +1 -1
- package/dist/esm/decimal/convert.js +76 -25
- package/dist/esm/decimal/convert.js.map +1 -1
- package/dist/esm/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/esm/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/esm/decimal/decimalStringToBig.js +40 -0
- package/dist/esm/decimal/decimalStringToBig.js.map +1 -0
- package/dist/esm/decimal/format.d.ts +4 -1
- package/dist/esm/decimal/format.d.ts.map +1 -1
- package/dist/esm/decimal/format.js +71 -70
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +22 -13
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +56 -46
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/index.d.ts +13 -11
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +9 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +6 -3
- package/dist/esm/math/fills.d.ts.map +1 -1
- package/dist/esm/math/fills.js +31 -16
- package/dist/esm/math/fills.js.map +1 -1
- package/dist/esm/math/order.d.ts +85 -65
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +158 -166
- package/dist/esm/math/order.js.map +1 -1
- package/dist/esm/math/position.d.ts +155 -98
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +157 -155
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/services/getGasRecommendation.js +1 -1
- package/dist/esm/services/getGasRecommendation.js.map +1 -1
- package/dist/esm/services/getQuote.d.ts +2 -2
- package/dist/esm/services/getQuote.js +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js +4 -3
- package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/esm/transport/request.d.ts +5 -0
- package/dist/esm/transport/request.d.ts.map +1 -1
- package/dist/esm/transport/request.js +2 -2
- package/dist/esm/transport/request.js.map +1 -1
- package/dist/esm/types/api.d.ts +9 -0
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +26 -13
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/types/withdrawal.d.ts +32 -7
- package/dist/esm/types/withdrawal.d.ts.map +1 -1
- package/dist/esm/utils/createSafeFunction.d.ts +9 -0
- package/dist/esm/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/esm/utils/createSafeFunction.js +19 -0
- package/dist/esm/utils/createSafeFunction.js.map +1 -0
- package/dist/esm/utils/timestampToIsoString.d.ts +15 -0
- package/dist/esm/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/esm/utils/timestampToIsoString.js +24 -0
- package/dist/esm/utils/timestampToIsoString.js.map +1 -0
- package/dist/esm/utils/warnOnce.d.ts +16 -0
- package/dist/esm/utils/warnOnce.d.ts.map +1 -1
- package/dist/esm/utils/warnOnce.js +24 -0
- package/dist/esm/utils/warnOnce.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/esm/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/esm/websocket/PerpsWsClient.js +20 -2
- package/dist/esm/websocket/PerpsWsClient.js.map +1 -1
- package/dist/esm/websocket/wsLog.d.ts +12 -0
- package/dist/esm/websocket/wsLog.d.ts.map +1 -1
- package/dist/esm/websocket/wsLog.js +18 -0
- package/dist/esm/websocket/wsLog.js.map +1 -1
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +2 -2
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/esm/wire/refuel.d.ts +24 -14
- package/dist/esm/wire/refuel.d.ts.map +1 -1
- package/dist/esm/wire/refuel.js +38 -17
- package/dist/esm/wire/refuel.js.map +1 -1
- package/dist/esm/wire/transferable.js +3 -3
- package/dist/esm/wire/transferable.js.map +1 -1
- package/dist/esm/wire/withdraw.d.ts.map +1 -1
- package/dist/esm/wire/withdraw.js +3 -3
- package/dist/esm/wire/withdraw.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +17 -4
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/decimal/arithmetic.d.ts +84 -0
- package/dist/types/decimal/arithmetic.d.ts.map +1 -0
- package/dist/types/decimal/big.d.ts +0 -2
- package/dist/types/decimal/big.d.ts.map +1 -1
- package/dist/types/decimal/compare.d.ts +39 -5
- package/dist/types/decimal/compare.d.ts.map +1 -1
- package/dist/types/decimal/convert.d.ts +62 -26
- package/dist/types/decimal/convert.d.ts.map +1 -1
- package/dist/types/decimal/decimalStringToBig.d.ts +26 -0
- package/dist/types/decimal/decimalStringToBig.d.ts.map +1 -0
- package/dist/types/decimal/format.d.ts +4 -1
- package/dist/types/decimal/format.d.ts.map +1 -1
- package/dist/types/decimal/parse.d.ts +22 -13
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -11
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +6 -3
- package/dist/types/math/fills.d.ts.map +1 -1
- package/dist/types/math/order.d.ts +85 -65
- package/dist/types/math/order.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +155 -98
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/services/getQuote.d.ts +2 -2
- package/dist/types/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/types/transport/request.d.ts +5 -0
- package/dist/types/transport/request.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +9 -0
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +26 -13
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/types/withdrawal.d.ts +32 -7
- package/dist/types/types/withdrawal.d.ts.map +1 -1
- package/dist/types/utils/createSafeFunction.d.ts +9 -0
- package/dist/types/utils/createSafeFunction.d.ts.map +1 -0
- package/dist/types/utils/timestampToIsoString.d.ts +15 -0
- package/dist/types/utils/timestampToIsoString.d.ts.map +1 -0
- package/dist/types/utils/warnOnce.d.ts +16 -0
- package/dist/types/utils/warnOnce.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/websocket/PerpsWsClient.d.ts +7 -0
- package/dist/types/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/types/websocket/wsLog.d.ts +12 -0
- package/dist/types/websocket/wsLog.d.ts.map +1 -1
- package/dist/types/wire/orderAmounts.d.ts.map +1 -1
- package/dist/types/wire/refuel.d.ts +24 -14
- package/dist/types/wire/refuel.d.ts.map +1 -1
- package/dist/types/wire/withdraw.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +48 -10
- package/src/decimal/arithmetic.ts +144 -0
- package/src/decimal/big.ts +0 -5
- package/src/decimal/compare.ts +60 -10
- package/src/decimal/convert.ts +123 -44
- package/src/decimal/decimalStringToBig.ts +47 -0
- package/src/decimal/format.ts +81 -72
- package/src/decimal/parse.ts +71 -61
- package/src/index.ts +73 -10
- package/src/math/fills.ts +32 -17
- package/src/math/order.ts +261 -216
- package/src/math/position.ts +277 -194
- package/src/services/getGasRecommendation.ts +1 -1
- package/src/services/getQuote.ts +2 -2
- package/src/services/resolveSubscribeQuote.ts +4 -3
- package/src/transport/request.ts +7 -2
- package/src/types/api.ts +10 -0
- package/src/types/provider.ts +35 -13
- package/src/types/withdrawal.ts +35 -8
- package/src/utils/createSafeFunction.ts +20 -0
- package/src/utils/timestampToIsoString.ts +31 -0
- package/src/utils/warnOnce.ts +44 -0
- package/src/version.ts +1 -1
- package/src/websocket/PerpsWsClient.ts +25 -2
- package/src/websocket/wsLog.ts +31 -0
- package/src/wire/orderAmounts.ts +3 -2
- package/src/wire/refuel.ts +56 -20
- package/src/wire/transferable.ts +3 -3
- package/src/wire/withdraw.ts +3 -5
- package/dist/cjs/decimal/requireDecimal.d.ts +0 -4
- package/dist/cjs/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/cjs/decimal/requireDecimal.js +0 -12
- package/dist/cjs/decimal/requireDecimal.js.map +0 -1
- package/dist/esm/decimal/requireDecimal.d.ts +0 -5
- package/dist/esm/decimal/requireDecimal.d.ts.map +0 -1
- package/dist/esm/decimal/requireDecimal.js +0 -7
- package/dist/esm/decimal/requireDecimal.js.map +0 -1
- package/dist/types/decimal/requireDecimal.d.ts +0 -5
- package/dist/types/decimal/requireDecimal.d.ts.map +0 -1
- package/src/decimal/requireDecimal.ts +0 -8
|
@@ -1,181 +1,238 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Display-tier position formulas and margin-adjustment predicates. Every
|
|
3
|
-
* formula takes and gives
|
|
4
|
-
*
|
|
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 is not a decimal string 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
|
-
*
|
|
15
|
+
* Notional value of a position: `|size| × price`.
|
|
13
16
|
*
|
|
14
|
-
* @
|
|
15
|
-
* @param price - Current asset price
|
|
16
|
-
* @returns Notional value in USD
|
|
17
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
17
18
|
* @public
|
|
18
19
|
*/
|
|
19
|
-
export declare function calculateNotionalValue(size:
|
|
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
|
-
*
|
|
24
|
+
* Unrealized PnL: `(currentPrice - entryPrice) × size`, where `size` is
|
|
25
|
+
* positive for a long and negative for a short.
|
|
22
26
|
*
|
|
23
|
-
* @
|
|
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 is not a decimal string.
|
|
27
28
|
* @public
|
|
28
29
|
*/
|
|
29
|
-
export declare function calculateUnrealizedPnl(entryPrice:
|
|
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
|
-
*
|
|
34
|
+
* Return on equity as a percentage (`'10'` for 10%): `pnl ÷ margin × 100`.
|
|
35
|
+
* A zero margin gives `'0'`.
|
|
32
36
|
*
|
|
33
|
-
* @
|
|
34
|
-
* @param margin - Initial margin in USD
|
|
35
|
-
* @returns ROE as percentage (e.g., 10 for 10%)
|
|
37
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
36
38
|
* @public
|
|
37
39
|
*/
|
|
38
|
-
export declare function calculateRoe(pnl:
|
|
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
|
-
*
|
|
44
|
+
* Required margin: `notionalValue ÷ leverage`.
|
|
41
45
|
*
|
|
42
|
-
* @
|
|
43
|
-
*
|
|
44
|
-
* @returns Required margin in USD
|
|
46
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string
|
|
47
|
+
* or `leverage` is zero.
|
|
45
48
|
* @public
|
|
46
49
|
*/
|
|
47
|
-
export declare function calculateRequiredMargin(notionalValue:
|
|
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
|
|
50
|
-
* the current price.
|
|
54
|
+
* Distance from the current price to the liquidation price, as an absolute
|
|
55
|
+
* percentage of the current price. A zero current price gives `'0'`.
|
|
51
56
|
*
|
|
52
|
-
* @
|
|
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 is not a decimal string.
|
|
55
58
|
* @public
|
|
56
59
|
*/
|
|
57
60
|
export declare function calculateLiquidationDistance(params: {
|
|
58
|
-
liquidationPrice:
|
|
59
|
-
currentPrice:
|
|
60
|
-
}):
|
|
61
|
+
liquidationPrice: string;
|
|
62
|
+
currentPrice: string;
|
|
63
|
+
}): string;
|
|
64
|
+
/** @public */
|
|
65
|
+
export declare const safeCalculateLiquidationDistance: (params: {
|
|
66
|
+
liquidationPrice: string;
|
|
67
|
+
currentPrice: string;
|
|
68
|
+
}) => string | undefined;
|
|
61
69
|
/**
|
|
62
|
-
* Effective leverage of an open position
|
|
70
|
+
* Effective leverage of an open position: `positionValueUsd ÷ marginUsd`.
|
|
71
|
+
* A zero margin gives `'0'`.
|
|
63
72
|
*
|
|
64
|
-
*
|
|
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 is not a decimal string.
|
|
69
74
|
* @public
|
|
70
75
|
*/
|
|
71
76
|
export declare function calculateEffectiveLeverage(params: {
|
|
72
|
-
positionValueUsd:
|
|
73
|
-
marginUsd:
|
|
74
|
-
}):
|
|
77
|
+
positionValueUsd: string;
|
|
78
|
+
marginUsd: string;
|
|
79
|
+
}): string;
|
|
80
|
+
/** @public */
|
|
81
|
+
export declare const safeCalculateEffectiveLeverage: (params: {
|
|
82
|
+
positionValueUsd: string;
|
|
83
|
+
marginUsd: string;
|
|
84
|
+
}) => string | undefined;
|
|
75
85
|
/**
|
|
76
86
|
* Estimated liquidation price for an isolated-margin position, parameterised
|
|
77
87
|
* by the venue's maintenance margin rate.
|
|
78
88
|
*
|
|
79
89
|
* Standard isolated-margin model (new position, no existing exposure):
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* margin_available = margin_per_unit - maintenance_per_unit
|
|
83
|
-
* liq_price = entryPrice - side * margin_available / (1 - mmr * side)
|
|
90
|
+
* margin_available = entryPrice × (1 / leverage - mmr)
|
|
91
|
+
* liq_price = entryPrice - side × margin_available / (1 - mmr × side)
|
|
84
92
|
*
|
|
85
93
|
* For existing positions, prefer `Position.liquidationPrice` from the venue.
|
|
86
94
|
*
|
|
87
95
|
* @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
|
|
88
|
-
* (
|
|
89
|
-
* @returns
|
|
90
|
-
*
|
|
96
|
+
* (`'0.01'` = 1%).
|
|
97
|
+
* @returns The estimate, or `undefined` for a zero leverage or a zero
|
|
98
|
+
* denominator.
|
|
99
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
91
100
|
* @public
|
|
92
101
|
*/
|
|
93
102
|
export declare function estimateLiquidationPrice(params: {
|
|
94
|
-
entryPrice:
|
|
95
|
-
leverage:
|
|
103
|
+
entryPrice: string;
|
|
104
|
+
leverage: string;
|
|
105
|
+
isLong: boolean;
|
|
106
|
+
maintenanceMarginRate: string;
|
|
107
|
+
}): string | undefined;
|
|
108
|
+
/** @public */
|
|
109
|
+
export declare const safeEstimateLiquidationPrice: (params: {
|
|
110
|
+
entryPrice: string;
|
|
111
|
+
leverage: string;
|
|
96
112
|
isLong: boolean;
|
|
97
|
-
maintenanceMarginRate:
|
|
98
|
-
})
|
|
113
|
+
maintenanceMarginRate: string;
|
|
114
|
+
}) => string | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Estimate the liquidation price of a new isolated position from the flat
|
|
117
|
+
* `market.maintenanceMarginRate`.
|
|
118
|
+
*
|
|
119
|
+
* @returns The estimate, or `undefined` when the market carries no
|
|
120
|
+
* `maintenanceMarginRate` or `estimateLiquidationPrice` gives `undefined`.
|
|
121
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
export declare function estimateLiquidationPriceAtMarketRate(market: PerpsMarket, params: LiquidationEstimateParams): string | undefined;
|
|
125
|
+
/** @public */
|
|
126
|
+
export declare const safeEstimateLiquidationPriceAtMarketRate: (market: PerpsMarket, params: LiquidationEstimateParams) => string | undefined;
|
|
99
127
|
/**
|
|
100
128
|
* Whether an isolated position with this liquidation price is already past
|
|
101
129
|
* liquidation at `currentPrice`: a long liquidates when the price falls to its
|
|
102
130
|
* liquidation level, a short when it rises to it. A non-positive price on
|
|
103
131
|
* either side counts as unknown, so the result is `false`.
|
|
104
132
|
*
|
|
133
|
+
* @throws {PerpsError} `ValidationError` when a price is not a decimal string.
|
|
105
134
|
* @public
|
|
106
135
|
*/
|
|
107
136
|
export declare function wouldImmediatelyLiquidate(params: {
|
|
108
|
-
liquidationPrice:
|
|
109
|
-
currentPrice:
|
|
137
|
+
liquidationPrice: string;
|
|
138
|
+
currentPrice: string;
|
|
110
139
|
isLong: boolean;
|
|
111
140
|
}): boolean;
|
|
141
|
+
/** @public */
|
|
142
|
+
export declare const safeWouldImmediatelyLiquidate: (params: {
|
|
143
|
+
liquidationPrice: string;
|
|
144
|
+
currentPrice: string;
|
|
145
|
+
isLong: boolean;
|
|
146
|
+
}) => boolean | undefined;
|
|
112
147
|
/**
|
|
113
|
-
* Estimated average entry price after adding to an existing position
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @
|
|
120
|
-
*
|
|
121
|
-
* @
|
|
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).
|
|
148
|
+
* Estimated average entry price after adding to an existing position: the
|
|
149
|
+
* size-weighted average of the current entry and the fill price. Both legs
|
|
150
|
+
* are in the same direction (an add, not a flip).
|
|
151
|
+
*
|
|
152
|
+
* @param fillPrice - Expected fill price (mid for market, limit price for
|
|
153
|
+
* limit orders).
|
|
154
|
+
* @returns The new average entry price, or `undefined` when the combined size
|
|
155
|
+
* is not positive.
|
|
156
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
126
157
|
* @public
|
|
127
158
|
*/
|
|
128
159
|
export declare function estimateAverageEntryPrice(params: {
|
|
129
|
-
currentSize:
|
|
130
|
-
currentEntry:
|
|
131
|
-
addSize:
|
|
132
|
-
fillPrice:
|
|
133
|
-
}):
|
|
160
|
+
currentSize: string;
|
|
161
|
+
currentEntry: string;
|
|
162
|
+
addSize: string;
|
|
163
|
+
fillPrice: string;
|
|
164
|
+
}): string | undefined;
|
|
165
|
+
/** @public */
|
|
166
|
+
export declare const safeEstimateAverageEntryPrice: (params: {
|
|
167
|
+
currentSize: string;
|
|
168
|
+
currentEntry: string;
|
|
169
|
+
addSize: string;
|
|
170
|
+
fillPrice: string;
|
|
171
|
+
}) => string | undefined;
|
|
134
172
|
/**
|
|
135
|
-
* Estimated effective leverage after adding margin and notional
|
|
173
|
+
* Estimated effective leverage after adding margin and notional:
|
|
174
|
+
* `(currentNotional + addNotional) ÷ (currentMargin + addMargin)`.
|
|
136
175
|
*
|
|
137
|
-
* leverage
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* @returns The new effective leverage, or undefined if total margin is
|
|
142
|
-
* non-positive.
|
|
176
|
+
* @returns The new leverage, or `undefined` when the total margin is not
|
|
177
|
+
* positive.
|
|
178
|
+
* @throws {PerpsError} `ValidationError` when an input is not a decimal string.
|
|
143
179
|
* @public
|
|
144
180
|
*/
|
|
145
181
|
export declare function estimateNewLeverage(params: {
|
|
146
|
-
currentNotional:
|
|
147
|
-
currentMargin:
|
|
148
|
-
addNotional:
|
|
149
|
-
addMargin:
|
|
150
|
-
}):
|
|
182
|
+
currentNotional: string;
|
|
183
|
+
currentMargin: string;
|
|
184
|
+
addNotional: string;
|
|
185
|
+
addMargin: string;
|
|
186
|
+
}): string | undefined;
|
|
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
|
|
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 is not a decimal string.
|
|
157
200
|
* @public
|
|
158
201
|
*/
|
|
159
202
|
export declare function estimateUnrealizedPnl(params: {
|
|
160
|
-
entryPrice:
|
|
161
|
-
markPrice:
|
|
162
|
-
size:
|
|
203
|
+
entryPrice: string;
|
|
204
|
+
markPrice: string;
|
|
205
|
+
size: string;
|
|
163
206
|
isLong: boolean;
|
|
164
|
-
}):
|
|
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 is not a decimal string.
|
|
171
221
|
* @public
|
|
172
222
|
*/
|
|
173
223
|
export declare function calculateRealizedPnl(params: {
|
|
174
|
-
entryPrice:
|
|
175
|
-
closePrice:
|
|
176
|
-
closeSize:
|
|
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
|
-
})
|
|
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
|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