@lifi/perps-sdk-provider-lighter 42.0.0 → 43.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 (85) hide show
  1. package/README.md +5 -1
  2. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  3. package/dist/cjs/LighterProvider.js +5 -5
  4. package/dist/cjs/LighterProvider.js.map +1 -1
  5. package/dist/cjs/constants.d.ts +0 -1
  6. package/dist/cjs/constants.d.ts.map +1 -1
  7. package/dist/cjs/constants.js +1 -2
  8. package/dist/cjs/constants.js.map +1 -1
  9. package/dist/cjs/index.d.ts +1 -1
  10. package/dist/cjs/index.d.ts.map +1 -1
  11. package/dist/cjs/index.js +1 -2
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/types/ws.d.ts +2 -0
  14. package/dist/cjs/types/ws.d.ts.map +1 -1
  15. package/dist/cjs/utils/mapFill.d.ts.map +1 -1
  16. package/dist/cjs/utils/mapFill.js +2 -3
  17. package/dist/cjs/utils/mapFill.js.map +1 -1
  18. package/dist/cjs/utils/spotPrice.d.ts +1 -1
  19. package/dist/cjs/utils/spotPrice.d.ts.map +1 -1
  20. package/dist/cjs/utils/spotPrice.js +2 -2
  21. package/dist/cjs/utils/spotPrice.js.map +1 -1
  22. package/dist/cjs/utils/tradePrice.d.ts +3 -0
  23. package/dist/cjs/utils/tradePrice.d.ts.map +1 -0
  24. package/dist/cjs/utils/tradePrice.js +9 -0
  25. package/dist/cjs/utils/tradePrice.js.map +1 -0
  26. package/dist/cjs/utils/withdrawableBalances.d.ts.map +1 -1
  27. package/dist/cjs/utils/withdrawableBalances.js +2 -1
  28. package/dist/cjs/utils/withdrawableBalances.js.map +1 -1
  29. package/dist/cjs/websocket/LighterWsProvider.d.ts.map +1 -1
  30. package/dist/cjs/websocket/LighterWsProvider.js +4 -3
  31. package/dist/cjs/websocket/LighterWsProvider.js.map +1 -1
  32. package/dist/esm/LighterProvider.d.ts.map +1 -1
  33. package/dist/esm/LighterProvider.js +8 -8
  34. package/dist/esm/LighterProvider.js.map +1 -1
  35. package/dist/esm/constants.d.ts +0 -10
  36. package/dist/esm/constants.d.ts.map +1 -1
  37. package/dist/esm/constants.js +0 -10
  38. package/dist/esm/constants.js.map +1 -1
  39. package/dist/esm/index.d.ts +1 -1
  40. package/dist/esm/index.d.ts.map +1 -1
  41. package/dist/esm/index.js +1 -1
  42. package/dist/esm/index.js.map +1 -1
  43. package/dist/esm/types/ws.d.ts +2 -0
  44. package/dist/esm/types/ws.d.ts.map +1 -1
  45. package/dist/esm/utils/mapFill.d.ts.map +1 -1
  46. package/dist/esm/utils/mapFill.js +3 -6
  47. package/dist/esm/utils/mapFill.js.map +1 -1
  48. package/dist/esm/utils/spotPrice.d.ts +1 -1
  49. package/dist/esm/utils/spotPrice.d.ts.map +1 -1
  50. package/dist/esm/utils/spotPrice.js +3 -3
  51. package/dist/esm/utils/spotPrice.js.map +1 -1
  52. package/dist/esm/utils/tradePrice.d.ts +11 -0
  53. package/dist/esm/utils/tradePrice.d.ts.map +1 -0
  54. package/dist/esm/utils/tradePrice.js +13 -0
  55. package/dist/esm/utils/tradePrice.js.map +1 -0
  56. package/dist/esm/utils/withdrawableBalances.d.ts.map +1 -1
  57. package/dist/esm/utils/withdrawableBalances.js +3 -2
  58. package/dist/esm/utils/withdrawableBalances.js.map +1 -1
  59. package/dist/esm/websocket/LighterWsProvider.d.ts.map +1 -1
  60. package/dist/esm/websocket/LighterWsProvider.js +6 -5
  61. package/dist/esm/websocket/LighterWsProvider.js.map +1 -1
  62. package/dist/types/LighterProvider.d.ts.map +1 -1
  63. package/dist/types/constants.d.ts +0 -10
  64. package/dist/types/constants.d.ts.map +1 -1
  65. package/dist/types/index.d.ts +1 -1
  66. package/dist/types/index.d.ts.map +1 -1
  67. package/dist/types/types/ws.d.ts +2 -0
  68. package/dist/types/types/ws.d.ts.map +1 -1
  69. package/dist/types/utils/mapFill.d.ts.map +1 -1
  70. package/dist/types/utils/spotPrice.d.ts +1 -1
  71. package/dist/types/utils/spotPrice.d.ts.map +1 -1
  72. package/dist/types/utils/tradePrice.d.ts +11 -0
  73. package/dist/types/utils/tradePrice.d.ts.map +1 -0
  74. package/dist/types/utils/withdrawableBalances.d.ts.map +1 -1
  75. package/dist/types/websocket/LighterWsProvider.d.ts.map +1 -1
  76. package/package.json +3 -3
  77. package/src/LighterProvider.ts +6 -6
  78. package/src/constants.ts +0 -11
  79. package/src/index.ts +0 -1
  80. package/src/types/ws.ts +2 -0
  81. package/src/utils/mapFill.ts +2 -7
  82. package/src/utils/spotPrice.ts +2 -2
  83. package/src/utils/tradePrice.ts +20 -0
  84. package/src/utils/withdrawableBalances.ts +3 -2
  85. package/src/websocket/LighterWsProvider.ts +6 -7
