@lifi/perps-sdk 36.2.0 → 36.3.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 +108 -0
- package/dist/cjs/client/PerpsClient.d.ts +3 -2
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +16 -1
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/index.d.ts +4 -2
- 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/math/portfolioHistory.d.ts +7 -0
- package/dist/cjs/math/portfolioHistory.d.ts.map +1 -0
- package/dist/cjs/math/portfolioHistory.js +58 -0
- package/dist/cjs/math/portfolioHistory.js.map +1 -0
- package/dist/cjs/types/api.d.ts +7 -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/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client/PerpsClient.d.ts +19 -3
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +32 -2
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/portfolioHistory.d.ts +23 -0
- package/dist/esm/math/portfolioHistory.d.ts.map +1 -0
- package/dist/esm/math/portfolioHistory.js +66 -0
- package/dist/esm/math/portfolioHistory.js.map +1 -0
- package/dist/esm/types/api.d.ts +17 -1
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +22 -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/types/client/PerpsClient.d.ts +19 -3
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/portfolioHistory.d.ts +23 -0
- package/dist/types/math/portfolioHistory.d.ts.map +1 -0
- package/dist/types/types/api.d.ts +17 -1
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +22 -1
- 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 +46 -2
- package/src/index.ts +4 -0
- package/src/math/portfolioHistory.ts +95 -0
- package/src/types/api.ts +18 -0
- package/src/types/provider.ts +28 -0
- package/src/version.ts +1 -1
package/src/index.ts
CHANGED
|
@@ -125,6 +125,8 @@ export {
|
|
|
125
125
|
safeEstimateRealizedPnl,
|
|
126
126
|
safeResolveCloseSize,
|
|
127
127
|
} from './math/order.js'
|
|
128
|
+
export type { PortfolioHistoryWindow } from './math/portfolioHistory.js'
|
|
129
|
+
export { buildPortfolioHistoryWindow } from './math/portfolioHistory.js'
|
|
128
130
|
export {
|
|
129
131
|
buildPosition,
|
|
130
132
|
calculateEffectiveLeverage,
|
|
@@ -266,6 +268,7 @@ export type {
|
|
|
266
268
|
ExecuteMetaActionParams,
|
|
267
269
|
GetAccountResult,
|
|
268
270
|
GetDepositFlowParams,
|
|
271
|
+
GetMarginHealthParams,
|
|
269
272
|
GetPortfolioHistoryParams,
|
|
270
273
|
GetSetupParams,
|
|
271
274
|
GetWithdrawableBalancesParams,
|
|
@@ -309,6 +312,7 @@ export type {
|
|
|
309
312
|
ProviderGetAvailableToTradeParams,
|
|
310
313
|
ProviderGetDepositFlowParams,
|
|
311
314
|
ProviderGetFillsParams,
|
|
315
|
+
ProviderGetMarginHealthParams,
|
|
312
316
|
ProviderGetMarketSettingsParams,
|
|
313
317
|
ProviderGetOrderParams,
|
|
314
318
|
ProviderGetOrdersParams,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PortfolioHistoryBounds,
|
|
3
|
+
PortfolioHistoryPoint,
|
|
4
|
+
} from '@lifi/perps-types'
|
|
5
|
+
import { subtractDecimalString } from '../decimal/arithmetic.js'
|
|
6
|
+
import { isDecimalString } from '../decimal/parse.js'
|
|
7
|
+
import { warnSkippedVenueRow } from '../utils/warnOnce.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The samples of a bounded portfolio history read: the opening sample at or
|
|
11
|
+
* before the start, when the venue has one, and the samples inside the bounds.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export interface PortfolioHistoryWindow {
|
|
15
|
+
opening?: PortfolioHistoryPoint
|
|
16
|
+
points: PortfolioHistoryPoint[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const SAMPLE_ROW = 'portfolio history'
|
|
20
|
+
|
|
21
|
+
const isValidSample = (
|
|
22
|
+
provider: string,
|
|
23
|
+
point: PortfolioHistoryPoint
|
|
24
|
+
): boolean => {
|
|
25
|
+
if (!Number.isFinite(point.timestamp)) {
|
|
26
|
+
warnSkippedVenueRow(provider, SAMPLE_ROW, 'timestamp', point.timestamp, {
|
|
27
|
+
expected: 'timestamp',
|
|
28
|
+
})
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
const invalid = (['accountValue', 'pnl'] as const).find(
|
|
32
|
+
(field) => !isDecimalString(point[field])
|
|
33
|
+
)
|
|
34
|
+
if (invalid === undefined) {
|
|
35
|
+
return true
|
|
36
|
+
}
|
|
37
|
+
warnSkippedVenueRow(provider, SAMPLE_ROW, invalid, point[invalid])
|
|
38
|
+
return false
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const buildRelativeSample = (
|
|
42
|
+
point: PortfolioHistoryPoint,
|
|
43
|
+
basis: PortfolioHistoryPoint
|
|
44
|
+
): PortfolioHistoryPoint => {
|
|
45
|
+
const netTransfers =
|
|
46
|
+
isDecimalString(point.netTransfers) && isDecimalString(basis.netTransfers)
|
|
47
|
+
? subtractDecimalString(point.netTransfers, basis.netTransfers)
|
|
48
|
+
: undefined
|
|
49
|
+
return {
|
|
50
|
+
timestamp: point.timestamp,
|
|
51
|
+
accountValue: point.accountValue,
|
|
52
|
+
pnl: subtractDecimalString(point.pnl, basis.pnl),
|
|
53
|
+
...(netTransfers === undefined ? {} : { netTransfers }),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Cut venue samples to `bounds`. The latest valid sample at or before
|
|
59
|
+
* `bounds.start` is the `opening`, and the valid samples after `bounds.start`
|
|
60
|
+
* through `bounds.end` are the `points`, oldest first. `pnl` and
|
|
61
|
+
* `netTransfers` are relative to the first retained sample, so that sample
|
|
62
|
+
* carries `'0'` for both. A sample whose `timestamp` is not finite, or whose
|
|
63
|
+
* `accountValue` or `pnl` does not match the decimal pattern, is skipped with
|
|
64
|
+
* a warning. A retained sample stays without `netTransfers` when it or the
|
|
65
|
+
* first retained sample has none. Never throws.
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export function buildPortfolioHistoryWindow(
|
|
69
|
+
provider: string,
|
|
70
|
+
points: PortfolioHistoryPoint[],
|
|
71
|
+
bounds: PortfolioHistoryBounds
|
|
72
|
+
): PortfolioHistoryWindow {
|
|
73
|
+
const ordered = points
|
|
74
|
+
.filter((point) => isValidSample(provider, point))
|
|
75
|
+
.sort((a, b) => a.timestamp - b.timestamp)
|
|
76
|
+
let opening: PortfolioHistoryPoint | undefined
|
|
77
|
+
const inBounds: PortfolioHistoryPoint[] = []
|
|
78
|
+
for (const point of ordered) {
|
|
79
|
+
if (point.timestamp <= bounds.start) {
|
|
80
|
+
opening = point
|
|
81
|
+
} else if (point.timestamp <= bounds.end) {
|
|
82
|
+
inBounds.push(point)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const basis = opening ?? inBounds[0]
|
|
86
|
+
if (basis === undefined) {
|
|
87
|
+
return { points: [] }
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
...(opening === undefined
|
|
91
|
+
? {}
|
|
92
|
+
: { opening: buildRelativeSample(opening, basis) }),
|
|
93
|
+
points: inBounds.map((point) => buildRelativeSample(point, basis)),
|
|
94
|
+
}
|
|
95
|
+
}
|
package/src/types/api.ts
CHANGED
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
OnboardParams,
|
|
15
15
|
OrderSide,
|
|
16
16
|
OrderType,
|
|
17
|
+
PortfolioHistoryBounds,
|
|
17
18
|
PortfolioHistoryRange,
|
|
18
19
|
SetupAction,
|
|
19
20
|
SetupOption,
|
|
@@ -273,6 +274,18 @@ export interface GetDepositFlowParams {
|
|
|
273
274
|
address: Address
|
|
274
275
|
}
|
|
275
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Parameters for {@link PerpsClient.getMarginHealth}.
|
|
279
|
+
*
|
|
280
|
+
* @public
|
|
281
|
+
*/
|
|
282
|
+
export interface GetMarginHealthParams {
|
|
283
|
+
provider: string
|
|
284
|
+
address: Address
|
|
285
|
+
/** The `Balance.categoryId` of the cross pool, for a venue with one cross pool per category. */
|
|
286
|
+
categoryId?: string
|
|
287
|
+
}
|
|
288
|
+
|
|
276
289
|
/**
|
|
277
290
|
* Parameters for {@link PerpsClient.getWithdrawFlow}.
|
|
278
291
|
*
|
|
@@ -312,6 +325,11 @@ export interface GetPortfolioHistoryParams {
|
|
|
312
325
|
provider: string
|
|
313
326
|
address: Address
|
|
314
327
|
range: PortfolioHistoryRange
|
|
328
|
+
/**
|
|
329
|
+
* Cut the read to `start` through `end` and ask for the opening sample at
|
|
330
|
+
* or before `start`. Absent bounds keep the preset `range` read.
|
|
331
|
+
*/
|
|
332
|
+
bounds?: PortfolioHistoryBounds
|
|
315
333
|
}
|
|
316
334
|
|
|
317
335
|
/**
|
package/src/types/provider.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
AvailableToTrade,
|
|
13
13
|
DecimalString,
|
|
14
14
|
FillsResponse,
|
|
15
|
+
MarginHealth,
|
|
15
16
|
MarginMode,
|
|
16
17
|
Market,
|
|
17
18
|
MarketRef,
|
|
@@ -21,6 +22,7 @@ import type {
|
|
|
21
22
|
OrdersResponse,
|
|
22
23
|
PerpsMarket,
|
|
23
24
|
PerpsSigner,
|
|
25
|
+
PortfolioHistoryBounds,
|
|
24
26
|
PortfolioHistoryRange,
|
|
25
27
|
PortfolioHistoryResponse,
|
|
26
28
|
Position,
|
|
@@ -268,6 +270,20 @@ export interface ProviderGetMarketSettingsParams {
|
|
|
268
270
|
market: MarketRef
|
|
269
271
|
}
|
|
270
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Read params for {@link PerpsProviderPlugin.getMarginHealth}.
|
|
275
|
+
*
|
|
276
|
+
* @public
|
|
277
|
+
*/
|
|
278
|
+
export interface ProviderGetMarginHealthParams {
|
|
279
|
+
address: Address
|
|
280
|
+
/**
|
|
281
|
+
* The `Balance.categoryId` of the cross pool, for a venue with one cross pool
|
|
282
|
+
* per category. A venue with one cross pool ignores it.
|
|
283
|
+
*/
|
|
284
|
+
categoryId?: string
|
|
285
|
+
}
|
|
286
|
+
|
|
271
287
|
/**
|
|
272
288
|
* Read params for {@link PerpsProviderPlugin.getAvailableToTrade}.
|
|
273
289
|
*
|
|
@@ -350,6 +366,8 @@ export interface ProviderGetActivityParams {
|
|
|
350
366
|
export interface ProviderGetPortfolioHistoryParams {
|
|
351
367
|
address: Address
|
|
352
368
|
range: PortfolioHistoryRange
|
|
369
|
+
/** Valid bounds: the client checks them before the provider read. */
|
|
370
|
+
bounds?: PortfolioHistoryBounds
|
|
353
371
|
}
|
|
354
372
|
|
|
355
373
|
/**
|
|
@@ -551,6 +569,16 @@ export interface PerpsProviderPlugin {
|
|
|
551
569
|
options?: SDKRequestOptions
|
|
552
570
|
): Promise<AvailableToTrade | undefined>
|
|
553
571
|
|
|
572
|
+
/**
|
|
573
|
+
* Current cross-margin health of the account. Optional because each venue
|
|
574
|
+
* calculates it differently. `undefined` means the venue gives no health for
|
|
575
|
+
* the account in its current mode.
|
|
576
|
+
*/
|
|
577
|
+
getMarginHealth?(
|
|
578
|
+
params: ProviderGetMarginHealthParams,
|
|
579
|
+
options?: SDKRequestOptions
|
|
580
|
+
): Promise<MarginHealth | undefined>
|
|
581
|
+
|
|
554
582
|
getOrders(
|
|
555
583
|
params: ProviderGetOrdersParams,
|
|
556
584
|
options?: SDKRequestOptions
|
package/src/version.ts
CHANGED