@lifi/perps-sdk 28.1.0 → 29.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 +13 -0
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/math/position.d.ts +12 -0
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +43 -5
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/types/deposit.d.ts +3 -3
- package/dist/cjs/types/deposit.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +4 -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/index.d.ts +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/math/position.d.ts +36 -9
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +68 -15
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/services/getGasRecommendation.d.ts +2 -2
- package/dist/esm/services/getGasRecommendation.js +1 -1
- package/dist/esm/types/deposit.d.ts +9 -8
- package/dist/esm/types/deposit.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +20 -7
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/utils/depositAssets.d.ts +3 -3
- package/dist/esm/utils/depositAssets.js +3 -3
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +36 -9
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/services/getGasRecommendation.d.ts +2 -2
- package/dist/types/types/deposit.d.ts +9 -8
- package/dist/types/types/deposit.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +20 -7
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/utils/depositAssets.d.ts +3 -3
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/math/position.ts +74 -14
- package/src/services/getGasRecommendation.ts +2 -2
- package/src/types/deposit.ts +9 -8
- package/src/types/provider.ts +20 -6
- package/src/utils/depositAssets.ts +3 -3
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/perps-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "29.0.0",
|
|
4
4
|
"description": "LI.FI Perps SDK for perpetuals trading",
|
|
5
5
|
"homepage": "https://github.com/lifinance/perps-sdk",
|
|
6
6
|
"bugs": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"big.js": "^7.0.1",
|
|
47
47
|
"partysocket": "^1.1.19",
|
|
48
48
|
"viem": "^2.55.18",
|
|
49
|
-
"@lifi/perps-types": "^
|
|
49
|
+
"@lifi/perps-types": "^26.0.0"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -263,9 +263,9 @@ export type {
|
|
|
263
263
|
export type {
|
|
264
264
|
DeclaredDepositAsset,
|
|
265
265
|
DepositFlow,
|
|
266
|
-
DepositFlowFirstDepositPipeline,
|
|
267
266
|
DepositFlowLifiSwap,
|
|
268
267
|
DepositFlowSetupRequired,
|
|
268
|
+
DepositFlowVenueDepositPipeline,
|
|
269
269
|
} from './types/deposit.js'
|
|
270
270
|
export type {
|
|
271
271
|
ActionSignerContribution,
|
package/src/math/position.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
type PerpsMarket,
|
|
16
16
|
type Position,
|
|
17
17
|
PositionMarginAdjustment,
|
|
18
|
+
PositionSide,
|
|
18
19
|
} from '@lifi/perps-types'
|
|
19
20
|
import { DivBig } from '../decimal/big.js'
|
|
20
21
|
import {
|
|
@@ -174,16 +175,27 @@ export const safeCalculateEffectiveLeverage = createSafeFunction(
|
|
|
174
175
|
* margin_available = entryPrice × (1 / leverage - mmr)
|
|
175
176
|
* liq_price = entryPrice - side × margin_available / (1 - mmr × side)
|
|
176
177
|
*
|
|
178
|
+
* With `current`, the order of `size` adds to that position on the same side.
|
|
179
|
+
* The equity of the blended position at `entryPrice` is `current.margin`, the
|
|
180
|
+
* PnL of `current` at `entryPrice`, and the margin of the order. The margin
|
|
181
|
+
* available is that equity minus the maintenance margin of the blended size,
|
|
182
|
+
* and the formula above divides it by the blended size.
|
|
183
|
+
*
|
|
177
184
|
* For existing positions, prefer `Position.liquidationPrice` from the venue.
|
|
178
185
|
*
|
|
179
186
|
* @param maintenanceMarginRate - Venue maintenance margin rate as a fraction
|
|
180
187
|
* (`'0.01'` = 1%).
|
|
181
|
-
* @
|
|
182
|
-
* `
|
|
183
|
-
*
|
|
188
|
+
* @param size - Order size as a non-negative magnitude. Read only with
|
|
189
|
+
* `current`.
|
|
190
|
+
* @param current - Open isolated position on the same side that the order
|
|
191
|
+
* adds to. `margin` is its collateral net of unrealized PnL.
|
|
192
|
+
* @returns The estimate, or `undefined` when the margin above the
|
|
193
|
+
* maintenance requirement is not positive: the position opens at or past
|
|
194
|
+
* liquidation, so the model has no price on the correct side of entry.
|
|
184
195
|
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
185
|
-
* decimal pattern, `leverage` is zero,
|
|
186
|
-
* zero denominator (`1` for a long, `-1` for a short)
|
|
196
|
+
* decimal pattern, `leverage` is zero, `maintenanceMarginRate` gives a
|
|
197
|
+
* zero denominator (`1` for a long, `-1` for a short), `current` comes
|
|
198
|
+
* without `size`, or `current.size + size` is not positive.
|
|
187
199
|
* @public
|
|
188
200
|
*/
|
|
189
201
|
export function estimateLiquidationPrice(params: {
|
|
@@ -191,6 +203,8 @@ export function estimateLiquidationPrice(params: {
|
|
|
191
203
|
leverage: string
|
|
192
204
|
isLong: boolean
|
|
193
205
|
maintenanceMarginRate: string
|
|
206
|
+
size?: string
|
|
207
|
+
current?: { size: string; entryPrice: string; margin: string }
|
|
194
208
|
}): string | undefined {
|
|
195
209
|
const entryPrice = decimalStringToBig(params.entryPrice)
|
|
196
210
|
const leverage = decimalStringToBig(params.leverage)
|
|
@@ -206,13 +220,38 @@ export function estimateLiquidationPrice(params: {
|
|
|
206
220
|
`must not be ${params.isLong ? '1' : '-1'} for this side`
|
|
207
221
|
)
|
|
208
222
|
}
|
|
209
|
-
|
|
210
|
-
|
|
223
|
+
if (params.current === undefined) {
|
|
224
|
+
const marginFractionAvailable = new DivBig(1).div(leverage).minus(mmr)
|
|
225
|
+
if (marginFractionAvailable.lte(0)) {
|
|
226
|
+
return undefined
|
|
227
|
+
}
|
|
228
|
+
const marginAvailable = marginFractionAvailable.times(entryPrice)
|
|
229
|
+
return bigToDecimalString(
|
|
230
|
+
entryPrice.minus(marginAvailable.times(side).div(denominator))
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
if (params.size === undefined) {
|
|
234
|
+
throw invalidInput('size', 'is required with `current`')
|
|
235
|
+
}
|
|
236
|
+
const addSize = decimalStringToBig(params.size)
|
|
237
|
+
const currentSize = decimalStringToBig(params.current.size)
|
|
238
|
+
const currentEntry = decimalStringToBig(params.current.entryPrice)
|
|
239
|
+
const currentMargin = decimalStringToBig(params.current.margin)
|
|
240
|
+
const totalSize = currentSize.plus(addSize)
|
|
241
|
+
if (totalSize.lte(0)) {
|
|
242
|
+
throw invalidInput('size + current.size', 'must be positive')
|
|
243
|
+
}
|
|
244
|
+
const equity = currentMargin
|
|
245
|
+
.plus(currentSize.times(entryPrice.minus(currentEntry)).times(side))
|
|
246
|
+
.plus(new DivBig(entryPrice).times(addSize).div(leverage))
|
|
247
|
+
const marginAvailablePerUnit = new DivBig(equity)
|
|
248
|
+
.minus(mmr.times(totalSize).times(entryPrice))
|
|
249
|
+
.div(totalSize)
|
|
250
|
+
if (marginAvailablePerUnit.lte(0)) {
|
|
211
251
|
return undefined
|
|
212
252
|
}
|
|
213
|
-
const marginAvailable = marginFractionAvailable.times(entryPrice)
|
|
214
253
|
return bigToDecimalString(
|
|
215
|
-
entryPrice.minus(
|
|
254
|
+
entryPrice.minus(marginAvailablePerUnit.times(side).div(denominator))
|
|
216
255
|
)
|
|
217
256
|
}
|
|
218
257
|
|
|
@@ -223,12 +262,16 @@ export const safeEstimateLiquidationPrice = createSafeFunction(
|
|
|
223
262
|
)
|
|
224
263
|
|
|
225
264
|
/**
|
|
226
|
-
* Estimate the liquidation price of
|
|
227
|
-
* `market.maintenanceMarginRate
|
|
265
|
+
* Estimate the liquidation price of the isolated position that an order
|
|
266
|
+
* leaves, from the flat `market.maintenanceMarginRate`: a new position, or
|
|
267
|
+
* the blend of the order into `params.position`. The collateral of the
|
|
268
|
+
* position is `position.marginUsed`, so the venue `marginUsed` must exclude
|
|
269
|
+
* the unrealized PnL, as Lighter `allocated_margin` does.
|
|
228
270
|
*
|
|
229
271
|
* @returns The estimate, or `undefined` when the market carries no
|
|
230
|
-
* `maintenanceMarginRate
|
|
231
|
-
*
|
|
272
|
+
* `maintenanceMarginRate`, when the order or the position is cross (the
|
|
273
|
+
* liquidation depends on the whole account), when the order reduces the
|
|
274
|
+
* position, or when {@link estimateLiquidationPrice} gives `undefined`.
|
|
232
275
|
* @throws {PerpsError} `ValidationError` on an input that
|
|
233
276
|
* {@link estimateLiquidationPrice} rejects.
|
|
234
277
|
* @public
|
|
@@ -237,7 +280,14 @@ export function estimateLiquidationPriceAtMarketRate(
|
|
|
237
280
|
market: PerpsMarket,
|
|
238
281
|
params: LiquidationEstimateParams
|
|
239
282
|
): string | undefined {
|
|
240
|
-
|
|
283
|
+
const { position } = params
|
|
284
|
+
if (
|
|
285
|
+
market.maintenanceMarginRate === undefined ||
|
|
286
|
+
params.marginMode === MarginMode.CROSS ||
|
|
287
|
+
position?.marginMode === MarginMode.CROSS ||
|
|
288
|
+
(position !== undefined &&
|
|
289
|
+
(position.side === PositionSide.LONG) !== params.isLong)
|
|
290
|
+
) {
|
|
241
291
|
return undefined
|
|
242
292
|
}
|
|
243
293
|
return estimateLiquidationPrice({
|
|
@@ -245,6 +295,16 @@ export function estimateLiquidationPriceAtMarketRate(
|
|
|
245
295
|
leverage: params.leverage,
|
|
246
296
|
isLong: params.isLong,
|
|
247
297
|
maintenanceMarginRate: market.maintenanceMarginRate,
|
|
298
|
+
size: params.size,
|
|
299
|
+
...(position === undefined
|
|
300
|
+
? {}
|
|
301
|
+
: {
|
|
302
|
+
current: {
|
|
303
|
+
size: position.size,
|
|
304
|
+
entryPrice: position.entryPrice,
|
|
305
|
+
margin: position.marginUsed,
|
|
306
|
+
},
|
|
307
|
+
}),
|
|
248
308
|
})
|
|
249
309
|
}
|
|
250
310
|
|
|
@@ -17,13 +17,13 @@ export const LIFI_API_URL = 'https://li.quest/v1'
|
|
|
17
17
|
* @public
|
|
18
18
|
*/
|
|
19
19
|
export interface GetGasRecommendationParams {
|
|
20
|
-
/** Chain to hold native gas on — a `
|
|
20
|
+
/** Chain to hold native gas on — a `venueDepositPipeline` flow's `chainId`. */
|
|
21
21
|
chainId: number
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* How much native gas LI.FI suggests holding on `params.chainId` to transact
|
|
26
|
-
* there, for seeding the gas leg of a
|
|
26
|
+
* there, for seeding the gas leg of a venue-deposit pipeline. Read straight
|
|
27
27
|
* from the LI.FI API by the user's client.
|
|
28
28
|
*
|
|
29
29
|
* @returns The suggestion, with `available: false` and no amounts on a chain
|
package/src/types/deposit.ts
CHANGED
|
@@ -37,20 +37,21 @@ export interface DepositFlowLifiSwap {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* Fund an address that has no venue account yet
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
40
|
+
* Fund an address that has no venue account yet, or an existing account on a
|
|
41
|
+
* venue whose collateral no LI.FI route credits directly. Collateral and the
|
|
42
|
+
* native gas to broadcast with are both needed on `chainId`, where the venue's
|
|
43
|
+
* {@link ActionType.DEPOSIT} action stages the on-chain legs that credit the
|
|
44
|
+
* account and open it when none exists.
|
|
44
45
|
*
|
|
45
46
|
* @public
|
|
46
47
|
*/
|
|
47
|
-
export interface
|
|
48
|
-
kind: '
|
|
48
|
+
export interface DepositFlowVenueDepositPipeline {
|
|
49
|
+
kind: 'venueDepositPipeline'
|
|
49
50
|
/** Chain on which the deposit legs broadcast; the wallet must be switched here. */
|
|
50
51
|
chainId: number
|
|
51
52
|
/** Native gas asset needed to broadcast the deposit legs. */
|
|
52
53
|
gasAsset: DeclaredDepositAsset
|
|
53
|
-
/** Collateral asset
|
|
54
|
+
/** Collateral asset the venue deposit credits. */
|
|
54
55
|
collateral: DeclaredDepositAsset
|
|
55
56
|
/** Action type used to stage the deposit pipeline. */
|
|
56
57
|
bridgeAction: ActionType.DEPOSIT
|
|
@@ -77,5 +78,5 @@ export interface DepositFlowSetupRequired {
|
|
|
77
78
|
*/
|
|
78
79
|
export type DepositFlow =
|
|
79
80
|
| DepositFlowLifiSwap
|
|
80
|
-
|
|
|
81
|
+
| DepositFlowVenueDepositPipeline
|
|
81
82
|
| DepositFlowSetupRequired
|
package/src/types/provider.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
AvailableToTrade,
|
|
12
12
|
DecimalString,
|
|
13
13
|
FillsResponse,
|
|
14
|
+
MarginMode,
|
|
14
15
|
Market,
|
|
15
16
|
MarketRef,
|
|
16
17
|
MarketSettings,
|
|
@@ -160,7 +161,8 @@ export interface ActionSignerContribution {
|
|
|
160
161
|
}
|
|
161
162
|
|
|
162
163
|
/**
|
|
163
|
-
* Inputs for {@link PerpsProviderPlugin.estimateLiquidationPrice}:
|
|
164
|
+
* Inputs for {@link PerpsProviderPlugin.estimateLiquidationPrice}: the order
|
|
165
|
+
* and, when the order adds to one, the open position on the market.
|
|
164
166
|
* `leverage` is the user-selected leverage for the new position, `isLong`
|
|
165
167
|
* carries direction.
|
|
166
168
|
*
|
|
@@ -172,6 +174,14 @@ export interface LiquidationEstimateParams {
|
|
|
172
174
|
leverage: string
|
|
173
175
|
/** `true` for long, `false` for short. */
|
|
174
176
|
isLong: boolean
|
|
177
|
+
marginMode: MarginMode
|
|
178
|
+
/**
|
|
179
|
+
* Order size in base-asset units. The estimate reads it only with
|
|
180
|
+
* `position`, to blend the order into that position.
|
|
181
|
+
*/
|
|
182
|
+
size?: DecimalString
|
|
183
|
+
/** Open position on the market that the order joins. */
|
|
184
|
+
position?: Position
|
|
175
185
|
}
|
|
176
186
|
|
|
177
187
|
/**
|
|
@@ -633,16 +643,20 @@ export interface PerpsProviderPlugin {
|
|
|
633
643
|
}
|
|
634
644
|
|
|
635
645
|
/**
|
|
636
|
-
* Estimate the liquidation price of
|
|
637
|
-
* using the venue's margin model
|
|
646
|
+
* Estimate the liquidation price of the position that the order leaves on
|
|
647
|
+
* `market` using the venue's margin model: a new position, or the blend of
|
|
648
|
+
* the order into `params.position`. A preview helper — for existing
|
|
638
649
|
* positions, prefer `Position.liquidationPrice` from the venue. Pure —
|
|
639
650
|
* does no I/O.
|
|
640
651
|
*
|
|
641
652
|
* @returns The estimated liquidation price, or `undefined` when `market`
|
|
642
|
-
* lacks the margin metadata the model needs
|
|
643
|
-
* margin above the maintenance requirement
|
|
653
|
+
* lacks the margin metadata the model needs, `leverage` leaves no
|
|
654
|
+
* margin above the maintenance requirement, or the venue model cannot
|
|
655
|
+
* give a correct price for the margin mode and position, such as a cross
|
|
656
|
+
* position whose liquidation depends on the whole account.
|
|
644
657
|
* @throws {PerpsError} `ValidationError` when an input does not match the
|
|
645
|
-
* decimal pattern or is degenerate, such as a zero leverage
|
|
658
|
+
* decimal pattern or is degenerate, such as a zero leverage or a
|
|
659
|
+
* `position` without a `size`.
|
|
646
660
|
*/
|
|
647
661
|
estimateLiquidationPrice(
|
|
648
662
|
market: PerpsMarket,
|
|
@@ -3,7 +3,7 @@ import { zeroAddress } from 'viem'
|
|
|
3
3
|
import type { DeclaredDepositAsset } from '../types/deposit.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* USDC on Ethereum mainnet — the collateral leg of Lighter's
|
|
6
|
+
* USDC on Ethereum mainnet — the collateral leg of Lighter's venue-deposit
|
|
7
7
|
* pipeline, and the token Ondo's provisioned deposit address is funded in.
|
|
8
8
|
*
|
|
9
9
|
* @public
|
|
@@ -15,7 +15,7 @@ export const ETHEREUM_USDC: DeclaredDepositAsset = {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Ether on Ethereum mainnet, to broadcast the
|
|
18
|
+
* Ether on Ethereum mainnet, to broadcast the venue-deposit pipeline's legs
|
|
19
19
|
* with.
|
|
20
20
|
*
|
|
21
21
|
* @public
|
|
@@ -65,7 +65,7 @@ export const ROBINHOOD_USDG: DeclaredDepositAsset = {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
* Ether on Robinhood Chain, to broadcast the Robinhood
|
|
68
|
+
* Ether on Robinhood Chain, to broadcast the Robinhood venue-deposit pipeline's
|
|
69
69
|
* legs with.
|
|
70
70
|
*
|
|
71
71
|
* @public
|
package/src/version.ts
CHANGED