@lifi/perps-sdk 25.0.2 → 26.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 +54 -1
- package/dist/cjs/client/PerpsClient.d.ts +5 -5
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +75 -74
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/errors/PerpsExecutionError.d.ts +9 -0
- package/dist/cjs/errors/PerpsExecutionError.d.ts.map +1 -0
- package/dist/cjs/errors/PerpsExecutionError.js +29 -0
- package/dist/cjs/errors/PerpsExecutionError.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -4
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +14 -12
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/fills.d.ts +0 -1
- package/dist/cjs/math/fills.d.ts.map +1 -1
- package/dist/cjs/math/fills.js +0 -18
- package/dist/cjs/math/fills.js.map +1 -1
- package/dist/cjs/math/order.d.ts +5 -5
- package/dist/cjs/math/order.d.ts.map +1 -1
- package/dist/cjs/math/order.js +10 -10
- package/dist/cjs/math/order.js.map +1 -1
- package/dist/cjs/math/position.d.ts +2 -2
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +5 -5
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/types/api.d.ts +1 -10
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +5 -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/esm/client/PerpsClient.d.ts +63 -47
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +136 -128
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/errors/PerpsExecutionError.d.ts +24 -0
- package/dist/esm/errors/PerpsExecutionError.d.ts.map +1 -0
- package/dist/esm/errors/PerpsExecutionError.js +39 -0
- package/dist/esm/errors/PerpsExecutionError.js.map +1 -0
- package/dist/esm/index.d.ts +5 -4
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/fills.d.ts +0 -9
- package/dist/esm/math/fills.d.ts.map +1 -1
- package/dist/esm/math/fills.js +0 -25
- package/dist/esm/math/fills.js.map +1 -1
- package/dist/esm/math/order.d.ts +6 -6
- package/dist/esm/math/order.d.ts.map +1 -1
- package/dist/esm/math/order.js +6 -6
- package/dist/esm/math/order.js.map +1 -1
- package/dist/esm/math/position.d.ts +2 -2
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +3 -3
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/types/api.d.ts +1 -18
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +21 -4
- 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/types/client/PerpsClient.d.ts +63 -47
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/errors/PerpsExecutionError.d.ts +24 -0
- package/dist/types/errors/PerpsExecutionError.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/fills.d.ts +0 -9
- package/dist/types/math/fills.d.ts.map +1 -1
- package/dist/types/math/order.d.ts +6 -6
- package/dist/types/math/order.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +2 -2
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +1 -18
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +21 -4
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +167 -164
- package/src/errors/PerpsExecutionError.ts +54 -0
- package/src/index.ts +12 -8
- package/src/math/fills.ts +0 -29
- package/src/math/order.ts +11 -11
- package/src/math/position.ts +3 -3
- package/src/types/api.ts +0 -21
- package/src/types/provider.ts +22 -4
- package/src/version.ts +1 -1
package/src/math/fills.ts
CHANGED
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
OrderSide,
|
|
5
5
|
} from '@lifi/perps-types'
|
|
6
6
|
import Big from 'big.js'
|
|
7
|
-
import { isDecimalStringZero } from '../decimal/compare.js'
|
|
8
7
|
import { isDecimalString } from '../decimal/parse.js'
|
|
9
8
|
|
|
10
9
|
/**
|
|
@@ -69,31 +68,3 @@ export function classifyFillFromPosition(
|
|
|
69
68
|
}
|
|
70
69
|
return FillClassification.REDUCED_SHORT
|
|
71
70
|
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Classify a fill as open or close based on realizedPnl.
|
|
75
|
-
* @deprecated Use `Fill.classification` instead — it uses startPosition
|
|
76
|
-
* for accurate open/increase/reduce/close/reverse classification.
|
|
77
|
-
* @returns `BUY` or `SELL` when `realizedPnl` is a string that does not match the
|
|
78
|
-
* decimal pattern.
|
|
79
|
-
* @public
|
|
80
|
-
*/
|
|
81
|
-
export function classifyFill(
|
|
82
|
-
side: OrderSide,
|
|
83
|
-
realizedPnl: DecimalString | null | undefined
|
|
84
|
-
): FillClassification {
|
|
85
|
-
if (realizedPnl != null && !isDecimalString(realizedPnl)) {
|
|
86
|
-
return side === OrderSide.BUY
|
|
87
|
-
? FillClassification.BUY
|
|
88
|
-
: FillClassification.SELL
|
|
89
|
-
}
|
|
90
|
-
const isClose = realizedPnl != null && !isDecimalStringZero(realizedPnl)
|
|
91
|
-
if (side === OrderSide.BUY) {
|
|
92
|
-
return isClose
|
|
93
|
-
? FillClassification.CLOSED_SHORT
|
|
94
|
-
: FillClassification.OPENED_LONG
|
|
95
|
-
}
|
|
96
|
-
return isClose
|
|
97
|
-
? FillClassification.CLOSED_LONG
|
|
98
|
-
: FillClassification.OPENED_SHORT
|
|
99
|
-
}
|
package/src/math/order.ts
CHANGED
|
@@ -96,7 +96,7 @@ export const safeEstimateFees = createSafeFunction('estimateFees', estimateFees)
|
|
|
96
96
|
* decimal pattern, or `slippagePercent` is -100 or less.
|
|
97
97
|
* @public
|
|
98
98
|
*/
|
|
99
|
-
export function
|
|
99
|
+
export function calculateSlippagePrice(
|
|
100
100
|
price: string,
|
|
101
101
|
slippagePercent: string,
|
|
102
102
|
isBuy: boolean
|
|
@@ -116,9 +116,9 @@ export function applySlippageToPrice(
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/** @public */
|
|
119
|
-
export const
|
|
120
|
-
'
|
|
121
|
-
|
|
119
|
+
export const safeCalculateSlippagePrice = createSafeFunction(
|
|
120
|
+
'calculateSlippagePrice',
|
|
121
|
+
calculateSlippagePrice
|
|
122
122
|
)
|
|
123
123
|
|
|
124
124
|
/**
|
|
@@ -277,7 +277,7 @@ export const safeCalculateRealizedPnlPercent = createSafeFunction(
|
|
|
277
277
|
calculateRealizedPnlPercent
|
|
278
278
|
)
|
|
279
279
|
|
|
280
|
-
/** Result of {@link
|
|
280
|
+
/** Result of {@link calculateOrderbookWalk} — the fill obtained for a USD-notional walk. */
|
|
281
281
|
interface BookWalk {
|
|
282
282
|
/** Base amount filled. */
|
|
283
283
|
baseSize: string
|
|
@@ -301,7 +301,7 @@ interface BookWalk {
|
|
|
301
301
|
* or `size` does not match the decimal pattern.
|
|
302
302
|
* @public
|
|
303
303
|
*/
|
|
304
|
-
export function
|
|
304
|
+
export function calculateOrderbookWalk(
|
|
305
305
|
levels: OrderbookLevel[],
|
|
306
306
|
sizeUsd: string
|
|
307
307
|
): BookWalk {
|
|
@@ -333,9 +333,9 @@ export function walkOrderbook(
|
|
|
333
333
|
}
|
|
334
334
|
|
|
335
335
|
/** @public */
|
|
336
|
-
export const
|
|
337
|
-
'
|
|
338
|
-
|
|
336
|
+
export const safeCalculateOrderbookWalk = createSafeFunction(
|
|
337
|
+
'calculateOrderbookWalk',
|
|
338
|
+
calculateOrderbookWalk
|
|
339
339
|
)
|
|
340
340
|
|
|
341
341
|
/** Inputs to {@link buildQuote} — the resolved market, its live price, its book, and the trade ask. */
|
|
@@ -369,7 +369,7 @@ export function buildQuote(input: BuildQuoteInput): Quote {
|
|
|
369
369
|
const { market, price, side, sizeUsd, feeTier } = input
|
|
370
370
|
const markPrice = decimalStringToDivBig(price.markPrice)
|
|
371
371
|
const levels = side === 'buy' ? input.asks : input.bids
|
|
372
|
-
const walk =
|
|
372
|
+
const walk = calculateOrderbookWalk(levels, sizeUsd)
|
|
373
373
|
const vwap = decimalStringToBig(walk.vwap)
|
|
374
374
|
const priceImpactBps =
|
|
375
375
|
markPrice.eq(0) || vwap.eq(0)
|
|
@@ -407,7 +407,7 @@ export const safeBuildQuote = createSafeFunction('buildQuote', buildQuote)
|
|
|
407
407
|
* @param positions - Open positions list (any market).
|
|
408
408
|
* @public
|
|
409
409
|
*/
|
|
410
|
-
export function
|
|
410
|
+
export function resolveMatchingPosition(
|
|
411
411
|
marketId: string,
|
|
412
412
|
positions: readonly Position[]
|
|
413
413
|
): Position | undefined {
|
package/src/math/position.ts
CHANGED
|
@@ -417,7 +417,7 @@ export const safeCalculateRealizedPnl = createSafeFunction(
|
|
|
417
417
|
*
|
|
418
418
|
* @public
|
|
419
419
|
*/
|
|
420
|
-
export function
|
|
420
|
+
export function isPositionMarginAdjustable(position: Position): boolean {
|
|
421
421
|
return (
|
|
422
422
|
position.marginMode === MarginMode.ISOLATED &&
|
|
423
423
|
position.market.positionMarginAdjustment !== PositionMarginAdjustment.NONE
|
|
@@ -430,9 +430,9 @@ export function positionSupportsMarginAdjustment(position: Position): boolean {
|
|
|
430
430
|
*
|
|
431
431
|
* @public
|
|
432
432
|
*/
|
|
433
|
-
export function
|
|
433
|
+
export function isPositionMarginRemovable(position: Position): boolean {
|
|
434
434
|
return (
|
|
435
|
-
|
|
435
|
+
isPositionMarginAdjustable(position) &&
|
|
436
436
|
position.market.positionMarginAdjustment ===
|
|
437
437
|
PositionMarginAdjustment.ADD_AND_REMOVE
|
|
438
438
|
)
|
package/src/types/api.ts
CHANGED
|
@@ -7,7 +7,6 @@ import type {
|
|
|
7
7
|
PlaceTwapOrderParams as ActionPlaceTwapOrderParams,
|
|
8
8
|
ActionStep,
|
|
9
9
|
DecimalString,
|
|
10
|
-
ExecuteActionResponse,
|
|
11
10
|
MarginMode,
|
|
12
11
|
MarketRef,
|
|
13
12
|
MetaActionType,
|
|
@@ -18,7 +17,6 @@ import type {
|
|
|
18
17
|
PortfolioHistoryRange,
|
|
19
18
|
SetupAction,
|
|
20
19
|
SetupOption,
|
|
21
|
-
SignedActionStep,
|
|
22
20
|
TimeInForce,
|
|
23
21
|
TriggerOrderInput,
|
|
24
22
|
WithdrawalParams,
|
|
@@ -380,25 +378,6 @@ export interface ProviderSetup {
|
|
|
380
378
|
checklist: SetupChecklistItem[]
|
|
381
379
|
}
|
|
382
380
|
|
|
383
|
-
/**
|
|
384
|
-
* Parameters for the internal `PerpsClient.executeProviderSetup` batch submit.
|
|
385
|
-
*/
|
|
386
|
-
export interface ExecuteProviderSetupParams {
|
|
387
|
-
provider: string
|
|
388
|
-
address: Address
|
|
389
|
-
/** The unsatisfied setup steps from checkSetup() */
|
|
390
|
-
setup: ActionStep[]
|
|
391
|
-
/** Signed counterparts of `setup`, in the same order */
|
|
392
|
-
signedActions: SignedActionStep[]
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Result from the internal `PerpsClient.executeProviderSetup` batch submit.
|
|
397
|
-
*/
|
|
398
|
-
export interface ExecuteProviderSetupResult {
|
|
399
|
-
results: ExecuteActionResponse
|
|
400
|
-
}
|
|
401
|
-
|
|
402
381
|
/**
|
|
403
382
|
* Result envelope returned by {@link PerpsClient.getAccount} — wraps the
|
|
404
383
|
* backend's `AccountResponse` with a single SDK-projected `settings` array.
|
package/src/types/provider.ts
CHANGED
|
@@ -79,6 +79,23 @@ export interface SignActionProgress {
|
|
|
79
79
|
txHash: string
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Output of {@link PerpsProviderPlugin.signActions}, in batch order.
|
|
84
|
+
*
|
|
85
|
+
* A plugin that executes a step itself while signing (a venue REST mutation,
|
|
86
|
+
* a reverted on-chain leg) puts that step's real result in `executed` and
|
|
87
|
+
* leaves it out of `signed`. The plugin stops at the first `executed` result
|
|
88
|
+
* that is not `Submitted` or `Accepted`. Core submits `signed` to
|
|
89
|
+
* `/executeAction` only when every `executed` result is `Submitted` or
|
|
90
|
+
* `Accepted`, and reports `executed` before the `/executeAction` results.
|
|
91
|
+
*
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
94
|
+
export interface SignActionsResult {
|
|
95
|
+
signed: SignedActionStep[]
|
|
96
|
+
executed: ActionResult[]
|
|
97
|
+
}
|
|
98
|
+
|
|
82
99
|
/**
|
|
83
100
|
* Per-call context passed by `PerpsClient` to a provider's
|
|
84
101
|
* {@link PerpsProviderPlugin.signActions} implementation. Carries the
|
|
@@ -699,12 +716,13 @@ export interface PerpsProviderPlugin {
|
|
|
699
716
|
steps: ActionStep[],
|
|
700
717
|
address: Address,
|
|
701
718
|
ctx?: SignActionsContext
|
|
702
|
-
): Promise<
|
|
719
|
+
): Promise<SignActionsResult>
|
|
703
720
|
|
|
704
721
|
/**
|
|
705
|
-
* Observe the
|
|
706
|
-
*
|
|
707
|
-
*
|
|
722
|
+
* Observe the execution report of each `execute` call: the results of the
|
|
723
|
+
* steps the plugin executed while signing, then the `/executeAction`
|
|
724
|
+
* results. Lets a provider react to structured statuses — e.g. evicting a
|
|
725
|
+
* locally stored credential the venue refused.
|
|
708
726
|
*/
|
|
709
727
|
onExecuteResults?(address: Address, results: ActionResult[]): Promise<void>
|
|
710
728
|
|
package/src/version.ts
CHANGED