@lifi/perps-sdk-provider-lighter 47.1.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 +53 -1
- package/dist/cjs/LighterProvider.d.ts +1 -0
- package/dist/cjs/LighterProvider.d.ts.map +1 -1
- package/dist/cjs/LighterProvider.js +19 -1
- package/dist/cjs/LighterProvider.js.map +1 -1
- 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/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/cjs/utils/mapPortfolioHistory.js +4 -1
- package/dist/cjs/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/cjs/websocket/LighterWsProvider.d.ts +8 -2
- package/dist/cjs/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/LighterWsProvider.js +85 -13
- package/dist/cjs/websocket/LighterWsProvider.js.map +1 -1
- package/dist/esm/LighterProvider.d.ts +2 -0
- package/dist/esm/LighterProvider.d.ts.map +1 -1
- package/dist/esm/LighterProvider.js +19 -1
- package/dist/esm/LighterProvider.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/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/esm/utils/mapPortfolioHistory.js +4 -1
- package/dist/esm/utils/mapPortfolioHistory.js.map +1 -1
- package/dist/esm/websocket/LighterWsProvider.d.ts +8 -2
- package/dist/esm/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/LighterWsProvider.js +88 -14
- package/dist/esm/websocket/LighterWsProvider.js.map +1 -1
- package/dist/types/LighterProvider.d.ts +2 -0
- package/dist/types/LighterProvider.d.ts.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/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/types/websocket/LighterWsProvider.d.ts +8 -2
- package/dist/types/websocket/LighterWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/LighterProvider.ts +27 -1
- 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/utils/mapPortfolioHistory.ts +4 -1
- package/src/websocket/LighterWsProvider.ts +122 -21
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
DEFAULT_LIGHTER_WS_URL,
|
|
41
41
|
LIGHTER_BASE_FEE_TIER,
|
|
42
42
|
LIGHTER_COLLATERAL_ASSETS,
|
|
43
|
+
LIGHTER_INVALID_AUTH_CODE,
|
|
43
44
|
LIGHTER_PROVIDER_KEY,
|
|
44
45
|
LIGHTER_RH_PROVIDER_KEY,
|
|
45
46
|
} from '../constants.js'
|
|
@@ -148,6 +149,9 @@ interface SubState {
|
|
|
148
149
|
address?: Address
|
|
149
150
|
/** Whether this channel needs an `auth` field on each subscribe send. */
|
|
150
151
|
needsAuth: boolean
|
|
152
|
+
key?: string
|
|
153
|
+
token?: string
|
|
154
|
+
failure?: PerpsError
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
/** Settlement equity and per-asset route quantities for account valuation. */
|
|
@@ -263,6 +267,7 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
263
267
|
|
|
264
268
|
private readonly accountIndexCache = new Map<string, number>()
|
|
265
269
|
private readonly accountIndexPromises = new Map<string, Promise<number>>()
|
|
270
|
+
private readonly rejectedTokens = new Map<string, Set<string>>()
|
|
266
271
|
|
|
267
272
|
constructor(
|
|
268
273
|
wsUrl: string = DEFAULT_LIGHTER_WS_URL,
|
|
@@ -342,7 +347,10 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
342
347
|
await this.registry?.sync()
|
|
343
348
|
}
|
|
344
349
|
|
|
345
|
-
const wireChannels = await this.resolveChannel(sub)
|
|
350
|
+
const wireChannels: SubState[] = (await this.resolveChannel(sub)).map(
|
|
351
|
+
(state) => ({ ...state, key: this.toKey(sub) })
|
|
352
|
+
)
|
|
353
|
+
const summaryInputs = {}
|
|
346
354
|
|
|
347
355
|
// Acquired before the wire subs register, so a rejected spot-mark open
|
|
348
356
|
// leaves no `user_stats`/`account_all` entry behind for the next replay.
|
|
@@ -351,23 +359,36 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
351
359
|
? await this.subscribeSpotMarks(sub)
|
|
352
360
|
: undefined
|
|
353
361
|
if (sub.channel === 'accountSummary') {
|
|
354
|
-
this.accountSummaryInputs.set(sub.address.toLowerCase(),
|
|
362
|
+
this.accountSummaryInputs.set(sub.address.toLowerCase(), summaryInputs)
|
|
355
363
|
}
|
|
356
364
|
|
|
357
365
|
try {
|
|
358
366
|
// Registry keyed by the wire channel (unique per sub), so replay-failure
|
|
359
367
|
// logs name the channel the venue knows. A logical sub may fan out to
|
|
360
368
|
// several wire channels (e.g. `marketsContext`), each registered independently.
|
|
361
|
-
for (const
|
|
362
|
-
await this.registerSub(channel,
|
|
369
|
+
for (const state of wireChannels) {
|
|
370
|
+
await this.registerSub(state.channel, state)
|
|
371
|
+
if (state.failure !== undefined) {
|
|
372
|
+
throw state.failure
|
|
373
|
+
}
|
|
363
374
|
}
|
|
364
375
|
await this.rws.ready()
|
|
376
|
+
const failure = wireChannels.find((state) => state.failure)?.failure
|
|
377
|
+
if (failure !== undefined) {
|
|
378
|
+
throw failure
|
|
379
|
+
}
|
|
365
380
|
} catch (error) {
|
|
366
381
|
releaseSpotMarks?.()
|
|
367
|
-
for (const
|
|
368
|
-
this.
|
|
382
|
+
for (const state of wireChannels) {
|
|
383
|
+
if (this.wireSubs.get(state.channel) === state) {
|
|
384
|
+
this.unregisterSub(state.channel)
|
|
385
|
+
}
|
|
369
386
|
}
|
|
370
|
-
if (
|
|
387
|
+
if (
|
|
388
|
+
sub.channel === 'accountSummary' &&
|
|
389
|
+
this.accountSummaryInputs.get(sub.address.toLowerCase()) ===
|
|
390
|
+
summaryInputs
|
|
391
|
+
) {
|
|
371
392
|
this.accountSummaryInputs.delete(sub.address.toLowerCase())
|
|
372
393
|
}
|
|
373
394
|
throw error
|
|
@@ -375,9 +396,16 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
375
396
|
|
|
376
397
|
return () => {
|
|
377
398
|
releaseSpotMarks?.()
|
|
378
|
-
for (const
|
|
379
|
-
this.
|
|
380
|
-
|
|
399
|
+
for (const state of wireChannels) {
|
|
400
|
+
if (this.wireSubs.get(state.channel) === state) {
|
|
401
|
+
this.unregisterSub(state.channel)
|
|
402
|
+
this.rws.send(
|
|
403
|
+
JSON.stringify({
|
|
404
|
+
type: 'unsubscribe',
|
|
405
|
+
channel: state.channel,
|
|
406
|
+
})
|
|
407
|
+
)
|
|
408
|
+
}
|
|
381
409
|
}
|
|
382
410
|
if (sub.channel === 'orderbook') {
|
|
383
411
|
const id = Number(sub.marketId)
|
|
@@ -388,7 +416,11 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
388
416
|
if (sub.channel === 'positions') {
|
|
389
417
|
this.positionsByAddress.delete(sub.address.toLowerCase())
|
|
390
418
|
}
|
|
391
|
-
if (
|
|
419
|
+
if (
|
|
420
|
+
sub.channel === 'accountSummary' &&
|
|
421
|
+
this.accountSummaryInputs.get(sub.address.toLowerCase()) ===
|
|
422
|
+
summaryInputs
|
|
423
|
+
) {
|
|
392
424
|
this.accountSummaryInputs.delete(sub.address.toLowerCase())
|
|
393
425
|
}
|
|
394
426
|
}
|
|
@@ -415,17 +447,25 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
415
447
|
this.accountSummaryInputs.clear()
|
|
416
448
|
}
|
|
417
449
|
|
|
418
|
-
protected
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
450
|
+
protected override resolveChannelKeys(
|
|
451
|
+
_key: string,
|
|
452
|
+
state: SubState
|
|
453
|
+
): string[] {
|
|
454
|
+
return state.key === undefined ? [] : [state.key]
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
protected async sendSubscribe(state: SubState): Promise<void> {
|
|
458
|
+
const { channel, needsAuth, address } = state
|
|
423
459
|
const payload: { type: 'subscribe'; channel: string; auth?: string } = {
|
|
424
460
|
type: 'subscribe',
|
|
425
461
|
channel,
|
|
426
462
|
}
|
|
427
463
|
if (needsAuth) {
|
|
428
464
|
payload.auth = await this.requireAuthToken(channel, address)
|
|
465
|
+
if (this.wireSubs.get(channel) !== state) {
|
|
466
|
+
return
|
|
467
|
+
}
|
|
468
|
+
state.token = payload.auth
|
|
429
469
|
}
|
|
430
470
|
this.rws.send(JSON.stringify(payload))
|
|
431
471
|
}
|
|
@@ -457,6 +497,12 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
457
497
|
`Lighter WS channel '${channel}' requires authentication but no token was available for ${address}.`
|
|
458
498
|
)
|
|
459
499
|
}
|
|
500
|
+
if (this.rejectedTokens.get(address.toLowerCase())?.has(token)) {
|
|
501
|
+
throw new PerpsError(
|
|
502
|
+
PerpsErrorCode.Unauthorized,
|
|
503
|
+
'Lighter rejected the read credential. Authenticate again before subscribing.'
|
|
504
|
+
)
|
|
505
|
+
}
|
|
460
506
|
return token
|
|
461
507
|
}
|
|
462
508
|
|
|
@@ -622,14 +668,19 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
622
668
|
}
|
|
623
669
|
|
|
624
670
|
protected handleMessage(raw: string): void {
|
|
625
|
-
let msg:
|
|
671
|
+
let msg: unknown
|
|
626
672
|
try {
|
|
627
|
-
msg = JSON.parse(raw)
|
|
673
|
+
msg = JSON.parse(raw)
|
|
628
674
|
} catch {
|
|
629
675
|
wsLog.parseFailure(this.providerKey, raw)
|
|
630
676
|
return
|
|
631
677
|
}
|
|
632
678
|
|
|
679
|
+
if (isObject(msg) && isObject(msg.error)) {
|
|
680
|
+
this.handleSubscriptionError(msg.error)
|
|
681
|
+
return
|
|
682
|
+
}
|
|
683
|
+
|
|
633
684
|
if (!isValidLighterFrame(msg)) {
|
|
634
685
|
wsLog.parseFailure(this.providerKey, raw)
|
|
635
686
|
return
|
|
@@ -642,6 +693,56 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
642
693
|
}
|
|
643
694
|
}
|
|
644
695
|
|
|
696
|
+
private handleSubscriptionError(error: Record<string, unknown>): void {
|
|
697
|
+
const { code, message } = error
|
|
698
|
+
const target =
|
|
699
|
+
code === LIGHTER_INVALID_AUTH_CODE && typeof message === 'string'
|
|
700
|
+
? [...this.wireSubs.values()].find(
|
|
701
|
+
(state) =>
|
|
702
|
+
state.needsAuth &&
|
|
703
|
+
state.token !== undefined &&
|
|
704
|
+
message === `invalid auth: ${state.channel.replaceAll('/', ':')}`
|
|
705
|
+
)
|
|
706
|
+
: undefined
|
|
707
|
+
if (target?.address === undefined || target.token === undefined) {
|
|
708
|
+
wsLog.serverError(this.providerKey, `Venue error code: ${String(code)}`)
|
|
709
|
+
return
|
|
710
|
+
}
|
|
711
|
+
const address = target.address.toLowerCase()
|
|
712
|
+
let rejected = this.rejectedTokens.get(address)
|
|
713
|
+
if (rejected === undefined) {
|
|
714
|
+
rejected = new Set()
|
|
715
|
+
this.rejectedTokens.set(address, rejected)
|
|
716
|
+
}
|
|
717
|
+
rejected.add(target.token)
|
|
718
|
+
const provider = this.client?.getProvider(this.providerKey)
|
|
719
|
+
if (provider !== undefined && hasAuthTokenResolver(provider)) {
|
|
720
|
+
provider.rejectAuthToken?.(target.address, target.token)
|
|
721
|
+
}
|
|
722
|
+
const failure = new PerpsError(
|
|
723
|
+
PerpsErrorCode.Unauthorized,
|
|
724
|
+
'Lighter rejected the read credential. Authenticate again before subscribing.'
|
|
725
|
+
)
|
|
726
|
+
failure.tool = this.providerKey
|
|
727
|
+
const affected = [...this.wireSubs.values()].filter(
|
|
728
|
+
(state) =>
|
|
729
|
+
state.needsAuth &&
|
|
730
|
+
state.address?.toLowerCase() === address &&
|
|
731
|
+
state.token === target.token
|
|
732
|
+
)
|
|
733
|
+
// Unregister before the channel teardown runs, so a wire the venue never
|
|
734
|
+
// accepted gets no `unsubscribe` frame.
|
|
735
|
+
for (const state of affected) {
|
|
736
|
+
state.failure = failure
|
|
737
|
+
this.unregisterSub(state.channel)
|
|
738
|
+
}
|
|
739
|
+
for (const state of affected) {
|
|
740
|
+
if (state.key !== undefined) {
|
|
741
|
+
this.rejectChannel(state.key, failure)
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
645
746
|
private dispatch(msg: LtWsMessage): void {
|
|
646
747
|
if (msg.type === 'ping') {
|
|
647
748
|
this.rws.send(JSON.stringify({ type: 'pong' }))
|
|
@@ -1186,15 +1287,15 @@ const isObject = (v: unknown): v is Record<string, unknown> =>
|
|
|
1186
1287
|
* handler that throws on otherwise-shaped data. Frames for other types pass
|
|
1187
1288
|
* through to `dispatch`, which ignores types it does not recognise.
|
|
1188
1289
|
*/
|
|
1189
|
-
function isValidLighterFrame(msg:
|
|
1190
|
-
if (typeof msg.type !== 'string') {
|
|
1290
|
+
function isValidLighterFrame(msg: unknown): msg is LtWsMessage {
|
|
1291
|
+
if (!isObject(msg) || typeof msg.type !== 'string') {
|
|
1191
1292
|
return false
|
|
1192
1293
|
}
|
|
1193
1294
|
if (
|
|
1194
1295
|
msg.type === 'subscribed/order_book' ||
|
|
1195
1296
|
msg.type === 'update/order_book'
|
|
1196
1297
|
) {
|
|
1197
|
-
const book =
|
|
1298
|
+
const book = msg.order_book
|
|
1198
1299
|
return (
|
|
1199
1300
|
isObject(book) && Array.isArray(book.bids) && Array.isArray(book.asks)
|
|
1200
1301
|
)
|