package/src/constants.ts CHANGED
@@ -16,17 +16,6 @@ export const LIGHTER_PROVIDER_KEY = 'lighter'
16
16
  */
17
17
  export const LIGHTER_RH_PROVIDER_KEY = 'lighter-rh'
18
18
 
19
- /**
20
- * Category id for Lighter spot token holdings, matching the backend's Lighter
21
- * spot asset category. Every Lighter call site, REST and WebSocket, uses this
22
- * constant for the spot category. The perps category id is not a constant:
23
- * `LighterProvider.getAccount` reads it from the `/providers` category whose
24
- * `quoteAsset` is not null.
25
- *
26
- * @public
27
- */
28
- export const LIGHTER_SPOT_CATEGORY_ID = 'spot'
29
-
30
19
  /**
31
20
  * Default mainnet REST and WS base URL for Lighter.
32
21
  *
package/src/index.ts CHANGED
@@ -34,7 +34,6 @@ export {
34
34
  LIGHTER_RH_REST_URL,
35
35
  LIGHTER_RH_SIGNER_CHAIN_ID,
36
36
  LIGHTER_RH_WS_URL,
37
- LIGHTER_SPOT_CATEGORY_ID,
38
37
  } from './constants.js'
39
38
  export { lighterDepositFlow } from './depositFlow.js'
40
39
  export type {
package/src/types/ws.ts CHANGED
@@ -142,8 +142,10 @@ export type LtWsTrade = {
142
142
  trade_id: number
143
143
  trade_id_str?: string
144
144
  market_id?: number
145
+ type: string
145
146
  size: string
146
147
  price: string
148
+ usd_amount: string
147
149
  is_maker_ask: boolean
148
150
  timestamp: number
149
151
  }
@@ -10,7 +10,6 @@ import {
10
10
  isDecimalStringZero,
11
11
  safeAddDecimalString,
12
12
  safeDivideDecimalString,
13
- safeIsDecimalStringZero,
14
13
  safeMultiplyDecimalString,
15
14
  warnSkippedVenueRow,
16
15
  } from '@lifi/perps-sdk'
@@ -20,6 +19,7 @@ import { LIGHTER_FEE_TICK_SCALE, LIGHTER_PROVIDER_KEY } from '../constants.js'
20
19
  import type { LtTrade } from '../types/index.js'
21
20
  import { leverageFromScaledImf } from './mapPosition.js'
22
21
  import { rowTimestampToIsoStringOrUndefined } from './rowTimestamp.js'
22
+ import { resolveTradePrice } from './tradePrice.js'
23
23
  import { isPlaceholderTxHash } from './txHash.js'
24
24
 
25
25
  /**
@@ -144,12 +144,7 @@ export const mapFill = (
144
144
  market: MarketDisplay
145
145
  ): Fill | undefined => {
146
146
  const { size } = trade
147
- // Lighter reports a zero `price` on a `deleverage` row, but `usd_amount`
148
- // still carries the execution notional.
149
- const price =
150
- trade.type === 'deleverage' && safeIsDecimalStringZero(trade.price) === true
151
- ? (safeDivideDecimalString(trade.usd_amount, size) ?? trade.price)
152
- : trade.price
147
+ const price = resolveTradePrice(trade)
153
148
  const createdAt = rowTimestampToIsoStringOrUndefined(trade.timestamp)
154
149
  if (createdAt === undefined) {
155
150
  warnSkippedVenueRow(
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  isDecimalStringGreaterThan,
3
+ isSpotCategory,
3
4
  multiplyDecimalString,
4
5
  unknownToDecimalString,
5
6
  } from '@lifi/perps-sdk'
@@ -18,7 +19,6 @@ import { LIGHTER_PROVIDER_KEY } from '../constants.js'
18
19
  */
