@lifi/perps-sdk-provider-hyperliquid 0.0.0-preview-414653a → 0.0.0-preview-e1b82a0

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 (49) hide show
  1. package/dist/cjs/types/ws.d.ts +2 -2
  2. package/dist/cjs/types/ws.d.ts.map +1 -1
  3. package/dist/cjs/utils/decimal.d.ts +0 -1
  4. package/dist/cjs/utils/decimal.d.ts.map +1 -1
  5. package/dist/cjs/utils/decimal.js +1 -8
  6. package/dist/cjs/utils/decimal.js.map +1 -1
  7. package/dist/cjs/utils/mapMarketContext.d.ts.map +1 -1
  8. package/dist/cjs/utils/mapMarketContext.js +2 -3
  9. package/dist/cjs/utils/mapMarketContext.js.map +1 -1
  10. package/dist/cjs/utils/withdrawableBalances.d.ts.map +1 -1
  11. package/dist/cjs/utils/withdrawableBalances.js +3 -2
  12. package/dist/cjs/utils/withdrawableBalances.js.map +1 -1
  13. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  14. package/dist/cjs/websocket/HyperliquidWsProvider.js +26 -22
  15. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  16. package/dist/esm/services/getWithdrawableBalances.d.ts +1 -1
  17. package/dist/esm/services/getWithdrawableBalances.js +1 -1
  18. package/dist/esm/types/ws.d.ts +3 -4
  19. package/dist/esm/types/ws.d.ts.map +1 -1
  20. package/dist/esm/utils/decimal.d.ts +0 -5
  21. package/dist/esm/utils/decimal.d.ts.map +1 -1
  22. package/dist/esm/utils/decimal.js +1 -11
  23. package/dist/esm/utils/decimal.js.map +1 -1
  24. package/dist/esm/utils/mapMarketContext.d.ts.map +1 -1
  25. package/dist/esm/utils/mapMarketContext.js +2 -3
  26. package/dist/esm/utils/mapMarketContext.js.map +1 -1
  27. package/dist/esm/utils/withdrawableBalances.d.ts +4 -4
  28. package/dist/esm/utils/withdrawableBalances.d.ts.map +1 -1
  29. package/dist/esm/utils/withdrawableBalances.js +7 -6
  30. package/dist/esm/utils/withdrawableBalances.js.map +1 -1
  31. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  32. package/dist/esm/websocket/HyperliquidWsProvider.js +15 -11
  33. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  34. package/dist/types/services/getWithdrawableBalances.d.ts +1 -1
  35. package/dist/types/types/ws.d.ts +3 -4
  36. package/dist/types/types/ws.d.ts.map +1 -1
  37. package/dist/types/utils/decimal.d.ts +0 -5
  38. package/dist/types/utils/decimal.d.ts.map +1 -1
  39. package/dist/types/utils/mapMarketContext.d.ts.map +1 -1
  40. package/dist/types/utils/withdrawableBalances.d.ts +4 -4
  41. package/dist/types/utils/withdrawableBalances.d.ts.map +1 -1
  42. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  43. package/package.json +3 -3
  44. package/src/services/getWithdrawableBalances.ts +1 -1
  45. package/src/types/ws.ts +3 -4
  46. package/src/utils/decimal.ts +1 -12
  47. package/src/utils/mapMarketContext.ts +2 -3
  48. package/src/utils/withdrawableBalances.ts +7 -6
  49. package/src/websocket/HyperliquidWsProvider.ts +16 -11
@@ -1,4 +1,4 @@
1
- import { numberToDecimalString, PerpsError } from '@lifi/perps-sdk'
1
+ import { PerpsError } from '@lifi/perps-sdk'
2
2
  import { PerpsErrorCode } from '@lifi/perps-types'
3
3
  import Big from 'big.js'
4
4
 
@@ -19,14 +19,3 @@ export const toWireBig = (value: string, field: string): Big => {
19
19
  )
20
20
  }
21
21
  }
22
-
23
- /**
24
- * Decimal string for a Hyperliquid context field that the venue sends as a
25
- * string or a number. Any other value is absent.
26
- */
27
- export const toMarketContextString = (value: unknown): string | undefined => {
28
- if (typeof value === 'number') {
29
- return numberToDecimalString(value)
30
- }
31
- return typeof value === 'string' ? value : undefined
32
- }
@@ -1,7 +1,6 @@
1
1
  import type { MarketContext } from '@lifi/perps-types'
2
2
  import Big from 'big.js'
3
3
  import type { HlWsFastAssetCtx, HlWsPerpAssetCtx } from '../types/index.js'
4
- import { toMarketContextString } from './decimal.js'
5
4
 
6
5
  const NEXT_FUNDING_INTERVAL_MS = 60 * 60 * 1000 // 1 hour
7
6
 
