Skip to content

Commit 2443291

Browse files
authored
Merge pull request #20 from BrandonFlorian/feature/hyperliquid
Add Hyperliquid perpetual futures trading support
2 parents 5627572 + d59d789 commit 2443291

21 files changed

Lines changed: 2743 additions & 7 deletions

File tree

.env.example

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ REDIS_URL=
3939
# example: http://127.0.0.1:50051
4040
WALLET_SERVICE_URL=
4141

42+
#HYPERLIQUID
43+
HYPERLIQUID_PRIVATE_KEY=
44+
HYPERLIQUID_TESTNET=false
45+
HYPERLIQUID_API_PORT=3100
46+
HYPERLIQUID_WALLET_SERVICE_URL=http://127.0.0.1:50052
47+
4248
#REST CLIENT TESTING
4349
BASE_URL=
4450
WALLET_ADDRESS=
@@ -47,4 +53,8 @@ TRACKED_WALLET_ADDRESS=
4753
PUMP_FUN_TOKEN_ADDRESS=
4854
RAYDIUM_TOKEN_ADDRESS=
4955
JUPITER_TOKEN_ADDRESS=
50-
WATCHLIST_ID=
56+
WATCHLIST_ID=
57+
58+
#HYPERLIQUID REST CLIENT TESTING
59+
HYPERLIQUID_BASE_URL=http://localhost:3100
60+
HYPERLIQUID_TEST_ASSET=ETH

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ trading-common/src/generated/wallet.rs
66

77
CLAUDE.md
88

9-
supabase/
9+
supabase/
10+
11+
.vscode/

