@lifi/perps-sdk 20.1.0 → 21.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -6
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +4 -1
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
- package/dist/cjs/decimal/big.d.ts.map +1 -0
- package/dist/cjs/{utils/decimal.js → decimal/big.js} +1 -1
- package/dist/cjs/decimal/big.js.map +1 -0
- package/dist/cjs/decimal/convert.d.ts +11 -0
- package/dist/cjs/decimal/convert.d.ts.map +1 -0
- package/dist/cjs/decimal/convert.js +70 -0
- package/dist/cjs/decimal/convert.js.map +1 -0
- package/dist/cjs/decimal/format.d.ts.map +1 -0
- package/dist/cjs/decimal/format.js.map +1 -0
- package/dist/cjs/decimal/parse.d.ts +6 -0
- package/dist/cjs/decimal/parse.d.ts.map +1 -0
- package/dist/cjs/{utils → decimal}/parse.js +5 -4
- package/dist/cjs/decimal/parse.js.map +1 -0
- package/dist/cjs/index.d.ts +17 -15
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +75 -61
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +5 -0
- package/dist/cjs/math/fills.d.ts.map +1 -0
- package/dist/cjs/{utils/fillClassification.js → math/fills.js} +16 -1
- package/dist/cjs/math/fills.js.map +1 -0
- package/dist/cjs/math/order.d.ts +45 -0
- package/dist/cjs/math/order.d.ts.map +1 -0
- package/dist/cjs/math/order.js +256 -0
- package/dist/cjs/math/order.js.map +1 -0
- package/dist/cjs/math/position.d.ts +56 -0
- package/dist/cjs/math/position.d.ts.map +1 -0
- package/dist/cjs/math/position.js +160 -0
- package/dist/cjs/math/position.js.map +1 -0
- package/dist/cjs/services/resolveQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveQuote.js +2 -2
- package/dist/cjs/services/resolveQuote.js.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.js +2 -2
- package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/cjs/types/provider.d.ts +3 -3
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/types/withdrawal.d.ts +7 -5
- package/dist/cjs/types/withdrawal.d.ts.map +1 -1
- package/dist/cjs/utils/orderClassification.d.ts +1 -3
- package/dist/cjs/utils/orderClassification.d.ts.map +1 -1
- package/dist/cjs/utils/orderClassification.js +1 -15
- package/dist/cjs/utils/orderClassification.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/wire/orderAmounts.d.ts +18 -0
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -0
- package/dist/cjs/wire/orderAmounts.js +79 -0
- package/dist/cjs/wire/orderAmounts.js.map +1 -0
- package/dist/cjs/wire/refuel.d.ts +8 -0
- package/dist/cjs/wire/refuel.d.ts.map +1 -0
- package/dist/cjs/wire/refuel.js +23 -0
- package/dist/cjs/wire/refuel.js.map +1 -0
- package/dist/cjs/wire/requireDecimal.d.ts +4 -0
- package/dist/cjs/wire/requireDecimal.d.ts.map +1 -0
- package/dist/cjs/wire/requireDecimal.js +16 -0
- package/dist/cjs/wire/requireDecimal.js.map +1 -0
- package/dist/cjs/wire/snap.d.ts +5 -0
- package/dist/cjs/wire/snap.d.ts.map +1 -0
- package/dist/cjs/wire/snap.js +12 -0
- package/dist/cjs/wire/snap.js.map +1 -0
- package/dist/cjs/wire/transferable.d.ts +3 -0
- package/dist/cjs/wire/transferable.d.ts.map +1 -0
- package/dist/cjs/wire/transferable.js +13 -0
- package/dist/cjs/wire/transferable.js.map +1 -0
- package/dist/cjs/wire/withdraw.d.ts +4 -0
- package/dist/cjs/wire/withdraw.d.ts.map +1 -0
- package/dist/cjs/wire/withdraw.js +13 -0
- package/dist/cjs/wire/withdraw.js.map +1 -0
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +4 -1
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
- package/dist/esm/decimal/big.d.ts.map +1 -0
- package/dist/esm/{utils/decimal.js → decimal/big.js} +1 -1
- package/dist/esm/decimal/big.js.map +1 -0
- package/dist/esm/decimal/convert.d.ts +80 -0
- package/dist/esm/decimal/convert.d.ts.map +1 -0
- package/dist/esm/decimal/convert.js +118 -0
- package/dist/esm/decimal/convert.js.map +1 -0
- package/dist/esm/decimal/format.d.ts.map +1 -0
- package/dist/esm/decimal/format.js.map +1 -0
- package/dist/esm/decimal/parse.d.ts +29 -0
- package/dist/esm/decimal/parse.d.ts.map +1 -0
- package/dist/esm/{utils → decimal}/parse.js +9 -10
- package/dist/esm/decimal/parse.js.map +1 -0
- package/dist/esm/index.d.ts +17 -15
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +12 -12
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +26 -0
- package/dist/esm/math/fills.d.ts.map +1 -0
- package/dist/esm/{utils/fillClassification.js → math/fills.js} +26 -2
- package/dist/esm/math/fills.js.map +1 -0
- package/dist/esm/{utils/calculations.d.ts → math/order.d.ts} +70 -75
- package/dist/esm/math/order.d.ts.map +1 -0
- package/dist/esm/{utils/calculations.js → math/order.js} +155 -111
- package/dist/esm/math/order.js.map +1 -0
- package/dist/esm/math/position.d.ts +211 -0
- package/dist/esm/math/position.d.ts.map +1 -0
- package/dist/esm/math/position.js +299 -0
- package/dist/esm/math/position.js.map +1 -0
- package/dist/esm/services/resolveQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveQuote.js +1 -1
- package/dist/esm/services/resolveQuote.js.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/esm/types/provider.d.ts +23 -11
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/types/withdrawal.d.ts +17 -5
- package/dist/esm/types/withdrawal.d.ts.map +1 -1
- package/dist/esm/utils/orderClassification.d.ts +1 -14
- package/dist/esm/utils/orderClassification.d.ts.map +1 -1
- package/dist/esm/utils/orderClassification.js +1 -25
- package/dist/esm/utils/orderClassification.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/wire/orderAmounts.d.ts +55 -0
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -0
- package/dist/esm/wire/orderAmounts.js +114 -0
- package/dist/esm/wire/orderAmounts.js.map +1 -0
- package/dist/esm/wire/refuel.d.ts +28 -0
- package/dist/esm/wire/refuel.d.ts.map +1 -0
- package/dist/esm/wire/refuel.js +36 -0
- package/dist/esm/wire/refuel.js.map +1 -0
- package/dist/esm/wire/requireDecimal.d.ts +11 -0
- package/dist/esm/wire/requireDecimal.d.ts.map +1 -0
- package/dist/esm/wire/requireDecimal.js +17 -0
- package/dist/esm/wire/requireDecimal.js.map +1 -0
- package/dist/esm/wire/snap.d.ts +24 -0
- package/dist/esm/wire/snap.d.ts.map +1 -0
- package/dist/esm/wire/snap.js +27 -0
- package/dist/esm/wire/snap.js.map +1 -0
- package/dist/esm/wire/transferable.d.ts +15 -0
- package/dist/esm/wire/transferable.d.ts.map +1 -0
- package/dist/esm/wire/transferable.js +22 -0
- package/dist/esm/wire/transferable.js.map +1 -0
- package/dist/esm/wire/withdraw.d.ts +14 -0
- package/dist/esm/wire/withdraw.d.ts.map +1 -0
- package/dist/esm/wire/withdraw.js +20 -0
- package/dist/esm/wire/withdraw.js.map +1 -0
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/{utils/decimal.d.ts → decimal/big.d.ts} +1 -1
- package/dist/types/decimal/big.d.ts.map +1 -0
- package/dist/types/decimal/convert.d.ts +80 -0
- package/dist/types/decimal/convert.d.ts.map +1 -0
- package/dist/types/decimal/format.d.ts.map +1 -0
- package/dist/types/decimal/parse.d.ts +29 -0
- package/dist/types/decimal/parse.d.ts.map +1 -0
- package/dist/types/index.d.ts +17 -15
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +26 -0
- package/dist/types/math/fills.d.ts.map +1 -0
- package/dist/types/{utils/calculations.d.ts → math/order.d.ts} +70 -75
- package/dist/types/math/order.d.ts.map +1 -0
- package/dist/types/math/position.d.ts +211 -0
- package/dist/types/math/position.d.ts.map +1 -0
- package/dist/types/services/resolveQuote.d.ts.map +1 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +23 -11
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/types/withdrawal.d.ts +17 -5
- package/dist/types/types/withdrawal.d.ts.map +1 -1
- package/dist/types/utils/orderClassification.d.ts +1 -14
- package/dist/types/utils/orderClassification.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/wire/orderAmounts.d.ts +55 -0
- package/dist/types/wire/orderAmounts.d.ts.map +1 -0
- package/dist/types/wire/refuel.d.ts +28 -0
- package/dist/types/wire/refuel.d.ts.map +1 -0
- package/dist/types/wire/requireDecimal.d.ts +11 -0
- package/dist/types/wire/requireDecimal.d.ts.map +1 -0
- package/dist/types/wire/snap.d.ts +24 -0
- package/dist/types/wire/snap.d.ts.map +1 -0
- package/dist/types/wire/transferable.d.ts +15 -0
- package/dist/types/wire/transferable.d.ts.map +1 -0
- package/dist/types/wire/withdraw.d.ts +14 -0
- package/dist/types/wire/withdraw.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +4 -1
- package/src/decimal/convert.ts +164 -0
- package/src/{utils → decimal}/parse.ts +13 -11
- package/src/index.ts +80 -57
- package/src/{utils/fillClassification.ts → math/fills.ts} +30 -1
- package/src/{utils/calculations.ts → math/order.ts} +205 -127
- package/src/{utils/positionMath.ts → math/position.ts} +173 -21
- package/src/services/resolveQuote.ts +1 -1
- package/src/services/resolveSubscribeQuote.ts +1 -1
- package/src/types/provider.ts +23 -10
- package/src/types/withdrawal.ts +22 -5
- package/src/utils/orderClassification.ts +0 -30
- package/src/version.ts +1 -1
- package/src/wire/orderAmounts.ts +172 -0
- package/src/wire/refuel.ts +51 -0
- package/src/wire/requireDecimal.ts +24 -0
- package/src/wire/snap.ts +38 -0
- package/src/wire/transferable.ts +26 -0
- package/src/wire/withdraw.ts +29 -0
- package/dist/cjs/utils/calculations.d.ts +0 -47
- package/dist/cjs/utils/calculations.d.ts.map +0 -1
- package/dist/cjs/utils/calculations.js +0 -214
- package/dist/cjs/utils/calculations.js.map +0 -1
- package/dist/cjs/utils/decimal.d.ts.map +0 -1
- package/dist/cjs/utils/decimal.js.map +0 -1
- package/dist/cjs/utils/fillClassification.d.ts +0 -3
- package/dist/cjs/utils/fillClassification.d.ts.map +0 -1
- package/dist/cjs/utils/fillClassification.js.map +0 -1
- package/dist/cjs/utils/format.d.ts.map +0 -1
- package/dist/cjs/utils/format.js.map +0 -1
- package/dist/cjs/utils/math.d.ts +0 -4
- package/dist/cjs/utils/math.d.ts.map +0 -1
- package/dist/cjs/utils/math.js +0 -8
- package/dist/cjs/utils/math.js.map +0 -1
- package/dist/cjs/utils/orderEntry.d.ts +0 -6
- package/dist/cjs/utils/orderEntry.d.ts.map +0 -1
- package/dist/cjs/utils/orderEntry.js +0 -20
- package/dist/cjs/utils/orderEntry.js.map +0 -1
- package/dist/cjs/utils/orderMath.d.ts +0 -6
- package/dist/cjs/utils/orderMath.d.ts.map +0 -1
- package/dist/cjs/utils/orderMath.js +0 -85
- package/dist/cjs/utils/orderMath.js.map +0 -1
- package/dist/cjs/utils/parse.d.ts +0 -3
- package/dist/cjs/utils/parse.d.ts.map +0 -1
- package/dist/cjs/utils/parse.js.map +0 -1
- package/dist/cjs/utils/positionMath.d.ts +0 -37
- package/dist/cjs/utils/positionMath.d.ts.map +0 -1
- package/dist/cjs/utils/positionMath.js +0 -99
- package/dist/cjs/utils/positionMath.js.map +0 -1
- package/dist/cjs/utils/scaleToInteger.d.ts +0 -3
- package/dist/cjs/utils/scaleToInteger.d.ts.map +0 -1
- package/dist/cjs/utils/scaleToInteger.js +0 -30
- package/dist/cjs/utils/scaleToInteger.js.map +0 -1
- package/dist/cjs/utils/units.d.ts +0 -3
- package/dist/cjs/utils/units.d.ts.map +0 -1
- package/dist/cjs/utils/units.js +0 -18
- package/dist/cjs/utils/units.js.map +0 -1
- package/dist/cjs/utils/validation.d.ts +0 -2
- package/dist/cjs/utils/validation.d.ts.map +0 -1
- package/dist/cjs/utils/validation.js +0 -16
- package/dist/cjs/utils/validation.js.map +0 -1
- package/dist/esm/utils/calculations.d.ts.map +0 -1
- package/dist/esm/utils/calculations.js.map +0 -1
- package/dist/esm/utils/decimal.d.ts.map +0 -1
- package/dist/esm/utils/decimal.js.map +0 -1
- package/dist/esm/utils/fillClassification.d.ts +0 -13
- package/dist/esm/utils/fillClassification.d.ts.map +0 -1
- package/dist/esm/utils/fillClassification.js.map +0 -1
- package/dist/esm/utils/format.d.ts.map +0 -1
- package/dist/esm/utils/format.js.map +0 -1
- package/dist/esm/utils/math.d.ts +0 -6
- package/dist/esm/utils/math.d.ts.map +0 -1
- package/dist/esm/utils/math.js +0 -5
- package/dist/esm/utils/math.js.map +0 -1
- package/dist/esm/utils/orderEntry.d.ts +0 -26
- package/dist/esm/utils/orderEntry.d.ts.map +0 -1
- package/dist/esm/utils/orderEntry.js +0 -34
- package/dist/esm/utils/orderEntry.js.map +0 -1
- package/dist/esm/utils/orderMath.d.ts +0 -37
- package/dist/esm/utils/orderMath.d.ts.map +0 -1
- package/dist/esm/utils/orderMath.js +0 -113
- package/dist/esm/utils/orderMath.js.map +0 -1
- package/dist/esm/utils/parse.d.ts +0 -28
- package/dist/esm/utils/parse.d.ts.map +0 -1
- package/dist/esm/utils/parse.js.map +0 -1
- package/dist/esm/utils/positionMath.d.ts +0 -120
- package/dist/esm/utils/positionMath.d.ts.map +0 -1
- package/dist/esm/utils/positionMath.js +0 -173
- package/dist/esm/utils/positionMath.js.map +0 -1
- package/dist/esm/utils/scaleToInteger.d.ts +0 -22
- package/dist/esm/utils/scaleToInteger.d.ts.map +0 -1
- package/dist/esm/utils/scaleToInteger.js +0 -34
- package/dist/esm/utils/scaleToInteger.js.map +0 -1
- package/dist/esm/utils/units.d.ts +0 -27
- package/dist/esm/utils/units.d.ts.map +0 -1
- package/dist/esm/utils/units.js +0 -38
- package/dist/esm/utils/units.js.map +0 -1
- package/dist/esm/utils/validation.d.ts +0 -18
- package/dist/esm/utils/validation.d.ts.map +0 -1
- package/dist/esm/utils/validation.js +0 -29
- package/dist/esm/utils/validation.js.map +0 -1
- package/dist/types/utils/calculations.d.ts.map +0 -1
- package/dist/types/utils/decimal.d.ts.map +0 -1
- package/dist/types/utils/fillClassification.d.ts +0 -13
- package/dist/types/utils/fillClassification.d.ts.map +0 -1
- package/dist/types/utils/format.d.ts.map +0 -1
- package/dist/types/utils/math.d.ts +0 -6
- package/dist/types/utils/math.d.ts.map +0 -1
- package/dist/types/utils/orderEntry.d.ts +0 -26
- package/dist/types/utils/orderEntry.d.ts.map +0 -1
- package/dist/types/utils/orderMath.d.ts +0 -37
- package/dist/types/utils/orderMath.d.ts.map +0 -1
- package/dist/types/utils/parse.d.ts +0 -28
- package/dist/types/utils/parse.d.ts.map +0 -1
- package/dist/types/utils/positionMath.d.ts +0 -120
- package/dist/types/utils/positionMath.d.ts.map +0 -1
- package/dist/types/utils/scaleToInteger.d.ts +0 -22
- package/dist/types/utils/scaleToInteger.d.ts.map +0 -1
- package/dist/types/utils/units.d.ts +0 -27
- package/dist/types/utils/units.d.ts.map +0 -1
- package/dist/types/utils/validation.d.ts +0 -18
- package/dist/types/utils/validation.d.ts.map +0 -1
- package/src/utils/math.ts +0 -7
- package/src/utils/orderEntry.ts +0 -38
- package/src/utils/orderMath.ts +0 -152
- package/src/utils/scaleToInteger.ts +0 -56
- package/src/utils/units.ts +0 -39
- package/src/utils/validation.ts +0 -38
- /package/dist/cjs/{utils → decimal}/format.d.ts +0 -0
- /package/dist/cjs/{utils → decimal}/format.js +0 -0
- /package/dist/esm/{utils → decimal}/format.d.ts +0 -0
- /package/dist/esm/{utils → decimal}/format.js +0 -0
- /package/dist/types/{utils → decimal}/format.d.ts +0 -0
- /package/src/{utils/decimal.ts → decimal/big.ts} +0 -0
- /package/src/{utils → decimal}/format.ts +0 -0
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { OrderSide, OrderStatus, PositionSide, } from '@lifi/perps-types';
|
|
2
|
-
import { isActiveOrderStatus } from './orderClassification.js';
|
|
3
|
-
import { realizedPnlOnClose } from './positionMath.js';
|
|
4
|
-
/**
|
|
5
|
-
* Pick the matching open position for an order's market, if any.
|
|
6
|
-
*
|
|
7
|
-
* @param marketId - The order's `market.id`.
|
|
8
|
-
* @param positions - Open positions list (any market).
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
export function findMatchingPosition(marketId, positions) {
|
|
12
|
-
return positions.find((p) => p.market.id === marketId);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Resolve the close-size against a position, applying the spec's cap rules:
|
|
16
|
-
* - `orderSize === 0` is the Hyperliquid convention for "close entire
|
|
17
|
-
* position" → close the full position size.
|
|
18
|
-
* - Otherwise cap at the absolute position size; an order larger than the
|
|
19
|
-
* position can only close what's open.
|
|
20
|
-
*
|
|
21
|
-
* Inputs are non-negative magnitudes.
|
|
22
|
-
* @public
|
|
23
|
-
*/
|
|
24
|
-
export function resolveCloseSize(orderSize, positionSize) {
|
|
25
|
-
if (orderSize === 0) {
|
|
26
|
-
return positionSize;
|
|
27
|
-
}
|
|
28
|
-
return Math.min(orderSize, positionSize);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Expected rPnL for a resting limit order against a matching position.
|
|
32
|
-
*
|
|
33
|
-
* Reducing requires opposite sides (long position + SELL, short position +
|
|
34
|
-
* BUY). Same-side orders add to the position and have no rPnL → `null`.
|
|
35
|
-
* Projects `remainingSize`, because an already-filled quantity has realised
|
|
36
|
-
* its PnL at the fill price rather than at this order's limit price. A
|
|
37
|
-
* `remainingSize` of zero leaves nothing to project.
|
|
38
|
-
*
|
|
39
|
-
* @returns Realised PnL if the order would reduce the position, otherwise
|
|
40
|
-
* `null`.
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
export function expectedRealizedPnlForOpenOrder(order, position) {
|
|
44
|
-
if (!position || !isActiveOrderStatus(order.status)) {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
const isLong = position.side === PositionSide.LONG;
|
|
48
|
-
const reducesPosition = (isLong && order.side === OrderSide.SELL) ||
|
|
49
|
-
(!isLong && order.side === OrderSide.BUY);
|
|
50
|
-
if (!reducesPosition) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
const limitPrice = Number.parseFloat(order.price ?? '');
|
|
54
|
-
const entryPrice = Number.parseFloat(position.entryPrice);
|
|
55
|
-
const orderSize = Math.abs(Number.parseFloat(order.remainingSize));
|
|
56
|
-
const positionSize = Math.abs(Number.parseFloat(position.size));
|
|
57
|
-
if (!Number.isFinite(limitPrice) ||
|
|
58
|
-
!Number.isFinite(entryPrice) ||
|
|
59
|
-
!Number.isFinite(orderSize) ||
|
|
60
|
-
!Number.isFinite(positionSize) ||
|
|
61
|
-
positionSize <= 0) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
// `resolveCloseSize` reads a zero size as "close the whole position", a
|
|
65
|
-
// convention that belongs to an order's submitted size. `remainingSize` is
|
|
66
|
-
// the unfilled quantity, so zero means nothing is left to fill.
|
|
67
|
-
if (orderSize === 0) {
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
const closeSize = resolveCloseSize(orderSize, positionSize);
|
|
71
|
-
return realizedPnlOnClose({
|
|
72
|
-
entryPrice,
|
|
73
|
-
closePrice: limitPrice,
|
|
74
|
-
closeSize,
|
|
75
|
-
isLong,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
/** Project the unfilled closing quantity of an active trigger at its trigger price. */
|
|
79
|
-
export function expectedRealizedPnlForTriggerOrder(order, position) {
|
|
80
|
-
if (!position ||
|
|
81
|
-
!isActiveOrderStatus(order.status) ||
|
|
82
|
-
order.status === OrderStatus.PENDING) {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
const isLong = position.side === PositionSide.LONG;
|
|
86
|
-
if ((isLong && order.side !== OrderSide.SELL) ||
|
|
87
|
-
(!isLong && order.side !== OrderSide.BUY)) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const triggerPrice = Number.parseFloat(order.triggerPrice);
|
|
91
|
-
const entryPrice = Number.parseFloat(position.entryPrice);
|
|
92
|
-
const orderSize = Math.abs(Number.parseFloat(order.remainingSize));
|
|
93
|
-
const positionSize = Math.abs(Number.parseFloat(position.size));
|
|
94
|
-
if (!Number.isFinite(triggerPrice) ||
|
|
95
|
-
!Number.isFinite(entryPrice) ||
|
|
96
|
-
!Number.isFinite(orderSize) ||
|
|
97
|
-
!Number.isFinite(positionSize) ||
|
|
98
|
-
positionSize <= 0) {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
if (orderSize === 0 &&
|
|
102
|
-
(Number.parseFloat(order.originalSize) !== 0 || !order.reduceOnly)) {
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
const closeSize = resolveCloseSize(orderSize, positionSize);
|
|
106
|
-
return realizedPnlOnClose({
|
|
107
|
-
entryPrice,
|
|
108
|
-
closePrice: triggerPrice,
|
|
109
|
-
closeSize,
|
|
110
|
-
isLong,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
//# sourceMappingURL=orderMath.js.map
|
|
@@ -1 +0,0 @@
|
|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J,CAAC"}
|
|
@@ -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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../../../src/utils/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,CAAC,CAAA;IACV,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;IAC1E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,CAAC,CAAA;IACV,CAAC;IACD,OAAO,UAAU,CAAC,OAAO,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,gBAAgB,GACpB,2GAA2G,CAAA;AAE7G;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAgC;IAEhC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACxB,OAAO,aAAa,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;IACD,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACzC,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAA;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;AACrD,CAAC"}
|
|
@@ -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,173 +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
|
-
import { areFinite, DivBig } from './decimal.js';
|
|
9
|
-
/**
|
|
10
|
-
* Direction sign for a position.
|
|
11
|
-
*
|
|
12
|
-
* @param isLong - True for long positions, false for short.
|
|
13
|
-
* @returns +1 for long, -1 for short.
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export function directionSign(isLong) {
|
|
17
|
-
return isLong ? 1 : -1;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Estimated liquidation price for an isolated-margin position, parameterised
|
|
21
|
-
* by the venue's maintenance margin rate.
|
|
22
|
-
*
|
|
23
|
-
* Standard isolated-margin model (new position, no existing exposure):
|
|
24
|
-
* margin_per_unit = entryPrice / leverage
|
|
25
|
-
* maintenance_per_unit = entryPrice * mmr
|
|
26
|
-
* margin_available = margin_per_unit - maintenance_per_unit
|
|
27
|
-
* liq_price = entryPrice - side * margin_available / (1 - mmr * side)
|
|
28
|
-
*
|
|
29
|
-
* For existing positions, prefer `Position.liquidationPrice` from the venue.
|
|
30
|
-
*
|
|
31
|
-
* @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
|
|
32
|
-
* (e.g. 0.01 = 1%).
|
|
33
|
-
* @returns Estimated liquidation price, or undefined if the inputs cannot
|
|
34
|
-
* produce one (zero leverage, degenerate denominator).
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
export function estimateIsolatedLiquidationPrice(params) {
|
|
38
|
-
const { entryPrice, leverage, isLong, maintenanceMarginRate } = params;
|
|
39
|
-
if (leverage === 0) {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
const side = directionSign(isLong);
|
|
43
|
-
const denominator = 1 - maintenanceMarginRate * side;
|
|
44
|
-
if (denominator === 0) {
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
if (!areFinite(entryPrice, leverage, maintenanceMarginRate)) {
|
|
48
|
-
return Number.NaN;
|
|
49
|
-
}
|
|
50
|
-
const mmr = new DivBig(maintenanceMarginRate);
|
|
51
|
-
const marginAvailable = new DivBig(1)
|
|
52
|
-
.div(leverage)
|
|
53
|
-
.minus(mmr)
|
|
54
|
-
.times(entryPrice);
|
|
55
|
-
return new DivBig(entryPrice)
|
|
56
|
-
.minus(marginAvailable.times(side).div(mmr.times(-side).plus(1)))
|
|
57
|
-
.toNumber();
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Whether an isolated position with this liquidation price is already past
|
|
61
|
-
* liquidation at `currentPrice`: a long liquidates when the price falls to its
|
|
62
|
-
* liquidation level, a short when it rises to it. A non-positive price on
|
|
63
|
-
* either side counts as unknown, so the result is `false`.
|
|
64
|
-
*
|
|
65
|
-
* @public
|
|
66
|
-
*/
|
|
67
|
-
export function wouldImmediatelyLiquidate(params) {
|
|
68
|
-
const { liquidationPrice, currentPrice, isLong } = params;
|
|
69
|
-
if (currentPrice <= 0 || liquidationPrice <= 0) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
return isLong
|
|
73
|
-
? liquidationPrice >= currentPrice
|
|
74
|
-
: liquidationPrice <= currentPrice;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Predicted average entry price after adding to an existing position.
|
|
78
|
-
*
|
|
79
|
-
* Weighted average of the current entry and the new fill price, weighted by
|
|
80
|
-
* the size of each leg in coin units. Both legs assumed in the same direction
|
|
81
|
-
* (this helper is for adding to, not flipping, a position).
|
|
82
|
-
*
|
|
83
|
-
* @param currentSize - Existing position size in coin units (>= 0).
|
|
84
|
-
* @param currentEntry - Existing position's average entry price.
|
|
85
|
-
* @param addSize - Size being added in coin units (>= 0).
|
|
86
|
-
* @param fillPrice - Price the new size is expected to fill at (mid for
|
|
87
|
-
* market, limitPrice for limit orders).
|
|
88
|
-
* @returns The new weighted-average entry price, or undefined if the inputs
|
|
89
|
-
* cannot produce a valid average (zero combined size, non-finite values).
|
|
90
|
-
* @public
|
|
91
|
-
*/
|
|
92
|
-
export function predictAverageEntryPrice(params) {
|
|
93
|
-
const { currentSize, currentEntry, addSize, fillPrice } = params;
|
|
94
|
-
const totalSize = currentSize + addSize;
|
|
95
|
-
if (totalSize <= 0) {
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
if (!Number.isFinite(currentEntry) || !Number.isFinite(fillPrice)) {
|
|
99
|
-
return undefined;
|
|
100
|
-
}
|
|
101
|
-
if (!areFinite(currentSize, addSize)) {
|
|
102
|
-
return Number.NaN;
|
|
103
|
-
}
|
|
104
|
-
return new DivBig(currentSize)
|
|
105
|
-
.times(currentEntry)
|
|
106
|
-
.plus(new DivBig(addSize).times(fillPrice))
|
|
107
|
-
.div(new DivBig(currentSize).plus(addSize))
|
|
108
|
-
.toNumber();
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Predicted effective leverage after adding margin and notional.
|
|
112
|
-
*
|
|
113
|
-
* leverage = totalNotional / totalMargin. The caller computes notional from
|
|
114
|
-
* size and price (`calculateNotionalValue`) and supplies the additional
|
|
115
|
-
* margin the user is about to put up.
|
|
116
|
-
*
|
|
117
|
-
* @returns The new effective leverage, or undefined if total margin is
|
|
118
|
-
* non-positive.
|
|
119
|
-
* @public
|
|
120
|
-
*/
|
|
121
|
-
export function predictNewLeverage(params) {
|
|
122
|
-
const { currentNotional, currentMargin, addNotional, addMargin } = params;
|
|
123
|
-
const totalMargin = currentMargin + addMargin;
|
|
124
|
-
if (totalMargin <= 0) {
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
if (!areFinite(currentNotional, currentMargin, addNotional, addMargin)) {
|
|
128
|
-
return Number.NaN;
|
|
129
|
-
}
|
|
130
|
-
return new DivBig(currentNotional)
|
|
131
|
-
.plus(addNotional)
|
|
132
|
-
.div(new DivBig(currentMargin).plus(addMargin))
|
|
133
|
-
.toNumber();
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Predicted unrealised PnL at the current mark price.
|
|
137
|
-
*
|
|
138
|
-
* `pnl = (markPrice - entryPrice) * size * directionSign(isLong)`
|
|
139
|
-
*
|
|
140
|
-
* @param size - Position size as a non-negative magnitude.
|
|
141
|
-
* @public
|
|
142
|
-
*/
|
|
143
|
-
export function predictUnrealizedPnl(params) {
|
|
144
|
-
const { entryPrice, markPrice, size, isLong } = params;
|
|
145
|
-
if (!areFinite(entryPrice, markPrice, size)) {
|
|
146
|
-
return Number.NaN;
|
|
147
|
-
}
|
|
148
|
-
return new DivBig(markPrice)
|
|
149
|
-
.minus(entryPrice)
|
|
150
|
-
.times(size)
|
|
151
|
-
.times(directionSign(isLong))
|
|
152
|
-
.toNumber();
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Realised PnL on the portion of a position being closed.
|
|
156
|
-
*
|
|
157
|
-
* `rPnl = (closePrice - entryPrice) * closeSize * directionSign(isLong)`
|
|
158
|
-
*
|
|
159
|
-
* @param closeSize - Size being closed as a non-negative magnitude.
|
|
160
|
-
* @public
|
|
161
|
-
*/
|
|
162
|
-
export function realizedPnlOnClose(params) {
|
|
163
|
-
const { entryPrice, closePrice, closeSize, isLong } = params;
|
|
164
|
-
if (!areFinite(entryPrice, closePrice, closeSize)) {
|
|
165
|
-
return Number.NaN;
|
|
166
|
-
}
|
|
167
|
-
return new DivBig(closePrice)
|
|
168
|
-
.minus(entryPrice)
|
|
169
|
-
.times(closeSize)
|
|
170
|
-
.times(directionSign(isLong))
|
|
171
|
-
.toNumber();
|
|
172
|
-
}
|
|
173
|
-
//# sourceMappingURL=positionMath.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"positionMath.js","sourceRoot":"","sources":["../../../src/utils/positionMath.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAEhD;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAe;IAC3C,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACxB,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gCAAgC,CAAC,MAKhD;IACC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM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
|
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,34 +0,0 @@
|
|
|
1
|
-
import { PerpsErrorCode } from '@lifi/perps-types';
|
|
2
|
-
import Big from 'big.js';
|
|
3
|
-
import { PerpsError } from '../errors/PerpsError.js';
|
|
4
|
-
/**
|
|
5
|
-
* Scale a human-readable decimal string to a scaled integer in exact decimal
|
|
6
|
-
* arithmetic — an on-grid input maps to its exact scaled integer with no
|
|
7
|
-
* binary float artifacts (`'0.29'` at 2 decimals is 29, never 28).
|
|
8
|
-
* Off-grid input resolves per `policy`; there is no silent default.
|
|
9
|
-
*
|
|
10
|
-
* @throws {PerpsError} `ValidationError` when `value` is not a decimal
|
|
11
|
-
* numeric string, `decimals` is not a non-negative integer, or the scaled
|
|
12
|
-
* result's magnitude exceeds `Number.MAX_SAFE_INTEGER`.
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
export const scaleToInteger = (value, decimals, policy) => {
|
|
16
|
-
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
17
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid decimals for integer scaling: ${decimals}`);
|
|
18
|
-
}
|
|
19
|
-
let parsed;
|
|
20
|
-
try {
|
|
21
|
-
parsed = new Big(value);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Invalid decimal string for integer scaling: '${value}'`);
|
|
25
|
-
}
|
|
26
|
-
const scaled = parsed
|
|
27
|
-
.times(new Big(10).pow(decimals))
|
|
28
|
-
.round(0, policy === 'truncate' ? Big.roundDown : Big.roundHalfUp);
|
|
29
|
-
if (scaled.abs().gt(Number.MAX_SAFE_INTEGER)) {
|
|
30
|
-
throw new PerpsError(PerpsErrorCode.ValidationError, `Scaled value ${scaled.toFixed()} exceeds Number.MAX_SAFE_INTEGER and cannot be encoded exactly.`);
|
|
31
|
-
}
|
|
32
|
-
return scaled.toNumber();
|
|
33
|
-
};
|
|
34
|
-
//# sourceMappingURL=scaleToInteger.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"scaleToInteger.js","sourceRoot":"","sources":["../../../src/utils/scaleToInteger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAYpD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,KAAa,EACb,QAAgB,EAChB,MAA4B,EACpB,EAAE;IACV,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,UAAU,CAClB,cAAc,CAAC,eAAe,EAC9B,yCAAyC,QAAQ,EAAE,CACpD,CAAA;IACH,CAAC;IACD,IAAI,MAAW,CAAA;IACf,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAA;IACzB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,UAAU,CAClB,cAAc,CAAC,eAAe,EAC9B,gDAAgD,KAAK,GAAG,CACzD,CAAA;IACH,CAAC;IACD,MAAM,MAAM,GAAG,MAAM;SAClB,KAAK,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SAChC,KAAK,CAAC,CAAC,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;IACpE,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CAClB,cAAc,CAAC,eAAe,EAC9B,gBAAgB,MAAM,CAAC,OAAO,EAAE,iEAAiE,CAClG,CAAA;IACH,CAAC;IACD,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAA;AAC1B,CAAC,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"}
|
package/dist/esm/utils/units.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token unit conversion utilities.
|
|
3
|
-
*/
|
|
4
|
-
import { formatUnits } from 'viem';
|
|
5
|
-
/**
|
|
6
|
-
* Convert a base-unit amount (bigint string) to a human-readable decimal string.
|
|
7
|
-
*
|
|
8
|
-
* @param amount - Amount in base units (e.g. "1000000" for 1 USDC)
|
|
9
|
-
* @param decimals - Token decimals (e.g. 6 for USDC)
|
|
10
|
-
* @returns Decimal string (e.g. "1.0"); `"0"` when `amount` is not a valid
|
|
11
|
-
* bigint
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* fromBaseUnits('1000000', 6) // '1'
|
|
15
|
-
* ```
|
|
16
|
-
* @public
|
|
17
|
-
*/
|
|
18
|
-
export function fromBaseUnits(amount, decimals) {
|
|
19
|
-
try {
|
|
20
|
-
return formatUnits(BigInt(amount), decimals);
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
return '0';
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Convert a base-unit amount to a number.
|
|
28
|
-
*
|
|
29
|
-
* @param amount - Amount in base units
|
|
30
|
-
* @param decimals - Token decimals
|
|
31
|
-
* @returns Numeric value, or 0 if parsing fails
|
|
32
|
-
* @public
|
|
33
|
-
*/
|
|
34
|
-
export function fromBaseUnitsNumber(amount, decimals) {
|
|
35
|
-
const value = Number(fromBaseUnits(amount, decimals));
|
|
36
|
-
return Number.isFinite(value) ? value : 0;
|
|
37
|
-
}
|
|
38
|
-
//# sourceMappingURL=units.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"units.js","sourceRoot":"","sources":["../../../src/utils/units.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAElC;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,QAAgB;IAC5D,IAAI,CAAC;QACH,OAAO,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAA;IAC9C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAA;IACZ,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAc,EAAE,QAAgB;IAClE,MAAM,KAAK,GAAG,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAA;IACrD,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;AAC3C,CAAC"}
|
|
@@ -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"}
|
|
@@ -1,29 +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 function validateMargin(margin, _leverage, availableBalance, _feeRate, minMarginUsd) {
|
|
18
|
-
if (margin <= 0) {
|
|
19
|
-
return '';
|
|
20
|
-
}
|
|
21
|
-
if (availableBalance !== null && margin > availableBalance) {
|
|
22
|
-
return 'insufficient';
|
|
23
|
-
}
|
|
24
|
-
if (margin < minMarginUsd) {
|
|
25
|
-
return 'below-minimum';
|
|
26
|
-
}
|
|
27
|
-
return '';
|
|
28
|
-
}
|
|
29
|
-
//# sourceMappingURL=validation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../src/utils/validation.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAc,EACd,SAAiB,EACjB,gBAA+B,EAC/B,QAAuB,EACvB,YAAoB;IAEpB,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,OAAO,EAAE,CAAA;IACX,CAAC;IAED,IAAI,gBAAgB,KAAK,IAAI,IAAI,MAAM,GAAG,gBAAgB,EAAE,CAAC;QAC3D,OAAO,cAAc,CAAA;IACvB,CAAC;IAED,IAAI,MAAM,GAAG,YAAY,EAAE,CAAC;QAC1B,OAAO,eAAe,CAAA;IACxB,CAAC;IAED,OAAO,EAAE,CAAA;AACX,CAAC"}
|