@lifi/perps-sdk 5.0.0 → 6.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/dist/cjs/client/PerpsClient.d.ts +7 -1
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +10 -0
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +6 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/registry/marketRegistry.d.ts +2 -1
- package/dist/cjs/registry/marketRegistry.d.ts.map +1 -1
- package/dist/cjs/registry/marketRegistry.js +24 -1
- package/dist/cjs/registry/marketRegistry.js.map +1 -1
- package/dist/cjs/types/api.d.ts +2 -1
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +7 -1
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/utils/calculations.d.ts +0 -5
- package/dist/cjs/utils/calculations.d.ts.map +1 -1
- package/dist/cjs/utils/calculations.js +0 -9
- package/dist/cjs/utils/calculations.js.map +1 -1
- package/dist/cjs/utils/transferMargin.d.ts +7 -0
- package/dist/cjs/utils/transferMargin.d.ts.map +1 -0
- package/dist/cjs/utils/transferMargin.js +52 -0
- package/dist/cjs/utils/transferMargin.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client/PerpsClient.d.ts +22 -1
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +25 -0
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/registry/marketRegistry.d.ts +9 -1
- package/dist/esm/registry/marketRegistry.d.ts.map +1 -1
- package/dist/esm/registry/marketRegistry.js +30 -1
- package/dist/esm/registry/marketRegistry.js.map +1 -1
- package/dist/esm/types/api.d.ts +7 -1
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +29 -1
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/utils/calculations.d.ts +0 -20
- package/dist/esm/utils/calculations.d.ts.map +1 -1
- package/dist/esm/utils/calculations.js +0 -23
- package/dist/esm/utils/calculations.js.map +1 -1
- package/dist/esm/utils/transferMargin.d.ts +24 -0
- package/dist/esm/utils/transferMargin.d.ts.map +1 -0
- package/dist/esm/utils/transferMargin.js +58 -0
- package/dist/esm/utils/transferMargin.js.map +1 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/client/PerpsClient.d.ts +22 -1
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/registry/marketRegistry.d.ts +9 -1
- package/dist/types/registry/marketRegistry.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +7 -1
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +29 -1
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/utils/calculations.d.ts +0 -20
- package/dist/types/utils/calculations.d.ts.map +1 -1
- package/dist/types/utils/transferMargin.d.ts +24 -0
- package/dist/types/utils/transferMargin.d.ts.map +1 -0
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +37 -0
- package/src/index.ts +4 -1
- package/src/registry/marketRegistry.ts +45 -2
- package/src/types/api.ts +7 -0
- package/src/types/provider.ts +39 -0
- package/src/utils/calculations.ts +0 -28
- package/src/utils/transferMargin.ts +103 -0
- package/src/version.ts +1 -1
package/src/types/provider.ts
CHANGED
|
@@ -10,11 +10,14 @@ import type {
|
|
|
10
10
|
ActivityType,
|
|
11
11
|
FillsResponse,
|
|
12
12
|
Market,
|
|
13
|
+
MarketRef,
|
|
14
|
+
MarketSettings,
|
|
13
15
|
Order,
|
|
14
16
|
OrdersResponse,
|
|
15
17
|
PerpsMarket,
|
|
16
18
|
PerpsSigner,
|
|
17
19
|
Position,
|
|
20
|
+
PositionMarginConstraints,
|
|
18
21
|
PositionsResponse,
|
|
19
22
|
ProviderAction,
|
|
20
23
|
Quote,
|
|
@@ -185,6 +188,18 @@ export interface ProviderGetPositionsParams {
|
|
|
185
188
|
cursor?: string
|
|
186
189
|
}
|
|
187
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Read params for {@link PerpsProviderPlugin.getMarketSettings}.
|
|
193
|
+
*
|
|
194
|
+
* @public
|
|
195
|
+
*/
|
|
196
|
+
export interface ProviderGetMarketSettingsParams {
|
|
197
|
+
address: Address
|
|
198
|
+
/** The market's `id` and `categoryId`; the category identifies spot
|
|
199
|
+
* markets, which carry no venue leverage state. */
|
|
200
|
+
market: MarketRef
|
|
201
|
+
}
|
|
202
|
+
|
|
188
203
|
/**
|
|
189
204
|
* Read params for {@link PerpsProvider.getOrders}.
|
|
190
205
|
*
|
|
@@ -350,6 +365,18 @@ export interface PerpsProviderPlugin {
|
|
|
350
365
|
options?: SDKRequestOptions
|
|
351
366
|
): Promise<PositionsResponse>
|
|
352
367
|
|
|
368
|
+
/**
|
|
369
|
+
* The user's current venue-side settings for one market — the margin mode
|
|
370
|
+
* and leverage the next order on it will use. Optional because venues
|
|
371
|
+
* expose this unevenly: Hyperliquid reads it directly (`activeAssetData`),
|
|
372
|
+
* Lighter only reports it on an account's position row. `undefined` means
|
|
373
|
+
* the venue has nothing to read for this market.
|
|
374
|
+
*/
|
|
375
|
+
getMarketSettings?(
|
|
376
|
+
params: ProviderGetMarketSettingsParams,
|
|
377
|
+
options?: SDKRequestOptions
|
|
378
|
+
): Promise<MarketSettings | undefined>
|
|
379
|
+
|
|
353
380
|
getOrders(
|
|
354
381
|
params: ProviderGetOrdersParams,
|
|
355
382
|
options?: SDKRequestOptions
|
|
@@ -436,6 +463,18 @@ export interface PerpsProviderPlugin {
|
|
|
436
463
|
params: LiquidationEstimateParams
|
|
437
464
|
): number | undefined
|
|
438
465
|
|
|
466
|
+
/**
|
|
467
|
+
* Exact venue-owned constraints for changing `position`'s dedicated margin.
|
|
468
|
+
* Pure — providers normalize raw venue quantities onto the position before
|
|
469
|
+
* returning these inputs.
|
|
470
|
+
*
|
|
471
|
+
* @returns `undefined` when this position has no individual margin
|
|
472
|
+
* adjustment (for example a cross position or a cross-only venue).
|
|
473
|
+
*/
|
|
474
|
+
positionMarginConstraints(
|
|
475
|
+
position: Position
|
|
476
|
+
): PositionMarginConstraints | undefined
|
|
477
|
+
|
|
439
478
|
/**
|
|
440
479
|
* Project a typed {@link AccountConfig} against the provider's `setup`
|
|
441
480
|
* + `options` descriptors into `AccountConfigSetting[]`. Used by
|
|
@@ -169,34 +169,6 @@ export function effectiveLeverage(params: {
|
|
|
169
169
|
return positionValueUsd / marginUsd
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
/**
|
|
173
|
-
* Margin that can be removed from a position while keeping its leverage at or
|
|
174
|
-
* below the asset's maximum.
|
|
175
|
-
*
|
|
176
|
-
* The minimum margin to stay within `maxLeverage` is the required margin for
|
|
177
|
-
* the position's notional (see {@link calculateRequiredMargin}); anything above
|
|
178
|
-
* that is removable.
|
|
179
|
-
*
|
|
180
|
-
* @param marginUsed - Margin currently backing the position in USD
|
|
181
|
-
* @param positionValueUsd - Position notional value in USD
|
|
182
|
-
* @param maxLeverage - Maximum leverage allowed for the asset
|
|
183
|
-
* @returns Removable margin in USD (never negative); falls back to the full
|
|
184
|
-
* `marginUsed` when `maxLeverage` is zero (no constraint available)
|
|
185
|
-
* @public
|
|
186
|
-
*/
|
|
187
|
-
export function removableMargin(params: {
|
|
188
|
-
marginUsed: number
|
|
189
|
-
positionValueUsd: number
|
|
190
|
-
maxLeverage: number
|
|
191
|
-
}): number {
|
|
192
|
-
const { marginUsed, positionValueUsd, maxLeverage } = params
|
|
193
|
-
if (maxLeverage === 0) {
|
|
194
|
-
return marginUsed
|
|
195
|
-
}
|
|
196
|
-
const minMargin = calculateRequiredMargin(positionValueUsd, maxLeverage)
|
|
197
|
-
return Math.max(0, marginUsed - minMargin)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
172
|
/**
|
|
201
173
|
* Signed expected PnL for a trigger price — see {@link calculateExpectedPnl}.
|
|
202
174
|
*
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MarginMode,
|
|
3
|
+
PerpsErrorCode,
|
|
4
|
+
type Position,
|
|
5
|
+
PositionMarginAdjustment,
|
|
6
|
+
type PositionMarginConstraints,
|
|
7
|
+
} from '@lifi/perps-types'
|
|
8
|
+
import Big from 'big.js'
|
|
9
|
+
import { PerpsError } from '../errors/PerpsError.js'
|
|
10
|
+
|
|
11
|
+
function positionAmount(value: string, field: string): Big {
|
|
12
|
+
try {
|
|
13
|
+
return new Big(value)
|
|
14
|
+
} catch {
|
|
15
|
+
throw new PerpsError(
|
|
16
|
+
PerpsErrorCode.ValidationError,
|
|
17
|
+
`Invalid decimal string on Position.${field}: '${value}'`
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function constraintAmount(value: string, field: string): Big {
|
|
23
|
+
try {
|
|
24
|
+
return new Big(value)
|
|
25
|
+
} catch {
|
|
26
|
+
throw new PerpsError(
|
|
27
|
+
PerpsErrorCode.ValidationError,
|
|
28
|
+
`Invalid decimal string on PositionMarginConstraints.${field}: '${value}'`
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function requirePositive(value: Big, field: string): Big {
|
|
34
|
+
if (value.lte(0)) {
|
|
35
|
+
throw new PerpsError(
|
|
36
|
+
PerpsErrorCode.ValidationError,
|
|
37
|
+
`${field} must be greater than zero.`
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
return value
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Exact inputs for {@link removableIsolatedMargin}.
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export interface RemovableIsolatedMarginParams {
|
|
49
|
+
position: Position
|
|
50
|
+
constraints: PositionMarginConstraints
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Calculate margin that can be removed from an isolated position using the
|
|
55
|
+
* provider's exact retained-margin requirement. Position equity is allocated
|
|
56
|
+
* margin plus unrealized PnL.
|
|
57
|
+
*
|
|
58
|
+
* The result snaps down to the provider's accepted amount increment. Markets
|
|
59
|
+
* that are add-only and cross-margined positions return `'0'`.
|
|
60
|
+
*
|
|
61
|
+
* @throws {PerpsError} `ValidationError` for malformed, non-positive, or
|
|
62
|
+
* inconsistent risk inputs.
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export function removableIsolatedMargin({
|
|
66
|
+
position,
|
|
67
|
+
constraints,
|
|
68
|
+
}: RemovableIsolatedMarginParams): string {
|
|
69
|
+
if (
|
|
70
|
+
position.marginMode !== MarginMode.ISOLATED ||
|
|
71
|
+
position.market.positionMarginAdjustment !==
|
|
72
|
+
PositionMarginAdjustment.ADD_AND_REMOVE
|
|
73
|
+
) {
|
|
74
|
+
return '0'
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const marginUsed = requirePositive(
|
|
78
|
+
positionAmount(position.marginUsed, 'marginUsed'),
|
|
79
|
+
'Position.marginUsed'
|
|
80
|
+
)
|
|
81
|
+
const unrealizedPnl = positionAmount(position.unrealizedPnl, 'unrealizedPnl')
|
|
82
|
+
const minimumMargin = requirePositive(
|
|
83
|
+
constraintAmount(
|
|
84
|
+
constraints.minimumMarginRequirement,
|
|
85
|
+
'minimumMarginRequirement'
|
|
86
|
+
),
|
|
87
|
+
'PositionMarginConstraints.minimumMarginRequirement'
|
|
88
|
+
)
|
|
89
|
+
const amountIncrement = requirePositive(
|
|
90
|
+
constraintAmount(constraints.amountIncrement, 'amountIncrement'),
|
|
91
|
+
'PositionMarginConstraints.amountIncrement'
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
const removable = marginUsed.plus(unrealizedPnl).minus(minimumMargin)
|
|
95
|
+
if (removable.lte(0)) {
|
|
96
|
+
return '0'
|
|
97
|
+
}
|
|
98
|
+
return removable
|
|
99
|
+
.div(amountIncrement)
|
|
100
|
+
.round(0, Big.roundDown)
|
|
101
|
+
.times(amountIncrement)
|
|
102
|
+
.toFixed()
|
|
103
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const name = '@lifi/perps-sdk'
|
|
2
|
-
export const version = '
|
|
2
|
+
export const version = '6.0.0'
|