@hyperix/hooks 0.1.20 → 0.1.22
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/README.md +34 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/use-max-builder-fee.d.ts +9 -0
- package/dist/use-max-builder-fee.js +11 -0
- package/dist/use-portfolio.d.ts +5 -0
- package/dist/use-portfolio.js +11 -0
- package/dist/use-spot-asset-ctxs.d.ts +8 -0
- package/dist/use-spot-asset-ctxs.js +25 -0
- package/dist/use-twap-states.d.ts +9 -0
- package/dist/use-twap-states.js +25 -0
- package/dist/use-user-twap-history.d.ts +11 -0
- package/dist/use-user-twap-history.js +56 -0
- package/dist/use-user-twap-slice-fills.d.ts +11 -0
- package/dist/use-user-twap-slice-fills.js +59 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,40 @@ React hooks for the local workspace.
|
|
|
6
6
|
|
|
7
7
|
This package is intended for source-first usage inside the monorepo. Consumers should reference it with `"workspace:*"` and let the app bundler resolve the TypeScript source.
|
|
8
8
|
|
|
9
|
+
## Demo
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
13
|
+
import { usePortfolio } from "@hyperix/hooks";
|
|
14
|
+
|
|
15
|
+
const queryClient = new QueryClient();
|
|
16
|
+
|
|
17
|
+
function PortfolioDemo() {
|
|
18
|
+
const { data, isPending, error } = usePortfolio("0x1234567890abcdef1234567890abcdef12345678");
|
|
19
|
+
|
|
20
|
+
if (isPending) {
|
|
21
|
+
return <div>Loading portfolio...</div>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (error) {
|
|
25
|
+
return <div>Failed to load portfolio: {error.message}</div>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const perpDay = data?.find(([period]) => period === "perpDay")?.[1];
|
|
29
|
+
const latestPnl = Number(perpDay?.pnlHistory.at(-1)?.[1] ?? 0);
|
|
30
|
+
|
|
31
|
+
return <pre>{JSON.stringify({ latestPnl, periods: data?.map(([period]) => period) }, null, 2)}</pre>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function App() {
|
|
35
|
+
return (
|
|
36
|
+
<QueryClientProvider client={queryClient}>
|
|
37
|
+
<PortfolioDemo />
|
|
38
|
+
</QueryClientProvider>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
9
43
|
## Tests
|
|
10
44
|
|
|
11
45
|
```bash
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./use-all-perp-metas.js";
|
|
|
3
3
|
export * from "./use-active-asset-data.js";
|
|
4
4
|
export * from "./use-all-dexs-asset-ctxs.js";
|
|
5
5
|
export * from "./use-l2-book.js";
|
|
6
|
+
export * from "./use-max-builder-fee.js";
|
|
6
7
|
export * from "./use-mid.js";
|
|
7
8
|
export * from "./use-all-dexs-clearing-house-state.js";
|
|
8
9
|
export * from "./use-historical-orders.js";
|
|
@@ -13,10 +14,15 @@ export * from "./use-order-history.js";
|
|
|
13
14
|
export * from "./use-perp-market.js";
|
|
14
15
|
export * from "./use-perp-markets.js";
|
|
15
16
|
export * from "./use-perp-meta.js";
|
|
17
|
+
export * from "./use-portfolio.js";
|
|
16
18
|
export * from "./use-positions.js";
|
|
19
|
+
export * from "./use-spot-asset-ctxs.js";
|
|
17
20
|
export * from "./use-spot-meta.js";
|
|
18
21
|
export * from "./use-symbol-converter.js";
|
|
22
|
+
export * from "./use-twap-states.js";
|
|
19
23
|
export * from "./use-user-fundings.js";
|
|
20
24
|
export * from "./use-user-fills.js";
|
|
21
25
|
export * from "./use-user-non-funding-ledger-updates.js";
|
|
22
26
|
export * from "./use-user-account-activity.js";
|
|
27
|
+
export * from "./use-user-twap-history.js";
|
|
28
|
+
export * from "./use-user-twap-slice-fills.js";
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./use-all-perp-metas.js";
|
|
|
3
3
|
export * from "./use-active-asset-data.js";
|
|
4
4
|
export * from "./use-all-dexs-asset-ctxs.js";
|
|
5
5
|
export * from "./use-l2-book.js";
|
|
6
|
+
export * from "./use-max-builder-fee.js";
|
|
6
7
|
export * from "./use-mid.js";
|
|
7
8
|
export * from "./use-all-dexs-clearing-house-state.js";
|
|
8
9
|
export * from "./use-historical-orders.js";
|
|
@@ -13,10 +14,15 @@ export * from "./use-order-history.js";
|
|
|
13
14
|
export * from "./use-perp-market.js";
|
|
14
15
|
export * from "./use-perp-markets.js";
|
|
15
16
|
export * from "./use-perp-meta.js";
|
|
17
|
+
export * from "./use-portfolio.js";
|
|
16
18
|
export * from "./use-positions.js";
|
|
19
|
+
export * from "./use-spot-asset-ctxs.js";
|
|
17
20
|
export * from "./use-spot-meta.js";
|
|
18
21
|
export * from "./use-symbol-converter.js";
|
|
22
|
+
export * from "./use-twap-states.js";
|
|
19
23
|
export * from "./use-user-fundings.js";
|
|
20
24
|
export * from "./use-user-fills.js";
|
|
21
25
|
export * from "./use-user-non-funding-ledger-updates.js";
|
|
22
26
|
export * from "./use-user-account-activity.js";
|
|
27
|
+
export * from "./use-user-twap-history.js";
|
|
28
|
+
export * from "./use-user-twap-slice-fills.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type UseQueryOptions, type UseQueryResult } from "@tanstack/react-query";
|
|
2
|
+
import type { MaxBuilderFeeResponse } from "@nktkas/hyperliquid/api/info";
|
|
3
|
+
export type MaxBuilderFeeData = MaxBuilderFeeResponse;
|
|
4
|
+
export type UseMaxBuilderFeeOptions = Omit<UseQueryOptions<MaxBuilderFeeData, Error, MaxBuilderFeeData, [
|
|
5
|
+
"max-builder-fee",
|
|
6
|
+
`0x${string}`,
|
|
7
|
+
`0x${string}`
|
|
8
|
+
]>, "queryKey" | "queryFn">;
|
|
9
|
+
export declare function useMaxBuilderFee(user: `0x${string}`, builder: `0x${string}`, options?: UseMaxBuilderFeeOptions): UseQueryResult<MaxBuilderFeeData, Error>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { infoClient } from "./config/hl.js";
|
|
3
|
+
export function useMaxBuilderFee(user, builder, options = {}) {
|
|
4
|
+
const enabled = options.enabled ?? Boolean(user && builder);
|
|
5
|
+
return useQuery({
|
|
6
|
+
queryKey: ["max-builder-fee", user, builder],
|
|
7
|
+
queryFn: () => infoClient.maxBuilderFee({ user, builder }),
|
|
8
|
+
...options,
|
|
9
|
+
enabled,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type UseQueryOptions, type UseQueryResult } from "@tanstack/react-query";
|
|
2
|
+
import type { PortfolioResponse } from "@nktkas/hyperliquid/api/info";
|
|
3
|
+
export type PortfolioData = PortfolioResponse;
|
|
4
|
+
export type UsePortfolioOptions = Omit<UseQueryOptions<PortfolioData, Error, PortfolioData, ["portfolio", `0x${string}`]>, "queryKey" | "queryFn">;
|
|
5
|
+
export declare function usePortfolio(user: `0x${string}`, options?: UsePortfolioOptions): UseQueryResult<PortfolioData, Error>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { infoClient } from "./config/hl.js";
|
|
3
|
+
export function usePortfolio(user, options = {}) {
|
|
4
|
+
const enabled = options.enabled ?? Boolean(user);
|
|
5
|
+
return useQuery({
|
|
6
|
+
queryKey: ["portfolio", user],
|
|
7
|
+
queryFn: () => infoClient.portfolio({ user }),
|
|
8
|
+
...options,
|
|
9
|
+
enabled,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type UseSubscribeState } from "@outofgas/react-stream";
|
|
2
|
+
import type { SpotAssetCtxsEvent } from "@nktkas/hyperliquid/api/subscription";
|
|
3
|
+
export type SpotAssetCtxsData = SpotAssetCtxsEvent;
|
|
4
|
+
export type UseSpotAssetCtxsOptions = {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
onUpdate?: (event: SpotAssetCtxsEvent) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare function useSpotAssetCtxs(options?: UseSpotAssetCtxsOptions): UseSubscribeState<SpotAssetCtxsData>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useSubscribe } from "@outofgas/react-stream";
|
|
2
|
+
import { wsClient } from "./config/hl.js";
|
|
3
|
+
export function useSpotAssetCtxs(options = {}) {
|
|
4
|
+
const { enabled = true, onUpdate } = options;
|
|
5
|
+
return useSubscribe({
|
|
6
|
+
key: ["spot-asset-ctxs"],
|
|
7
|
+
enabled,
|
|
8
|
+
subscribe: async ({ onData, onError }) => {
|
|
9
|
+
const subscription = await wsClient.spotAssetCtxs((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 spot asset ctxs event"));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
unsubscribe: () => subscription.unsubscribe(),
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type UseSubscribeState } from "@outofgas/react-stream";
|
|
2
|
+
import type { TwapStatesEvent } from "@nktkas/hyperliquid/api/subscription";
|
|
3
|
+
export type TwapStatesData = TwapStatesEvent;
|
|
4
|
+
export type UseTwapStatesOptions = {
|
|
5
|
+
dex?: string;
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
onUpdate?: (event: TwapStatesEvent) => void;
|
|
8
|
+
};
|
|
9
|
+
export declare function useTwapStates(user: `0x${string}`, options?: UseTwapStatesOptions): UseSubscribeState<TwapStatesData>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useSubscribe } from "@outofgas/react-stream";
|
|
2
|
+
import { wsClient } from "./config/hl.js";
|
|
3
|
+
const DEFAULT_DEX = "";
|
|
4
|
+
export function useTwapStates(user, options = {}) {
|
|
5
|
+
const { dex = DEFAULT_DEX, enabled: enabledOverride, onUpdate } = options;
|
|
6
|
+
const enabled = enabledOverride ?? Boolean(user);
|
|
7
|
+
return useSubscribe({
|
|
8
|
+
key: ["twap-states", user, dex],
|
|
9
|
+
enabled,
|
|
10
|
+
subscribe: async ({ onData, onError }) => {
|
|
11
|
+
const subscription = await wsClient.twapStates({ user, dex }, (event) => {
|
|
12
|
+
try {
|
|
13
|
+
onUpdate?.(event);
|
|
14
|
+
onData(event);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
onError(error instanceof Error ? error : new Error("Failed to process twap states event"));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
unsubscribe: () => subscription.unsubscribe(),
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type UseSubscribeState } from "@outofgas/react-stream";
|
|
2
|
+
import type { UserTwapHistoryEvent } from "@nktkas/hyperliquid/api/subscription";
|
|
3
|
+
export type UserTwapHistoryData = {
|
|
4
|
+
user: `0x${string}`;
|
|
5
|
+
history: UserTwapHistoryEvent["history"];
|
|
6
|
+
};
|
|
7
|
+
export type UseUserTwapHistoryOptions = {
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
onUpdate?: (event: UserTwapHistoryEvent) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function useUserTwapHistory(user: `0x${string}`, options?: UseUserTwapHistoryOptions): UseSubscribeState<UserTwapHistoryData>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { useSubscribe } from "@outofgas/react-stream";
|
|
3
|
+
import { wsClient } from "./config/hl.js";
|
|
4
|
+
export function useUserTwapHistory(user, options = {}) {
|
|
5
|
+
const { enabled: enabledOverride, onUpdate } = options;
|
|
6
|
+
const enabled = enabledOverride ?? Boolean(user);
|
|
7
|
+
const rawState = useSubscribe({
|
|
8
|
+
key: ["user-twap-history", user],
|
|
9
|
+
enabled,
|
|
10
|
+
subscribe: async ({ onData, onError }) => {
|
|
11
|
+
const subscription = await wsClient.userTwapHistory({ user }, (event) => {
|
|
12
|
+
try {
|
|
13
|
+
onUpdate?.(event);
|
|
14
|
+
onData(event);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
onError(error instanceof Error
|
|
18
|
+
? error
|
|
19
|
+
: new Error("Failed to process user twap history event"));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
unsubscribe: () => subscription.unsubscribe(),
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
const [data, setData] = useState(undefined);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!enabled) {
|
|
30
|
+
setData(undefined);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const nextEvent = rawState.data;
|
|
34
|
+
if (!nextEvent) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
setData((previous) => {
|
|
38
|
+
if (nextEvent.isSnapshot ||
|
|
39
|
+
!previous ||
|
|
40
|
+
previous.user !== nextEvent.user) {
|
|
41
|
+
return {
|
|
42
|
+
user: nextEvent.user,
|
|
43
|
+
history: nextEvent.history,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
user: nextEvent.user,
|
|
48
|
+
history: [...previous.history, ...nextEvent.history],
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}, [enabled, rawState.data]);
|
|
52
|
+
return useMemo(() => ({
|
|
53
|
+
...rawState,
|
|
54
|
+
data,
|
|
55
|
+
}), [data, rawState]);
|
|
56
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type UseSubscribeState } from "@outofgas/react-stream";
|
|
2
|
+
import type { UserTwapSliceFillsEvent } from "@nktkas/hyperliquid/api/subscription";
|
|
3
|
+
export type UserTwapSliceFillsData = {
|
|
4
|
+
user: `0x${string}`;
|
|
5
|
+
twapSliceFills: UserTwapSliceFillsEvent["twapSliceFills"];
|
|
6
|
+
};
|
|
7
|
+
export type UseUserTwapSliceFillsOptions = {
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
onUpdate?: (event: UserTwapSliceFillsEvent) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare function useUserTwapSliceFills(user: `0x${string}`, options?: UseUserTwapSliceFillsOptions): UseSubscribeState<UserTwapSliceFillsData>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { useSubscribe } from "@outofgas/react-stream";
|
|
3
|
+
import { wsClient } from "./config/hl.js";
|
|
4
|
+
export function useUserTwapSliceFills(user, options = {}) {
|
|
5
|
+
const { enabled: enabledOverride, onUpdate } = options;
|
|
6
|
+
const enabled = enabledOverride ?? Boolean(user);
|
|
7
|
+
const rawState = useSubscribe({
|
|
8
|
+
key: ["user-twap-slice-fills", user],
|
|
9
|
+
enabled,
|
|
10
|
+
subscribe: async ({ onData, onError }) => {
|
|
11
|
+
const subscription = await wsClient.userTwapSliceFills({ user }, (event) => {
|
|
12
|
+
try {
|
|
13
|
+
onUpdate?.(event);
|
|
14
|
+
onData(event);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
onError(error instanceof Error
|
|
18
|
+
? error
|
|
19
|
+
: new Error("Failed to process user twap slice fills event"));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
unsubscribe: () => subscription.unsubscribe(),
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
const [data, setData] = useState(undefined);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!enabled) {
|
|
30
|
+
setData(undefined);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const nextEvent = rawState.data;
|
|
34
|
+
if (!nextEvent) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
setData((previous) => {
|
|
38
|
+
if (nextEvent.isSnapshot ||
|
|
39
|
+
!previous ||
|
|
40
|
+
previous.user !== nextEvent.user) {
|
|
41
|
+
return {
|
|
42
|
+
user: nextEvent.user,
|
|
43
|
+
twapSliceFills: nextEvent.twapSliceFills,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
user: nextEvent.user,
|
|
48
|
+
twapSliceFills: [
|
|
49
|
+
...previous.twapSliceFills,
|
|
50
|
+
...nextEvent.twapSliceFills,
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}, [enabled, rawState.data]);
|
|
55
|
+
return useMemo(() => ({
|
|
56
|
+
...rawState,
|
|
57
|
+
data,
|
|
58
|
+
}), [data, rawState]);
|
|
59
|
+
}
|