@hyperix/hooks 0.1.10 → 0.1.11

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,6 @@
1
1
  export * from "./use-l2-book.js";
2
2
  export * from "./use-historical-orders.js";
3
+ export * from "./use-open-orders.js";
3
4
  export * from "./use-trade-history.js";
4
5
  export * from "./use-trades.js";
5
6
  export * from "./use-order-history.js";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./use-l2-book.js";
2
2
  export * from "./use-historical-orders.js";
3
+ export * from "./use-open-orders.js";
3
4
  export * from "./use-trade-history.js";
4
5
  export * from "./use-trades.js";
5
6
  export * from "./use-order-history.js";
@@ -1,7 +1,26 @@
1
1
  import { useSubscribe } from "@outofgas/react-stream";
2
2
  import { wsClient } from "./config/hl.js";
3
+ function getHistoricalOrderStatusRank(status) {
4
+ if (status === "filled")
5
+ return 0;
6
+ if (status === "open")
7
+ return 1;
8
+ return 2;
9
+ }
3
10
  function sortHistoricalOrders(orderHistory) {
4
- return [...orderHistory].sort((a, b) => b.statusTimestamp - a.statusTimestamp);
11
+ return [...orderHistory].sort((a, b) => {
12
+ if (a.statusTimestamp !== b.statusTimestamp) {
13
+ return b.statusTimestamp - a.statusTimestamp;
14
+ }
15
+ const statusRankDelta = getHistoricalOrderStatusRank(a.status) - getHistoricalOrderStatusRank(b.status);
16
+ if (statusRankDelta !== 0) {
17
+ return statusRankDelta;
18
+ }
19
+ if (a.order.timestamp !== b.order.timestamp) {
20
+ return b.order.timestamp - a.order.timestamp;
21
+ }
22
+ return b.order.oid - a.order.oid;
23
+ });
5
24
  }
6
25
  function mergeHistoricalOrders(previousData, incomingEvent) {
7
26
  const orderHistory = incomingEvent.isSnapshot
@@ -0,0 +1,14 @@
1
+ import { type UseSubscribeState } from "@outofgas/react-stream";
2
+ import type { OpenOrdersEvent } from "@nktkas/hyperliquid/api/subscription";
3
+ export type OpenOrder = OpenOrdersEvent["orders"][number];
4
+ export type OpenOrdersData = {
5
+ dex: string;
6
+ user: `0x${string}`;
7
+ orders: OpenOrder[];
8
+ };
9
+ export type UseOpenOrdersOptions = {
10
+ dex?: string;
11
+ enabled?: boolean;
12
+ onUpdate?: (event: OpenOrdersEvent) => void;
13
+ };
14
+ export declare function useOpenOrders(user: `0x${string}`, options?: UseOpenOrdersOptions): UseSubscribeState<OpenOrdersData>;
@@ -0,0 +1,37 @@
1
+ import { useSubscribe } from "@outofgas/react-stream";
2
+ import { wsClient } from "./config/hl.js";
3
+ const DEFAULT_DEX = "ALL_DEXS";
4
+ function compareOpenOrders(left, right) {
5
+ return right.timestamp - left.timestamp;
6
+ }
7
+ function formatOpenOrders(event) {
8
+ return {
9
+ dex: event.dex,
10
+ user: event.user,
11
+ orders: [...event.orders].sort(compareOpenOrders),
12
+ };
13
+ }
14
+ export function useOpenOrders(user, options = {}) {
15
+ const { dex = DEFAULT_DEX, enabled: enabledOverride, onUpdate } = options;
16
+ const enabled = enabledOverride ?? Boolean(user);
17
+ return useSubscribe({
18
+ key: ["open-orders", user, dex ?? ""],
19
+ enabled,
20
+ subscribe: async ({ onData, onError }) => {
21
+ const subscription = await wsClient.openOrders({ user, dex }, (event) => {
22
+ try {
23
+ onUpdate?.(event);
24
+ onData(formatOpenOrders(event));
25
+ }
26
+ catch (error) {
27
+ onError(error instanceof Error
28
+ ? error
29
+ : new Error("Failed to process open orders event"));
30
+ }
31
+ });
32
+ return {
33
+ unsubscribe: () => subscription.unsubscribe(),
34
+ };
35
+ },
36
+ });
37
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperix/hooks",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",