Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ REDIS_URL=
# example: http://127.0.0.1:50051
WALLET_SERVICE_URL=

#HYPERLIQUID
HYPERLIQUID_PRIVATE_KEY=
HYPERLIQUID_TESTNET=false
HYPERLIQUID_API_PORT=3100
HYPERLIQUID_WALLET_SERVICE_URL=http://127.0.0.1:50052

#REST CLIENT TESTING
BASE_URL=
WALLET_ADDRESS=
Expand All @@ -47,4 +53,8 @@ TRACKED_WALLET_ADDRESS=
PUMP_FUN_TOKEN_ADDRESS=
RAYDIUM_TOKEN_ADDRESS=
JUPITER_TOKEN_ADDRESS=
WATCHLIST_ID=
WATCHLIST_ID=

#HYPERLIQUID REST CLIENT TESTING
HYPERLIQUID_BASE_URL=http://localhost:3100
HYPERLIQUID_TEST_ASSET=ETH
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ trading-common/src/generated/wallet.rs

CLAUDE.md

supabase/
supabase/

.vscode/
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ members = [
"trading-wallet",
"trading-price-feed",
"trading-sol-price-feed",
"hyperliquid-common",
"hyperliquid-api",
"hyperliquid-wallet",
]
resolver = "2"
[workspace.dependencies]
tokio = { version = "1.46.1", features = ["full"] }
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }
tokio-stream = "0.1.17"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
serde_json = "1.0.141"
uuid = { version = "1.17.0", features = ["serde", "v4"] }
chrono = { version = "0.4.41", features = ["serde"] }
postgrest = "1.6.0"
Expand All @@ -29,15 +32,15 @@ base64 = "0.22.1"
arrayref = "0.3.9"
futures-util = "0.3.31"
solana-sdk = "2.3.1"
solana-client = "2.3.3"
solana-account-decoder = "2.3.3"
solana-client = "2.3.5"
solana-account-decoder = "2.3.5"
solana-program = "2.3.0"
spl-token = "8.0.0"
surf = "2.3.2"
once_cell = "1.21.3"
spl-associated-token-account = "7.0.0"
arc-swap = "1.7.1"
solana-transaction-status = "2.3.3"
solana-transaction-status = "2.3.5"
borsh = "1.5.7"
bs58 = "0.5.1"
# parking_lot = "0.12.3" # REMOVED - using tokio::sync instead
Expand All @@ -46,7 +49,7 @@ scopeguard = "1.2.0"
backoff = "0.4.0"
tokio-native-tls = "0.3.1"
reqwest = { version = "0.12.22", features = ["json"] }
redis = { version = "0.32.3", features = [
redis = { version = "0.32.4", features = [
"tokio-comp",
"connection-manager",
"aio",
Expand All @@ -59,3 +62,4 @@ rust_decimal = "1.37.2"
hex = "0.4.3"
validator = { version = "0.20.0", features = ["derive"] }
tower = "0.5.2"
tonic-build = "0.13.1"
181 changes: 181 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Trading Platform

A multi-chain cryptocurrency trading platform supporting Solana DEXs and Hyperliquid perpetual futures.

## Architecture

This platform consists of multiple microservices organized into Solana and Hyperliquid ecosystems:

### Solana Services

#### Core Trading Services
- **trading-common**: Shared library with models, database client, DEX integrations (pump.fun, Raydium, Jupiter), Redis pool, WebSocket server, and gRPC protocol definitions
- **trading-api**: REST API server with CRUD operations and trade execution endpoints (port 3000)
- **trading-bot**: Core trading engine with WebSocket wallet monitoring and copy trading functionality (port 3001)
- **trading-wallet**: gRPC wallet management service for centralized wallet operations (port 50051)

#### Price Feed Services
- **trading-price-feed**: Real-time price monitoring for any Solana token using Raydium DEX vault subscriptions (port 3005)
- Zero-RPC polling approach via WebSocket subscriptions to vault account changes
- Automatic pool discovery for any token address
- Multi-layer caching (in-memory + Redis) with health monitoring
- Endpoints: `/health`, `/status`, `/ws` for real-time updates
- Publishes price updates to Redis and WebSocket clients

- **trading-sol-price-feed**: Dedicated SOL/USD price monitoring with dual data sources (port 3006)
- Primary: Pyth Network oracle (`7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE`) with confidence intervals
- Fallback: Raydium USDC/SOL pool (`58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2`)
- 1-second polling with automatic failover between sources
- Endpoints: `/price` (JSON), `/ws` for real-time SOL price streaming

### Hyperliquid Services

#### Core Trading Services
- **hyperliquid-common**: Shared types, error handling, and SDK wrapper for Hyperliquid API integration
- **hyperliquid-api**: REST API for Hyperliquid perpetual futures trading (port 3100)
- Market orders (long/short with slippage protection)
- Limit orders (GTC, IOC, ALO time-in-force options)
- Stop loss creation, modification, and cancellation
- Position and account management
- Order cancellation by order ID
- Asset universe querying
- **hyperliquid-wallet**: gRPC service for secure Hyperliquid wallet operations (port 50052)
- Private key management and transaction signing
- All trading operations routed through secure gRPC interface

## Service Dependencies

Services should be started in this order due to dependencies:

1. **Infrastructure**: `docker-compose up -d redis`
2. **Wallet Services**:
- `cargo run --bin trading-wallet`
- `cargo run --bin hyperliquid-wallet`
3. **API Servers**:
- `cargo run --bin trading-api`
- `cargo run --bin hyperliquid-api`
4. **Trading Bots**: `cargo run --bin trading-bot`
5. **Price Feeds** (optional):
- `cargo run --bin trading-price-feed`
- `cargo run --bin trading-sol-price-feed`

## Getting Started

### Prerequisites
- Rust 1.70+ with Cargo
- Docker and Docker Compose (for Redis)
- Solana RPC access (Helius, QuickNode, etc.)
- Hyperliquid account with API access

### Setup
1. Copy `.env.example` to `.env` and configure:
```bash
# Solana Configuration
SOLANA_RPC_HTTP_URL="your-solana-rpc-endpoint"
SOLANA_RPC_WS_URL="your-solana-websocket-endpoint"

# Hyperliquid Configuration
HYPERLIQUID_PRIVATE_KEY="your-hyperliquid-private-key"
HYPERLIQUID_TESTNET=false # Set to true for testnet

# Database (Supabase)
SUPABASE_URL="your-supabase-url"
SUPABASE_SERVICE_ROLE_KEY="your-service-role-key"

# Redis
REDIS_URL=redis://localhost:6379

# Wallet Management
SERVER_WALLET_SECRET_KEY="your-solana-wallet-private-key"
```

2. Start Redis: `docker-compose up -d redis`
3. Build the workspace: `cargo build --workspace`
4. Start services in dependency order (see above)

## Development

### Build Commands
```bash
# Build entire workspace
cargo build --workspace

# Build specific services
cargo build --bin trading-api
cargo build --bin hyperliquid-api
cargo build --bin trading-bot

# Release builds
cargo build --workspace --release
```

### Development Tools
```bash
# Watch and rebuild on changes
cargo watch -x "build --workspace"

# Run tests
cargo test --workspace

# Format code
cargo fmt --all

# Run linter
cargo clippy --workspace --all-targets

# Check for compilation errors
cargo check --workspace
```

### Protocol Buffers
- Definitions in `trading-common/proto/wallet.proto` and `hyperliquid-common/proto/wallet.proto`
- Auto-generated during build via build scripts
- Force recompilation: `cargo clean -p trading-common && cargo build`

## API Documentation

### Solana Trading
See `rest-client.http` for complete Solana API examples including:
- Wallet management: `/api/wallets/*`
- Copy trade settings: `/api/copy-trade-settings/*`
- Trade execution: `/api/trade/pump`, `/api/trade/raydium`, `/api/trade/jupiter`
- Transaction history: `/api/transactions/*`
- Watchlist management: `/api/watchlist/*`
- Token metadata: `/api/token/*`

### Hyperliquid Trading
See `hyperliquid-rest-client.http` for complete Hyperliquid API examples including:
- Market orders: `POST /api/trade/market`
- Limit orders: `POST /api/trade/limit`
- Position management: `GET /api/positions`
- Account info: `GET /api/account`
- Order cancellation: `POST /api/orders/{order_id}/cancel`
- Stop loss management: `/api/stop-loss/*`
- Asset universe: `GET /api/universe`

### Price Feed APIs
- **General tokens**: `ws://localhost:3005/ws` for real-time price updates
- **SOL price**: `GET http://localhost:3006/price` or `ws://localhost:3006/ws`

## Key Features

### Solana Integration
- Support for pump.fun, Raydium, and Jupiter DEX protocols
- Copy trading with configurable parameters
- Wallet-based authentication and multi-wallet support
- Real-time price feeds with automatic pool discovery
- Transaction history and watchlist management

### Hyperliquid Integration
- Perpetual futures trading (long/short positions)
- Advanced order types (market, limit, stop loss)
- Real-time position and account monitoring
- Risk management with stop loss automation
- Secure wallet operations via gRPC

### Infrastructure
- Redis-based event broadcasting and caching
- WebSocket real-time updates for frontend integration
- Microservice architecture with service discovery
- Comprehensive error handling and logging
- Docker support for easy deployment
24 changes: 24 additions & 0 deletions hyperliquid-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "hyperliquid-api"
version = "0.1.0"
edition = "2021"

[dependencies]
hyperliquid-common = { path = "../hyperliquid-common" }
trading-common = { path = "../trading-common" }
tokio = { workspace = true }
axum = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
uuid = { workspace = true }
chrono = { workspace = true }
anyhow = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
dotenv = { workspace = true }
redis = { workspace = true }
tower-http = { workspace = true }
tower = { workspace = true }
validator = { workspace = true }
rust_decimal = { workspace = true }
Loading