@hyperix/hooks 0.1.7 → 0.1.8

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,4 +1,5 @@
1
1
  export * from "./use-l2-book.js";
2
2
  export * from "./use-trade-history.js";
3
3
  export * from "./use-trades.js";
4
+ export * from "./use-user-fundings.js";
4
5
  export * from "./use-user-fills.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./use-l2-book.js";
2
2
  export * from "./use-trade-history.js";
3
3
  export * from "./use-trades.js";
4
+ export * from "./use-user-fundings.js";
4
5
  export * from "./use-user-fills.js";
@@ -0,0 +1,12 @@
1
+ import { type UseSubscribeState } from "@outofgas/react-stream";
2
+ import type { UserFundingsEvent } from "@nktkas/hyperliquid/api/subscription";
3
+ export type UserFunding = UserFundingsEvent["fundings"][number];
4
+ export type UserFundingsData = {
5
+ user: `0x${string}`;
6
+ fundings: UserFunding[];
7
+ };
8
+ export type UseUserFundingsOptions = {
9
+ enabled?: boolean;
10
+ onUpdate?: (event: UserFundingsEvent) => void;
11
+ };
12
+ export declare function useUserFundings(user: `0x${string}`, options?: UseUserFundingsOptions): UseSubscribeState<UserFundingsData>;
@@ -0,0 +1,43 @@
1
+ import { useSubscribe } from "@outofgas/react-stream";
2
+ import { wsClient } from "./config/hl.js";
3
+ function mergeUserFundings(previousData, incomingEvent) {
4
+ const fundings = incomingEvent.isSnapshot
5
+ ? incomingEvent.fundings
6
+ : [...(previousData?.fundings ?? []), ...incomingEvent.fundings];
7
+ if (incomingEvent.isSnapshot) {
8
+ return {
9
+ user: incomingEvent.user,
10
+ fundings: [...fundings].sort((a, b) => b.time - a.time),
11
+ };
12
+ }
13
+ return {
14
+ user: incomingEvent.user,
15
+ fundings: fundings.sort((a, b) => b.time - a.time),
16
+ };
17
+ }
18
+ export function useUserFundings(user, options = {}) {
19
+ const { enabled: enabledOverride, onUpdate } = options;
20
+ const enabled = enabledOverride ?? Boolean(user);
21
+ return useSubscribe({
22
+ key: ["user-fundings", user],
23
+ enabled,
24
+ subscribe: async ({ onData, onError }) => {
25
+ let data;
26
+ const subscription = await wsClient.userFundings({ user }, (event) => {
27
+ try {
28
+ if (!event.isSnapshot) {
29
+ onUpdate?.(event);
30
+ }
31
+ data = mergeUserFundings(data, event);
32
+ onData(data);
33
+ }
34
+ catch (error) {
35
+ onError(error instanceof Error ? error : new Error("Failed to process user fundings event"));
36
+ }
37
+ });
38
+ return {
39
+ unsubscribe: () => subscription.unsubscribe(),
40
+ };
41
+ },
42
+ });
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperix/hooks",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",