@lifi/perps-sdk-provider-hyperliquid 4.1.0 → 4.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.
Files changed (84) hide show
  1. package/dist/cjs/services/getAccount.d.ts.map +1 -1
  2. package/dist/cjs/services/getAccount.js +1 -11
  3. package/dist/cjs/services/getAccount.js.map +1 -1
  4. package/dist/cjs/types/account.d.ts +1 -0
  5. package/dist/cjs/types/account.d.ts.map +1 -1
  6. package/dist/cjs/types/account.js +1 -0
  7. package/dist/cjs/types/account.js.map +1 -1
  8. package/dist/cjs/utils/assetLogo.d.ts +5 -0
  9. package/dist/cjs/utils/assetLogo.d.ts.map +1 -0
  10. package/dist/cjs/utils/assetLogo.js +47 -0
  11. package/dist/cjs/utils/assetLogo.js.map +1 -0
  12. package/dist/cjs/utils/index.d.ts +2 -0
  13. package/dist/cjs/utils/index.d.ts.map +1 -1
  14. package/dist/cjs/utils/index.js +7 -1
  15. package/dist/cjs/utils/index.js.map +1 -1
  16. package/dist/cjs/utils/marketDisplay.d.ts.map +1 -1
  17. package/dist/cjs/utils/marketDisplay.js +5 -2
  18. package/dist/cjs/utils/marketDisplay.js.map +1 -1
  19. package/dist/cjs/utils/spotBalance.d.ts.map +1 -1
  20. package/dist/cjs/utils/spotBalance.js +2 -0
  21. package/dist/cjs/utils/spotBalance.js.map +1 -1
  22. package/dist/cjs/utils/spotCollateral.d.ts +7 -0
  23. package/dist/cjs/utils/spotCollateral.d.ts.map +1 -0
  24. package/dist/cjs/utils/spotCollateral.js +29 -0
  25. package/dist/cjs/utils/spotCollateral.js.map +1 -0
  26. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts +15 -1
  27. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  28. package/dist/cjs/websocket/HyperliquidWsProvider.js +184 -27
  29. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  30. package/dist/esm/services/getAccount.d.ts.map +1 -1
  31. package/dist/esm/services/getAccount.js +2 -13
  32. package/dist/esm/services/getAccount.js.map +1 -1
  33. package/dist/esm/types/account.d.ts +3 -1
  34. package/dist/esm/types/account.d.ts.map +1 -1
  35. package/dist/esm/types/account.js +3 -1
  36. package/dist/esm/types/account.js.map +1 -1
  37. package/dist/esm/utils/assetLogo.d.ts +20 -0
  38. package/dist/esm/utils/assetLogo.d.ts.map +1 -0
  39. package/dist/esm/utils/assetLogo.js +74 -0
  40. package/dist/esm/utils/assetLogo.js.map +1 -0
  41. package/dist/esm/utils/index.d.ts +2 -0
  42. package/dist/esm/utils/index.d.ts.map +1 -1
  43. package/dist/esm/utils/index.js +2 -0
  44. package/dist/esm/utils/index.js.map +1 -1
  45. package/dist/esm/utils/marketDisplay.d.ts +5 -4
  46. package/dist/esm/utils/marketDisplay.d.ts.map +1 -1
  47. package/dist/esm/utils/marketDisplay.js +10 -6
  48. package/dist/esm/utils/marketDisplay.js.map +1 -1
  49. package/dist/esm/utils/spotBalance.d.ts +3 -1
  50. package/dist/esm/utils/spotBalance.d.ts.map +1 -1
  51. package/dist/esm/utils/spotBalance.js +5 -1
  52. package/dist/esm/utils/spotBalance.js.map +1 -1
  53. package/dist/esm/utils/spotCollateral.d.ts +14 -0
  54. package/dist/esm/utils/spotCollateral.d.ts.map +1 -0
  55. package/dist/esm/utils/spotCollateral.js +39 -0
  56. package/dist/esm/utils/spotCollateral.js.map +1 -0
  57. package/dist/esm/websocket/HyperliquidWsProvider.d.ts +23 -1
  58. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  59. package/dist/esm/websocket/HyperliquidWsProvider.js +223 -17
  60. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  61. package/dist/types/services/getAccount.d.ts.map +1 -1
  62. package/dist/types/types/account.d.ts +3 -1
  63. package/dist/types/types/account.d.ts.map +1 -1
  64. package/dist/types/utils/assetLogo.d.ts +20 -0
  65. package/dist/types/utils/assetLogo.d.ts.map +1 -0
  66. package/dist/types/utils/index.d.ts +2 -0
  67. package/dist/types/utils/index.d.ts.map +1 -1
  68. package/dist/types/utils/marketDisplay.d.ts +5 -4
  69. package/dist/types/utils/marketDisplay.d.ts.map +1 -1
  70. package/dist/types/utils/spotBalance.d.ts +3 -1
  71. package/dist/types/utils/spotBalance.d.ts.map +1 -1
  72. package/dist/types/utils/spotCollateral.d.ts +14 -0
  73. package/dist/types/utils/spotCollateral.d.ts.map +1 -0
  74. package/dist/types/websocket/HyperliquidWsProvider.d.ts +23 -1
  75. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  76. package/package.json +3 -3
  77. package/src/services/getAccount.ts +8 -12
  78. package/src/types/account.ts +3 -1
  79. package/src/utils/assetLogo.ts +85 -0
  80. package/src/utils/index.ts +6 -0
  81. package/src/utils/marketDisplay.ts +15 -10
  82. package/src/utils/spotBalance.ts +5 -1
  83. package/src/utils/spotCollateral.ts +50 -0
  84. package/src/websocket/HyperliquidWsProvider.ts +304 -18