@@ -41,11 +40,11 @@ export const mapMarketContext = (
41
40
  const now = Date.now()
42
41
  const nextFundingTime =
43
42
  Math.ceil(now / NEXT_FUNDING_INTERVAL_MS) * NEXT_FUNDING_INTERVAL_MS
44
- const markPrice = toMarketContextString(fast?.markPx) ?? ctx.markPx
43
+ const markPrice = fast?.markPx != null ? fast.markPx : ctx.markPx
45
44
 
46
45
  return {
47
46
  marketId,
48
- midPrice: toMarketContextString(fast?.midPx) ?? ctx.midPx ?? ctx.markPx,
47
+ midPrice: fast?.midPx != null ? fast.midPx : (ctx.midPx ?? ctx.markPx),
49
48
  markPrice,
50
49
  oraclePrice: ctx.oraclePx,
51
50
  prevDayPrice: ctx.prevDayPx,
@@ -2,6 +2,7 @@ import {
2
2
  calculateWithdrawMax,
3
3
  type ProviderWithdrawableBalance,
4
4
  } from '@lifi/perps-sdk'
5
+ import { PROVIDER_KEY, SPOT_MARKET_ID } from '../constants.js'
5
6
  import type {
6
7
  HlAbstractionMode,
7
8
  HlClearinghouseState,
@@ -12,13 +13,13 @@ import { assetIsOutcome } from './assetId.js'
12
13
  import { toWireBig } from './decimal.js'
13
14
 
14
15
  /**
15
- * Split a Hyperliquid account's venue figures into the routes a withdrawal
16
- * draws on. A unified or portfolio-margin account holds its collateral in spot,
16
+ * Split a Hyperliquid account's venue figures into the categories a
17
+ * withdrawal draws on. A unified or portfolio-margin account holds its collateral in spot,
17
18
  * so each spot token adds the part of `total` that no order or margin holds. A
18
- * route with nothing left to draw carries no row, and the caller applies the
19
+ * category with nothing left to draw carries no row, and the caller applies the
19
20
  * per-asset venue minimum.
20
21
  *
21
- * @param quoteAssetId - `Asset.id` the perps-route row is keyed by.
22
+ * @param quoteAssetId - `Asset.id` the perps-category row is keyed by.
22
23
  * @param withdrawalFee - Flat venue fee in quote-asset units, set on every
23
24
  * quote-asset row. Hyperliquid deducts it from the requested amount. Absent
24
25
  * leaves every row without a fee.
@@ -55,7 +56,7 @@ export const hyperliquidWithdrawableBalances = (
55
56
  const assetId = String(balance.token)
56
57
  const row = {
57
58
  assetId,
58
- route: 'spot' as const,
59
+ categoryId: SPOT_MARKET_ID,
59
60
  available: spot.toFixed(),
60
61
  ...feeFor(assetId),
61
62
  }
@@ -68,7 +69,7 @@ export const hyperliquidWithdrawableBalances = (
68
69
  if (perps.gt(0)) {
69
70
  const row = {
70
71
  assetId: quoteAssetId,
71
- route: 'perps' as const,
72
+ categoryId: PROVIDER_KEY,
72
73
  available: perps.toFixed(),
73
74
  ...feeFor(quoteAssetId),
74
75
  }
@@ -5,6 +5,7 @@ import {
5
5
  isActiveMarket,
6
6
  isActiveOrderStatus,
7
7
  type MarketRegistry,
8
+ numberToDecimalString,
8
9
  PerpsError,
9
10
  type PerpsSDKClient,
10
11
  type ProviderGetQuoteParams,
@@ -66,7 +67,7 @@ import type {
66
67
  HlWsUserFillsData,
67
68
  } from '../types/index.js'
68
69
  import { HlAbstractionMode } from '../types/index.js'
69
- import { toMarketContextString, toWireBig } from '../utils/decimal.js'
70
+ import { toWireBig } from '../utils/decimal.js'
70
71
  import {
71
72
  assetIsOutcome,
72
73
  decodeCompressedJson,
@@ -915,11 +916,9 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
915
916
  if (perpCtx !== undefined) {
916
917
  return mapMarketContext(marketId, perpCtx, fast)
917
918
  }
918
- const fastMid = toMarketContextString(fast?.midPx)
919
- const fastMark = toMarketContextString(fast?.markPx)
920
- const midPrice = fastMid ?? fastMark
921
- const markPrice = fastMark ?? fastMid
922
- if (midPrice !== undefined && markPrice !== undefined) {
919
+ const midPrice = fast?.midPx ?? fast?.markPx
920
+ const markPrice = fast?.markPx ?? fast?.midPx
921
+ if (midPrice != null && markPrice != null) {
923
922
  return { marketId, midPrice, markPrice }
924
923
  }
925
924
  return undefined
@@ -970,9 +969,8 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
970
969
  }
971
970
  }
972
971
  for (const [id, fast] of Object.entries(this.fastCtxByMarketId)) {
973
- const mid =
974
- toMarketContextString(fast.midPx) ?? toMarketContextString(fast.markPx)
975
- if (mid !== undefined) {
972
+ const mid = fast.midPx ?? fast.markPx
973
+ if (mid != null) {
976
974
  map.set(id, Number(mid))
977
975
  }
978
976
  }
@@ -1611,6 +1609,13 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
1611
1609
  const isObject = (v: unknown): v is Record<string, unknown> =>
1612
1610
  typeof v === 'object' && v !== null
1613
1611
 
1612
+ const toMarketContextString = (value: unknown): string | undefined => {
1613
+ if (typeof value === 'number') {
1614
+ return numberToDecimalString(value)
1615
+ }
1616
+ return typeof value === 'string' ? value : undefined
1617
+ }
1618
+
1614
1619
  const toMarketCapString = (
1615
1620
  price: unknown,
1616
1621
  circulatingSupply: unknown
@@ -1725,10 +1730,10 @@ function mapSpotMarketContext(
1725
1730
  return undefined
1726
1731
  }
1727
1732
 
1728
- const emittedMarkPrice = toMarketContextString(fast?.markPx) ?? markPrice
1733
+ const emittedMarkPrice = fast?.markPx != null ? fast.markPx : markPrice
1729
1734
  return {
1730
1735
  marketId,
1731
- midPrice: toMarketContextString(fast?.midPx) ?? midPrice,
1736
+ midPrice: fast?.midPx != null ? fast.midPx : midPrice,
1732
1737
  markPrice: emittedMarkPrice,
1733
1738
  prevDayPrice: toMarketContextString(ctx.prevDayPx),
1734
1739
  volume24h: toMarketContextString(ctx.dayNtlVlm),