-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathcancelOrders.ts
88 lines (83 loc) · 2.63 KB
/
cancelOrders.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
Action,
ActionExample,
IAgentRuntime,
Memory,
State,
HandlerCallback,
elizaLogger,
} from "@elizaos/core";
import { Hyperliquid } from "hyperliquid";
export const cancelOrders: Action = {
name: "CANCEL_ORDERS",
similes: ["CANCEL_ALL_ORDERS", "CANCEL", "CANCEL_ALL"],
description: "Cancel all open orders on Hyperliquid",
validate: async (runtime: IAgentRuntime) => {
return !!runtime.getSetting("HYPERLIQUID_PRIVATE_KEY");
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: Record<string, unknown>,
callback?: HandlerCallback
) => {
try {
// Initialize SDK
const sdk = new Hyperliquid({
privateKey: runtime.getSetting("HYPERLIQUID_PRIVATE_KEY"),
testnet: runtime.getSetting("HYPERLIQUID_TESTNET") === "true",
enableWs: false,
});
await sdk.connect();
elizaLogger.info("Cancelling all open orders...");
const result = await sdk.custom.cancelAllOrders();
elizaLogger.info("Cancel result:", result);
if (callback) {
const cancelledCount =
result?.response?.data?.statuses?.length || 0;
callback({
text:
cancelledCount > 0
? `Successfully cancelled ${cancelledCount} open order${cancelledCount > 1 ? "s" : ""}`
: "No open orders to cancel",
content: result,
});
}
return true;
} catch (error) {
elizaLogger.error("Error cancelling orders:", error);
if (callback) {
callback({
text: `Error cancelling orders: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Cancel all my orders",
},
},
{
user: "{{agent}}",
content: {
text: "I'll cancel all your open orders.",
action: "CANCEL_ORDERS",
},
},
{
user: "{{agent}}",
content: {
text: "Successfully cancelled 2 open orders",
},
},
],
] as ActionExample[][],
};
export default cancelOrders;