@lifi/perps-sdk-provider-lighter 47.2.0 → 47.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 +33 -0
- package/dist/cjs/signers/LighterSigner.d.ts.map +1 -1
- package/dist/cjs/signers/LighterSigner.js +48 -22
- package/dist/cjs/signers/LighterSigner.js.map +1 -1
- package/dist/cjs/signers/signActions.d.ts.map +1 -1
- package/dist/cjs/signers/signActions.js +11 -3
- package/dist/cjs/signers/signActions.js.map +1 -1
- package/dist/cjs/signers/wasmLoader.d.ts +1 -0
- package/dist/cjs/signers/wasmLoader.d.ts.map +1 -1
- package/dist/cjs/signers/wasmLoader.js +1 -0
- package/dist/cjs/signers/wasmLoader.js.map +1 -1
- package/dist/cjs/types/action.d.ts +19 -4
- package/dist/cjs/types/action.d.ts.map +1 -1
- package/dist/cjs/types/action.js +20 -1
- package/dist/cjs/types/action.js.map +1 -1
- package/dist/cjs/types/fill.d.ts +2 -0
- package/dist/cjs/types/fill.d.ts.map +1 -1
- package/dist/cjs/utils/apiClient.d.ts.map +1 -1
- package/dist/cjs/utils/apiClient.js +10 -2
- package/dist/cjs/utils/apiClient.js.map +1 -1
- package/dist/cjs/utils/mapFill.d.ts.map +1 -1
- package/dist/cjs/utils/mapFill.js +29 -21
- package/dist/cjs/utils/mapFill.js.map +1 -1
- package/dist/cjs/websocket/LighterWsProvider.d.ts +1 -0
- package/dist/cjs/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/LighterWsProvider.js +3 -0
- package/dist/cjs/websocket/LighterWsProvider.js.map +1 -1
- package/dist/esm/signers/LighterSigner.d.ts.map +1 -1
- package/dist/esm/signers/LighterSigner.js +49 -23
- package/dist/esm/signers/LighterSigner.js.map +1 -1
- package/dist/esm/signers/signActions.d.ts.map +1 -1
- package/dist/esm/signers/signActions.js +14 -4
- package/dist/esm/signers/signActions.js.map +1 -1
- package/dist/esm/signers/wasmLoader.d.ts +6 -0
- package/dist/esm/signers/wasmLoader.d.ts.map +1 -1
- package/dist/esm/signers/wasmLoader.js +1 -0
- package/dist/esm/signers/wasmLoader.js.map +1 -1
- package/dist/esm/types/action.d.ts +68 -5
- package/dist/esm/types/action.d.ts.map +1 -1
- package/dist/esm/types/action.js +48 -1
- package/dist/esm/types/action.js.map +1 -1
- package/dist/esm/types/fill.d.ts +2 -0
- package/dist/esm/types/fill.d.ts.map +1 -1
- package/dist/esm/utils/apiClient.d.ts.map +1 -1
- package/dist/esm/utils/apiClient.js +12 -2
- package/dist/esm/utils/apiClient.js.map +1 -1
- package/dist/esm/utils/mapFill.d.ts.map +1 -1
- package/dist/esm/utils/mapFill.js +43 -29
- package/dist/esm/utils/mapFill.js.map +1 -1
- package/dist/esm/websocket/LighterWsProvider.d.ts +1 -0
- package/dist/esm/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/LighterWsProvider.js +3 -0
- package/dist/esm/websocket/LighterWsProvider.js.map +1 -1
- package/dist/types/signers/LighterSigner.d.ts.map +1 -1
- package/dist/types/signers/signActions.d.ts.map +1 -1
- package/dist/types/signers/wasmLoader.d.ts +6 -0
- package/dist/types/signers/wasmLoader.d.ts.map +1 -1
- package/dist/types/types/action.d.ts +68 -5
- package/dist/types/types/action.d.ts.map +1 -1
- package/dist/types/types/fill.d.ts +2 -0
- package/dist/types/types/fill.d.ts.map +1 -1
- package/dist/types/utils/apiClient.d.ts.map +1 -1
- package/dist/types/utils/mapFill.d.ts.map +1 -1
- package/dist/types/websocket/LighterWsProvider.d.ts +1 -0
- package/dist/types/websocket/LighterWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/signers/LighterSigner.ts +104 -46
- package/src/signers/signActions.ts +16 -6
- package/src/signers/wasmLoader.ts +12 -0
- package/src/types/action.ts +95 -6
- package/src/types/fill.ts +5 -0
- package/src/utils/apiClient.ts +12 -2
- package/src/utils/mapFill.ts +79 -63
- package/src/websocket/LighterWsProvider.ts +7 -0
|
@@ -5,9 +5,13 @@ import {
|
|
|
5
5
|
} from '@lifi/perps-sdk'
|
|
6
6
|
import { ActionType, PerpsErrorCode } from '@lifi/perps-types'
|
|
7
7
|
import {
|
|
8
|
+
isLtGroupedOrderStep,
|
|
8
9
|
LT_ROUTE_PERP,
|
|
9
10
|
LT_ROUTE_SPOT,
|
|
10
|
-
|
|
11
|
+
LT_TX_TYPE_APPROVE_INTEGRATOR,
|
|
12
|
+
LT_TX_TYPE_CHANGE_PUB_KEY,
|
|
13
|
+
LT_TX_TYPE_TRANSFER,
|
|
14
|
+
resolveLtTxType,
|
|
11
15
|
} from '../types/action.js'
|
|
12
16
|
import { assetMarginModeInt } from '../utils/assetCollateral.js'
|
|
13
17
|
import {
|
|
@@ -214,6 +218,7 @@ export class LighterSigner {
|
|
|
214
218
|
): Promise<LighterSignedBlob> {
|
|
215
219
|
return this.signWith(
|
|
216
220
|
ActionType.REGISTER_API_KEY,
|
|
221
|
+
LT_TX_TYPE_CHANGE_PUB_KEY,
|
|
217
222
|
context,
|
|
218
223
|
skipNonce,
|
|
219
224
|
(wasm) =>
|
|
@@ -279,25 +284,35 @@ export class LighterSigner {
|
|
|
279
284
|
const nonce = numberField(p, 'nonce')
|
|
280
285
|
if (action === ActionType.TRANSFER) {
|
|
281
286
|
const args = this.transferArgs(p)
|
|
282
|
-
return this.signWith(
|
|
283
|
-
|
|
287
|
+
return this.signWith(
|
|
288
|
+
action,
|
|
289
|
+
LT_TX_TYPE_TRANSFER,
|
|
290
|
+
context,
|
|
291
|
+
false,
|
|
292
|
+
(wasm) =>
|
|
293
|
+
wasm._signTransfer(
|
|
294
|
+
context.accountIndex,
|
|
295
|
+
l1Signature,
|
|
296
|
+
nonce,
|
|
297
|
+
context.apiKeyIndex,
|
|
298
|
+
...args
|
|
299
|
+
)
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
const args = approveIntegratorArgs(p)
|
|
303
|
+
return this.signWith(
|
|
304
|
+
action,
|
|
305
|
+
LT_TX_TYPE_APPROVE_INTEGRATOR,
|
|
306
|
+
context,
|
|
307
|
+
false,
|
|
308
|
+
(wasm) =>
|
|
309
|
+
wasm._signApproveIntegrator(
|
|
284
310
|
context.accountIndex,
|
|
285
311
|
l1Signature,
|
|
286
312
|
nonce,
|
|
287
313
|
context.apiKeyIndex,
|
|
288
314
|
...args
|
|
289
315
|
)
|
|
290
|
-
)
|
|
291
|
-
}
|
|
292
|
-
const args = approveIntegratorArgs(p)
|
|
293
|
-
return this.signWith(action, context, false, (wasm) =>
|
|
294
|
-
wasm._signApproveIntegrator(
|
|
295
|
-
context.accountIndex,
|
|
296
|
-
l1Signature,
|
|
297
|
-
nonce,
|
|
298
|
-
context.apiKeyIndex,
|
|
299
|
-
...args
|
|
300
|
-
)
|
|
301
316
|
)
|
|
302
317
|
}
|
|
303
318
|
|
|
@@ -325,6 +340,7 @@ export class LighterSigner {
|
|
|
325
340
|
}
|
|
326
341
|
return this.signWith(
|
|
327
342
|
action,
|
|
343
|
+
resolveLtTxType(action, wasmSignParams),
|
|
328
344
|
context,
|
|
329
345
|
false,
|
|
330
346
|
this.dispatch(action, wasmSignParams, context)
|
|
@@ -368,17 +384,11 @@ export class LighterSigner {
|
|
|
368
384
|
|
|
369
385
|
private async signWith(
|
|
370
386
|
action: ActionType,
|
|
387
|
+
txType: number,
|
|
371
388
|
context: LighterSignerContext,
|
|
372
389
|
skipNonce: boolean,
|
|
373
390
|
call: LighterWasmCall<LighterWasmTx>
|
|
374
391
|
): Promise<LighterSignedBlob> {
|
|
375
|
-
const txType = LT_TX_TYPE_BY_ACTION[action]
|
|
376
|
-
if (txType === undefined) {
|
|
377
|
-
throw new PerpsError(
|
|
378
|
-
PerpsErrorCode.ValidationError,
|
|
379
|
-
`Lighter WASM signer does not support action: ${action}`
|
|
380
|
-
)
|
|
381
|
-
}
|
|
382
392
|
const wasm = await this.ensureLoaded()
|
|
383
393
|
return this.exclusive(wasm, async () => {
|
|
384
394
|
await this.ensureClient(wasm, context, skipNonce)
|
|
@@ -442,34 +452,25 @@ export class LighterSigner {
|
|
|
442
452
|
case ActionType.PLACE_ORDER:
|
|
443
453
|
case ActionType.PLACE_TRIGGER_ORDER:
|
|
444
454
|
case ActionType.PLACE_TWAP_ORDER: {
|
|
455
|
+
if (isLtGroupedOrderStep(action, p)) {
|
|
456
|
+
const orders = orderListField(p, 'orders')
|
|
457
|
+
const args: Parameters<
|
|
458
|
+
LighterWasmExports['_signCreateGroupedOrders']
|
|
459
|
+
> = [
|
|
460
|
+
ctx.accountIndex,
|
|
461
|
+
numberField(p, 'grouping_type'),
|
|
462
|
+
orders.length,
|
|
463
|
+
...orders.flatMap(createOrderArgs),
|
|
464
|
+
nonce,
|
|
465
|
+
...integratorArgs(p),
|
|
466
|
+
]
|
|
467
|
+
return (wasm) => wasm._signCreateGroupedOrders(...args)
|
|
468
|
+
}
|
|
445
469
|
const args: Parameters<LighterWasmExports['_signCreateOrder']> = [
|
|
446
470
|
ctx.accountIndex,
|
|
447
|
-
|
|
448
|
-
numberField(p, 'client_order_index'),
|
|
449
|
-
int64StringField(p, 'base_amount'),
|
|
450
|
-
int64StringField(p, 'price'),
|
|
451
|
-
numberField(p, 'is_ask'),
|
|
452
|
-
numberField(p, 'order_type'),
|
|
453
|
-
numberField(p, 'time_in_force'),
|
|
454
|
-
numberField(p, 'reduce_only'),
|
|
455
|
-
int64StringField(p, 'trigger_price'),
|
|
456
|
-
numberField(p, 'order_expiry'),
|
|
471
|
+
...createOrderArgs(p),
|
|
457
472
|
nonce,
|
|
458
|
-
|
|
459
|
-
p,
|
|
460
|
-
'integrator_account_index',
|
|
461
|
-
NIL_INTEGRATOR_INDEX
|
|
462
|
-
),
|
|
463
|
-
optionalNumberField(
|
|
464
|
-
p,
|
|
465
|
-
'integrator_taker_fee',
|
|
466
|
-
NIL_INTEGRATOR_TAKER_FEE
|
|
467
|
-
),
|
|
468
|
-
optionalNumberField(
|
|
469
|
-
p,
|
|
470
|
-
'integrator_maker_fee',
|
|
471
|
-
NIL_INTEGRATOR_MAKER_FEE
|
|
472
|
-
),
|
|
473
|
+
...integratorArgs(p),
|
|
473
474
|
]
|
|
474
475
|
return (wasm) => wasm._signCreateOrder(...args)
|
|
475
476
|
}
|
|
@@ -649,6 +650,63 @@ function approveIntegratorArgs(
|
|
|
649
650
|
]
|
|
650
651
|
}
|
|
651
652
|
|
|
653
|
+
/** The ten values `_signCreateOrder` and `_signCreateGroupedOrders` read for one order. */
|
|
654
|
+
type CreateOrderArgs = [
|
|
655
|
+
marketIndex: number,
|
|
656
|
+
clientOrderIndex: number,
|
|
657
|
+
baseAmount: string,
|
|
658
|
+
price: string,
|
|
659
|
+
isAsk: number,
|
|
660
|
+
orderType: number,
|
|
661
|
+
timeInForce: number,
|
|
662
|
+
reduceOnly: number,
|
|
663
|
+
triggerPrice: string,
|
|
664
|
+
orderExpiry: number,
|
|
665
|
+
]
|
|
666
|
+
|
|
667
|
+
function createOrderArgs(p: Record<string, unknown>): CreateOrderArgs {
|
|
668
|
+
return [
|
|
669
|
+
numberField(p, 'market_index'),
|
|
670
|
+
numberField(p, 'client_order_index'),
|
|
671
|
+
int64StringField(p, 'base_amount'),
|
|
672
|
+
int64StringField(p, 'price'),
|
|
673
|
+
numberField(p, 'is_ask'),
|
|
674
|
+
numberField(p, 'order_type'),
|
|
675
|
+
numberField(p, 'time_in_force'),
|
|
676
|
+
numberField(p, 'reduce_only'),
|
|
677
|
+
int64StringField(p, 'trigger_price'),
|
|
678
|
+
numberField(p, 'order_expiry'),
|
|
679
|
+
]
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function integratorArgs(p: Record<string, unknown>): [number, number, number] {
|
|
683
|
+
return [
|
|
684
|
+
optionalNumberField(p, 'integrator_account_index', NIL_INTEGRATOR_INDEX),
|
|
685
|
+
optionalNumberField(p, 'integrator_taker_fee', NIL_INTEGRATOR_TAKER_FEE),
|
|
686
|
+
optionalNumberField(p, 'integrator_maker_fee', NIL_INTEGRATOR_MAKER_FEE),
|
|
687
|
+
]
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function orderListField(
|
|
691
|
+
p: Record<string, unknown>,
|
|
692
|
+
key: string
|
|
693
|
+
): Record<string, unknown>[] {
|
|
694
|
+
const v = p[key]
|
|
695
|
+
if (
|
|
696
|
+
Array.isArray(v) &&
|
|
697
|
+
v.every(
|
|
698
|
+
(order): order is Record<string, unknown> =>
|
|
699
|
+
typeof order === 'object' && order !== null
|
|
700
|
+
)
|
|
701
|
+
) {
|
|
702
|
+
return v
|
|
703
|
+
}
|
|
704
|
+
throw new PerpsError(
|
|
705
|
+
PerpsErrorCode.ValidationError,
|
|
706
|
+
`Lighter sign params field '${key}' is not a list of order objects`
|
|
707
|
+
)
|
|
708
|
+
}
|
|
709
|
+
|
|
652
710
|
function numberField(p: Record<string, unknown>, key: string): number {
|
|
653
711
|
const v = p[key]
|
|
654
712
|
if (typeof v === 'number') {
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
revertedFailedErrorText,
|
|
31
31
|
unconfirmedIndeterminateErrorText,
|
|
32
32
|
} from '../copy.js'
|
|
33
|
+
import { isLtGroupedOrderStep } from '../types/action.js'
|
|
33
34
|
import type { ApiParams, LighterApiClient } from '../utils/apiClient.js'
|
|
34
35
|
import { fetchAppliedReferralCode } from '../utils/appliedReferralCode.js'
|
|
35
36
|
import { lighterErrorCodeFromBody } from '../utils/lighterErrorCode.js'
|
|
@@ -252,7 +253,7 @@ async function signStandardWasmAction(
|
|
|
252
253
|
apiKeyIndex: apiKey.apiKeyIndex,
|
|
253
254
|
accountIndex: apiKey.accountIndex,
|
|
254
255
|
})
|
|
255
|
-
const clientOrderIndex = clientOrderIndexOf(step
|
|
256
|
+
const clientOrderIndex = clientOrderIndexOf(step)
|
|
256
257
|
return {
|
|
257
258
|
action: step.action,
|
|
258
259
|
wasmSignParams: step.wasmSignParams,
|
|
@@ -265,12 +266,21 @@ async function signStandardWasmAction(
|
|
|
265
266
|
* Project the signed order's `client_order_index` so the caller can resolve it
|
|
266
267
|
* through `getOrder` before the venue assigns an `order_index`. Only the
|
|
267
268
|
* order-placing actions carry one, and the value is read back off the params
|
|
268
|
-
* that were signed rather than generated a second time.
|
|
269
|
+
* that were signed rather than generated a second time. A grouped step
|
|
270
|
+
* projects the first of its `orders`: the parent of an OTO or OTOCO group, or
|
|
271
|
+
* the first leg of an OCO pair, which has no parent.
|
|
269
272
|
*/
|
|
270
|
-
function clientOrderIndexOf(
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
273
|
+
function clientOrderIndexOf(step: WasmBlobActionStep): string | undefined {
|
|
274
|
+
const { orders } = step.wasmSignParams
|
|
275
|
+
const order: unknown =
|
|
276
|
+
isLtGroupedOrderStep(step.action, step.wasmSignParams) &&
|
|
277
|
+
Array.isArray(orders)
|
|
278
|
+
? orders[0]
|
|
279
|
+
: step.wasmSignParams
|
|
280
|
+
const value =
|
|
281
|
+
typeof order === 'object' && order !== null && 'client_order_index' in order
|
|
282
|
+
? order.client_order_index
|
|
283
|
+
: undefined
|
|
274
284
|
return typeof value === 'number' && Number.isInteger(value)
|
|
275
285
|
? String(value)
|
|
276
286
|
: undefined
|
|
@@ -129,6 +129,17 @@ export interface LighterWasmExports {
|
|
|
129
129
|
integratorTakerFee: number,
|
|
130
130
|
integratorMakerFee: number
|
|
131
131
|
) => LighterWasmPending<LighterWasmTx>
|
|
132
|
+
/**
|
|
133
|
+
* `orderValues` holds the ten `_signCreateOrder` values from `marketIndex`
|
|
134
|
+
* through `orderExpiry` for each of `orderCount` orders, parent first, then
|
|
135
|
+
* the nonce and the three integrator values.
|
|
136
|
+
*/
|
|
137
|
+
_signCreateGroupedOrders: (
|
|
138
|
+
accountIndex: number,
|
|
139
|
+
groupingType: number,
|
|
140
|
+
orderCount: number,
|
|
141
|
+
...orderValues: (number | string)[]
|
|
142
|
+
) => LighterWasmPending<LighterWasmTx>
|
|
132
143
|
_signCancelOrder: (
|
|
133
144
|
accountIndex: number,
|
|
134
145
|
marketIndex: number,
|
|
@@ -258,6 +269,7 @@ const WASM_FUNCTION_NAMES = [
|
|
|
258
269
|
'_signApproveIntegrator',
|
|
259
270
|
'_signUpdateAccountConfig',
|
|
260
271
|
'_signUpdateAccountAssetConfig',
|
|
272
|
+
'_signCreateGroupedOrders',
|
|
261
273
|
] as const satisfies readonly (keyof LighterWasmExports)[]
|
|
262
274
|
|
|
263
275
|
/**
|
package/src/types/action.ts
CHANGED
|
@@ -1,25 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PerpsError } from '@lifi/perps-sdk'
|
|
2
|
+
import { ActionType, PerpsErrorCode } from '@lifi/perps-types'
|
|
2
3
|
|
|
3
4
|
// Shapes of `wasmSignParams` per action. The backend populates these and the
|
|
4
5
|
// SDK passes them to the WASM signer alongside SDK-managed fields (nonce,
|
|
5
6
|
// account_index, api_key_index).
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* The order fields SignCreateOrder reads, and the ten values
|
|
10
|
+
* SignCreateGroupedOrders reads for each order of a group.
|
|
9
11
|
*
|
|
10
12
|
* @public
|
|
11
13
|
*/
|
|
12
|
-
export type
|
|
14
|
+
export type LtCreateOrderWasmParamsBase = {
|
|
13
15
|
market_index: number
|
|
14
16
|
client_order_index: number
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
/** An int64 decimal string, or a number while it is a safe integer. */
|
|
18
|
+
base_amount: string | number
|
|
19
|
+
/** An int64 decimal string, or a number while it is a safe integer. */
|
|
20
|
+
price: string | number
|
|
17
21
|
is_ask: number
|
|
18
22
|
order_type: number
|
|
19
23
|
time_in_force: number
|
|
20
24
|
reduce_only: boolean
|
|
21
|
-
|
|
25
|
+
/** An int64 decimal string, or a number while it is a safe integer. */
|
|
26
|
+
trigger_price: string | number
|
|
22
27
|
order_expiry: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Backend-provided params for SignCreateOrder.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export type LtCreateOrderWasmParams = LtCreateOrderWasmParamsBase & {
|
|
36
|
+
integrator_account_index: number
|
|
37
|
+
integrator_taker_fee: number
|
|
38
|
+
integrator_maker_fee: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Backend-provided params for SignCreateGroupedOrders: a `PLACE_ORDER` step
|
|
43
|
+
* that the signer emits as one `TxTypeL2CreateGroupedOrders` tx. `orders`
|
|
44
|
+
* lists the parent first. A child carries `base_amount` 0 and takes the
|
|
45
|
+
* filled size of the parent once the parent fills.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export type LtCreateGroupedOrdersWasmParams = {
|
|
50
|
+
/** {@link LT_GROUPING_TYPE_OTO}, {@link LT_GROUPING_TYPE_OCO}, or {@link LT_GROUPING_TYPE_OTOCO}. */
|
|
51
|
+
grouping_type: number
|
|
52
|
+
orders: LtCreateOrderWasmParamsBase[]
|
|
23
53
|
integrator_account_index: number
|
|
24
54
|
integrator_taker_fee: number
|
|
25
55
|
integrator_maker_fee: number
|
|
@@ -276,6 +306,11 @@ export const LT_TX_TYPE_WITHDRAW = 13
|
|
|
276
306
|
* @public
|
|
277
307
|
*/
|
|
278
308
|
export const LT_TX_TYPE_CREATE_ORDER = 14
|
|
309
|
+
/**
|
|
310
|
+
* Lighter tx type `TxTypeL2CreateGroupedOrders`.
|
|
311
|
+
* @public
|
|
312
|
+
*/
|
|
313
|
+
export const LT_TX_TYPE_CREATE_GROUPED_ORDERS = 28
|
|
279
314
|
/**
|
|
280
315
|
* Lighter tx type `TxTypeL2CancelOrder`.
|
|
281
316
|
* @public
|
|
@@ -343,3 +378,57 @@ export const LT_TX_TYPE_BY_ACTION: Readonly<
|
|
|
343
378
|
[ActionType.APPROVE_INTEGRATOR]: LT_TX_TYPE_APPROVE_INTEGRATOR,
|
|
344
379
|
[ActionType.REVOKE_INTEGRATOR]: LT_TX_TYPE_APPROVE_INTEGRATOR,
|
|
345
380
|
})
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Lighter grouping type `GroupingType_OneTriggersTheOther`: a parent and one
|
|
384
|
+
* child that the parent fill activates.
|
|
385
|
+
* @public
|
|
386
|
+
*/
|
|
387
|
+
export const LT_GROUPING_TYPE_OTO = 1
|
|
388
|
+
/**
|
|
389
|
+
* Lighter grouping type `GroupingType_OneCancelsTheOther`: two orders, and the
|
|
390
|
+
* first execution cancels the other.
|
|
391
|
+
* @public
|
|
392
|
+
*/
|
|
393
|
+
export const LT_GROUPING_TYPE_OCO = 2
|
|
394
|
+
/**
|
|
395
|
+
* Lighter grouping type `GroupingType_OneTriggersAOneCancelsTheOther`: a
|
|
396
|
+
* parent and an OCO pair that the parent fill activates.
|
|
397
|
+
* @public
|
|
398
|
+
*/
|
|
399
|
+
export const LT_GROUPING_TYPE_OTOCO = 3
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Whether a step is a grouped order: a `PLACE_ORDER` step whose
|
|
403
|
+
* `wasmSignParams` carry a `grouping_type`.
|
|
404
|
+
* @public
|
|
405
|
+
*/
|
|
406
|
+
export const isLtGroupedOrderStep = (
|
|
407
|
+
action: ActionType,
|
|
408
|
+
wasmSignParams: Record<string, unknown>
|
|
409
|
+
): boolean =>
|
|
410
|
+
action === ActionType.PLACE_ORDER &&
|
|
411
|
+
wasmSignParams.grouping_type !== undefined
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Lighter tx type of the signed tx for one step: `TxTypeL2CreateGroupedOrders`
|
|
415
|
+
* for a grouped order step, otherwise the {@link LT_TX_TYPE_BY_ACTION} entry.
|
|
416
|
+
* Throws a `ValidationError` for an action without a Lighter signing path.
|
|
417
|
+
* @public
|
|
418
|
+
*/
|
|
419
|
+
export const resolveLtTxType = (
|
|
420
|
+
action: ActionType,
|
|
421
|
+
wasmSignParams: Record<string, unknown>
|
|
422
|
+
): number => {
|
|
423
|
+
if (isLtGroupedOrderStep(action, wasmSignParams)) {
|
|
424
|
+
return LT_TX_TYPE_CREATE_GROUPED_ORDERS
|
|
425
|
+
}
|
|
426
|
+
const txType = LT_TX_TYPE_BY_ACTION[action]
|
|
427
|
+
if (txType === undefined) {
|
|
428
|
+
throw new PerpsError(
|
|
429
|
+
PerpsErrorCode.ValidationError,
|
|
430
|
+
`Lighter WASM signer does not support action: ${action}`
|
|
431
|
+
)
|
|
432
|
+
}
|
|
433
|
+
return txType
|
|
434
|
+
}
|
package/src/types/fill.ts
CHANGED
|
@@ -61,6 +61,11 @@ export type LtTrade = {
|
|
|
61
61
|
// older trade rows that predate the field.
|
|
62
62
|
taker_initial_margin_fraction_before?: number
|
|
63
63
|
maker_initial_margin_fraction_before?: number
|
|
64
|
+
// Realized PnL of the queried account on its side of the trade, as a
|
|
65
|
+
// decimal string: the bid side reduces a short, the ask side a long.
|
|
66
|
+
// Optional: absent on rows that carry no realized PnL for that side.
|
|
67
|
+
bid_account_pnl?: string
|
|
68
|
+
ask_account_pnl?: string
|
|
64
69
|
/** Version of the resting ask order the trade matched against. */
|
|
65
70
|
ask_order_version?: number
|
|
66
71
|
/** Version of the resting bid order the trade matched against. */
|
package/src/utils/apiClient.ts
CHANGED
|
@@ -24,6 +24,16 @@ export type ApiParams = Record<string, string | number | boolean>
|
|
|
24
24
|
export const lighterBodyExcerpt = (data: unknown): string =>
|
|
25
25
|
data === undefined ? 'non-JSON body' : JSON.stringify(data).slice(0, 200)
|
|
26
26
|
|
|
27
|
+
// The caller reads the body with `response.text()` first, so a failed read
|
|
28
|
+
// (an abort or a reset stream) rejects instead of reading as a non-JSON body.
|
|
29
|
+
const parseJsonBody = (text: string): unknown => {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(text)
|
|
32
|
+
} catch {
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
/**
|
|
28
38
|
* Lighter signals errors on two channels: a non-2xx HTTP status, or an HTTP 200
|
|
29
39
|
* carrying an error `code` in the JSON body. Returns the body error `code` when
|
|
@@ -299,7 +309,7 @@ export class LighterApiClient {
|
|
|
299
309
|
}
|
|
300
310
|
)
|
|
301
311
|
this.assertNotRateLimited(response)
|
|
302
|
-
const data = (await response.
|
|
312
|
+
const data = parseJsonBody(await response.text()) as T | undefined
|
|
303
313
|
return { status: response.status, data }
|
|
304
314
|
}
|
|
305
315
|
|
|
@@ -329,7 +339,7 @@ export class LighterApiClient {
|
|
|
329
339
|
signal: this.signal,
|
|
330
340
|
})
|
|
331
341
|
this.assertNotRateLimited(response)
|
|
332
|
-
const data = (await response.
|
|
342
|
+
const data = parseJsonBody(await response.text()) as T | undefined
|
|
333
343
|
return { status: response.status, data }
|
|
334
344
|
}
|
|
335
345
|
|
package/src/utils/mapFill.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
absDecimalString,
|
|
3
|
+
buildFill,
|
|
4
|
+
calculateFillClosedSize,
|
|
3
5
|
calculateRealizedPnl,
|
|
4
6
|
classifyFillFromPosition,
|
|
5
7
|
divideDecimalString,
|
|
6
8
|
ExplorerChainId,
|
|
7
9
|
explorerTxUrl,
|
|
8
10
|
isDecimalString,
|
|
9
|
-
isDecimalStringGreaterThan,
|
|
10
11
|
isDecimalStringZero,
|
|
11
12
|
safeAddDecimalString,
|
|
12
13
|
safeDivideDecimalString,
|
|
@@ -64,50 +65,54 @@ const leverageFromTradeImf = (imf: number | undefined): string | undefined =>
|
|
|
64
65
|
imf === undefined ? undefined : leverageFromScaledImf(imf)
|
|
65
66
|
|
|
66
67
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* entry-quote snapshot
|
|
70
|
-
*
|
|
71
|
-
* `null`-for-zero convention.
|
|
68
|
+
* Average entry price of the position before the trade:
|
|
69
|
+
* `|entryQuoteBefore| ÷ |startPosition|`. Returns `undefined` when the row
|
|
70
|
+
* omits the entry-quote snapshot, the position was flat, or an input is
|
|
71
|
+
* malformed.
|
|
72
72
|
*/
|
|
73
|
-
const
|
|
73
|
+
const entryPriceBefore = (
|
|
74
74
|
startPosition: string,
|
|
75
|
-
entryQuoteBefore: string | undefined
|
|
76
|
-
|
|
75
|
+
entryQuoteBefore: string | undefined
|
|
76
|
+
): string | undefined => {
|
|
77
|
+
if (
|
|
78
|
+
!isDecimalString(startPosition) ||
|
|
79
|
+
!isDecimalString(entryQuoteBefore) ||
|
|
80
|
+
isDecimalStringZero(startPosition)
|
|
81
|
+
) {
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
return divideDecimalString(
|
|
85
|
+
absDecimalString(entryQuoteBefore),
|
|
86
|
+
absDecimalString(startPosition)
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Realized PnL on the closed part of a fill, derived from the pre-trade entry
|
|
92
|
+
* price. Returns `undefined` for opens/increases (nothing closes) or when the
|
|
93
|
+
* entry price or the fill price is unusable, and `null` when the closed
|
|
94
|
+
* portion realizes exactly zero — mirroring the Hyperliquid mapper's
|
|
95
|
+
* `null`-for-zero convention.
|
|
96
|
+
*/
|
|
97
|
+
const calculateFillRealizedPnl = (
|
|
98
|
+
closedSize: string | undefined,
|
|
99
|
+
entryPrice: string | undefined,
|
|
77
100
|
fillPrice: string,
|
|
78
101
|
isBuyer: boolean
|
|
79
102
|
): string | null | undefined => {
|
|
80
103
|
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
)
|
|
104
|
+
closedSize === undefined ||
|
|
105
|
+
entryPrice === undefined ||
|
|
106
|
+
!isDecimalString(fillPrice)
|
|
85
107
|
) {
|
|
86
108
|
return undefined
|
|
87
109
|
}
|
|
88
|
-
if (isDecimalStringZero(startPosition)) {
|
|
89
|
-
return undefined
|
|
90
|
-
}
|
|
91
|
-
const isLong = isDecimalStringGreaterThan(startPosition, '0')
|
|
92
|
-
const reducing = isLong ? !isBuyer : isBuyer
|
|
93
|
-
if (!reducing) {
|
|
94
|
-
return undefined
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const absStart = absDecimalString(startPosition)
|
|
98
|
-
// A fill larger than the open size flips the position; only the portion that
|
|
99
|
-
// unwinds the existing position realizes PnL.
|
|
100
|
-
const closedSize = isDecimalStringGreaterThan(fillSize, absStart)
|
|
101
|
-
? absStart
|
|
102
|
-
: fillSize
|
|
103
110
|
const pnl = calculateRealizedPnl({
|
|
104
|
-
entryPrice
|
|
105
|
-
absDecimalString(entryQuoteBefore),
|
|
106
|
-
absStart
|
|
107
|
-
),
|
|
111
|
+
entryPrice,
|
|
108
112
|
closePrice: fillPrice,
|
|
109
113
|
closeSize: closedSize,
|
|
110
|
-
|
|
114
|
+
// A buy closes a short and a sell closes a long.
|
|
115
|
+
isLong: !isBuyer,
|
|
111
116
|
})
|
|
112
117
|
return isDecimalStringZero(pnl) ? null : pnl
|
|
113
118
|
}
|
|
@@ -182,38 +187,49 @@ export const mapFill = (
|
|
|
182
187
|
? undefined
|
|
183
188
|
: tickToFeeAmount(trade.usd_amount, feeTick, integratorFeeTick)
|
|
184
189
|
const liquidation = mapFillLiquidation(trade.type, isMaker)
|
|
190
|
+
const side = isBuyer ? OrderSide.BUY : OrderSide.SELL
|
|
191
|
+
const classification = classifyFillFromPosition(startPosition, side, size)
|
|
192
|
+
const entryPrice = entryPriceBefore(startPosition, entryQuoteBefore)
|
|
193
|
+
const venuePnl = isBuyer ? trade.bid_account_pnl : trade.ask_account_pnl
|
|
194
|
+
// The venue figure wins and keeps the mapper's `null`-for-zero convention.
|
|
195
|
+
const realizedPnl =
|
|
196
|
+
venuePnl === undefined
|
|
197
|
+
? calculateFillRealizedPnl(
|
|
198
|
+
calculateFillClosedSize({ size, startPosition, classification }),
|
|
199
|
+
entryPrice,
|
|
200
|
+
price,
|
|
201
|
+
isBuyer
|
|
202
|
+
)
|
|
203
|
+
: isDecimalString(venuePnl) && isDecimalStringZero(venuePnl)
|
|
204
|
+
? null
|
|
205
|
+
: venuePnl
|
|
206
|
+
const leverage = leverageFromTradeImf(imfBefore)
|
|
185
207
|
|
|
186
|
-
return
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
price,
|
|
193
|
-
liquidity: isMaker ? LiquidityRole.MAKER : LiquidityRole.TAKER,
|
|
194
|
-
// Lighter charges the fill fee in the market's quote asset.
|
|
195
|
-
fee:
|
|
196
|
-
feeAmount === undefined
|
|
197
|
-
? undefined
|
|
198
|
-
: { amount: feeAmount, asset: market.quoteAsset.displaySymbol },
|
|
199
|
-
leverage: leverageFromTradeImf(imfBefore),
|
|
200
|
-
realizedPnl: deriveRealizedPnl(
|
|
201
|
-
startPosition,
|
|
202
|
-
entryQuoteBefore,
|
|
208
|
+
return buildFill(
|
|
209
|
+
{
|
|
210
|
+
id: trade.trade_id_str,
|
|
211
|
+
orderId: isBuyer ? trade.bid_id_str : trade.ask_id_str,
|
|
212
|
+
market,
|
|
213
|
+
side,
|
|
203
214
|
size,
|
|
204
215
|
price,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
216
|
+
liquidity: isMaker ? LiquidityRole.MAKER : LiquidityRole.TAKER,
|
|
217
|
+
// Lighter charges the fill fee in the market's quote asset.
|
|
218
|
+
fee:
|
|
219
|
+
feeAmount === undefined
|
|
220
|
+
? undefined
|
|
221
|
+
: { amount: feeAmount, asset: market.quoteAsset.displaySymbol },
|
|
222
|
+
leverage,
|
|
223
|
+
realizedPnl,
|
|
209
224
|
startPosition,
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
:
|
|
217
|
-
|
|
218
|
-
|
|
225
|
+
entryPrice,
|
|
226
|
+
classification,
|
|
227
|
+
createdAt,
|
|
228
|
+
explorerLink: isPlaceholderTxHash(trade.tx_hash)
|
|
229
|
+
? undefined
|
|
230
|
+
: explorerTxUrl(ExplorerChainId.LIGHTER, trade.tx_hash),
|
|
231
|
+
...(liquidation === undefined ? {} : { liquidation }),
|
|
232
|
+
},
|
|
233
|
+
leverage
|
|
234
|
+
)
|
|
219
235
|
}
|
|
@@ -447,6 +447,13 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
447
447
|
this.accountSummaryInputs.clear()
|
|
448
448
|
}
|
|
449
449
|
|
|
450
|
+
protected override resolveChannelKeys(
|
|
451
|
+
_key: string,
|
|
452
|
+
state: SubState
|
|
453
|
+
): string[] {
|
|
454
|
+
return state.key === undefined ? [] : [state.key]
|
|
455
|
+
}
|
|
456
|
+
|
|
450
457
|
protected async sendSubscribe(state: SubState): Promise<void> {
|
|
451
458
|
const { channel, needsAuth, address } = state
|
|
452
459
|
const payload: { type: 'subscribe'; channel: string; auth?: string } = {
|