@lifi/perps-sdk 35.0.0 → 36.1.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 +44 -37
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +4 -4
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/decimal/compare.d.ts +2 -0
- package/dist/cjs/decimal/compare.d.ts.map +1 -1
- package/dist/cjs/decimal/compare.js +6 -1
- package/dist/cjs/decimal/compare.js.map +1 -1
- package/dist/cjs/index.d.ts +4 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +12 -9
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/leverage.d.ts +10 -0
- package/dist/cjs/math/leverage.d.ts.map +1 -0
- package/dist/cjs/math/leverage.js +32 -0
- package/dist/cjs/math/leverage.js.map +1 -0
- package/dist/cjs/math/position.d.ts +1 -1
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +3 -10
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/types/provider.d.ts +5 -3
- 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 +8 -2
- package/dist/cjs/wire/orderAmounts.d.ts.map +1 -1
- package/dist/cjs/wire/orderAmounts.js +26 -6
- package/dist/cjs/wire/orderAmounts.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +4 -3
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +8 -7
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/decimal/compare.d.ts +14 -0
- package/dist/esm/decimal/compare.d.ts.map +1 -1
- package/dist/esm/decimal/compare.js +16 -0
- package/dist/esm/decimal/compare.js.map +1 -1
- package/dist/esm/index.d.ts +4 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/leverage.d.ts +40 -0
- package/dist/esm/math/leverage.d.ts.map +1 -0
- package/dist/esm/math/leverage.js +50 -0
- package/dist/esm/math/leverage.js.map +1 -0
- package/dist/esm/math/position.d.ts +5 -6
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +6 -14
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/types/provider.d.ts +21 -10
- 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 +29 -9
- package/dist/esm/wire/orderAmounts.d.ts.map +1 -1
- package/dist/esm/wire/orderAmounts.js +39 -11
- package/dist/esm/wire/orderAmounts.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +4 -3
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/decimal/compare.d.ts +14 -0
- package/dist/types/decimal/compare.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/leverage.d.ts +40 -0
- package/dist/types/math/leverage.d.ts.map +1 -0
- package/dist/types/math/position.d.ts +5 -6
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +21 -10
- 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 +29 -9
- package/dist/types/wire/orderAmounts.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +16 -7
- package/src/decimal/compare.ts +21 -0
- package/src/index.ts +9 -2
- package/src/math/leverage.ts +83 -0
- package/src/math/position.ts +6 -29
- package/src/types/provider.ts +28 -9
- package/src/version.ts +1 -1
- package/src/wire/orderAmounts.ts +72 -18
- package/dist/cjs/utils/leverage.d.ts +0 -5
- package/dist/cjs/utils/leverage.d.ts.map +0 -1
- package/dist/cjs/utils/leverage.js +0 -20
- package/dist/cjs/utils/leverage.js.map +0 -1
- package/dist/esm/utils/leverage.d.ts +0 -11
- package/dist/esm/utils/leverage.d.ts.map +0 -1
- package/dist/esm/utils/leverage.js +0 -22
- package/dist/esm/utils/leverage.js.map +0 -1
- package/dist/types/utils/leverage.d.ts +0 -11
- package/dist/types/utils/leverage.d.ts.map +0 -1
- package/src/utils/leverage.ts +0 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/perps-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "36.1.0",
|
|
4
4
|
"description": "LI.FI Perps SDK for perpetuals trading",
|
|
5
5
|
"homepage": "https://github.com/lifinance/perps-sdk",
|
|
6
6
|
"bugs": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@lifi/types": "^18.3.0",
|
|
46
46
|
"big.js": "^7.0.1",
|
|
47
47
|
"partysocket": "^1.1.19",
|
|
48
|
-
"@lifi/perps-types": "^30.
|
|
48
|
+
"@lifi/perps-types": "^30.2.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"viem": ">=2.57.11 <2.57.12"
|
|
@@ -415,14 +415,16 @@ export class PerpsClient {
|
|
|
415
415
|
* Ask the provider plugin for the signer-bearing wire fields of `action` —
|
|
416
416
|
* the on-wire `signerAddress` and any signer-derived params (e.g.
|
|
417
417
|
* Hyperliquid's `agentAddress` for `APPROVE_AGENT`). Forwards the descriptor's
|
|
418
|
-
* `signer` so the plugin can branch on signer role
|
|
419
|
-
* plugin
|
|
420
|
-
*
|
|
418
|
+
* `signer` so the plugin can branch on signer role, and the caller's `params`
|
|
419
|
+
* so the plugin can contribute a venue encoding of a caller value. Returns
|
|
420
|
+
* empty when the plugin signs as the user or with a non-EVM credential. Core
|
|
421
|
+
* constructs no `signerAddress` itself; signer identity is plugin-owned.
|
|
421
422
|
*/
|
|
422
423
|
private async resolveActionRequest(
|
|
423
424
|
provider: string,
|
|
424
425
|
descriptor: ProviderAction,
|
|
425
|
-
address: Address
|
|
426
|
+
address: Address,
|
|
427
|
+
params?: ActionParamsMap[ActionType]
|
|
426
428
|
): Promise<ActionSignerContribution> {
|
|
427
429
|
const plugin = this.requireProvider(provider)
|
|
428
430
|
if (typeof plugin.resolveActionRequest !== 'function') {
|
|
@@ -431,7 +433,8 @@ export class PerpsClient {
|
|
|
431
433
|
return plugin.resolveActionRequest(
|
|
432
434
|
descriptor.type,
|
|
433
435
|
address,
|
|
434
|
-
descriptor.signer
|
|
436
|
+
descriptor.signer,
|
|
437
|
+
params
|
|
435
438
|
)
|
|
436
439
|
}
|
|
437
440
|
|
|
@@ -723,7 +726,8 @@ export class PerpsClient {
|
|
|
723
726
|
await this.resolveActionRequest(
|
|
724
727
|
params.provider,
|
|
725
728
|
descriptor,
|
|
726
|
-
params.address
|
|
729
|
+
params.address,
|
|
730
|
+
params.params
|
|
727
731
|
)
|
|
728
732
|
return createAction(this.sdkClient, {
|
|
729
733
|
provider: params.provider,
|
|
@@ -1673,7 +1677,12 @@ export class PerpsClient {
|
|
|
1673
1677
|
const descriptor = findActionDescriptor(metadata, action)
|
|
1674
1678
|
|
|
1675
1679
|
const { signerAddress, params: signerParams } =
|
|
1676
|
-
await this.resolveActionRequest(
|
|
1680
|
+
await this.resolveActionRequest(
|
|
1681
|
+
provider,
|
|
1682
|
+
descriptor,
|
|
1683
|
+
address,
|
|
1684
|
+
params.params
|
|
1685
|
+
)
|
|
1677
1686
|
|
|
1678
1687
|
const { actions } = await createAction(this.sdkClient, {
|
|
1679
1688
|
provider,
|
package/src/decimal/compare.ts
CHANGED
|
@@ -45,6 +45,27 @@ export const safeIsDecimalStringZero = createSafeFunction(
|
|
|
45
45
|
isDecimalStringZero
|
|
46
46
|
)
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Exact `value` has no fractional part, so `'700.0'` is an integer and
|
|
50
|
+
* `'700.5'` is not.
|
|
51
|
+
*
|
|
52
|
+
* @throws {PerpsError} `ValidationError` when `value` does not match the decimal pattern.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export function isDecimalStringInteger(value: string): boolean {
|
|
56
|
+
return decimalStringToBig(value).mod(1).eq(0)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* {@link isDecimalStringInteger}, or `undefined` with a warning when it throws.
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export const safeIsDecimalStringInteger = createSafeFunction(
|
|
65
|
+
'isDecimalStringInteger',
|
|
66
|
+
isDecimalStringInteger
|
|
67
|
+
)
|
|
68
|
+
|
|
48
69
|
/**
|
|
49
70
|
* Exact three-way compare for a sort: `-1` when `a < b`, `0` when equal, `1`
|
|
50
71
|
* when `a > b`.
|
package/src/index.ts
CHANGED
|
@@ -36,9 +36,11 @@ export {
|
|
|
36
36
|
export {
|
|
37
37
|
compareDecimalStrings,
|
|
38
38
|
isDecimalStringGreaterThan,
|
|
39
|
+
isDecimalStringInteger,
|
|
39
40
|
isDecimalStringZero,
|
|
40
41
|
safeCompareDecimalStrings,
|
|
41
42
|
safeIsDecimalStringGreaterThan,
|
|
43
|
+
safeIsDecimalStringInteger,
|
|
42
44
|
safeIsDecimalStringZero,
|
|
43
45
|
} from './decimal/compare.js'
|
|
44
46
|
export type { DecimalRounding } from './decimal/convert.js'
|
|
@@ -89,6 +91,12 @@ export {
|
|
|
89
91
|
annualizeFundingRate,
|
|
90
92
|
safeAnnualizeFundingRate,
|
|
91
93
|
} from './math/funding.js'
|
|
94
|
+
export type { LeverageSource } from './math/leverage.js'
|
|
95
|
+
export {
|
|
96
|
+
resolveExactLeverage,
|
|
97
|
+
safeResolveExactLeverage,
|
|
98
|
+
validateInitialMarginFractionBps,
|
|
99
|
+
} from './math/leverage.js'
|
|
92
100
|
export type { ExpectedPnl } from './math/order.js'
|
|
93
101
|
export {
|
|
94
102
|
buildQuote,
|
|
@@ -133,7 +141,7 @@ export {
|
|
|
133
141
|
estimateUnrealizedPnl,
|
|
134
142
|
isPositionMarginAdjustable,
|
|
135
143
|
isPositionMarginRemovable,
|
|
136
|
-
|
|
144
|
+
remarkPositionValues,
|
|
137
145
|
safeCalculateEffectiveLeverage,
|
|
138
146
|
safeCalculateInitialMarginRoe,
|
|
139
147
|
safeCalculateLiquidationDistance,
|
|
@@ -338,7 +346,6 @@ export {
|
|
|
338
346
|
explorerTxUrl,
|
|
339
347
|
explorerTxUrlFromBase,
|
|
340
348
|
} from './utils/explorer.js'
|
|
341
|
-
export { isLeverageEqual, safeIsLeverageEqual } from './utils/leverage.js'
|
|
342
349
|
export { isSpotCategory } from './utils/marketCategory.js'
|
|
343
350
|
export {
|
|
344
351
|
ACTIVE_ORDER_STATUSES,
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { DecimalString } from '@lifi/perps-types'
|
|
2
|
+
import { divideDecimalString } from '../decimal/arithmetic.js'
|
|
3
|
+
import {
|
|
4
|
+
isDecimalStringGreaterThan,
|
|
5
|
+
isDecimalStringInteger,
|
|
6
|
+
} from '../decimal/compare.js'
|
|
7
|
+
import { roundDecimalString } from '../decimal/convert.js'
|
|
8
|
+
import { isDecimalString } from '../decimal/parse.js'
|
|
9
|
+
import { invalidInput } from '../errors/invalidInput.js'
|
|
10
|
+
import { createSafeFunction } from '../utils/createSafeFunction.js'
|
|
11
|
+
|
|
12
|
+
/** Basis points in one whole unit: an IMF of `'700'` is `700 ÷ 10000`. */
|
|
13
|
+
export const BASIS_POINTS_PER_UNIT = '10000'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The leverage fields of a `MarketSettings` or a `Position`: the venue's
|
|
17
|
+
* native integer IMF in basis points when it stores one, and the leverage
|
|
18
|
+
* multiple.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export interface LeverageSource {
|
|
23
|
+
leverage?: DecimalString
|
|
24
|
+
initialMarginFractionBps?: DecimalString
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The native IMF as a canonical integer string (`'700.0'` gives `'700'`), so
|
|
29
|
+
* a formula reads it as an exact margin rate and a venue request carries
|
|
30
|
+
* plain digits. `parameter` names the input in the error.
|
|
31
|
+
*
|
|
32
|
+
* @throws {PerpsError} `ValidationError` when `value` is not a positive
|
|
33
|
+
* integer decimal string.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export function validateInitialMarginFractionBps(
|
|
37
|
+
value: DecimalString,
|
|
38
|
+
parameter = 'initialMarginFractionBps'
|
|
39
|
+
): DecimalString {
|
|
40
|
+
if (
|
|
41
|
+
!isDecimalString(value) ||
|
|
42
|
+
!isDecimalStringGreaterThan(value, '0') ||
|
|
43
|
+
!isDecimalStringInteger(value)
|
|
44
|
+
) {
|
|
45
|
+
throw invalidInput(parameter, `must be a positive integer, got '${value}'`)
|
|
46
|
+
}
|
|
47
|
+
return roundDecimalString(value, 0, 'truncate')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The exact leverage multiple of a venue setting, for the TP/SL and
|
|
52
|
+
* liquidation formulas: `10000 ÷ initialMarginFractionBps` (40 places,
|
|
53
|
+
* half-up) when the native IMF is present, else `leverage` unchanged. A
|
|
54
|
+
* display leverage never feeds a formula. `calculateOrderAmounts` takes the
|
|
55
|
+
* IMF itself, so a derived margin stays exact.
|
|
56
|
+
*
|
|
57
|
+
* @throws {PerpsError} `ValidationError` when `initialMarginFractionBps` is
|
|
58
|
+
* present but not a positive integer, or when it is absent and `leverage`
|
|
59
|
+
* is absent or not a positive decimal.
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export function resolveExactLeverage(source: LeverageSource): DecimalString {
|
|
63
|
+
const { initialMarginFractionBps, leverage } = source
|
|
64
|
+
if (initialMarginFractionBps !== undefined) {
|
|
65
|
+
return divideDecimalString(
|
|
66
|
+
BASIS_POINTS_PER_UNIT,
|
|
67
|
+
validateInitialMarginFractionBps(initialMarginFractionBps)
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
if (leverage === undefined || !isDecimalStringGreaterThan(leverage, '0')) {
|
|
71
|
+
throw invalidInput(
|
|
72
|
+
'leverage',
|
|
73
|
+
`must be a positive decimal, got '${String(leverage)}'`
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
return leverage
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @public */
|
|
80
|
+
export const safeResolveExactLeverage = createSafeFunction(
|
|
81
|
+
'resolveExactLeverage',
|
|
82
|
+
resolveExactLeverage
|
|
83
|
+
)
|
package/src/math/position.ts
CHANGED
|
@@ -599,9 +599,8 @@ export function isPositionMarginRemovable(position: Position): boolean {
|
|
|
599
599
|
export const isPositiveDecimalString = (value: unknown): value is string =>
|
|
600
600
|
isDecimalString(value) && isDecimalStringGreaterThan(value, '0')
|
|
601
601
|
|
|
602
|
-
/** Complete
|
|
603
|
-
*
|
|
604
|
-
* Rebuild leverage and liquidation distance with half-up rounding to two decimals.
|
|
602
|
+
/** Complete position value, effective leverage, and liquidation distance.
|
|
603
|
+
* Retain explicit ROE; provider plugins calculate it.
|
|
605
604
|
* Unusable inputs omit calculated fields. Never throws or warns.
|
|
606
605
|
* @public
|
|
607
606
|
*/
|
|
@@ -612,15 +611,7 @@ export function buildPosition(position: Position): Position {
|
|
|
612
611
|
...fields
|
|
613
612
|
} = position
|
|
614
613
|
const built: Position = fields
|
|
615
|
-
const {
|
|
616
|
-
size,
|
|
617
|
-
entryPrice,
|
|
618
|
-
leverage,
|
|
619
|
-
markPrice,
|
|
620
|
-
liquidationPrice,
|
|
621
|
-
unrealizedPnl,
|
|
622
|
-
marginUsed,
|
|
623
|
-
} = position
|
|
614
|
+
const { size, markPrice, liquidationPrice, marginUsed } = position
|
|
624
615
|
const hasMarkPrice = isPositiveDecimalString(markPrice)
|
|
625
616
|
const hasMargin = isPositiveDecimalString(marginUsed)
|
|
626
617
|
if (
|
|
@@ -630,20 +621,6 @@ export function buildPosition(position: Position): Position {
|
|
|
630
621
|
) {
|
|
631
622
|
built.positionValue = calculateNotionalValue(size, markPrice)
|
|
632
623
|
}
|
|
633
|
-
if (
|
|
634
|
-
built.returnOnEquity === undefined &&
|
|
635
|
-
isDecimalString(unrealizedPnl) &&
|
|
636
|
-
isPositiveDecimalString(size) &&
|
|
637
|
-
isPositiveDecimalString(entryPrice) &&
|
|
638
|
-
isPositiveDecimalString(leverage)
|
|
639
|
-
) {
|
|
640
|
-
built.returnOnEquity = calculateInitialMarginRoe(
|
|
641
|
-
unrealizedPnl,
|
|
642
|
-
size,
|
|
643
|
-
entryPrice,
|
|
644
|
-
leverage
|
|
645
|
-
)
|
|
646
|
-
}
|
|
647
624
|
if (hasMargin && isDecimalString(built.positionValue)) {
|
|
648
625
|
built.effectiveLeverage = roundDecimalString(
|
|
649
626
|
calculateEffectiveLeverage({
|
|
@@ -667,13 +644,13 @@ export function buildPosition(position: Position): Position {
|
|
|
667
644
|
return built
|
|
668
645
|
}
|
|
669
646
|
|
|
670
|
-
/** Recalculate
|
|
671
|
-
*
|
|
647
|
+
/** Recalculate provider-independent mark fields and clear stale ROE.
|
|
648
|
+
* Effective leverage uses the input snapshot's margin.
|
|
672
649
|
* Invalid marks return the input. Invalid entry prices or sizes retain its PnL.
|
|
673
650
|
* The input is unchanged, and valid marks return a new position.
|
|
674
651
|
* @public
|
|
675
652
|
*/
|
|
676
|
-
export function
|
|
653
|
+
export function remarkPositionValues(
|
|
677
654
|
position: Position,
|
|
678
655
|
markPrice: string
|
|
679
656
|
): Position {
|
package/src/types/provider.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
AccountConfigSetting,
|
|
4
4
|
AccountResponse,
|
|
5
5
|
AccountSummary,
|
|
6
|
+
ActionParamsMap,
|
|
6
7
|
ActionResult,
|
|
7
8
|
ActionStep,
|
|
8
9
|
ActionType,
|
|
@@ -153,9 +154,10 @@ export interface ActionSignerContribution {
|
|
|
153
154
|
*/
|
|
154
155
|
signerAddress?: Address
|
|
155
156
|
/**
|
|
156
|
-
* Extra action params the plugin injects from its own signer state
|
|
157
|
-
* Hyperliquid's `agentAddress` for `APPROVE_AGENT
|
|
158
|
-
* params.
|
|
157
|
+
* Extra action params the plugin injects from its own signer state (e.g.
|
|
158
|
+
* Hyperliquid's `agentAddress` for `APPROVE_AGENT`) or from the caller's
|
|
159
|
+
* params (Lighter's `initialMarginFractionBps` for a leverage action).
|
|
160
|
+
* Merged over the caller's params.
|
|
159
161
|
*/
|
|
160
162
|
params?: Record<string, unknown>
|
|
161
163
|
}
|
|
@@ -506,6 +508,12 @@ export interface PerpsProviderPlugin {
|
|
|
506
508
|
options?: SDKRequestOptions
|
|
507
509
|
): Promise<PositionsResponse>
|
|
508
510
|
|
|
511
|
+
/** Calculate position ROE as a percentage without I/O; omit it for unusable inputs. */
|
|
512
|
+
calculateRoe(position: Position): DecimalString | undefined
|
|
513
|
+
|
|
514
|
+
/** Recalculate mark-dependent position fields under the venue's financial rules, without I/O. */
|
|
515
|
+
remarkPosition(position: Position, markPrice: DecimalString): Position
|
|
516
|
+
|
|
509
517
|
/**
|
|
510
518
|
* The user's current venue-side settings for one perps market — the margin
|
|
511
519
|
* mode and leverage the next order on it will use. Resolves the venue value
|
|
@@ -519,9 +527,16 @@ export interface PerpsProviderPlugin {
|
|
|
519
527
|
options?: SDKRequestOptions
|
|
520
528
|
): Promise<MarketSettings>
|
|
521
529
|
|
|
522
|
-
/** Compare
|
|
523
|
-
*
|
|
524
|
-
|
|
530
|
+
/** Compare a requested leverage with the venue setting, without I/O.
|
|
531
|
+
* `requestedInitialMarginFractionBps` is the exact native source that the
|
|
532
|
+
* request carried, when the caller sent one; a venue that stores an IMF
|
|
533
|
+
* compares it instead of encoding `requested`.
|
|
534
|
+
* Throw `ValidationError` for unusable or unsupported values or missing required source data. */
|
|
535
|
+
isLeverageEqual(
|
|
536
|
+
requested: DecimalString,
|
|
537
|
+
reported: MarketSettings,
|
|
538
|
+
requestedInitialMarginFractionBps?: DecimalString
|
|
539
|
+
): boolean
|
|
525
540
|
|
|
526
541
|
/**
|
|
527
542
|
* The amounts the account can still buy and sell on one market, in that
|
|
@@ -723,15 +738,19 @@ export interface PerpsProviderPlugin {
|
|
|
723
738
|
* `agentAddress` param. The agent address must be known pre-build so the
|
|
724
739
|
* backend builds the right typed data, so core calls this before
|
|
725
740
|
* `createAction` and threads the same result into `executeAction`.
|
|
741
|
+
* The caller's `params` are forwarded so a plugin can contribute a venue
|
|
742
|
+
* encoding of a caller value (Lighter's `initialMarginFractionBps`). A setup
|
|
743
|
+
* step runs with the backend-staged params and forwards none.
|
|
726
744
|
*
|
|
727
745
|
* Optional: providers that sign as the user or with a non-EVM session
|
|
728
|
-
* credential
|
|
729
|
-
* and no injected params.
|
|
746
|
+
* credential and contribute no params omit it — core then sends no
|
|
747
|
+
* `signerAddress` and no injected params.
|
|
730
748
|
*/
|
|
731
749
|
resolveActionRequest?(
|
|
732
750
|
action: ActionType,
|
|
733
751
|
address: Address,
|
|
734
|
-
signer: PerpsSigner
|
|
752
|
+
signer: PerpsSigner,
|
|
753
|
+
params?: ActionParamsMap[ActionType]
|
|
735
754
|
): Promise<ActionSignerContribution>
|
|
736
755
|
|
|
737
756
|
/**
|
package/src/version.ts
CHANGED
package/src/wire/orderAmounts.ts
CHANGED
|
@@ -9,27 +9,47 @@ import { isDecimalStringZero } from '../decimal/compare.js'
|
|
|
9
9
|
import { isDecimalString } from '../decimal/parse.js'
|
|
10
10
|
import { invalidInput } from '../errors/invalidInput.js'
|
|
11
11
|
import { PerpsError } from '../errors/PerpsError.js'
|
|
12
|
+
import {
|
|
13
|
+
BASIS_POINTS_PER_UNIT,
|
|
14
|
+
validateInitialMarginFractionBps,
|
|
15
|
+
} from '../math/leverage.js'
|
|
12
16
|
import type { PerpsSDKClient } from '../types/provider.js'
|
|
13
17
|
import { createSafeFunction } from '../utils/createSafeFunction.js'
|
|
14
18
|
import { snapOrderSize } from './snap.js'
|
|
15
19
|
|
|
16
20
|
/** @public */
|
|
17
|
-
export interface
|
|
21
|
+
export interface OrderAmountsInputBase {
|
|
18
22
|
sdk: PerpsSDKClient
|
|
19
23
|
market: Market
|
|
20
24
|
/** The field the user typed; the other two are calculated from it. */
|
|
21
25
|
held: 'margin' | 'size' | 'notional'
|
|
22
26
|
amount: DecimalString
|
|
23
|
-
leverage
|
|
27
|
+
/** The leverage multiple. Not read when `initialMarginFractionBps` is present. */
|
|
28
|
+
leverage?: number
|
|
29
|
+
/**
|
|
30
|
+
* The venue's native integer IMF in basis points (`'700'` is 7.00%), the
|
|
31
|
+
* exact margin rate of a venue that stores one. Pass the reported
|
|
32
|
+
* `MarketSettings.initialMarginFractionBps`; `leverage` then stays unread.
|
|
33
|
+
*/
|
|
34
|
+
initialMarginFractionBps?: DecimalString
|
|
24
35
|
price: DecimalString
|
|
25
36
|
}
|
|
26
37
|
|
|
38
|
+
/**
|
|
39
|
+
* The order-entry input with at least one margin-rate source: the leverage
|
|
40
|
+
* multiple, the native IMF, or both.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export type OrderAmountsInput = OrderAmountsInputBase &
|
|
45
|
+
({ leverage: number } | { initialMarginFractionBps: DecimalString })
|
|
46
|
+
|
|
27
47
|
/** @public */
|
|
28
48
|
export interface OrderAmounts {
|
|
29
49
|
/**
|
|
30
50
|
* The held `amount`, byte-identical, when the user holds the margin;
|
|
31
|
-
* otherwise `notional ÷ leverage
|
|
32
|
-
* never reaches a venue.
|
|
51
|
+
* otherwise `notional ÷ leverage` or `notional × IMF ÷ 10000`, a display
|
|
52
|
+
* and comparison value that never reaches a venue.
|
|
33
53
|
*/
|
|
34
54
|
margin: DecimalString
|
|
35
55
|
/** Snapped onto the venue lot grid; the one amount an order sends. */
|
|
@@ -52,37 +72,68 @@ function requirePositiveDecimal(value: DecimalString, parameter: string): Big {
|
|
|
52
72
|
const lotSize = (market: Market): DecimalString =>
|
|
53
73
|
market.sizeIncrement ?? new TruncBig(`1e-${market.szDecimals}`).toFixed()
|
|
54
74
|
|
|
75
|
+
/**
|
|
76
|
+
* The margin rate as `[numerator, denominator]`, so each amount divides one
|
|
77
|
+
* time: the native IMF over its scale, else one over the leverage.
|
|
78
|
+
*
|
|
79
|
+
* @throws {PerpsError} `ValidationError` as {@link calculateOrderAmounts}.
|
|
80
|
+
*/
|
|
81
|
+
function resolveMarginRate(
|
|
82
|
+
input: OrderAmountsInput
|
|
83
|
+
): [Big | number, Big | number] {
|
|
84
|
+
if (input.initialMarginFractionBps !== undefined) {
|
|
85
|
+
return [
|
|
86
|
+
new TruncBig(
|
|
87
|
+
validateInitialMarginFractionBps(input.initialMarginFractionBps)
|
|
88
|
+
),
|
|
89
|
+
new TruncBig(BASIS_POINTS_PER_UNIT),
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
const { leverage } = input
|
|
93
|
+
if (
|
|
94
|
+
typeof leverage !== 'number' ||
|
|
95
|
+
!Number.isFinite(leverage) ||
|
|
96
|
+
leverage <= 0
|
|
97
|
+
) {
|
|
98
|
+
throw invalidInput(
|
|
99
|
+
'leverage',
|
|
100
|
+
`must be a finite number above zero, got ${leverage}`
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
return [1, leverage]
|
|
104
|
+
}
|
|
105
|
+
|
|
55
106
|
/**
|
|
56
107
|
* The three order-entry amounts, derived from whichever one the user typed.
|
|
57
108
|
* Only `size` snaps, onto the market's venue lot grid, because an order sends
|
|
58
109
|
* size alone; no quote amount has a grid. A held margin returns byte-identical,
|
|
59
110
|
* `notional` is the snapped size × price, and a derived margin is
|
|
60
|
-
* `notional ÷ leverage
|
|
111
|
+
* `notional ÷ leverage`, or `notional × initialMarginFractionBps ÷ 10000`
|
|
112
|
+
* from a native IMF, in `DivBig` (40 places, half-up).
|
|
61
113
|
*
|
|
62
114
|
* @throws {PerpsError} `ValidationError` that names `amount` or `price` when
|
|
63
|
-
* it is not a positive decimal,
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
115
|
+
* it is not a positive decimal, `initialMarginFractionBps` when it is
|
|
116
|
+
* present and not a positive integer, or `leverage` when no IMF is present
|
|
117
|
+
* and it is not a finite number above zero. `ValidationError` with the lot
|
|
118
|
+
* size when the snapped size is zero (below one lot). `SDKError` when
|
|
119
|
+
* `market.providerId` names no registered provider plugin.
|
|
67
120
|
* @public
|
|
68
121
|
*/
|
|
69
122
|
export function calculateOrderAmounts(input: OrderAmountsInput): OrderAmounts {
|
|
70
|
-
const { sdk, market, held
|
|
123
|
+
const { sdk, market, held } = input
|
|
71
124
|
const amount = requirePositiveDecimal(input.amount, 'amount')
|
|
72
125
|
const price = requirePositiveDecimal(input.price, 'price')
|
|
73
|
-
|
|
74
|
-
throw invalidInput(
|
|
75
|
-
'leverage',
|
|
76
|
-
`must be a finite number above zero, got ${leverage}`
|
|
77
|
-
)
|
|
78
|
-
}
|
|
126
|
+
const [rateNumerator, rateDenominator] = resolveMarginRate(input)
|
|
79
127
|
|
|
80
128
|
const requested =
|
|
81
129
|
held === 'size'
|
|
82
130
|
? input.amount
|
|
83
131
|
: held === 'notional'
|
|
84
132
|
? amount.div(price).toFixed()
|
|
85
|
-
: amount
|
|
133
|
+
: amount
|
|
134
|
+
.times(rateDenominator)
|
|
135
|
+
.div(price.times(rateNumerator))
|
|
136
|
+
.toFixed()
|
|
86
137
|
const size = snapOrderSize(sdk, market, requested)
|
|
87
138
|
if (isDecimalStringZero(size)) {
|
|
88
139
|
throw new PerpsError(
|
|
@@ -96,7 +147,10 @@ export function calculateOrderAmounts(input: OrderAmountsInput): OrderAmounts {
|
|
|
96
147
|
margin:
|
|
97
148
|
held === 'margin'
|
|
98
149
|
? input.amount
|
|
99
|
-
: new DivBig(notional)
|
|
150
|
+
: new DivBig(notional)
|
|
151
|
+
.times(rateNumerator)
|
|
152
|
+
.div(rateDenominator)
|
|
153
|
+
.toFixed(),
|
|
100
154
|
size,
|
|
101
155
|
notional: notional.toFixed(),
|
|
102
156
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import type { PerpsSDKClient } from '../types/provider.js';
|
|
3
|
-
export declare function isLeverageEqual(sdk: PerpsSDKClient, provider: string, requested: DecimalString, reported: DecimalString): boolean;
|
|
4
|
-
export declare const safeIsLeverageEqual: (sdk: PerpsSDKClient, provider: string, requested: string, reported: string) => boolean | undefined;
|
|
5
|
-
//# sourceMappingURL=leverage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"leverage.d.ts","sourceRoot":"","sources":["../../../src/utils/leverage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAItD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAQ1D,wBAAgB,eAAe,CAC7B,GAAG,EAAE,cAAc,EACnB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,aAAa,EACxB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAST;AAGD,eAAO,MAAM,mBAAmB,qGAG/B,CAAA"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safeIsLeverageEqual = void 0;
|
|
4
|
-
exports.isLeverageEqual = isLeverageEqual;
|
|
5
|
-
const requireProvider_js_1 = require("../client/requireProvider.js");
|
|
6
|
-
const compare_js_1 = require("../decimal/compare.js");
|
|
7
|
-
const invalidInput_js_1 = require("../errors/invalidInput.js");
|
|
8
|
-
const createSafeFunction_js_1 = require("../utils/createSafeFunction.js");
|
|
9
|
-
function isLeverageEqual(sdk, provider, requested, reported) {
|
|
10
|
-
const plugin = (0, requireProvider_js_1.requireProvider)(sdk, provider);
|
|
11
|
-
if (!(0, compare_js_1.isDecimalStringGreaterThan)(requested, '0')) {
|
|
12
|
-
throw (0, invalidInput_js_1.invalidInput)('requested', 'leverage must be above zero');
|
|
13
|
-
}
|
|
14
|
-
if (!(0, compare_js_1.isDecimalStringGreaterThan)(reported, '0')) {
|
|
15
|
-
throw (0, invalidInput_js_1.invalidInput)('reported', 'leverage must be above zero');
|
|
16
|
-
}
|
|
17
|
-
return plugin.isLeverageEqual(requested, reported);
|
|
18
|
-
}
|
|
19
|
-
exports.safeIsLeverageEqual = (0, createSafeFunction_js_1.createSafeFunction)('isLeverageEqual', isLeverageEqual);
|
|
20
|
-
//# sourceMappingURL=leverage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"leverage.js","sourceRoot":"","sources":["../../../src/utils/leverage.ts"],"names":[],"mappings":";;;AAYA,0CAcC;AAzBD,qEAA8D;AAC9D,sDAAkE;AAClE,+DAAwD;AAExD,0EAAmE;AAOnE,SAAgB,eAAe,CAC7B,GAAmB,EACnB,QAAgB,EAChB,SAAwB,EACxB,QAAuB;IAEvB,MAAM,MAAM,GAAG,IAAA,oCAAe,EAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IAC7C,IAAI,CAAC,IAAA,uCAA0B,EAAC,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC;QAChD,MAAM,IAAA,8BAAY,EAAC,WAAW,EAAE,6BAA6B,CAAC,CAAA;IAChE,CAAC;IACD,IAAI,CAAC,IAAA,uCAA0B,EAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,IAAA,8BAAY,EAAC,UAAU,EAAE,6BAA6B,CAAC,CAAA;IAC/D,CAAC;IACD,OAAO,MAAM,CAAC,eAAe,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AACpD,CAAC;AAGY,QAAA,mBAAmB,GAAG,IAAA,0CAAkB,EACnD,iBAAiB,EACjB,eAAe,CAChB,CAAA"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import type { PerpsSDKClient } from '../types/provider.js';
|
|
3
|
-
/**
|
|
4
|
-
* Compare leverage under the registered provider's storage rules.
|
|
5
|
-
* @throws {PerpsError} `ValidationError` for invalid, non-positive, or provider-unsupported leverage; `SDKError` for an unregistered provider.
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export declare function isLeverageEqual(sdk: PerpsSDKClient, provider: string, requested: DecimalString, reported: DecimalString): boolean;
|
|
9
|
-
/** Return no comparison and log a warning when {@link isLeverageEqual} throws. @public */
|
|
10
|
-
export declare const safeIsLeverageEqual: (sdk: PerpsSDKClient, provider: string, requested: string, reported: string) => boolean | undefined;
|
|
11
|
-
//# sourceMappingURL=leverage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"leverage.d.ts","sourceRoot":"","sources":["../../../src/utils/leverage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAItD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG1D;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,cAAc,EACnB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,aAAa,EACxB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAST;AAED,0FAA0F;AAC1F,eAAO,MAAM,mBAAmB,qGAG/B,CAAA"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { requireProvider } from '../client/requireProvider.js';
|
|
2
|
-
import { isDecimalStringGreaterThan } from '../decimal/compare.js';
|
|
3
|
-
import { invalidInput } from '../errors/invalidInput.js';
|
|
4
|
-
import { createSafeFunction } from '../utils/createSafeFunction.js';
|
|
5
|
-
/**
|
|
6
|
-
* Compare leverage under the registered provider's storage rules.
|
|
7
|
-
* @throws {PerpsError} `ValidationError` for invalid, non-positive, or provider-unsupported leverage; `SDKError` for an unregistered provider.
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export function isLeverageEqual(sdk, provider, requested, reported) {
|
|
11
|
-
const plugin = requireProvider(sdk, provider);
|
|
12
|
-
if (!isDecimalStringGreaterThan(requested, '0')) {
|
|
13
|
-
throw invalidInput('requested', 'leverage must be above zero');
|
|
14
|
-
}
|
|
15
|
-
if (!isDecimalStringGreaterThan(reported, '0')) {
|
|
16
|
-
throw invalidInput('reported', 'leverage must be above zero');
|
|
17
|
-
}
|
|
18
|
-
return plugin.isLeverageEqual(requested, reported);
|
|
19
|
-
}
|
|
20
|
-
/** Return no comparison and log a warning when {@link isLeverageEqual} throws. @public */
|
|
21
|
-
export const safeIsLeverageEqual = createSafeFunction('isLeverageEqual', isLeverageEqual);
|
|
22
|
-
//# sourceMappingURL=leverage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"leverage.js","sourceRoot":"","sources":["../../../src/utils/leverage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAA;AAC9D,OAAO,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAA;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAEnE;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,GAAmB,EACnB,QAAgB,EAChB,SAAwB,EACxB,QAAuB;IAEvB,MAAM,MAAM,GAAG,eAAe,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IAC7C,IAAI,CAAC,0BAA0B,CAAC,SAAS,EAAE,GAAG,CAAC,EAAE,CAAC;QAChD,MAAM,YAAY,CAAC,WAAW,EAAE,6BAA6B,CAAC,CAAA;IAChE,CAAC;IACD,IAAI,CAAC,0BAA0B,CAAC,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,YAAY,CAAC,UAAU,EAAE,6BAA6B,CAAC,CAAA;IAC/D,CAAC;IACD,OAAO,MAAM,CAAC,eAAe,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AACpD,CAAC;AAED,0FAA0F;AAC1F,MAAM,CAAC,MAAM,mBAAmB,GAAG,kBAAkB,CACnD,iBAAiB,EACjB,eAAe,CAChB,CAAA"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types';
|
|
2
|
-
import type { PerpsSDKClient } from '../types/provider.js';
|
|
3
|
-
/**
|
|
4
|
-
* Compare leverage under the registered provider's storage rules.
|
|
5
|
-
* @throws {PerpsError} `ValidationError` for invalid, non-positive, or provider-unsupported leverage; `SDKError` for an unregistered provider.
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export declare function isLeverageEqual(sdk: PerpsSDKClient, provider: string, requested: DecimalString, reported: DecimalString): boolean;
|
|
9
|
-
/** Return no comparison and log a warning when {@link isLeverageEqual} throws. @public */
|
|
10
|
-
export declare const safeIsLeverageEqual: (sdk: PerpsSDKClient, provider: string, requested: string, reported: string) => boolean | undefined;
|
|
11
|
-
//# sourceMappingURL=leverage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"leverage.d.ts","sourceRoot":"","sources":["../../../src/utils/leverage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAItD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAG1D;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,cAAc,EACnB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,aAAa,EACxB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAST;AAED,0FAA0F;AAC1F,eAAO,MAAM,mBAAmB,qGAG/B,CAAA"}
|
package/src/utils/leverage.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { DecimalString } from '@lifi/perps-types'
|
|
2
|
-
import { requireProvider } from '../client/requireProvider.js'
|
|
3
|
-
import { isDecimalStringGreaterThan } from '../decimal/compare.js'
|
|
4
|
-
import { invalidInput } from '../errors/invalidInput.js'
|
|
5
|
-
import type { PerpsSDKClient } from '../types/provider.js'
|
|
6
|
-
import { createSafeFunction } from '../utils/createSafeFunction.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Compare leverage under the registered provider's storage rules.
|
|
10
|
-
* @throws {PerpsError} `ValidationError` for invalid, non-positive, or provider-unsupported leverage; `SDKError` for an unregistered provider.
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export function isLeverageEqual(
|
|
14
|
-
sdk: PerpsSDKClient,
|
|
15
|
-
provider: string,
|
|
16
|
-
requested: DecimalString,
|
|
17
|
-
reported: DecimalString
|
|
18
|
-
): boolean {
|
|
19
|
-
const plugin = requireProvider(sdk, provider)
|
|
20
|
-
if (!isDecimalStringGreaterThan(requested, '0')) {
|
|
21
|
-
throw invalidInput('requested', 'leverage must be above zero')
|
|
22
|
-
}
|
|
23
|
-
if (!isDecimalStringGreaterThan(reported, '0')) {
|
|
24
|
-
throw invalidInput('reported', 'leverage must be above zero')
|
|
25
|
-
}
|
|
26
|
-
return plugin.isLeverageEqual(requested, reported)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Return no comparison and log a warning when {@link isLeverageEqual} throws. @public */
|
|
30
|
-
export const safeIsLeverageEqual = createSafeFunction(
|
|
31
|
-
'isLeverageEqual',
|
|
32
|
-
isLeverageEqual
|
|
33
|
-
)
|