@lifi/perps-sdk-provider-lighter 32.0.0 → 34.0.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.
Files changed (46) hide show
  1. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  2. package/dist/cjs/LighterProvider.js +20 -45
  3. package/dist/cjs/LighterProvider.js.map +1 -1
  4. package/dist/cjs/utils/liquidation.js.map +1 -1
  5. package/dist/cjs/utils/mapFill.js +1 -1
  6. package/dist/cjs/utils/mapFill.js.map +1 -1
  7. package/dist/cjs/utils/mapOrder.js +1 -1
  8. package/dist/cjs/utils/mapOrder.js.map +1 -1
  9. package/dist/cjs/utils/transferMargin.d.ts +2 -2
  10. package/dist/cjs/utils/transferMargin.d.ts.map +1 -1
  11. package/dist/cjs/utils/transferMargin.js +2 -2
  12. package/dist/cjs/utils/transferMargin.js.map +1 -1
  13. package/dist/cjs/websocket/LighterWsProvider.d.ts.map +1 -1
  14. package/dist/cjs/websocket/LighterWsProvider.js +6 -8
  15. package/dist/cjs/websocket/LighterWsProvider.js.map +1 -1
  16. package/dist/esm/LighterProvider.d.ts +21 -6
  17. package/dist/esm/LighterProvider.d.ts.map +1 -1
  18. package/dist/esm/LighterProvider.js +23 -47
  19. package/dist/esm/LighterProvider.js.map +1 -1
  20. package/dist/esm/utils/liquidation.js +2 -2
  21. package/dist/esm/utils/liquidation.js.map +1 -1
  22. package/dist/esm/utils/mapFill.js +1 -1
  23. package/dist/esm/utils/mapFill.js.map +1 -1
  24. package/dist/esm/utils/mapOrder.js +1 -1
  25. package/dist/esm/utils/mapOrder.js.map +1 -1
  26. package/dist/esm/utils/transferMargin.d.ts +2 -2
  27. package/dist/esm/utils/transferMargin.d.ts.map +1 -1
  28. package/dist/esm/utils/transferMargin.js +2 -2
  29. package/dist/esm/utils/transferMargin.js.map +1 -1
  30. package/dist/esm/websocket/LighterWsProvider.d.ts +3 -2
  31. package/dist/esm/websocket/LighterWsProvider.d.ts.map +1 -1
  32. package/dist/esm/websocket/LighterWsProvider.js +11 -13
  33. package/dist/esm/websocket/LighterWsProvider.js.map +1 -1
  34. package/dist/types/LighterProvider.d.ts +21 -6
  35. package/dist/types/LighterProvider.d.ts.map +1 -1
  36. package/dist/types/utils/transferMargin.d.ts +2 -2
  37. package/dist/types/utils/transferMargin.d.ts.map +1 -1
  38. package/dist/types/websocket/LighterWsProvider.d.ts +3 -2
  39. package/dist/types/websocket/LighterWsProvider.d.ts.map +1 -1
  40. package/package.json +3 -3
  41. package/src/LighterProvider.ts +58 -55
  42. package/src/utils/liquidation.ts +2 -2
  43. package/src/utils/mapFill.ts +1 -1
  44. package/src/utils/mapOrder.ts +1 -1
  45. package/src/utils/transferMargin.ts +7 -4
  46. package/src/websocket/LighterWsProvider.ts +11 -14
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import {
11
- estimateLiquidationPrice as estimateIsolatedLiquidationPrice,
11
+ estimateLiquidationPrice as estimateLiquidationPriceAtRate,
12
12
  type LiquidationEstimateParams,
13
13
  } from '@lifi/perps-sdk'
14
14
  import type { PerpsMarket } from '@lifi/perps-types'
