@hyperix/hooks 0.1.10 → 0.1.12
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 +1 -0
- package/dist/index.js +1 -0
- package/dist/use-historical-orders.js +20 -1
- package/dist/use-open-orders.d.ts +22 -0
- package/dist/use-open-orders.js +63 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -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) =>
|
|
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,22 @@
|
|
|
1
|
+
import { type UseSubscribeState } from "@outofgas/react-stream";
|
|
2
|
+
import type { OpenOrdersEvent } from "@nktkas/hyperliquid/api/subscription";
|
|
3
|
+
type RawOpenOrder = OpenOrdersEvent["orders"][number];
|
|
4
|
+
export type OpenOrderDirection = "Buy" | "Sell" | "Long" | "Short" | "Close Long" | "Close Short";
|
|
5
|
+
export type OpenOrder = RawOpenOrder & {
|
|
6
|
+
isSpot: boolean;
|
|
7
|
+
displayCoin: string;
|
|
8
|
+
direction: OpenOrderDirection;
|
|
9
|
+
assetId: number | undefined;
|
|
10
|
+
};
|
|
11
|
+
export type OpenOrdersData = {
|
|
12
|
+
dex: string;
|
|
13
|
+
user: `0x${string}`;
|
|
14
|
+
orders: OpenOrder[];
|
|
15
|
+
};
|
|
16
|
+
export type UseOpenOrdersOptions = {
|
|
17
|
+
dex?: string;
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
onUpdate?: (event: OpenOrdersEvent) => void;
|
|
20
|
+
};
|
|
21
|
+
export declare function useOpenOrders(user: `0x${string}`, options?: UseOpenOrdersOptions): UseSubscribeState<OpenOrdersData>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useSubscribe } from "@outofgas/react-stream";
|
|
2
|
+
import { wsClient } from "./config/hl.js";
|
|
3
|
+
import { useSymbolConverter } from "./use-symbol-converter.js";
|
|
4
|
+
const DEFAULT_DEX = "ALL_DEXS";
|
|
5
|
+
function compareOpenOrders(left, right) {
|
|
6
|
+
return right.timestamp - left.timestamp;
|
|
7
|
+
}
|
|
8
|
+
function formatOpenOrderDirection(order, isSpot) {
|
|
9
|
+
if (order.reduceOnly) {
|
|
10
|
+
return order.side === "A" ? "Close Long" : "Close Short";
|
|
11
|
+
}
|
|
12
|
+
if (order.side === "B") {
|
|
13
|
+
return isSpot ? "Buy" : "Long";
|
|
14
|
+
}
|
|
15
|
+
return isSpot ? "Sell" : "Short";
|
|
16
|
+
}
|
|
17
|
+
function formatOpenOrder(order, displayCoin, assetId) {
|
|
18
|
+
const isSpot = Boolean(displayCoin);
|
|
19
|
+
return {
|
|
20
|
+
...order,
|
|
21
|
+
isSpot,
|
|
22
|
+
displayCoin: displayCoin ?? order.coin,
|
|
23
|
+
direction: formatOpenOrderDirection(order, isSpot),
|
|
24
|
+
assetId,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function formatOpenOrders(event, symbolConverter) {
|
|
28
|
+
return {
|
|
29
|
+
dex: event.dex,
|
|
30
|
+
user: event.user,
|
|
31
|
+
orders: [...event.orders]
|
|
32
|
+
.map((order) => {
|
|
33
|
+
const displayCoin = symbolConverter?.getSpotByPairId(order.coin);
|
|
34
|
+
return formatOpenOrder(order, displayCoin, symbolConverter?.getAssetId(displayCoin ?? order.coin));
|
|
35
|
+
})
|
|
36
|
+
.sort(compareOpenOrders),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function useOpenOrders(user, options = {}) {
|
|
40
|
+
const { dex = DEFAULT_DEX, enabled: enabledOverride, onUpdate } = options;
|
|
41
|
+
const enabled = enabledOverride ?? Boolean(user);
|
|
42
|
+
const symbolConverter = useSymbolConverter();
|
|
43
|
+
return useSubscribe({
|
|
44
|
+
key: ["open-orders", user, dex ?? ""],
|
|
45
|
+
enabled,
|
|
46
|
+
subscribe: async ({ onData, onError }) => {
|
|
47
|
+
const subscription = await wsClient.openOrders({ user, dex }, (event) => {
|
|
48
|
+
try {
|
|
49
|
+
onUpdate?.(event);
|
|
50
|
+
onData(formatOpenOrders(event, symbolConverter));
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
onError(error instanceof Error
|
|
54
|
+
? error
|
|
55
|
+
: new Error("Failed to process open orders event"));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
unsubscribe: () => subscription.unsubscribe(),
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|