19
20
  export const spotPriceByAssetId = (
20
21
  markets: readonly Market[],
21
- spotCategoryId: string,
22
22
  contexts: readonly MarketContext[],
23
23
  assetIds: ReadonlySet<string>
24
24
  ): Map<string, string> => {
@@ -27,7 +27,7 @@ export const spotPriceByAssetId = (
27
27
  for (const market of markets) {
28
28
  const mark = markByMarketId.get(market.id)
29
29
  if (
30
- market.categoryId !== spotCategoryId ||
30
+ !isSpotCategory(market.categoryId) ||
31
31
  mark === undefined ||
32
32
  !assetIds.has(market.baseAsset.id) ||
33
33
  priceByAssetId.has(market.baseAsset.id)
@@ -0,0 +1,20 @@
1
+ import {
2
+ safeDivideDecimalString,
3
+ safeIsDecimalStringZero,
4
+ } from '@lifi/perps-sdk'
5
+ import type { LtTrade } from '../types/index.js'
6
+
7
+ /**
8
+ * Execution price of a Lighter trade row. Lighter reports a zero `price` on a
9
+ * `deleverage` row, but `usd_amount` still carries the execution notional, so
10
+ * that row resolves to `usd_amount / size`. Every other row, and a row whose
11
+ * division fails, keeps the venue `price`.
12
+ *
13
+ * @internal
14
+ */
15
+ export const resolveTradePrice = (
16
+ trade: Pick<LtTrade, 'type' | 'price' | 'size' | 'usd_amount'>
17
+ ): string =>
18
+ trade.type === 'deleverage' && safeIsDecimalStringZero(trade.price) === true
19
+ ? (safeDivideDecimalString(trade.usd_amount, trade.size) ?? trade.price)
20
+ : trade.price
@@ -6,7 +6,8 @@ import {
6
6
  subtractDecimalString,
7
7
  unknownToDecimalString,
8
8
  } from '@lifi/perps-sdk'
9
- import { LIGHTER_PROVIDER_KEY, LIGHTER_SPOT_CATEGORY_ID } from '../constants.js'
9
+ import { SPOT_CATEGORY_ID } from '@lifi/perps-types'
10
+ import { LIGHTER_PROVIDER_KEY } from '../constants.js'
10
11
  import type { LtAccount } from '../types/account.js'
11
12
 
12
13
  /**
@@ -45,7 +46,7 @@ export const lighterWithdrawableBalances = (
45
46
  if (isDecimalStringGreaterThan(spot, '0')) {
46
47
  rows.push({
47
48
  assetId,
48
- categoryId: LIGHTER_SPOT_CATEGORY_ID,
49
+ categoryId: SPOT_CATEGORY_ID,
49
50
  available: spot,
50
51
  max: calculateWithdrawMax({ available: spot }),
51
52
  })
@@ -4,6 +4,7 @@ import {
4
4
  createSafeFunction,
5
5
  getMarketRegistry,
6
6
  isDecimalStringGreaterThan,
7
+ isSpotCategory,
7
8
  type MarketRegistry,
8
9
  numberToDecimalString,
9
10
  PerpsError,
@@ -41,7 +42,6 @@ import {
41
42
  LIGHTER_COLLATERAL_ASSETS,
42
43
  LIGHTER_PROVIDER_KEY,
43
44
  LIGHTER_RH_PROVIDER_KEY,
44
- LIGHTER_SPOT_CATEGORY_ID,
45
45
  } from '../constants.js'
46
46
  import type { LighterPerpsProvider } from '../LighterProvider.js'
47
47
  import type {
@@ -73,6 +73,7 @@ import {
73
73
  mapPosition,
74
74
  } from '../utils/index.js'
75
75
  import { spotPriceByAssetId, spotValuation } from '../utils/spotPrice.js'
76
+ import { resolveTradePrice } from '../utils/tradePrice.js'
76
77
  import { askOrder, bidOrder, OrderBookSide } from './orderBookSide.js'
77
78
 
78
79
  // Public channels: `marketsContext` (market_stats/all + spot_market_stats/all),
@@ -207,7 +208,6 @@ const heldPortfolioValue = (
207
208
  .filter(([, balance]) => isDecimalStringGreaterThan(balance, '0'))
208
209
  const spotPrices = spotPriceByAssetId(
209
210
  markets,
210
- LIGHTER_SPOT_CATEGORY_ID,
211
211
  contexts,
212
212
  new Set(
213
213
  held
@@ -526,10 +526,9 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
526
526
  )
527
527
  }
528
528
  const market = this.registry.require(sub.marketId)
529
- const prefix =
530
- market.categoryId === LIGHTER_SPOT_CATEGORY_ID
531
- ? 'spot_market_stats'
532
- : 'market_stats'
529
+ const prefix = isSpotCategory(market.categoryId)
530
+ ? 'spot_market_stats'
531
+ : 'market_stats'
533
532
  return [{ channel: `${prefix}/${market.id}`, needsAuth: false }]
534
533
  }
535
534
  if (sub.channel === 'orderbook') {
@@ -1097,7 +1096,7 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
1097
1096
  data: trades.map((t) => ({
1098
1097
  provider: this.providerKey,
1099
1098
  marketId: assetId,
1100
- price: t.price,
1099
+ price: resolveTradePrice(t),
1101
1100
  size: t.size,
1102
1101
  timestamp: t.timestamp,
1103
1102
  side: t.is_maker_ask ? 'buy' : 'sell',