@lifi/perps-sdk 21.0.0 → 22.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 +84 -16
- package/dist/cjs/client/PerpsClient.d.ts +3 -3
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/decimal/convert.d.ts +1 -5
- package/dist/cjs/decimal/convert.d.ts.map +1 -1
- package/dist/cjs/decimal/convert.js +10 -28
- package/dist/cjs/decimal/convert.js.map +1 -1
- package/dist/cjs/decimal/format.d.ts +2 -1
- package/dist/cjs/decimal/format.d.ts.map +1 -1
- package/dist/cjs/decimal/format.js.map +1 -1
- package/dist/cjs/decimal/parse.d.ts +2 -4
- package/dist/cjs/decimal/parse.d.ts.map +1 -1
- package/dist/cjs/decimal/parse.js +5 -2
- package/dist/cjs/decimal/parse.js.map +1 -1
- package/dist/cjs/decimal/requireDecimal.d.ts.map +1 -0
- package/dist/cjs/{wire → decimal}/requireDecimal.js +2 -1
- package/dist/cjs/decimal/requireDecimal.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -5
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +6 -20
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +3 -3
- package/dist/cjs/math/fills.d.ts.map +1 -1
- package/dist/cjs/math/fills.js.map +1 -1
- package/dist/cjs/math/order.d.ts +1 -8
- package/dist/cjs/math/order.d.ts.map +1 -1
- package/dist/cjs/math/order.js +6 -11
- package/dist/cjs/math/order.js.map +1 -1
- package/dist/cjs/math/position.d.ts +3 -8
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +12 -12
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/services/getActivity.d.ts.map +1 -1
- package/dist/cjs/services/getActivity.js.map +1 -1
- package/dist/cjs/services/getFills.d.ts.map +1 -1
- package/dist/cjs/services/getFills.js.map +1 -1
- package/dist/cjs/services/getOrder.d.ts.map +1 -1
- package/dist/cjs/services/getOrder.js.map +1 -1
- package/dist/cjs/services/getOrders.d.ts.map +1 -1
- package/dist/cjs/services/getOrders.js.map +1 -1
- package/dist/cjs/services/getPositions.d.ts.map +1 -1
- package/dist/cjs/services/getPositions.js.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/cjs/services/resolveSubscribeQuote.js +24 -13
- package/dist/cjs/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -4
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +1 -1
- package/dist/cjs/types/provider.d.ts.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 +1 -2
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +19 -52
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/cjs/wire/refuel.js +1 -1
- package/dist/cjs/wire/refuel.js.map +1 -1
- package/dist/cjs/wire/transferable.js +1 -1
- 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 +1 -1
- package/dist/cjs/wire/withdraw.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +40 -8
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +37 -5
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/decimal/convert.d.ts +8 -24
- package/dist/esm/decimal/convert.d.ts.map +1 -1
- package/dist/esm/decimal/convert.js +16 -42
- package/dist/esm/decimal/convert.js.map +1 -1
- package/dist/esm/decimal/format.d.ts +2 -1
- package/dist/esm/decimal/format.d.ts.map +1 -1
- package/dist/esm/decimal/format.js.map +1 -1
- package/dist/esm/decimal/parse.d.ts +7 -20
- package/dist/esm/decimal/parse.d.ts.map +1 -1
- package/dist/esm/decimal/parse.js +9 -4
- package/dist/esm/decimal/parse.js.map +1 -1
- package/dist/esm/{wire → decimal}/requireDecimal.d.ts +1 -1
- package/dist/esm/decimal/requireDecimal.d.ts.map +1 -0
- package/dist/esm/{wire → decimal}/requireDecimal.js +3 -2
- package/dist/esm/decimal/requireDecimal.js.map +1 -0
- package/dist/esm/index.d.ts +5 -5
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +3 -3
- package/dist/esm/math/fills.d.ts.map +1 -1
- package/dist/esm/math/fills.js +1 -1
- package/dist/esm/math/fills.js.map +1 -1
- package/dist/esm/math/order.d.ts +3 -34
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +8 -26
- package/dist/esm/math/order.js.map +1 -1
- package/dist/esm/math/position.d.ts +16 -34
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +23 -36
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/services/getActivity.d.ts +4 -0
- package/dist/esm/services/getActivity.d.ts.map +1 -1
- package/dist/esm/services/getActivity.js +4 -0
- package/dist/esm/services/getActivity.js.map +1 -1
- package/dist/esm/services/getFills.d.ts +4 -0
- package/dist/esm/services/getFills.d.ts.map +1 -1
- package/dist/esm/services/getFills.js +4 -0
- package/dist/esm/services/getFills.js.map +1 -1
- package/dist/esm/services/getOrder.d.ts +4 -0
- package/dist/esm/services/getOrder.d.ts.map +1 -1
- package/dist/esm/services/getOrder.js +4 -0
- package/dist/esm/services/getOrder.js.map +1 -1
- package/dist/esm/services/getOrders.d.ts +10 -1
- package/dist/esm/services/getOrders.d.ts.map +1 -1
- package/dist/esm/services/getOrders.js +10 -1
- package/dist/esm/services/getOrders.js.map +1 -1
- package/dist/esm/services/getPositions.d.ts +4 -0
- package/dist/esm/services/getPositions.d.ts.map +1 -1
- package/dist/esm/services/getPositions.js +4 -0
- package/dist/esm/services/getPositions.js.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts +6 -1
- package/dist/esm/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/esm/services/resolveSubscribeQuote.js +29 -13
- package/dist/esm/services/resolveSubscribeQuote.js.map +1 -1
- package/dist/esm/types/api.d.ts +4 -4
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +1 -1
- package/dist/esm/types/provider.d.ts.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 +15 -29
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +28 -88
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/esm/wire/refuel.js +1 -1
- package/dist/esm/wire/refuel.js.map +1 -1
- package/dist/esm/wire/transferable.js +1 -1
- 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 +1 -1
- package/dist/esm/wire/withdraw.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +40 -8
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/decimal/convert.d.ts +8 -24
- package/dist/types/decimal/convert.d.ts.map +1 -1
- package/dist/types/decimal/format.d.ts +2 -1
- package/dist/types/decimal/format.d.ts.map +1 -1
- package/dist/types/decimal/parse.d.ts +7 -20
- package/dist/types/decimal/parse.d.ts.map +1 -1
- package/dist/types/{wire → decimal}/requireDecimal.d.ts +1 -1
- package/dist/types/decimal/requireDecimal.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +3 -3
- package/dist/types/math/fills.d.ts.map +1 -1
- package/dist/types/math/order.d.ts +3 -34
- package/dist/types/math/order.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +16 -34
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/services/getActivity.d.ts +4 -0
- package/dist/types/services/getActivity.d.ts.map +1 -1
- package/dist/types/services/getFills.d.ts +4 -0
- package/dist/types/services/getFills.d.ts.map +1 -1
- package/dist/types/services/getOrder.d.ts +4 -0
- package/dist/types/services/getOrder.d.ts.map +1 -1
- package/dist/types/services/getOrders.d.ts +10 -1
- package/dist/types/services/getOrders.d.ts.map +1 -1
- package/dist/types/services/getPositions.d.ts +4 -0
- package/dist/types/services/getPositions.d.ts.map +1 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts +6 -1
- package/dist/types/services/resolveSubscribeQuote.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +4 -4
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +1 -1
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/wire/orderAmounts.d.ts +15 -29
- package/dist/types/wire/orderAmounts.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 +40 -7
- package/src/decimal/convert.ts +27 -54
- package/src/decimal/format.ts +3 -1
- package/src/decimal/parse.ts +10 -6
- package/src/{wire → decimal}/requireDecimal.ts +3 -6
- package/src/index.ts +4 -21
- package/src/math/fills.ts +8 -4
- package/src/math/order.ts +8 -31
- package/src/math/position.ts +32 -43
- package/src/services/getActivity.ts +4 -0
- package/src/services/getFills.ts +4 -0
- package/src/services/getOrder.ts +4 -0
- package/src/services/getOrders.ts +10 -1
- package/src/services/getPositions.ts +4 -0
- package/src/services/resolveSubscribeQuote.ts +37 -19
- package/src/types/api.ts +4 -3
- package/src/types/provider.ts +1 -1
- package/src/version.ts +1 -1
- package/src/wire/orderAmounts.ts +35 -117
- package/src/wire/refuel.ts +1 -1
- package/src/wire/transferable.ts +1 -1
- package/src/wire/withdraw.ts +1 -1
- package/dist/cjs/wire/requireDecimal.d.ts.map +0 -1
- package/dist/cjs/wire/requireDecimal.js.map +0 -1
- package/dist/esm/wire/requireDecimal.d.ts.map +0 -1
- package/dist/esm/wire/requireDecimal.js.map +0 -1
- package/dist/types/wire/requireDecimal.d.ts.map +0 -1
- /package/dist/cjs/{wire → decimal}/requireDecimal.d.ts +0 -0
package/src/index.ts
CHANGED
|
@@ -17,17 +17,11 @@ 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'
|
|
20
|
+
export type { BaseUnitsRounding } from './decimal/convert.js'
|
|
24
21
|
export {
|
|
25
22
|
baseUnitsToDecimal,
|
|
26
23
|
decimalToBaseUnits,
|
|
27
|
-
fromBaseUnits,
|
|
28
|
-
fromBaseUnitsNumber,
|
|
29
24
|
numberToDecimalString,
|
|
30
|
-
scaleToInteger,
|
|
31
25
|
truncateDecimal,
|
|
32
26
|
} from './decimal/convert.js'
|
|
33
27
|
export type { FormatOptions, RoundingMode } from './decimal/format.js'
|
|
@@ -39,7 +33,7 @@ export {
|
|
|
39
33
|
formatSignedUsd,
|
|
40
34
|
formatUsd,
|
|
41
35
|
} from './decimal/format.js'
|
|
42
|
-
export {
|
|
36
|
+
export { isDecimalString, parseDecimal } from './decimal/parse.js'
|
|
43
37
|
export { PerpsErrorMessage } from './errors/constants.js'
|
|
44
38
|
export { PerpsError } from './errors/PerpsError.js'
|
|
45
39
|
export {
|
|
@@ -52,18 +46,13 @@ export {
|
|
|
52
46
|
applySlippage,
|
|
53
47
|
buildQuote,
|
|
54
48
|
calculateExpectedPnl,
|
|
55
|
-
calculatePositionSize,
|
|
56
49
|
calculateRealizedPnlPercent,
|
|
57
50
|
calculateSize,
|
|
58
51
|
calculateTriggerPercent,
|
|
59
52
|
calculateTriggerPrice,
|
|
60
53
|
estimateFees,
|
|
61
54
|
estimateRealizedPnl,
|
|
62
|
-
expectedRealizedPnlForOpenOrder,
|
|
63
|
-
expectedRealizedPnlForTriggerOrder,
|
|
64
55
|
findMatchingPosition,
|
|
65
|
-
percentFromPrice,
|
|
66
|
-
priceFromPercent,
|
|
67
56
|
resolveCloseSize,
|
|
68
57
|
walkOrderbook,
|
|
69
58
|
} from './math/order.js'
|
|
@@ -75,18 +64,12 @@ export {
|
|
|
75
64
|
calculateRequiredMargin,
|
|
76
65
|
calculateRoe,
|
|
77
66
|
calculateUnrealizedPnl,
|
|
78
|
-
directionSign,
|
|
79
|
-
effectiveLeverage,
|
|
80
67
|
estimateAverageEntryPrice,
|
|
81
|
-
estimateIsolatedLiquidationPrice,
|
|
82
68
|
estimateLiquidationPrice,
|
|
83
69
|
estimateNewLeverage,
|
|
84
70
|
estimateUnrealizedPnl,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
predictNewLeverage,
|
|
88
|
-
predictUnrealizedPnl,
|
|
89
|
-
realizedPnlOnClose,
|
|
71
|
+
positionSupportsMarginAdjustment,
|
|
72
|
+
positionSupportsMarginRemoval,
|
|
90
73
|
wouldImmediatelyLiquidate,
|
|
91
74
|
} from './math/position.js'
|
|
92
75
|
// Registries
|
package/src/math/fills.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type DecimalString,
|
|
3
|
+
FillClassification,
|
|
4
|
+
OrderSide,
|
|
5
|
+
} from '@lifi/perps-types'
|
|
2
6
|
import { parseDecimal } from '../decimal/parse.js'
|
|
3
7
|
|
|
4
8
|
/**
|
|
@@ -19,9 +23,9 @@ export { FillClassification }
|
|
|
19
23
|
* @public
|
|
20
24
|
*/
|
|
21
25
|
export function classifyFillFromPosition(
|
|
22
|
-
startPosition:
|
|
26
|
+
startPosition: DecimalString,
|
|
23
27
|
side: string,
|
|
24
|
-
sz:
|
|
28
|
+
sz: DecimalString
|
|
25
29
|
): FillClassification {
|
|
26
30
|
const start = parseFloat(startPosition)
|
|
27
31
|
const delta = side === 'B' ? parseFloat(sz) : -parseFloat(sz)
|
|
@@ -66,7 +70,7 @@ export function classifyFillFromPosition(
|
|
|
66
70
|
*/
|
|
67
71
|
export function classifyFill(
|
|
68
72
|
side: OrderSide,
|
|
69
|
-
realizedPnl:
|
|
73
|
+
realizedPnl: DecimalString | null | undefined
|
|
70
74
|
): FillClassification {
|
|
71
75
|
const isClose = realizedPnl != null && parseDecimal(realizedPnl) !== 0
|
|
72
76
|
if (side === OrderSide.BUY) {
|
package/src/math/order.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
} from '@lifi/perps-types'
|
|
23
23
|
import type Big from 'big.js'
|
|
24
24
|
import { areFinite, DivBig } from '../decimal/big.js'
|
|
25
|
+
import { numberToDecimalString } from '../decimal/convert.js'
|
|
25
26
|
import { PerpsError } from '../errors/PerpsError.js'
|
|
26
27
|
import {
|
|
27
28
|
isActiveOrderStatus,
|
|
@@ -57,11 +58,6 @@ export function calculateSize(
|
|
|
57
58
|
return new DivBig(marginUsd).times(leverage).div(price).toNumber()
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
/**
|
|
61
|
-
* @deprecated Use `calculateSize`. Removed in the next major.
|
|
62
|
-
*/
|
|
63
|
-
export const calculatePositionSize = calculateSize
|
|
64
|
-
|
|
65
61
|
/**
|
|
66
62
|
* Estimate trading fees.
|
|
67
63
|
*
|
|
@@ -176,11 +172,6 @@ export function calculateTriggerPrice(
|
|
|
176
172
|
return (isLong ? entry.plus(priceDelta) : entry.minus(priceDelta)).toNumber()
|
|
177
173
|
}
|
|
178
174
|
|
|
179
|
-
/**
|
|
180
|
-
* @deprecated Use `calculateTriggerPrice`. Removed in the next major.
|
|
181
|
-
*/
|
|
182
|
-
export const priceFromPercent = calculateTriggerPrice
|
|
183
|
-
|
|
184
175
|
/**
|
|
185
176
|
* Calculate the percentage gain/loss a trigger price realises.
|
|
186
177
|
*
|
|
@@ -208,11 +199,6 @@ export function calculateTriggerPercent(
|
|
|
208
199
|
return priceDiff.div(entryPrice).times(leverage).times(100).toNumber()
|
|
209
200
|
}
|
|
210
201
|
|
|
211
|
-
/**
|
|
212
|
-
* @deprecated Use `calculateTriggerPercent`. Removed in the next major.
|
|
213
|
-
*/
|
|
214
|
-
export const percentFromPrice = calculateTriggerPercent
|
|
215
|
-
|
|
216
202
|
/**
|
|
217
203
|
* Calculate realized PnL as a percentage of position value at close.
|
|
218
204
|
*
|
|
@@ -334,7 +320,8 @@ interface BuildQuoteInput {
|
|
|
334
320
|
* the market's `funding` (`null` for spot, which has none).
|
|
335
321
|
*
|
|
336
322
|
* @throws {PerpsError} `ValidationError` when a book level does not parse to
|
|
337
|
-
* a finite number — see {@link walkOrderbook}
|
|
323
|
+
* a finite number — see {@link walkOrderbook} — or when a quoted figure is
|
|
324
|
+
* not finite (a non-finite `sizeUsd`, mark price or taker fee).
|
|
338
325
|
* @public
|
|
339
326
|
*/
|
|
340
327
|
export function buildQuote(input: BuildQuoteInput): Quote {
|
|
@@ -356,14 +343,14 @@ export function buildQuote(input: BuildQuoteInput): Quote {
|
|
|
356
343
|
marketId: market.id,
|
|
357
344
|
type: input.type,
|
|
358
345
|
side,
|
|
359
|
-
sizeUsd: sizeUsd
|
|
360
|
-
baseSize: walk.baseSize
|
|
346
|
+
sizeUsd: numberToDecimalString(sizeUsd),
|
|
347
|
+
baseSize: numberToDecimalString(walk.baseSize),
|
|
361
348
|
markPrice: price.markPrice,
|
|
362
|
-
expectedFillPrice: walk.vwap
|
|
363
|
-
priceImpactBps: priceImpactBps
|
|
349
|
+
expectedFillPrice: numberToDecimalString(walk.vwap),
|
|
350
|
+
priceImpactBps: numberToDecimalString(priceImpactBps),
|
|
364
351
|
feeTier,
|
|
365
352
|
isDefaultFeeTier: true,
|
|
366
|
-
feeUsd: feeUsd
|
|
353
|
+
feeUsd: numberToDecimalString(feeUsd),
|
|
367
354
|
funding: price.funding ?? null,
|
|
368
355
|
insufficientLiquidity: walk.insufficientLiquidity,
|
|
369
356
|
timestamp: input.timestamp,
|
|
@@ -530,13 +517,3 @@ export function estimateRealizedPnl(
|
|
|
530
517
|
}
|
|
531
518
|
return null
|
|
532
519
|
}
|
|
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
|
package/src/math/position.ts
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Display-tier position formulas
|
|
3
|
-
* exact decimal arithmetic happens
|
|
2
|
+
* Display-tier position formulas and margin-adjustment predicates. Every
|
|
3
|
+
* formula takes and gives `number`; exact decimal arithmetic happens
|
|
4
|
+
* internally with `DivBig`.
|
|
4
5
|
*
|
|
5
6
|
* Sign convention: long = +1, short = -1. Sizes passed to the `estimate*`
|
|
6
7
|
* helpers are always non-negative magnitudes; direction is carried by
|
|
7
8
|
* `isLong`.
|
|
8
9
|
*/
|
|
9
10
|
|
|
11
|
+
import {
|
|
12
|
+
MarginMode,
|
|
13
|
+
type Position,
|
|
14
|
+
PositionMarginAdjustment,
|
|
15
|
+
} from '@lifi/perps-types'
|
|
10
16
|
import { areFinite, DivBig } from '../decimal/big.js'
|
|
11
17
|
|
|
12
|
-
/**
|
|
13
|
-
* Direction sign for a position.
|
|
14
|
-
*
|
|
15
|
-
* @deprecated Inline `isLong ? 1 : -1`. Removed in the next major.
|
|
16
|
-
*/
|
|
17
|
-
export function directionSign(isLong: boolean): 1 | -1 {
|
|
18
|
-
return isLong ? 1 : -1
|
|
19
|
-
}
|
|
20
|
-
|
|
21
18
|
/**
|
|
22
19
|
* Calculate notional value of a position.
|
|
23
20
|
*
|
|
@@ -112,11 +109,6 @@ export function calculateLiquidationDistance(params: {
|
|
|
112
109
|
return Math.abs((liquidationPrice - currentPrice) / currentPrice) * 100
|
|
113
110
|
}
|
|
114
111
|
|
|
115
|
-
/**
|
|
116
|
-
* @deprecated Use `calculateLiquidationDistance`. Removed in the next major.
|
|
117
|
-
*/
|
|
118
|
-
export const liquidationDistancePercent = calculateLiquidationDistance
|
|
119
|
-
|
|
120
112
|
/**
|
|
121
113
|
* Effective leverage of an open position.
|
|
122
114
|
*
|
|
@@ -141,11 +133,6 @@ export function calculateEffectiveLeverage(params: {
|
|
|
141
133
|
return new DivBig(positionValueUsd).div(marginUsd).toNumber()
|
|
142
134
|
}
|
|
143
135
|
|
|
144
|
-
/**
|
|
145
|
-
* @deprecated Use `calculateEffectiveLeverage`. Removed in the next major.
|
|
146
|
-
*/
|
|
147
|
-
export const effectiveLeverage = calculateEffectiveLeverage
|
|
148
|
-
|
|
149
136
|
/**
|
|
150
137
|
* Estimated liquidation price for an isolated-margin position, parameterised
|
|
151
138
|
* by the venue's maintenance margin rate.
|
|
@@ -192,11 +179,6 @@ export function estimateLiquidationPrice(params: {
|
|
|
192
179
|
.toNumber()
|
|
193
180
|
}
|
|
194
181
|
|
|
195
|
-
/**
|
|
196
|
-
* @deprecated Use `estimateLiquidationPrice`. Removed in the next major.
|
|
197
|
-
*/
|
|
198
|
-
export const estimateIsolatedLiquidationPrice = estimateLiquidationPrice
|
|
199
|
-
|
|
200
182
|
/**
|
|
201
183
|
* Whether an isolated position with this liquidation price is already past
|
|
202
184
|
* liquidation at `currentPrice`: a long liquidates when the price falls to its
|
|
@@ -259,11 +241,6 @@ export function estimateAverageEntryPrice(params: {
|
|
|
259
241
|
.toNumber()
|
|
260
242
|
}
|
|
261
243
|
|
|
262
|
-
/**
|
|
263
|
-
* @deprecated Use `estimateAverageEntryPrice`. Removed in the next major.
|
|
264
|
-
*/
|
|
265
|
-
export const predictAverageEntryPrice = estimateAverageEntryPrice
|
|
266
|
-
|
|
267
244
|
/**
|
|
268
245
|
* Estimated effective leverage after adding margin and notional.
|
|
269
246
|
*
|
|
@@ -295,11 +272,6 @@ export function estimateNewLeverage(params: {
|
|
|
295
272
|
.toNumber()
|
|
296
273
|
}
|
|
297
274
|
|
|
298
|
-
/**
|
|
299
|
-
* @deprecated Use `estimateNewLeverage`. Removed in the next major.
|
|
300
|
-
*/
|
|
301
|
-
export const predictNewLeverage = estimateNewLeverage
|
|
302
|
-
|
|
303
275
|
/**
|
|
304
276
|
* Estimated unrealised PnL at the current mark price.
|
|
305
277
|
*
|
|
@@ -325,11 +297,6 @@ export function estimateUnrealizedPnl(params: {
|
|
|
325
297
|
.toNumber()
|
|
326
298
|
}
|
|
327
299
|
|
|
328
|
-
/**
|
|
329
|
-
* @deprecated Use `estimateUnrealizedPnl`. Removed in the next major.
|
|
330
|
-
*/
|
|
331
|
-
export const predictUnrealizedPnl = estimateUnrealizedPnl
|
|
332
|
-
|
|
333
300
|
/**
|
|
334
301
|
* Realised PnL on the portion of a position being closed.
|
|
335
302
|
*
|
|
@@ -356,6 +323,28 @@ export function calculateRealizedPnl(params: {
|
|
|
356
323
|
}
|
|
357
324
|
|
|
358
325
|
/**
|
|
359
|
-
*
|
|
326
|
+
* Whether this position can take a margin adjustment at all: it holds margin of
|
|
327
|
+
* its own and its market exposes individual position margin.
|
|
328
|
+
*
|
|
329
|
+
* @public
|
|
330
|
+
*/
|
|
331
|
+
export function positionSupportsMarginAdjustment(position: Position): boolean {
|
|
332
|
+
return (
|
|
333
|
+
position.marginMode === MarginMode.ISOLATED &&
|
|
334
|
+
position.market.positionMarginAdjustment !== PositionMarginAdjustment.NONE
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Whether a removal is among the adjustments this position permits. An
|
|
340
|
+
* `ADD_ONLY` market takes adds and no withdrawal.
|
|
341
|
+
*
|
|
342
|
+
* @public
|
|
360
343
|
*/
|
|
361
|
-
export
|
|
344
|
+
export function positionSupportsMarginRemoval(position: Position): boolean {
|
|
345
|
+
return (
|
|
346
|
+
positionSupportsMarginAdjustment(position) &&
|
|
347
|
+
position.market.positionMarginAdjustment ===
|
|
348
|
+
PositionMarginAdjustment.ADD_AND_REMOVE
|
|
349
|
+
)
|
|
350
|
+
}
|
|
@@ -30,6 +30,10 @@ export interface GetActivityParams {
|
|
|
30
30
|
*
|
|
31
31
|
* @throws {PerpsError} When the provider plugin is not registered, or on
|
|
32
32
|
* backend / network / parsing errors.
|
|
33
|
+
* @throws {PerpsError} `SetupRequired` when the venue needs a credential (a
|
|
34
|
+
* Lighter auth token or an Ondo session) and none is stored.
|
|
35
|
+
* @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
|
|
36
|
+
* @throws {PerpsError} `AccountNotFound` when the wallet has no venue account.
|
|
33
37
|
* @example
|
|
34
38
|
* ```ts
|
|
35
39
|
* const { items, pagination } = await getActivity(client, {
|
package/src/services/getFills.ts
CHANGED
|
@@ -29,6 +29,10 @@ export interface GetFillsParams {
|
|
|
29
29
|
*
|
|
30
30
|
* @throws {PerpsError} When the provider plugin is not registered, or on
|
|
31
31
|
* backend / network / parsing errors.
|
|
32
|
+
* @throws {PerpsError} `SetupRequired` when the venue needs a credential (a
|
|
33
|
+
* Lighter auth token or an Ondo session) and none is stored.
|
|
34
|
+
* @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
|
|
35
|
+
* @throws {PerpsError} `AccountNotFound` when the wallet has no venue account.
|
|
32
36
|
* @example
|
|
33
37
|
* ```ts
|
|
34
38
|
* const { items, pagination } = await getFills(client, {
|
package/src/services/getOrder.ts
CHANGED
|
@@ -23,6 +23,10 @@ export interface GetOrderParams {
|
|
|
23
23
|
*
|
|
24
24
|
* @throws {PerpsError} When the provider plugin is not registered, or on
|
|
25
25
|
* backend / network / parsing errors.
|
|
26
|
+
* @throws {PerpsError} `SetupRequired` when the venue needs a credential (a
|
|
27
|
+
* Lighter auth token or an Ondo session) and none is stored.
|
|
28
|
+
* @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
|
|
29
|
+
* @throws {PerpsError} `AccountNotFound` when the wallet has no venue account.
|
|
26
30
|
* @example
|
|
27
31
|
* ```ts
|
|
28
32
|
* const order = await getOrder(client, {
|
|
@@ -22,7 +22,16 @@ export interface GetOrdersParams {
|
|
|
22
22
|
cursor?: string
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Read account orders with active lifecycle statuses as the default filter.
|
|
27
|
+
*
|
|
28
|
+
* @throws {PerpsError} When the provider plugin is not registered, or on
|
|
29
|
+
* backend / network / parsing errors.
|
|
30
|
+
* @throws {PerpsError} `SetupRequired` when the venue needs a credential (a
|
|
31
|
+
* Lighter auth token or an Ondo session) and none is stored.
|
|
32
|
+
* @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
|
|
33
|
+
* @throws {PerpsError} `AccountNotFound` when the wallet has no venue account.
|
|
34
|
+
*/
|
|
26
35
|
export async function getOrders(
|
|
27
36
|
client: PerpsSDKClient,
|
|
28
37
|
params: GetOrdersParams,
|
|
@@ -27,6 +27,10 @@ export interface GetPositionsParams {
|
|
|
27
27
|
*
|
|
28
28
|
* @throws {PerpsError} When the provider plugin is not registered, or on
|
|
29
29
|
* backend / network / parsing errors.
|
|
30
|
+
* @throws {PerpsError} `SetupRequired` when the venue needs a credential (a
|
|
31
|
+
* Lighter auth token or an Ondo session) and none is stored.
|
|
32
|
+
* @throws {PerpsError} `Unauthorized` when the venue rejects the credential.
|
|
33
|
+
* @throws {PerpsError} `AccountNotFound` when the wallet has no venue account.
|
|
30
34
|
* @example
|
|
31
35
|
* ```ts
|
|
32
36
|
* const { positions } = await getPositions(client, {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
FeeTier,
|
|
3
|
-
MarketContext,
|
|
4
|
-
OrderbookResponse,
|
|
1
|
+
import {
|
|
2
|
+
type FeeTier,
|
|
3
|
+
type MarketContext,
|
|
4
|
+
type OrderbookResponse,
|
|
5
|
+
PerpsErrorCode,
|
|
5
6
|
} from '@lifi/perps-types'
|
|
7
|
+
import { PerpsError } from '../errors/PerpsError.js'
|
|
6
8
|
import { buildQuote } from '../math/order.js'
|
|
7
9
|
import type {
|
|
8
10
|
PerpsSDKClient,
|
|
@@ -10,6 +12,7 @@ import type {
|
|
|
10
12
|
QuoteListener,
|
|
11
13
|
} from '../types/provider.js'
|
|
12
14
|
import type { WsProvider } from '../websocket/types.js'
|
|
15
|
+
import { wsLog } from '../websocket/wsLog.js'
|
|
13
16
|
import { resolveQuoteMarket, resolveQuotePrice } from './resolveQuote.js'
|
|
14
17
|
|
|
15
18
|
/**
|
|
@@ -37,6 +40,11 @@ export const QUOTE_THROTTLE_MS = 100
|
|
|
37
40
|
* listeners once and cancels any pending trailing emission, so the wire
|
|
38
41
|
* subscriptions' ref counts cannot be double-decremented.
|
|
39
42
|
*
|
|
43
|
+
* A throw while building or delivering a streamed quote (an unparsable mark
|
|
44
|
+
* price, a throwing `onQuote`) is logged via `wsLog.listenerFailure` and the
|
|
45
|
+
* emission is skipped, on both the immediate and the trailing-timer path.
|
|
46
|
+
*
|
|
47
|
+
* @throws {PerpsError} `ValidationError` when `params.size` is not finite.
|
|
40
48
|
* @throws {PerpsError} `MarketNotFound` when no market matches symbol+type.
|
|
41
49
|
* @internal
|
|
42
50
|
*/
|
|
@@ -48,6 +56,12 @@ export async function resolveSubscribeQuote(
|
|
|
48
56
|
feeTier: FeeTier,
|
|
49
57
|
onQuote: QuoteListener
|
|
50
58
|
): Promise<() => void> {
|
|
59
|
+
if (!Number.isFinite(params.size)) {
|
|
60
|
+
throw new PerpsError(
|
|
61
|
+
PerpsErrorCode.ValidationError,
|
|
62
|
+
`Quote size must be a finite number, got ${params.size}`
|
|
63
|
+
)
|
|
64
|
+
}
|
|
51
65
|
const market = await resolveQuoteMarket(client, provider, params)
|
|
52
66
|
// REST snapshot seeds the context; the marketContext subscription below
|
|
53
67
|
// keeps it live so streamed impact/funding track the moving market.
|
|
@@ -66,21 +80,25 @@ export async function resolveSubscribeQuote(
|
|
|
66
80
|
return
|
|
67
81
|
}
|
|
68
82
|
lastEmitAt = Date.now()
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
try {
|
|
84
|
+
onQuote(
|
|
85
|
+
buildQuote({
|
|
86
|
+
provider,
|
|
87
|
+
symbol: params.symbol,
|
|
88
|
+
type: params.type,
|
|
89
|
+
side: params.side,
|
|
90
|
+
sizeUsd: params.size,
|
|
91
|
+
market,
|
|
92
|
+
price: latestContext,
|
|
93
|
+
bids: latestBook.bids,
|
|
94
|
+
asks: latestBook.asks,
|
|
95
|
+
feeTier,
|
|
96
|
+
timestamp: Date.now(),
|
|
97
|
+
})
|
|
98
|
+
)
|
|
99
|
+
} catch (error) {
|
|
100
|
+
wsLog.listenerFailure(provider, 'quote', error)
|
|
101
|
+
}
|
|
84
102
|
}
|
|
85
103
|
|
|
86
104
|
const unsubscribeContext = await ws.subscribe(
|
package/src/types/api.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ActionParamsMap,
|
|
7
7
|
PlaceTwapOrderParams as ActionPlaceTwapOrderParams,
|
|
8
8
|
ActionStep,
|
|
9
|
+
DecimalString,
|
|
9
10
|
ExecuteActionResponse,
|
|
10
11
|
MarginMode,
|
|
11
12
|
MarketRef,
|
|
@@ -137,9 +138,9 @@ export interface PlaceOrderParams {
|
|
|
137
138
|
side: OrderSide
|
|
138
139
|
type: Exclude<OrderType, OrderType.TWAP>
|
|
139
140
|
/** Base-asset size as a decimal wire string. */
|
|
140
|
-
size:
|
|
141
|
+
size: DecimalString
|
|
141
142
|
/** Limit price as a decimal wire string; required by limit-style orders. */
|
|
142
|
-
price:
|
|
143
|
+
price: DecimalString
|
|
143
144
|
/** Optional leverage multiplier; provider defaults apply when omitted. */
|
|
144
145
|
leverage?: number
|
|
145
146
|
/**
|
|
@@ -223,7 +224,7 @@ export interface SendAssetActionParams {
|
|
|
223
224
|
/** Destination DEX/account identifier understood by the provider. */
|
|
224
225
|
destinationDex: string
|
|
225
226
|
/** Transfer amount as a provider-compatible decimal wire string. */
|
|
226
|
-
amount:
|
|
227
|
+
amount: DecimalString
|
|
227
228
|
}
|
|
228
229
|
|
|
229
230
|
/**
|
package/src/types/provider.ts
CHANGED
|
@@ -597,7 +597,7 @@ export interface PerpsProviderPlugin {
|
|
|
597
597
|
* @throws {PerpsError} `ValidationError` when a `Position` decimal that the
|
|
598
598
|
* venue formula reads is malformed.
|
|
599
599
|
*/
|
|
600
|
-
positionRemovableMargin(position: Position):
|
|
600
|
+
positionRemovableMargin(position: Position): DecimalString | undefined
|
|
601
601
|
|
|
602
602
|
/**
|
|
603
603
|
* Project a typed {@link AccountConfig} against the provider's `setup`
|
package/src/version.ts
CHANGED