@@ -30,7 +30,7 @@ export function estimateLiquidationPrice(
30
30
  if (market.maintenanceMarginRate === undefined) {
31
31
  return undefined
32
32
  }
33
- return estimateIsolatedLiquidationPrice({
33
+ return estimateLiquidationPriceAtRate({
34
34
  entryPrice: params.entryPrice,
35
35
  leverage: params.leverage,
36
36
  isLong: params.isLong,
@@ -76,7 +76,7 @@ const deriveRealizedPnl = (
76
76
  const pnl = isLong
77
77
  ? price.minus(avgEntry).times(closedSize)
78
78
  : avgEntry.minus(price).times(closedSize)
79
- return pnl.eq(0) ? null : pnl.toString()
79
+ return pnl.eq(0) ? null : pnl.toFixed()
80
80
  }
81
81
 
82
82
  /**
@@ -118,7 +118,7 @@ export const mapOrder = (order: LtOrder, market: MarketDisplay): Order => {
118
118
  ? {
119
119
  averagePrice: new Big(order.filled_quote_amount)
120
120
  .div(filledSize)
121
- .toString(),
121
+ .toFixed(),
122
122
  }
123
123
  : {}),
124
124
  reduceOnly: order.reduce_only,
@@ -1,9 +1,12 @@
1
- import { PerpsError } from '@lifi/perps-sdk'
2
1
  import {
3
- PerpsErrorCode,
4
- type Position,
2
+ PerpsError,
5
3
  positionSupportsMarginAdjustment,
6
4
  positionSupportsMarginRemoval,
5
+ } from '@lifi/perps-sdk'
6
+ import {
7
+ type DecimalString,
8
+ PerpsErrorCode,
9
+ type Position,
7
10
  } from '@lifi/perps-types'
8
11
  import Big from 'big.js'
9
12
 
@@ -47,7 +50,7 @@ function positivePositionAmount(value: string, field: string): Big {
47
50
  */
48
51
  export function positionRemovableMargin(
49
52
  position: Position
50
- ): string | undefined {
53
+ ): DecimalString | undefined {
51
54
  if (!positionSupportsMarginAdjustment(position)) {
52
55
  return undefined
53
56
  }
@@ -293,15 +293,6 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
293
293
 
294
294
  const wireChannels = await this.resolveChannel(sub)
295
295
 
296
- // Reject before any wire sub registers: a registered sub whose token can
297
- // never be resolved is retried on every reopen without the caller ever
298
- // seeing the failure.
299
- for (const { channel, needsAuth, address } of wireChannels) {
300
- if (needsAuth) {
301
- await this.requireAuthToken(channel, address)
302
- }
303
- }
304
-
305
296
  // Acquired before the wire subs register, so a rejected spot-mark open
306
297
  // leaves no `user_stats`/`account_all` entry behind for the next replay.
307
298
  const releaseSpotMarks =
@@ -392,8 +383,9 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
392
383
  * The auth token a gated channel's subscribe frame carries, or a throw when
393
384
  * no resolver is wired or the resolver yields none. Shared by the
394
385
  * subscribe-time guard and the per-send resolve so both report one message.
395
- * Throws {@link PerpsError} with `Unauthorized` so a caller branches on the
396
- * code, as it does for the Ondo and Lighter REST auth failures.
386
+ * Throws {@link PerpsError}: `SetupRequired` when the resolver yields no
387
+ * token, the code the REST reads throw for the same missing credential, and
388
+ * `SDKError` when no resolver is wired, which no account setup can fix.
397
389
  */
398
390
  private async requireAuthToken(
399
391
  channel: string,
@@ -402,7 +394,7 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
402
394
  const resolve = this.authTokenResolver()
403
395
  if (!resolve || !address) {
404
396
  throw new PerpsError(
405
- PerpsErrorCode.Unauthorized,
397
+ PerpsErrorCode.SDKError,
406
398
  `Lighter WS channel '${channel}' requires authentication but no auth-token resolver was available. ` +
407
399
  'Register `lighterProvider()` on the same client, or pass `resolveAuthToken` to `lighterWsProvider`.'
408
400
  )
@@ -410,7 +402,7 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
410
402
  const token = await resolve(address)
411
403
  if (!token) {
412
404
  throw new PerpsError(
413
- PerpsErrorCode.Unauthorized,
405
+ PerpsErrorCode.SetupRequired,
414
406
  `Lighter WS channel '${channel}' requires authentication but no token was available for ${address}.`
415
407
  )
416
408
  }
@@ -534,13 +526,18 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
534
526
  sub.channel === 'fills' ||
535
527
  sub.channel === 'positions'
536
528
  ) {
537
- const accountIndex = await this.resolveAccountIndex(sub.address)
538
529
  const lighterChannel = LIGHTER_AUTH_CHANNEL[sub.channel]
539
530
  // `account_all_trades` is publicly readable per the Lighter WS spec —
540
531
  // an auth token only filters events to the user's own account, which
541
532
  // we don't currently use to scope further. Skip the token here so a
542
533
  // user without a registered API key still gets their fill stream.
543
534
  const needsAuth = sub.channel !== 'fills'
535
+ // Before the account lookup and before registration: a registered sub
536
+ // with no token would fail on every reopen without the caller seeing it.
537
+ if (needsAuth) {
538
+ await this.requireAuthToken(lighterChannel, sub.address)
539
+ }
540
+ const accountIndex = await this.resolveAccountIndex(sub.address)
544
541
  return [
545
542
  {
546
543
  channel: `${lighterChannel}/${accountIndex}`,