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