Cargo.toml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ members = [
66
"trading-wallet",
77
"trading-price-feed",
88
"trading-sol-price-feed",
9+
"hyperliquid-common",
10+
"hyperliquid-api",
11+
"hyperliquid-wallet",
912
]
1013
resolver = "2"
1114
[workspace.dependencies]
1215
tokio = { version = "1.46.1", features = ["full"] }
1316
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }
1417
tokio-stream = "0.1.17"
1518
serde = { version = "1.0.219", features = ["derive"] }
16-
serde_json = "1.0.140"
19+
serde_json = "1.0.141"
1720
uuid = { version = "1.17.0", features = ["serde", "v4"] }
1821
chrono = { version = "0.4.41", features = ["serde"] }
1922
postgrest = "1.6.0"
@@ -29,15 +32,15 @@ base64 = "0.22.1"
2932
arrayref = "0.3.9"
3033
futures-util = "0.3.31"
3134
solana-sdk = "2.3.1"
32-
solana-client = "2.3.3"
33-
solana-account-decoder = "2.3.3"
35+
solana-client = "2.3.5"
36+
solana-account-decoder = "2.3.5"
3437
solana-program = "2.3.0"
3538
spl-token = "8.0.0"
3639
surf = "2.3.2"
3740
once_cell = "1.21.3"
3841
spl-associated-token-account = "7.0.0"
3942
arc-swap = "1.7.1"
40-
solana-transaction-status = "2.3.3"
43+
solana-transaction-status = "2.3.5"
4144
borsh = "1.5.7"
4245
bs58 = "0.5.1"
4346
# parking_lot = "0.12.3" # REMOVED - using tokio::sync instead
@@ -46,7 +49,7 @@ scopeguard = "1.2.0"
4649
backoff = "0.4.0"
4750
tokio-native-tls = "0.3.1"
4851
reqwest = { version = "0.12.22", features = ["json"] }
49-
redis = { version = "0.32.3", features = [
52+
redis = { version = "0.32.4", features = [
5053
"tokio-comp",
5154
"connection-manager",
5255
"aio",
@@ -59,3 +62,4 @@ rust_decimal = "1.37.2"
5962
hex = "0.4.3"
6063
validator = { version = "0.20.0", features = ["derive"] }
6164
tower = "0.5.2"
65+
tonic-build = "0.13.1"

README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Trading Platform
2+
3+
A multi-chain cryptocurrency trading platform supporting Solana DEXs and Hyperliquid perpetual futures.
4+
5+
## Architecture
6+
7+
This platform consists of multiple microservices organized into Solana and Hyperliquid ecosystems:
8+
9+
### Solana Services
10+
11+
#### Core Trading Services
12+
- **trading-common**: Shared library with models, database client, DEX integrations (pump.fun, Raydium, Jupiter), Redis pool, WebSocket server, and gRPC protocol definitions
13+
- **trading-api**: REST API server with CRUD operations and trade execution endpoints (port 3000)
14+
- **trading-bot**: Core trading engine with WebSocket wallet monitoring and copy trading functionality (port 3001)
15+
- **trading-wallet**: gRPC wallet management service for centralized wallet operations (port 50051)
16+
17+
#### Price Feed Services
18+
- **trading-price-feed**: Real-time price monitoring for any Solana token using Raydium DEX vault subscriptions (port 3005)
19+
- Zero-RPC polling approach via WebSocket subscriptions to vault account changes
20+
- Automatic pool discovery for any token address
21+
- Multi-layer caching (in-memory + Redis) with health monitoring
22+
- Endpoints: `/health`, `/status`, `/ws` for real-time updates
23+
- Publishes price updates to Redis and WebSocket clients
24+
25+
- **trading-sol-price-feed**: Dedicated SOL/USD price monitoring with dual data sources (port 3006)
26+
- Primary: Pyth Network oracle (`7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE`) with confidence intervals
27+
- Fallback: Raydium USDC/SOL pool (`58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2`)
28+
- 1-second polling with automatic failover between sources
29+
- Endpoints: `/price` (JSON), `/ws` for real-time SOL price streaming
30+
31+
### Hyperliquid Services
32+
33+
#### Core Trading Services
34+
- **hyperliquid-common**: Shared types, error handling, and SDK wrapper for Hyperliquid API integration
35+
- **hyperliquid-api**: REST API for Hyperliquid perpetual futures trading (port 3100)
36+
- Market orders (long/short with slippage protection)
37+
- Limit orders (GTC, IOC, ALO time-in-force options)
38+
- Stop loss creation, modification, and cancellation
39+
- Position and account management
40+
- Order cancellation by order ID
41+
- Asset universe querying
42+
- **hyperliquid-wallet**: gRPC service for secure Hyperliquid wallet operations (port 50052)
43+
- Private key management and transaction signing
44+
- All trading operations routed through secure gRPC interface
45+
46+
## Service Dependencies
47+
48+
Services should be started in this order due to dependencies:
49+
50+
1. **Infrastructure**: `docker-compose up -d redis`
51+
2. **Wallet Services**:
52+
- `cargo run --bin trading-wallet`
53+
- `cargo run --bin hyperliquid-wallet`
54+
3. **API Servers**:
55+
- `cargo run --bin trading-api`
56+
- `cargo run --bin hyperliquid-api`
57+
4. **Trading Bots**: `cargo run --bin trading-bot`
58+
5. **Price Feeds** (optional):
59+
- `cargo run --bin trading-price-feed`
60+
- `cargo run --bin trading-sol-price-feed`
61+
62+
## Getting Started
63+
64+
### Prerequisites
65+
- Rust 1.70+ with Cargo
66+
- Docker and Docker Compose (for Redis)
67+
- Solana RPC access (Helius, QuickNode, etc.)
68+
- Hyperliquid account with API access
69+
70+
### Setup
71+
1. Copy `.env.example` to `.env` and configure:
72+
```bash
73+
# Solana Configuration
74+
SOLANA_RPC_HTTP_URL="your-solana-rpc-endpoint"
75+
SOLANA_RPC_WS_URL="your-solana-websocket-endpoint"
76+
77+
# Hyperliquid Configuration
78+
HYPERLIQUID_PRIVATE_KEY="your-hyperliquid-private-key"
79+
HYPERLIQUID_TESTNET=false # Set to true for testnet
80+
81+
# Database (Supabase)
82+
SUPABASE_URL="your-supabase-url"
83+
SUPABASE_SERVICE_ROLE_KEY="your-service-role-key"
84+
85+
# Redis
86+
REDIS_URL=redis://localhost:6379
87+
88+
# Wallet Management
89+
SERVER_WALLET_SECRET_KEY="your-solana-wallet-private-key"
90+
```
91+
92+
2. Start Redis: `docker-compose up -d redis`
93+
3. Build the workspace: `cargo build --workspace`
94+
4. Start services in dependency order (see above)
95+
96+
## Development
97+
98+
### Build Commands
99+
```bash
100+
# Build entire workspace
101+
cargo build --workspace
102+
103+
# Build specific services
104+
cargo build --bin trading-api
105+
cargo build --bin hyperliquid-api
106+
cargo build --bin trading-bot
107+
108+
# Release builds
109+
cargo build --workspace --release
110+
```
111+
112+
### Development Tools
113+
```bash
114+
# Watch and rebuild on changes
115+
cargo watch -x "build --workspace"
116+
117+
# Run tests
118+
cargo test --workspace
119+
120+
# Format code
121+
cargo fmt --all
122+
123+
# Run linter
124+
cargo clippy --workspace --all-targets
125+
126+
# Check for compilation errors
127+
cargo check --workspace
128+
```
129+
130+
### Protocol Buffers
131+
- Definitions in `trading-common/proto/wallet.proto` and `hyperliquid-common/proto/wallet.proto`
132+
- Auto-generated during build via build scripts
133+
- Force recompilation: `cargo clean -p trading-common && cargo build`
134+
135+
## API Documentation
136+
137+
### Solana Trading
138+
See `rest-client.http` for complete Solana API examples including:
139+
- Wallet management: `/api/wallets/*`
140+
- Copy trade settings: `/api/copy-trade-settings/*`
141+
- Trade execution: `/api/trade/pump`, `/api/trade/raydium`, `/api/trade/jupiter`
142+
- Transaction history: `/api/transactions/*`
143+
- Watchlist management: `/api/watchlist/*`
144+
- Token metadata: `/api/token/*`
145+
146+
### Hyperliquid Trading
147+
See `hyperliquid-rest-client.http` for complete Hyperliquid API examples including:
148+
- Market orders: `POST /api/trade/market`
149+
- Limit orders: `POST /api/trade/limit`
150+
- Position management: `GET /api/positions`
151+
- Account info: `GET /api/account`
152+
- Order cancellation: `POST /api/orders/{order_id}/cancel`
153+
- Stop loss management: `/api/stop-loss/*`
154+
- Asset universe: `GET /api/universe`
155+
156+
### Price Feed APIs
157+
- **General tokens**: `ws://localhost:3005/ws` for real-time price updates
158+
- **SOL price**: `GET http://localhost:3006/price` or `ws://localhost:3006/ws`
159+
160+
## Key Features
161+
162+
### Solana Integration
163+
- Support for pump.fun, Raydium, and Jupiter DEX protocols
164+
- Copy trading with configurable parameters
165+
- Wallet-based authentication and multi-wallet support
166+
- Real-time price feeds with automatic pool discovery
167+
- Transaction history and watchlist management
168+
169+
### Hyperliquid Integration
170+
- Perpetual futures trading (long/short positions)
171+
- Advanced order types (market, limit, stop loss)
172+
- Real-time position and account monitoring
173+
- Risk management with stop loss automation
174+
- Secure wallet operations via gRPC
175+
176+
### Infrastructure
177+
- Redis-based event broadcasting and caching
178+
- WebSocket real-time updates for frontend integration
179+
- Microservice architecture with service discovery
180+
- Comprehensive error handling and logging
181+
- Docker support for easy deployment

hyperliquid-api/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "hyperliquid-api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
hyperliquid-common = { path = "../hyperliquid-common" }
8+
trading-common = { path = "../trading-common" }
9+
tokio = { workspace = true }
10+
axum = { workspace = true }
11+
serde = { workspace = true }
12+
serde_json = { workspace = true }
13+
uuid = { workspace = true }
14+
chrono = { workspace = true }
15+
anyhow = { workspace = true }
16+
thiserror = { workspace = true }
17+
tracing = { workspace = true }
18+
tracing-subscriber = { workspace = true }
19+
dotenv = { workspace = true }
20+
redis = { workspace = true }
21+
tower-http = { workspace = true }
22+
tower = { workspace = true }
23+
validator = { workspace = true }
24+
rust_decimal = { workspace = true }

0 commit comments

Comments
 (0)