@@ -1,4 +1,4 @@
1
- import { type PerpsSDKClient, type ProviderGetQuoteParams, type QuoteListener, WsProviderBase, type WsProviderFactory } from '@lifi/perps-sdk';
1
+ import { type PerpsSDKClient, type ProviderGetQuoteParams, type QuoteListener, type SubscriptionListener, WsProviderBase, type WsProviderFactory, type WsStatusListener } from '@lifi/perps-sdk';
2
2
  import { type Subscription } from '@lifi/perps-types';
3
3
  /**
4
4
  * `WsProviderFactory` constructor for Hyperliquid — pass to
@@ -29,6 +29,11 @@ export declare class HyperliquidWsProvider extends WsProviderBase<object> {
29
29
  private readonly clearinghouseRefs;
30
30
  private readonly client;
31
31
  private readonly registry;
32
+ private readonly abstractionByUser;
33
+ private readonly spotRefs;
34
+ private readonly unifiedSummaryByUser;
35
+ private readonly latestPositionsByUser;
36
+ private readonly heldSummaryByUser;
32
37
  private perpCtxBySubDex;
33
38
  private spotCtxByMarketId;
34
39
  private marketsContextByMarketId;
@@ -41,6 +46,7 @@ export declare class HyperliquidWsProvider extends WsProviderBase<object> {
41
46
  private readonly sacDecodeChain;
42
47
  private readonly orderbookDecodeChain;
43
48
  constructor(wsUrl: string, providerKey: string, client?: PerpsSDKClient);
49
+ subscribe(sub: Subscription, listener: SubscriptionListener, onStatus?: WsStatusListener): Promise<() => void>;
44
50
  subscribeQuote(params: ProviderGetQuoteParams, onQuote: QuoteListener): Promise<() => void>;
45
51
  protected openChannel(sub: Subscription): Promise<() => void>;
46
52
  /**
@@ -108,6 +114,22 @@ export declare class HyperliquidWsProvider extends WsProviderBase<object> {
108
114
  private handleOrderUpdates;
109
115
  private handleUserFills;
110
116
  private handleAllDexsClearinghouseState;
117
+ /** Whether the mode's collateral lives per-dex, making the perps-only
118
+ * summary frame honest. Unified/portfolio hold collateral in spot. */
119
+ private static summaryComputableFor;
120
+ private acquireSpotWire;
121
+ /** Start or stop the spot-fed unified pipeline to match the resolved mode. */
122
+ private syncUnifiedPipeline;
123
+ private emitUnifiedSummary;
124
+ /** Re-read the abstraction mode this long after the previous read, so an
125
+ * in-session account-mode switch flips the gate within one refresh. */
126
+ private static readonly ABSTRACTION_TTL_MS;
127
+ /** Subscribe-triggered re-reads younger than this are skipped: a config
128
+ * change resubscribes every consumer hook in one React commit, and one
129
+ * read serves them all. */
130
+ private static readonly REFRESH_COALESCE_MS;
131
+ private emitSummaryIfModeAllows;
132
+ private fetchAbstractionMode;
111
133
  private handleSpotState;
