# Meteora DLMM Copy Trading Bot A copy trading bot for [Meteora DLMM](https://www.meteora.ag/) (Dynamic Liquidity Market Maker) on Solana. Monitors leader wallets in real-time via WebSocket and automatically mirrors their DLMM liquidity operations with proportional scaling. ## Features - Real-time monitoring of leader wallets via Solana WebSocket (`onLogs`) - Automatic copy trading for DLMM operations: - Add liquidity (including by-strategy and one-side) - Remove liquidity (by range) - Rebalance liquidity - Close position - Proportional position scaling based on configurable copy ratio - Web dashboard for managing monitored wallets - SQLite database for tracking positions, trades, and activity logs - Docker support for production deployment ## Tech Stack - **Frontend**: Next.js 16, React 19, Tailwind CSS v4, lucide-react, sonner - **Backend**: Next.js API Routes, Prisma 7 + SQLite - **Worker**: Standalone TypeScript process (tsx) - **Solana**: @solana/web3.js, @coral-xyz/anchor, @meteora-ag/dlmm - **Package Manager**: pnpm ## Project Structure ``` src/ ├── app/ │ ├── api/ │ │ ├── health/ # Health check endpoint │ │ ├── positions/ # Leader/follower position queries │ │ ├── settings/ # Worker settings (copy ratio, slippage, etc.) │ │ ├── trades/ # Copy trade history │ │ └── wallets/ # CRUD for monitored wallets │ ├── layout.tsx # Root layout with Toaster │ ├── page.tsx # Wallet management dashboard │ └── globals.css ├── lib/ │ ├── db/ │ │ ├── prisma.ts # Prisma client singleton │ │ └── queries.ts # Database query helpers │ ├── meteora/ │ │ ├── dlmm-client.ts # DLMM SDK wrapper (open, remove, close, rebalance) │ │ └── types.ts # Shared types (ParsedDlmmOperation, etc.) │ ├── solana/ │ │ ├── connection.ts # RPC connection + follower keypair │ │ └── transaction-parser.ts # Decode DLMM instructions from transactions │ ├── constants.ts # Program IDs, discriminators, defaults │ ├── utils.ts # Helpers (sleep, getEnvOrThrow, etc.) │ └── validation.ts # Zod schemas for API input validation └── worker/ ├── index.ts # Worker entry point ├── monitor.ts # WebSocket transaction monitor ├── executor.ts # Copy trade executor ├── position-tracker.ts # Leader/follower position state tracking └── heartbeat.ts # WebSocket keepalive + reconnect ``` ## Prerequisites - Node.js 20+ - pnpm - A Solana RPC endpoint with WebSocket support (e.g. Helius, QuickNode) - A funded follower wallet (base58 private key) ## Setup ```bash # Install dependencies pnpm install # Create environment file cp .env.example .env.local ``` ### Environment Variables Create `.env.local` with: ```env # Solana RPC (must support WebSocket) RPC_ENDPOINT=https://your-rpc-endpoint.com WS_ENDPOINT=wss://your-ws-endpoint.com # Follower wallet private key (base58) FOLLOWER_PRIVATE_KEY=your_base58_private_key # Database DATABASE_URL=file:./dev.db # Optional settings COPY_RATIO=1.0 # Scale factor (1.0 = same size as leader) MAX_POSITION_SIZE_SOL=10 # Max SOL per position SLIPPAGE_BPS=100 # Slippage tolerance in basis points AUTO_COPY_ENABLED=true # Enable/disable auto copy ``` ## Development ```bash # Push database schema pnpm db:push # Start Next.js dev server (dashboard + API) pnpm dev # Start the copy trading worker (separate terminal) pnpm worker # Open Prisma Studio (database browser) pnpm db:studio ``` Open http://localhost:3000 to access the wallet management dashboard. ## Production (Docker) ```bash # Create production environment file cp .env.example .env.production # Build and start docker compose up -d # View logs docker compose logs -f ``` The Docker setup runs two containers: - **web**: Next.js standalone server (port 3000) - **worker**: Copy trading worker process Both share a SQLite database via a Docker volume. ## How It Works 1. **Monitor**: The worker subscribes to Solana WebSocket `onLogs` for each monitored wallet. When a transaction mentions the DLMM program, it fetches the full transaction data. 2. **Parse**: The transaction parser decodes DLMM instructions using Anchor's BorshInstructionCoder, extracting operation type, position/pair addresses, bin ranges, and amounts. 3. **Track**: The position tracker maintains leader position state in the database (open, bin range, strategy). 4. **Execute**: The executor mirrors the operation on the follower wallet using the DLMM SDK, scaling amounts by the configured copy ratio. ## API Endpoints | Method | Path | Description | |--------|------|-------------| | GET | `/api/wallets` | List all monitored wallets | | POST | `/api/wallets` | Add a wallet `{address, label?}` | | PATCH | `/api/wallets` | Toggle active `{id, isActive}` | | DELETE | `/api/wallets?id=xxx` | Remove a wallet | | GET | `/api/positions` | List leader/follower positions | | GET | `/api/trades` | List copy trade history | | GET/PATCH | `/api/settings` | Get/update worker settings | | GET | `/api/health` | Health check | ## License MIT