@lifi/perps-sdk-provider-lighter 47.0.0 → 47.2.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 +20 -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/types/asset.d.ts +2 -0
- package/dist/cjs/types/asset.d.ts.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 +7 -2
- package/dist/cjs/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/LighterWsProvider.js +82 -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/types/asset.d.ts +2 -0
- package/dist/esm/types/asset.d.ts.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 +7 -2
- package/dist/esm/websocket/LighterWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/LighterWsProvider.js +85 -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/types/asset.d.ts +2 -0
- package/dist/types/types/asset.d.ts.map +1 -1
- package/dist/types/utils/mapPortfolioHistory.d.ts.map +1 -1
- package/dist/types/websocket/LighterWsProvider.d.ts +7 -2
- package/dist/types/websocket/LighterWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/LighterProvider.ts +27 -1
- package/src/types/asset.ts +2 -0
- package/src/utils/mapPortfolioHistory.ts +4 -1
- package/src/websocket/LighterWsProvider.ts +115 -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,18 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
415
447
|
this.accountSummaryInputs.clear()
|
|
416
448
|
}
|
|
417
449
|
|
|
418
|
-
protected async sendSubscribe({
|
|
419
|
-
channel,
|
|
420
|
-
needsAuth,
|
|
421
|
-
address,
|
|
422
|
-
}: SubState): Promise<void> {
|
|
450
|
+
protected async sendSubscribe(state: SubState): Promise<void> {
|
|
451
|
+
const { channel, needsAuth, address } = state
|
|
423
452
|
const payload: { type: 'subscribe'; channel: string; auth?: string } = {
|
|
424
453
|
type: 'subscribe',
|
|
425
454
|
channel,
|
|
426
455
|
}
|
|
427
456
|
if (needsAuth) {
|
|
428
457
|
payload.auth = await this.requireAuthToken(channel, address)
|
|
458
|
+
if (this.wireSubs.get(channel) !== state) {
|
|
459
|
+
return
|
|
460
|
+
}
|
|
461
|
+
state.token = payload.auth
|
|
429
462
|
}
|
|
430
463
|
this.rws.send(JSON.stringify(payload))
|
|
431
464
|
}
|
|
@@ -457,6 +490,12 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
457
490
|
`Lighter WS channel '${channel}' requires authentication but no token was available for ${address}.`
|
|
458
491
|
)
|
|
459
492
|
}
|
|
493
|
+
if (this.rejectedTokens.get(address.toLowerCase())?.has(token)) {
|
|
494
|
+
throw new PerpsError(
|
|
495
|
+
PerpsErrorCode.Unauthorized,
|
|
496
|
+
'Lighter rejected the read credential. Authenticate again before subscribing.'
|
|
497
|
+
)
|
|
498
|
+
}
|
|
460
499
|
return token
|
|
461
500
|
}
|
|
462
501
|
|
|
@@ -622,14 +661,19 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
622
661
|
}
|
|
623
662
|
|
|
624
663
|
protected handleMessage(raw: string): void {
|
|
625
|
-
let msg:
|
|
664
|
+
let msg: unknown
|
|
626
665
|
try {
|
|
627
|
-
msg = JSON.parse(raw)
|
|
666
|
+
msg = JSON.parse(raw)
|
|
628
667
|
} catch {
|
|
629
668
|
wsLog.parseFailure(this.providerKey, raw)
|
|
630
669
|
return
|
|
631
670
|
}
|
|
632
671
|
|
|
672
|
+
if (isObject(msg) && isObject(msg.error)) {
|
|
673
|
+
this.handleSubscriptionError(msg.error)
|
|
674
|
+
return
|
|
675
|
+
}
|
|
676
|
+
|
|
633
677
|
if (!isValidLighterFrame(msg)) {
|
|
634
678
|
wsLog.parseFailure(this.providerKey, raw)
|
|
635
679
|
return
|
|
@@ -642,6 +686,56 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
|
|
|
642
686
|
}
|
|
643
687
|
}
|
|
644
688
|
|
|
689
|
+
private handleSubscriptionError(error: Record<string, unknown>): void {
|
|
690
|
+
const { code, message } = error
|
|
691
|
+
const target =
|
|
692
|
+
code === LIGHTER_INVALID_AUTH_CODE && typeof message === 'string'
|
|
693
|
+
? [...this.wireSubs.values()].find(
|
|
694
|
+
(state) =>
|
|
695
|
+
state.needsAuth &&
|
|
696
|
+
state.token !== undefined &&
|
|
697
|
+
message === `invalid auth: ${state.channel.replaceAll('/', ':')}`
|
|
698
|
+
)
|
|
699
|
+
: undefined
|
|
700
|
+
if (target?.address === undefined || target.token === undefined) {
|
|
701
|
+
wsLog.serverError(this.providerKey, `Venue error code: ${String(code)}`)
|
|
702
|
+
return
|
|
703
|
+
}
|
|
704
|
+
const address = target.address.toLowerCase()
|
|
705
|
+
let rejected = this.rejectedTokens.get(address)
|
|
706
|
+
if (rejected === undefined) {
|
|
707
|
+
rejected = new Set()
|
|
708
|
+
this.rejectedTokens.set(address, rejected)
|
|
709
|
+
}
|
|
710
|
+
rejected.add(target.token)
|
|
711
|
+
const provider = this.client?.getProvider(this.providerKey)
|
|
712
|
+
if (provider !== undefined && hasAuthTokenResolver(provider)) {
|
|
713
|
+
provider.rejectAuthToken?.(target.address, target.token)
|
|
714
|
+
}
|
|
715
|
+
const failure = new PerpsError(
|
|
716
|
+
PerpsErrorCode.Unauthorized,
|
|
717
|
+
'Lighter rejected the read credential. Authenticate again before subscribing.'
|
|
718
|
+
)
|
|
719
|
+
failure.tool = this.providerKey
|
|
720
|
+
const affected = [...this.wireSubs.values()].filter(
|
|
721
|
+
(state) =>
|
|
722
|
+
state.needsAuth &&
|
|
723
|
+
state.address?.toLowerCase() === address &&
|
|
724
|
+
state.token === target.token
|
|
725
|
+
)
|
|
726
|
+
// Unregister before the channel teardown runs, so a wire the venue never
|
|
727
|
+
// accepted gets no `unsubscribe` frame.
|
|
728
|
+
for (const state of affected) {
|
|
729
|
+
state.failure = failure
|
|
730
|
+
this.unregisterSub(state.channel)
|
|
731
|
+
}
|
|
732
|
+
for (const state of affected) {
|
|
733
|
+
if (state.key !== undefined) {
|
|
734
|
+
this.rejectChannel(state.key, failure)
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
645
739
|
private dispatch(msg: LtWsMessage): void {
|
|
646
740
|
if (msg.type === 'ping') {
|
|
647
741
|
this.rws.send(JSON.stringify({ type: 'pong' }))
|
|
@@ -1186,15 +1280,15 @@ const isObject = (v: unknown): v is Record<string, unknown> =>
|
|
|
1186
1280
|
* handler that throws on otherwise-shaped data. Frames for other types pass
|
|
1187
1281
|
* through to `dispatch`, which ignores types it does not recognise.
|
|
1188
1282
|
*/
|
|
1189
|
-
function isValidLighterFrame(msg:
|
|
1190
|
-
if (typeof msg.type !== 'string') {
|
|
1283
|
+
function isValidLighterFrame(msg: unknown): msg is LtWsMessage {
|
|
1284
|
+
if (!isObject(msg) || typeof msg.type !== 'string') {
|
|
1191
1285
|
return false
|
|
1192
1286
|
}
|
|
1193
1287
|
if (
|
|
1194
1288
|
msg.type === 'subscribed/order_book' ||
|
|
1195
1289
|
msg.type === 'update/order_book'
|
|
1196
1290
|
) {
|
|
1197
|
-
const book =
|
|
1291
|
+
const book = msg.order_book
|
|
1198
1292
|
return (
|
|
1199
1293
|
isObject(book) && Array.isArray(book.bids) && Array.isArray(book.asks)
|
|
1200
1294
|
)
|