Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rellfy authored Jan 11, 2025
2 parents c856f32 + 5bade12 commit 75e8e5a
Show file tree
Hide file tree
Showing 14 changed files with 832 additions and 2 deletions.
3 changes: 2 additions & 1 deletion agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@elizaos/plugin-arthera": "workspace:*",
"@elizaos/plugin-allora": "workspace:*",
"@elizaos/plugin-opacity": "workspace:*",
"@elizaos/plugin-hyperliquid": "workspace:*",
"@elizaos/plugin-akash": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
Expand All @@ -99,4 +100,4 @@
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
}
8 changes: 7 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { webSearchPlugin } from "@elizaos/plugin-web-search";
import { giphyPlugin } from "@elizaos/plugin-giphy";
import { letzAIPlugin } from "@elizaos/plugin-letzai";
import { thirdwebPlugin } from "@elizaos/plugin-thirdweb";

import { hyperliquidPlugin } from "@elizaos/plugin-hyperliquid";
import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";

import { OpacityAdapter } from "@elizaos/plugin-opacity";
Expand Down Expand Up @@ -758,6 +758,12 @@ export async function createAgent(
? artheraPlugin
: null,
getSecret(character, "ALLORA_API_KEY") ? alloraPlugin : null,
getSecret(character, "HYPERLIQUID_PRIVATE_KEY")
? hyperliquidPlugin
: null,
getSecret(character, "HYPERLIQUID_TESTNET")
? hyperliquidPlugin
: null,
getSecret(character, "AKASH_MNEMONIC") &&
getSecret(character, "AKASH_WALLET_ADDRESS")
? akashPlugin
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-hyperliquid/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
111 changes: 111 additions & 0 deletions packages/plugin-hyperliquid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Hyperliquid Plugin for Eliza

This plugin enables interaction with the Hyperliquid DEX through Eliza, providing spot trading capabilities.

## Features

- 💱 Spot Trading
- Market orders (immediate execution)
- Limit orders (price-specific)
- Smart price validation to prevent mistakes
- 📊 Price Checking
- Real-time price information
- 24h price change
- Volume statistics
- 🔄 Order Management
- Cancel all open orders
- Clear feedback on execution

## Installation

Add the plugin to your Eliza configuration:

```json
{
"plugins": ["@elizaos/plugin-hyperliquid"]
}
```

## Configuration

Set the following environment variables:

```env
HYPERLIQUID_PRIVATE_KEY=your_private_key # Required for trading and cancelling orders
HYPERLIQUID_TESTNET=true_or_false # Optional, defaults to false
```

## Available Actions

### 1. SPOT_TRADE

Place spot market or limit orders.

Examples:

```
# Market Orders
"buy 1 PIP" -> Buys 1 PIP at market price
"sell 2 HYPE" -> Sells 2 HYPE at market price
"market buy 1 ETH" -> Buys 1 ETH at market price
# Limit Orders
"buy 1 PIP at 20 USDC" -> Places buy order for 1 PIP at 20 USDC
"sell 0.5 HYPE at 21 USDC" -> Places sell order for 0.5 HYPE at 21 USDC
```

### 2. PRICE_CHECK

Get current price information for any token.

Examples:

```
"What's the price of PIP?"
"Check HYPE price"
"Get ETH price"
```

Returns: Current price, 24h change, and volume.

### 3. CANCEL_ORDERS

Cancel all your open orders.

Examples:

```
"Cancel all orders"
"Cancel my orders"
```

## Price Validation

The plugin includes smart price validation to prevent mistakes:

- Market Orders: Validates price is within ±50% of market price
- Limit Orders:
- Buy orders must be below market price
- Sell orders must be above market price
- Warns if price is very different from market (±80%)

## Error Handling

The plugin provides clear error messages for common issues:

- Invalid token symbols
- Price validation failures
- Network connection issues
- Order execution failures

## Security Notes

- Store your private key securely using environment variables
- Test with small amounts first
- Use testnet for initial testing
- Monitor your orders regularly
- Double-check prices before confirming trades

## License

MIT
3 changes: 3 additions & 0 deletions packages/plugin-hyperliquid/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
21 changes: 21 additions & 0 deletions packages/plugin-hyperliquid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@elizaos/plugin-hyperliquid",
"version": "0.1.7",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@elizaos/core": "workspace:*",
"hyperliquid": "^1.5.6",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20.0.0",
"tsup": "8.3.5"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
}
}
88 changes: 88 additions & 0 deletions packages/plugin-hyperliquid/src/actions/cancelOrders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
Loading

0 comments on commit 75e8e5a

Please sign in to comment.