@hyperix/hooks 0.1.7 → 0.1.9

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,7 @@
1
1
  export * from "./use-l2-book.js";
2
+ export * from "./use-historical-orders.js";
2
3
  export * from "./use-trade-history.js";
3
4
  export * from "./use-trades.js";
5
+ export * from "./use-order-history.js";
6
+ export * from "./use-user-fundings.js";
4
7
  export * from "./use-user-fills.js";
package/dist/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  export * from "./use-l2-book.js";
2
+ export * from "./use-historical-orders.js";
2
3
  export * from "./use-trade-history.js";
3
4
  export * from "./use-trades.js";
5
+ export * from "./use-order-history.js";
6
+ export * from "./use-user-fundings.js";
4
7
  export * from "./use-user-fills.js";
@@ -0,0 +1,12 @@
1
+ import { type UseSubscribeState } from "@outofgas/react-stream";
2
+ import type { UserHistoricalOrdersEvent } from "@nktkas/hyperliquid/api/subscription";
3
+ export type HistoricalOrder = UserHistoricalOrdersEvent["orderHistory"][number];
4
+ export type HistoricalOrdersData = {
5
+ user: `0x${string}`;
6
+ orderHistory: HistoricalOrder[];
7
+ };
8
+ export type UseHistoricalOrdersOptions = {
9
+ enabled?: boolean;
10
+ onUpdate?: (event: UserHistoricalOrdersEvent) => void;
11
+ };
12
+ export declare function useHistoricalOrders(user: `0x${string}`, options?: UseHistoricalOrdersOptions): UseSubscribeState<HistoricalOrdersData>;
@@ -0,0 +1,42 @@
1
+ import { useSubscribe } from "@outofgas/react-stream";
2
+ import { wsClient } from "./config/hl.js";
3
+ function sortHistoricalOrders(orderHistory) {
4
+ return [...orderHistory].sort((a, b) => b.statusTimestamp - a.statusTimestamp);
5
+ }
6
+ function mergeHistoricalOrders(previousData, incomingEvent) {
7
+ const orderHistory = incomingEvent.isSnapshot
8
+ ? incomingEvent.orderHistory
9
+ : [...(previousData?.orderHistory ?? []), ...incomingEvent.orderHistory];
10
+ return {
11
+ user: incomingEvent.user,
12
+ orderHistory: sortHistoricalOrders(orderHistory),
13
+ };
14
+ }
15
+ export function useHistoricalOrders(user, options = {}) {
16
+ const { enabled: enabledOverride, onUpdate } = options;
17
+ const enabled = enabledOverride ?? Boolean(user);
18
+ return useSubscribe({
19
+ key: ["historical-orders", user],
20
+ enabled,
21
+ subscribe: async ({ onData, onError }) => {
22
+ let data;
23
+ const subscription = await wsClient.userHistoricalOrders({ user }, (event) => {
24
+ try {
25
+ if (!event.isSnapshot) {
26
+ onUpdate?.(event);
27
+ }
28
+ data = mergeHistoricalOrders(data, event);
29
+ onData(data);
30
+ }
31
+ catch (error) {
32
+ onError(error instanceof Error
33
+ ? error
34
+ : new Error("Failed to process user historical orders event"));
35
+ }
36
+ });
37
+ return {
38
+ unsubscribe: () => subscription.unsubscribe(),
39
+ };
40
+ },
41
+ });
42
+ }
@@ -0,0 +1,16 @@
1
+ import { type HistoricalOrder, type UseHistoricalOrdersOptions } from "./use-historical-orders.js";
2
+ export type OrderHistoryDirection = "Buy" | "Sell" | "Long" | "Short" | "Close Long" | "Close Short";
3
+ export type OrderHistory = HistoricalOrder & {
4
+ displayCoin: string;
5
+ direction: OrderHistoryDirection;
6
+ };
7
+ export type OrderHistoryData = {
8
+ user: `0x${string}`;
9
+ orderHistory: OrderHistory[];
10
+ };
11
+ export declare function useOrderHistory(user: `0x${string}`, options?: UseHistoricalOrdersOptions): {
12
+ data: OrderHistoryData | undefined;
13
+ ready: boolean;
14
+ loading: boolean;
15
+ error?: string;
16
+ };
@@ -0,0 +1,37 @@
1
+ import { useMemo } from "react";
2
+ import { useSymbolConverter } from "./use-symbol-converter.js";
3
+ import { useHistoricalOrders, } from "./use-historical-orders.js";
4
+ function formatOrderDirection(order, isSpot) {
5
+ if (order.reduceOnly) {
6
+ return order.side === "A" ? "Close Long" : "Close Short";
7
+ }
8
+ if (order.side === "B") {
9
+ return isSpot ? "Buy" : "Long";
10
+ }
11
+ return isSpot ? "Sell" : "Short";
12
+ }
13
+ function formatOrderHistoryEntry(historicalOrder, displayCoin) {
14
+ const isSpot = Boolean(displayCoin);
15
+ return {
16
+ ...historicalOrder,
17
+ displayCoin: displayCoin ?? historicalOrder.order.coin,
18
+ direction: formatOrderDirection(historicalOrder.order, isSpot),
19
+ };
20
+ }
21
+ export function useOrderHistory(user, options = {}) {
22
+ const historicalOrdersState = useHistoricalOrders(user, options);
23
+ const symbolConverter = useSymbolConverter();
24
+ const data = useMemo(() => {
25
+ if (!historicalOrdersState.data) {
26
+ return undefined;
27
+ }
28
+ return {
29
+ ...historicalOrdersState.data,
30
+ orderHistory: historicalOrdersState.data.orderHistory.map((historicalOrder) => formatOrderHistoryEntry(historicalOrder, symbolConverter?.getSpotByPairId(historicalOrder.order.coin))),
31
+ };
32
+ }, [historicalOrdersState.data, symbolConverter]);
33
+ return {
34
+ ...historicalOrdersState,
35
+ data,
36
+ };
37
+ }
@@ -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.9",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",