112
134
  }
113
135
  //# sourceMappingURL=HyperliquidWsProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"HyperliquidWsProvider.d.ts","sourceRoot":"","sources":["../../../src/websocket/HyperliquidWsProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC3B,KAAK,aAAa,EAGlB,cAAc,EACd,KAAK,iBAAiB,EAEvB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAOL,KAAK,YAAY,EAElB,MAAM,mBAAmB,CAAA;AA8C1B;;;;;;;;GAQG;AACH,eAA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OAAO,CAAC,MAAM,CAAC,oBAAoB;YAOrB,eAAe;IA8B7B,8EAA8E;IAC9E,OAAO,CAAC,mBAAmB;IAoB3B,OAAO,CAAC,kBAAkB;IAuB1B;2EACuE;IACvE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAEnD;;+BAE2B;IAC3B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAM;IAEjD,OAAO,CAAC,uBAAuB;IAyC/B,OAAO,CAAC,oBAAoB;IA4C5B,OAAO,CAAC,eAAe;CA8BxB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/perps-sdk-provider-hyperliquid",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "description": "Hyperliquid provider plugin for @lifi/perps-sdk",
5
5
  "homepage": "https://github.com/lifinance/perps-sdk",
6
6
  "bugs": {
@@ -48,10 +48,10 @@
48
48
  "dependencies": {
49
49
  "big.js": "^7.0.1",
50
50
  "viem": "^2.48.8",
51
- "@lifi/perps-types": "^3.0.0"
51
+ "@lifi/perps-types": "^3.2.0"
52
52
  },
53
53
  "peerDependencies": {
54
- "@lifi/perps-sdk": "^4.1.0"
54
+ "@lifi/perps-sdk": "^4.3.0"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"
@@ -22,6 +22,7 @@ import {
22
22
  type HlUserFees,
23
23
  } from '../types/index.js'
24
24
  import {
25
+ partitionSpotBalances,
25
26
  perpsDexNames,
26
27
  spotAssetFromToken,
27
28
  spotBalance,
@@ -96,18 +97,13 @@ const buildBalances = (
96
97
  priceById: Map<string, number>,
97
98
  quoteAssetByCategory: Map<string, Asset>
98
99
  ): BalancePartition => {
99
- const balances: Balance[] = []
100
- const collateralBalances: Balance[] = []
101
-
102
- // Spot balances: collateral if and only if the token is a category quote asset.
103
- for (const b of spotState.balances) {
104
- const balance = spotBalance(spotAssetFromToken(b), b.total, priceById)
105
- if (quoteAssetIds.has(balance.asset.id)) {
106
- collateralBalances.push(balance)
107
- } else {
108
- balances.push(balance)
109
- }
110
- }
100
+ const { balances, collateralBalances } = partitionSpotBalances(
101
+ spotState.balances.map((b) =>
102
+ spotBalance(spotAssetFromToken(b), b.total, priceById)
103
+ ),
104
+ quoteAssetIds,
105
+ abstraction === HlAbstractionMode.PORTFOLIO_MARGIN
106
+ )
111
107
 
112
108
  // Unified/portfolio modes hold everything in spot — per-dex equity would
113
109
  // double-count. Only disabled/dexAbstraction carry separate venue collateral.
@@ -69,10 +69,12 @@ export type HlPreTransferCheck = {
69
69
 
70
70
  /**
71
71
  * Possible values returned by the `userAbstraction` info endpoint.
72
- * `null` means abstraction has never been set (standard mode).
72
+ * `null` means abstraction has never been set; `'default'`/`'disabled'` are
73
+ * the live and legacy spellings of standard (non-abstracted) mode.
73
74
  * @public
74
75
  */
75
76
  export const HlAbstractionMode = {
77
+ DEFAULT: 'default',
76
78
  DISABLED: 'disabled',
77
79
  UNIFIED_ACCOUNT: 'unifiedAccount',
78
80
  PORTFOLIO_MARGIN: 'portfolioMargin',
@@ -0,0 +1,85 @@
1
+ import type { Asset } from '@lifi/perps-types'
2
+
3
+ const HL_COIN_CDN = 'https://app.hyperliquid.xyz/coins'
4
+
5
+ const HYPE_LOGO_URI =
6
+ 'https://static.debank.com/image/hyper_token/logo_url/hyper/0b3e288cfe418e9ce69eef4c96374583.png'
7
+
8
+ /**
9
+ * Per-symbol logo overrides applied on top of the derived HL CDN URL, keyed by
10
+ * `Asset.displaySymbol`. Each entry notes why the default URL is wrong.
11
+ */
12
+ const BASE_ASSET_LOGO_OVERRIDES: Readonly<Record<string, string>> = {
13
+ // coins/HYPE.svg is low quality; the debank hyper_token PNG is the canonical icon.
14
+ HYPE: HYPE_LOGO_URI,
15
+ // coins/USDC_spot.svg doesn't exist — HL's app 200s with its SPA shell HTML
16
+ // instead of a 404, so the collateral asset needs the non-suffixed path.
17
+ USDC: `${HL_COIN_CDN}/USDC.svg`,
18
+ // Neither coins/USDT0.svg nor coins/USDT0_spot.svg serves an image (both 200
19
+ // with the SPA shell); USDT0 is HL's USDT deployment, so reuse USDT's icon.
20
+ USDT0: `${HL_COIN_CDN}/USDT.svg`,
21
+ }
22
+
23
+ /**
24
+ * Underlying spot symbol for each Unit-bridged token (`fullName` starts with
25
+ * "Unit"), keyed by the token `name`. Hand-verified that `coins/<symbol>.svg`
26
+ * serves a real image; the Unit-suffixed `NAME_spot.svg` path 200s with HTML
27
+ * instead. Unit tokens absent here have no working HL icon and intentionally
28
+ * resolve to '' (client letter avatar).
29
+ */
30
+ const UNIT_UNDERLYING_SYMBOL: Readonly<Record<string, string>> = {
31
+ UBTC: 'BTC',
32
+ UETH: 'ETH',
33
+ USOL: 'SOL',
34
+ UFART: 'FARTCOIN',
35
+ UPUMP: 'PUMP',
36
+ UUUSPX: 'SPX',
37
+ UVIRT: 'VIRTUAL',
38
+ UBONK: 'BONK',
39
+ UDOGE: 'DOGE',
40
+ UZEC: 'ZEC',
41
+ UMON: 'MON',
42
+ UWLD: 'WLD',
43
+ UENA: 'ENA',
44
+ UXPL: 'XPL',
45
+ UAVAX: 'AVAX',
46
+ UMEGA: 'MEGA',
47
+ UANSEM: 'ANSEM',
48
+ }
49
+
50
+ /** Token `name`s of the Unit-bridged HL spot assets. */
51
+ export const UNIT_TOKEN_NAMES: ReadonlySet<string> = new Set(
52
+ Object.keys(UNIT_UNDERLYING_SYMBOL)
53
+ )
54
+
55
+ /** Logo URI for a Unit-bridged spot token; '' when the underlying is unmapped. */
56
+ const unitSpotLogoURI = (name: string): string => {
57
+ const underlying = UNIT_UNDERLYING_SYMBOL[name]
58
+ return underlying === undefined ? '' : `${HL_COIN_CDN}/${underlying}.svg`
59
+ }
60
+
61
+ /** Return `asset` with its `logoURI` replaced when a display-symbol override exists. */
62
+ export const applyLogoOverride = (asset: Asset): Asset => {
63
+ const override = BASE_ASSET_LOGO_OVERRIDES[asset.displaySymbol]
64
+ return override === undefined ? asset : { ...asset, logoURI: override }
65
+ }
66
+
67
+ /**
68
+ * Override-aware logo URI for a Hyperliquid spot token. Base rule is
69
+ * `coins/${name}_spot.svg`; a Unit-bridged token (`fullName` starts with
70
+ * "Unit") resolves to its underlying's icon instead; the override table wins
71
+ * over both.
72
+ *
73
+ * When `fullName` is absent — spot *balances* (`HlSpotBalance`) carry no
74
+ * `fullName` and this issue deliberately does not fetch `spotMeta` on the
75
+ * account path — the derivation degrades to the symbol-keyed override + base
76
+ * `_spot` rule, so a Unit-bridged balance uses the base rule rather than its
77
+ * underlying's icon. The registry path (which has `fullName`) keeps full Unit
78
+ * resolution.
79
+ */
80
+ export const spotLogoURI = (name: string, fullName?: string | null): string => {
81
+ const base = fullName?.startsWith('Unit')
82
+ ? unitSpotLogoURI(name)
83
+ : `${HL_COIN_CDN}/${name}_spot.svg`
84
+ return BASE_ASSET_LOGO_OVERRIDES[name] ?? base
85
+ }
@@ -3,6 +3,11 @@
3
3
  // (e.g. `./assetId.js`, `./mapFill.js`) directly.
4
4
 
5
5
  export { assetIsSpot, calculateAssetId, getProviderIndex } from './assetId.js'
6
+ export {
7
+ applyLogoOverride,
8
+ spotLogoURI,
9
+ UNIT_TOKEN_NAMES,
10
+ } from './assetLogo.js'
6
11
  export {
7
12
  decodeCompressedJson,
8
13
  decodeFastAssetCtxs,
@@ -40,3 +45,4 @@ export {
40
45
  spotBalance,
41
46
  spotPriceById,
42
47
  } from './spotBalance.js'
48
+ export { partitionSpotBalances, type SpotPartition } from './spotCollateral.js'
@@ -5,20 +5,25 @@ import {
5
5
  PROVIDER_KEY,
6
6
  SPOT_MARKET_ID,
7
7
  } from '../constants.js'
8
+ import { applyLogoOverride } from './assetLogo.js'
8
9
 
9
10
  /**
10
- * Synthesise the {@link Asset} for a Hyperliquid wire coin. `id` and `logoURI`
11
- * keep the raw coin — the logo CDN is keyed by it, HIP-3 `dex:` prefix
12
- * included. `displaySymbol` drops the prefix: the category is carried by
13
- * `categoryId`, never by the display string.
11
+ * Synthesise the {@link Asset} for a Hyperliquid wire coin. `id` keeps the raw
12
+ * coin — the logo CDN is keyed by it, HIP-3 `dex:` prefix included.
13
+ * `displaySymbol` drops the prefix: the category is carried by `categoryId`,
14
+ * never by the display string. `logoURI` is the CDN URL corrected by the
15
+ * shared override table (see {@link applyLogoOverride}).
14
16
  * @public
15
17
  */
16
- export const coinAsset = (coin: string): Asset => ({
17
- providerId: PROVIDER_KEY,
18
- id: coin,
19
- displaySymbol: coin.includes(':') ? coin.slice(coin.indexOf(':') + 1) : coin,
20
- logoURI: `https://app.hyperliquid.xyz/coins/${coin}.svg`,
21
- })
18
+ export const coinAsset = (coin: string): Asset =>
19
+ applyLogoOverride({
20
+ providerId: PROVIDER_KEY,
21
+ id: coin,
22
+ displaySymbol: coin.includes(':')
23
+ ? coin.slice(coin.indexOf(':') + 1)
24
+ : coin,
25
+ logoURI: `https://app.hyperliquid.xyz/coins/${coin}.svg`,
26
+ })
22
27
 
23
28
  /**
24
29
  * Distinct wire `dex` names to fan `clearinghouseState` / `frontendOpenOrders`
@@ -2,6 +2,7 @@ import { stringToFloat } from '@lifi/perps-sdk'
2
2
  import type { Asset, Balance, Market } from '@lifi/perps-types'
3
3
  import { SPOT_MARKET_ID } from '../constants.js'
4
4
  import type { HlSpotBalance } from '../types/index.js'
5
+ import { spotLogoURI } from './assetLogo.js'
5
6
  import { coinAsset } from './marketDisplay.js'
6
7
 
7
8
  /**
@@ -31,11 +32,14 @@ export const spotPriceById = (
31
32
  /**
32
33
  * The held spot {@link Asset} for an HL balance: the venue token index
33
34
  * (`b.token`) as `Asset.id` — equal to that token's spot `Market.baseAsset.id`
34
- * — with display fields derived from the coin.
35
+ * — with display fields derived from the coin and the override-aware spot logo.
36
+ * `HlSpotBalance` carries no `fullName`, so Unit-bridged balances take the base
37
+ * `_spot` rule (see {@link spotLogoURI}).
35
38
  */
36
39
  export const spotAssetFromToken = (b: HlSpotBalance): Asset => ({
37
40
  ...coinAsset(b.coin),
38
41
  id: String(b.token),
42
+ logoURI: spotLogoURI(b.coin),
39
43
  })
40
44
 
41
45
  /** Assemble a typed spot {@link Balance} from its resolved asset and raw size. */
@@ -0,0 +1,50 @@
1
+ import type { Balance } from '@lifi/perps-types'
2
+
3
+ /**
4
+ * Portfolio-margin collateral beyond the category quote assets, keyed by spot
5
+ * display symbol with its loan-to-value weight. Hyperliquid credits these
6
+ * toward buying power at the given fraction (0.5 for both today). Quote assets
7
+ * (USDC/USDT) are already full-value collateral via the category model, so
8
+ * they are not listed here.
9
+ * https://hyperliquid.gitbook.io/hyperliquid-docs/trading/portfolio-margin
10
+ */
11
+ const PORTFOLIO_MARGIN_LTV: Readonly<Record<string, number>> = {
12
+ HYPE: 0.5,
13
+ UBTC: 0.5,
14
+ }
15
+
16
+ /** @internal */
17
+ export interface SpotPartition {
18
+ collateralBalances: Balance[]
19
+ balances: Balance[]
20
+ }
21
+
22
+ /**
23
+ * Split spot balances into margin collateral and flat holdings. A balance is
24
+ * collateral when its token is a category quote asset (full value) or, under
25
+ * portfolio margin, an LTV-weighted asset (HYPE/BTC); everything else is a
26
+ * flat holding.
27
+ */
28
+ export const partitionSpotBalances = (
29
+ spotBalances: readonly Balance[],
30
+ quoteAssetIds: ReadonlySet<string>,
31
+ portfolioMargin: boolean
32
+ ): SpotPartition => {
33
+ const collateralBalances: Balance[] = []
34
+ const balances: Balance[] = []
35
+ for (const balance of spotBalances) {
36
+ if (quoteAssetIds.has(balance.asset.id)) {
37
+ collateralBalances.push(balance)
38
+ continue
39
+ }
40
+ const ltv = portfolioMargin
41
+ ? PORTFOLIO_MARGIN_LTV[balance.asset.displaySymbol]
42
+ : undefined
43
+ if (ltv !== undefined) {
44
+ collateralBalances.push({ ...balance, collateralWeight: ltv })
45
+ } else {
46
+ balances.push(balance)
47
+ }
48
+ }
49
+ return { collateralBalances, balances }
50
+ }