|
| 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 |
0 commit comments