@hyperix/hooks 0.1.15 → 0.1.17

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/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  export * from "./use-all-mids.js";
2
+ export * from "./use-active-asset-data.js";
3
+ export * from "./use-all-dexs-asset-ctxs.js";
2
4
  export * from "./use-l2-book.js";
5
+ export * from "./use-mid.js";
3
6
  export * from "./use-all-dexs-clearing-house-state.js";
4
7
  export * from "./use-historical-orders.js";
5
8
  export * from "./use-open-orders.js";
package/dist/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export * from "./use-all-mids.js";
2
+ export * from "./use-active-asset-data.js";
3
+ export * from "./use-all-dexs-asset-ctxs.js";
2
4
  export * from "./use-l2-book.js";
5
+ export * from "./use-mid.js";
3
6
  export * from "./use-all-dexs-clearing-house-state.js";
4
7
  export * from "./use-historical-orders.js";
5
8
  export * from "./use-open-orders.js";
@@ -0,0 +1,8 @@
1
+ import { type UseSubscribeState } from "@outofgas/react-stream";
2
+ import type { ActiveAssetDataEvent } from "@nktkas/hyperliquid/api/subscription";
3
+ export type ActiveAssetData = ActiveAssetDataEvent;
4
+ export type UseActiveAssetDataOptions = {
5
+ enabled?: boolean;
6
+ onUpdate?: (event: ActiveAssetDataEvent) => void;
7
+ };
8
+ export declare function useActiveAssetData(coin: string, user: `0x${string}`, options?: UseActiveAssetDataOptions): UseSubscribeState<ActiveAssetData>;
@@ -0,0 +1,26 @@
1
+ import { useSubscribe } from "@outofgas/react-stream";
2
+ import { wsClient } from "./config/hl.js";
3
+ export function useActiveAssetData(coin, user, options = {}) {
4
+ const { enabled: enabledOverride, onUpdate } = options;
5
+ const enabled = enabledOverride ?? Boolean(coin && user);
6
+ return useSubscribe({
7
+ key: ["active-asset-data", coin, user],
8
+ enabled,
9
+ subscribe: async ({ onData, onError }) => {
10
+ const subscription = await wsClient.activeAssetData({ coin, user }, (event) => {
11
+ try {
12
+ onUpdate?.(event);
13
+ onData(event);
14
+ }
15
+ catch (error) {
16
+ onError(error instanceof Error
17
+ ? error
18
+ : new Error("Failed to process active asset data event"));
19
+ }
20
+ });
21
+ return {
22
+ unsubscribe: () => subscription.unsubscribe(),
23
+ };
24
+ },
25
+ });
26
+ }
@@ -0,0 +1,8 @@
1
+ import { type UseSubscribeState } from "@outofgas/react-stream";
2
+ import type { AllDexsAssetCtxsEvent } from "@nktkas/hyperliquid/api/subscription";
3
+ export type AllDexsAssetCtxsData = AllDexsAssetCtxsEvent;
4
+ export type UseAllDexsAssetCtxsOptions = {
5
+ enabled?: boolean;
6
+ onUpdate?: (event: AllDexsAssetCtxsEvent) => void;
7
+ };
8
+ export declare function useAllDexsAssetCtxs(options?: UseAllDexsAssetCtxsOptions): UseSubscribeState<AllDexsAssetCtxsData>;
@@ -0,0 +1,25 @@
1
+ import { useSubscribe } from "@outofgas/react-stream";
2
+ import { wsClient } from "./config/hl.js";
3
+ export function useAllDexsAssetCtxs(options = {}) {
4
+ const { enabled = true, onUpdate } = options;
5
+ return useSubscribe({
6
+ key: ["all-dexs-asset-ctxs"],
7
+ enabled,
8
+ subscribe: async ({ onData, onError }) => {
9
+ const subscription = await wsClient.allDexsAssetCtxs((event) => {
10
+ try {
11
+ onUpdate?.(event);
12
+ onData(event);
13
+ }
14
+ catch (error) {
15
+ onError(error instanceof Error
16
+ ? error
17
+ : new Error("Failed to process all dexs asset ctxs event"));
18
+ }
19
+ });
20
+ return {
21
+ unsubscribe: () => subscription.unsubscribe(),
22
+ };
23
+ },
24
+ });
25
+ }
@@ -0,0 +1,9 @@
1
+ export type MidSource = "all-mids" | "all-dexs-asset-ctxs";
2
+ export type UseMidResult = {
3
+ data: string | undefined;
4
+ loading: boolean;
5
+ ready: boolean;
6
+ error: string | undefined;
7
+ source: MidSource | undefined;
8
+ };
9
+ export declare function useMid(coin: string): UseMidResult;
@@ -0,0 +1,64 @@
1
+ import { useMemo } from "react";
2
+ import { useAllDexsAssetCtxs } from "./use-all-dexs-asset-ctxs.js";
3
+ import { useAllMids } from "./use-all-mids.js";
4
+ import { useSymbolConverter } from "./use-symbol-converter.js";
5
+ function isSpotCoin(coin) {
6
+ return coin.includes("/");
7
+ }
8
+ function getPerpCtx(coin, assetCtxs, symbolConverter) {
9
+ if (!symbolConverter || !assetCtxs) {
10
+ return undefined;
11
+ }
12
+ const assetId = symbolConverter.getAssetId(coin);
13
+ if (assetId === undefined) {
14
+ return undefined;
15
+ }
16
+ const builder = coin.includes(":") ? coin.split(":")[0] : "";
17
+ const index = assetId % 10_000;
18
+ const prices = assetCtxs.ctxs.find(([dex]) => dex === builder)?.[1];
19
+ return prices?.[index];
20
+ }
21
+ export function useMid(coin) {
22
+ const symbolConverter = useSymbolConverter();
23
+ const spot = isSpotCoin(coin);
24
+ const midsState = useAllMids({ enabled: spot && Boolean(coin) });
25
+ const assetCtxsState = useAllDexsAssetCtxs({ enabled: !spot && Boolean(coin) });
26
+ return useMemo(() => {
27
+ if (!coin) {
28
+ return {
29
+ data: undefined,
30
+ loading: false,
31
+ ready: false,
32
+ error: undefined,
33
+ source: undefined,
34
+ };
35
+ }
36
+ if (!symbolConverter) {
37
+ return {
38
+ data: undefined,
39
+ loading: true,
40
+ ready: false,
41
+ error: undefined,
42
+ source: undefined,
43
+ };
44
+ }
45
+ if (spot) {
46
+ const id = symbolConverter.getSpotPairId(coin);
47
+ return {
48
+ data: id ? midsState.data?.mids?.[id] : undefined,
49
+ loading: midsState.loading,
50
+ ready: midsState.ready,
51
+ error: midsState.error,
52
+ source: "all-mids",
53
+ };
54
+ }
55
+ const ctx = getPerpCtx(coin, assetCtxsState.data, symbolConverter);
56
+ return {
57
+ data: ctx?.midPx ?? undefined,
58
+ loading: assetCtxsState.loading,
59
+ ready: assetCtxsState.ready,
60
+ error: assetCtxsState.error,
61
+ source: "all-dexs-asset-ctxs",
62
+ };
63
+ }, [coin, spot, symbolConverter, midsState.data, midsState.loading, midsState.ready, midsState.error, assetCtxsState.data, assetCtxsState.loading, assetCtxsState.ready, assetCtxsState.error]);
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperix/hooks",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@nktkas/hyperliquid": "^0.32.1",
18
- "@outofgas/react-stream": "0.1.3",
18
+ "@outofgas/react-stream": "^0.1.4",
19
19
  "react": "^19.1.1"
20
20
  },
21
21
  "devDependencies": {