@hyperix/hooks 0.1.17 → 0.1.19

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.
@@ -1,5 +1,8 @@
1
- import { HttpTransport, SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid";
1
+ import { HttpTransport, InfoClient, SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid";
2
2
  export declare const transport: HttpTransport;
3
+ export declare const infoClient: InfoClient<{
4
+ transport: HttpTransport;
5
+ }>;
3
6
  export declare const wsTransport: WebSocketTransport;
4
7
  export declare const wsClient: SubscriptionClient<{
5
8
  transport: WebSocketTransport;
package/dist/config/hl.js CHANGED
@@ -1,4 +1,5 @@
1
- import { HttpTransport, SubscriptionClient, WebSocketTransport } from "@nktkas/hyperliquid";
1
+ import { HttpTransport, InfoClient, SubscriptionClient, WebSocketTransport, } from "@nktkas/hyperliquid";
2
2
  export const transport = new HttpTransport();
3
+ export const infoClient = new InfoClient({ transport: transport });
3
4
  export const wsTransport = new WebSocketTransport();
4
5
  export const wsClient = new SubscriptionClient({ transport: wsTransport });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./use-all-mids.js";
2
+ export * from "./use-all-perp-metas.js";
2
3
  export * from "./use-active-asset-data.js";
3
4
  export * from "./use-all-dexs-asset-ctxs.js";
4
5
  export * from "./use-l2-book.js";
@@ -9,7 +10,11 @@ export * from "./use-open-orders.js";
9
10
  export * from "./use-trade-history.js";
10
11
  export * from "./use-trades.js";
11
12
  export * from "./use-order-history.js";
13
+ export * from "./use-perp-market.js";
14
+ export * from "./use-perp-markets.js";
15
+ export * from "./use-perp-meta.js";
12
16
  export * from "./use-positions.js";
17
+ export * from "./use-spot-meta.js";
13
18
  export * from "./use-symbol-converter.js";
14
19
  export * from "./use-user-fundings.js";
15
20
  export * from "./use-user-fills.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./use-all-mids.js";
2
+ export * from "./use-all-perp-metas.js";
2
3
  export * from "./use-active-asset-data.js";
3
4
  export * from "./use-all-dexs-asset-ctxs.js";
4
5
  export * from "./use-l2-book.js";
@@ -9,7 +10,11 @@ export * from "./use-open-orders.js";
9
10
  export * from "./use-trade-history.js";
10
11
  export * from "./use-trades.js";
11
12
  export * from "./use-order-history.js";
13
+ export * from "./use-perp-market.js";
14
+ export * from "./use-perp-markets.js";
15
+ export * from "./use-perp-meta.js";
12
16
  export * from "./use-positions.js";
17
+ export * from "./use-spot-meta.js";
13
18
  export * from "./use-symbol-converter.js";
14
19
  export * from "./use-user-fundings.js";
15
20
  export * from "./use-user-fills.js";
@@ -0,0 +1,5 @@
1
+ import { type UseQueryOptions, type UseQueryResult } from "@tanstack/react-query";
2
+ import type { AllPerpMetasResponse } from "@nktkas/hyperliquid/api/info";
3
+ export type AllPerpMetasData = AllPerpMetasResponse;
4
+ export type UseAllPerpMetasOptions = Omit<UseQueryOptions<AllPerpMetasData, Error, AllPerpMetasData, ["all-perp-metas"]>, "queryKey" | "queryFn">;
5
+ export declare function useAllPerpMetas(options?: UseAllPerpMetasOptions): UseQueryResult<AllPerpMetasData, Error>;
@@ -0,0 +1,9 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { infoClient } from "./config/hl.js";
3
+ export function useAllPerpMetas(options = {}) {
4
+ return useQuery({
5
+ queryKey: ["all-perp-metas"],
6
+ queryFn: () => infoClient.allPerpMetas(),
7
+ ...options,
8
+ });
9
+ }
@@ -0,0 +1,8 @@
1
+ import type { PerpMarket } from "./use-perp-markets.js";
2
+ import { type UsePerpMarketsOptions } from "./use-perp-markets.js";
3
+ export declare function usePerpMarket(coin: string, options?: UsePerpMarketsOptions): {
4
+ data: PerpMarket | undefined;
5
+ loading: boolean;
6
+ ready: boolean;
7
+ error: string | undefined;
8
+ };
@@ -0,0 +1,12 @@
1
+ import { useMemo } from "react";
2
+ import { usePerpMarkets } from "./use-perp-markets.js";
3
+ export function usePerpMarket(coin, options = {}) {
4
+ const marketsState = usePerpMarkets(options);
5
+ const data = useMemo(() => {
6
+ return marketsState.data.find((market) => market.symbol === coin || market.coin === coin);
7
+ }, [coin, marketsState.data]);
8
+ return {
9
+ ...marketsState,
10
+ data,
11
+ };
12
+ }
@@ -0,0 +1,29 @@
1
+ export type PerpMarket = {
2
+ symbol: string;
3
+ coin: string;
4
+ dex: string;
5
+ universeIndex: number;
6
+ collateralToken: string;
7
+ maxLeverage: number;
8
+ szDecimals: number;
9
+ onlyIsolated: boolean;
10
+ isDelisted: boolean;
11
+ markPrice: number;
12
+ midPrice: number | null;
13
+ oraclePrice: number;
14
+ premium: number | null;
15
+ fundingRate: number;
16
+ openInterest: number;
17
+ volume24h: number;
18
+ prevDayPrice: number;
19
+ };
20
+ export type UsePerpMarketsOptions = {
21
+ enabled?: boolean;
22
+ includeDelisted?: boolean;
23
+ };
24
+ export declare function usePerpMarkets(options?: UsePerpMarketsOptions): {
25
+ data: PerpMarket[];
26
+ loading: boolean;
27
+ ready: boolean;
28
+ error: string | undefined;
29
+ };
@@ -0,0 +1,74 @@
1
+ import { useMemo } from "react";
2
+ import { useAllDexsAssetCtxs } from "./use-all-dexs-asset-ctxs.js";
3
+ import { useAllPerpMetas } from "./use-all-perp-metas.js";
4
+ import { useSpotMeta } from "./use-spot-meta.js";
5
+ function getDexName(symbol) {
6
+ return symbol.includes(":") ? (symbol.split(":")[0] ?? "") : "";
7
+ }
8
+ function getCoinName(symbol) {
9
+ return symbol.includes(":") ? (symbol.split(":")[1] ?? symbol) : symbol;
10
+ }
11
+ function toNumber(value) {
12
+ const numericValue = Number(value);
13
+ return Number.isFinite(numericValue) ? numericValue : 0;
14
+ }
15
+ function toOptionalNumber(value) {
16
+ if (value === null || value === undefined) {
17
+ return null;
18
+ }
19
+ const numericValue = Number(value);
20
+ return Number.isFinite(numericValue) ? numericValue : null;
21
+ }
22
+ function buildCtxMap(data) {
23
+ return new Map(data?.ctxs ?? []);
24
+ }
25
+ export function usePerpMarkets(options = {}) {
26
+ const { enabled = true, includeDelisted = false } = options;
27
+ const allPerpMetasState = useAllPerpMetas({ enabled });
28
+ const spotMetaState = useSpotMeta({ enabled });
29
+ const assetCtxsState = useAllDexsAssetCtxs({ enabled });
30
+ const markets = useMemo(() => {
31
+ const allPerpMetas = allPerpMetasState.data;
32
+ const spotMeta = spotMetaState.data;
33
+ const assetCtxsByDex = buildCtxMap(assetCtxsState.data);
34
+ if (!allPerpMetas || !spotMeta) {
35
+ return [];
36
+ }
37
+ const tokenByIndex = new Map(spotMeta.tokens.map((token) => [token.index, token]));
38
+ return allPerpMetas
39
+ .flatMap((dexMetadata) => {
40
+ const dex = getDexName(dexMetadata.universe[0]?.name ?? "");
41
+ const collateralToken = tokenByIndex.get(dexMetadata.collateralToken)?.name ?? "USDC";
42
+ const ctxs = assetCtxsByDex.get(dex) ?? [];
43
+ return dexMetadata.universe.map((meta, universeIndex) => {
44
+ const ctx = ctxs[universeIndex];
45
+ return {
46
+ symbol: meta.name,
47
+ coin: getCoinName(meta.name),
48
+ dex,
49
+ universeIndex,
50
+ collateralToken,
51
+ maxLeverage: meta.maxLeverage,
52
+ szDecimals: meta.szDecimals,
53
+ onlyIsolated: meta.onlyIsolated ?? false,
54
+ isDelisted: meta.isDelisted ?? false,
55
+ markPrice: toNumber(ctx?.markPx),
56
+ midPrice: toOptionalNumber(ctx?.midPx),
57
+ oraclePrice: toNumber(ctx?.oraclePx),
58
+ premium: toOptionalNumber(ctx?.premium),
59
+ fundingRate: toNumber(ctx?.funding),
60
+ openInterest: toNumber(ctx?.openInterest),
61
+ volume24h: toNumber(ctx?.dayNtlVlm),
62
+ prevDayPrice: toNumber(ctx?.prevDayPx),
63
+ };
64
+ });
65
+ })
66
+ .filter((market) => includeDelisted || !market.isDelisted);
67
+ }, [allPerpMetasState.data, spotMetaState.data, assetCtxsState.data, includeDelisted]);
68
+ return {
69
+ data: markets,
70
+ loading: allPerpMetasState.isLoading || spotMetaState.isLoading || assetCtxsState.loading,
71
+ ready: allPerpMetasState.isSuccess && spotMetaState.isSuccess && assetCtxsState.ready,
72
+ error: allPerpMetasState.error?.message ?? spotMetaState.error?.message ?? assetCtxsState.error,
73
+ };
74
+ }
@@ -0,0 +1,17 @@
1
+ import type { AllPerpMetasData } from "./use-all-perp-metas.js";
2
+ import { type UseAllPerpMetasOptions } from "./use-all-perp-metas.js";
3
+ export type PerpMeta = AllPerpMetasData[number]["universe"][number];
4
+ export declare function usePerpMeta(coin: string, options?: UseAllPerpMetasOptions): {
5
+ isPending: boolean;
6
+ meta: {
7
+ szDecimals: number;
8
+ name: string;
9
+ maxLeverage: number;
10
+ marginTableId: number;
11
+ onlyIsolated?: true;
12
+ isDelisted?: true;
13
+ marginMode?: "strictIsolated" | "noCross";
14
+ growthMode?: "enabled";
15
+ lastGrowthModeChangeTime?: string;
16
+ } | undefined;
17
+ };
@@ -0,0 +1,13 @@
1
+ import { useMemo } from "react";
2
+ import { useAllPerpMetas, } from "./use-all-perp-metas.js";
3
+ export function usePerpMeta(coin, options = {}) {
4
+ const allPerpMetasState = useAllPerpMetas(options);
5
+ const meta = useMemo(() => {
6
+ const universes = allPerpMetasState.data?.flatMap((perpMeta) => perpMeta.universe) ?? [];
7
+ return universes.find((universe) => universe.name === coin);
8
+ }, [coin, allPerpMetasState.data]);
9
+ return {
10
+ isPending: allPerpMetasState.isPending,
11
+ meta,
12
+ };
13
+ }
@@ -4,8 +4,7 @@ export function usePositions(user, options = {}) {
4
4
  const positionsState = useAllDexsClearingHouseState(user, options);
5
5
  const data = useMemo(() => {
6
6
  return positionsState.data?.clearinghouseStates
7
- .map(([, state]) => state.assetPositions)
8
- .flatMap((positions) => positions)
7
+ .flatMap(([, state]) => state.assetPositions)
9
8
  .sort((left, right) => Number(left.position.positionValue) < Number(right.position.positionValue) ? 1 : -1);
10
9
  }, [positionsState.data]);
11
10
  return {
@@ -0,0 +1,5 @@
1
+ import { type UseQueryOptions, type UseQueryResult } from "@tanstack/react-query";
2
+ import type { SpotMetaResponse } from "@nktkas/hyperliquid";
3
+ export type SpotMetaData = SpotMetaResponse;
4
+ export type UseSpotMetaOptions = Omit<UseQueryOptions<SpotMetaData, Error, SpotMetaData, ["spot-meta"]>, "queryKey" | "queryFn">;
5
+ export declare function useSpotMeta(options?: UseSpotMetaOptions): UseQueryResult<SpotMetaData, Error>;
@@ -0,0 +1,9 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { infoClient } from "./config/hl.js";
3
+ export function useSpotMeta(options = {}) {
4
+ return useQuery({
5
+ queryKey: ["spot-meta"],
6
+ queryFn: () => infoClient.spotMeta(),
7
+ ...options,
8
+ });
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperix/hooks",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,6 +14,7 @@
14
14
  "typecheck": "tsc -p tsconfig.json --noEmit"
15
15
  },
16
16
  "dependencies": {
17
+ "@tanstack/react-query": "^5.90.2",
17
18
  "@nktkas/hyperliquid": "^0.32.1",
18
19
  "@outofgas/react-stream": "^0.1.4",
19
20
  "react": "^19.1.1"