# MEV Bot L2 Deployment Guide ## 🚀 Production Deployment - Arbitrum L2 Optimized This guide covers deploying the MEV bot with full L2 message processing capabilities for maximum competitive advantage on Arbitrum. ## Prerequisites ### System Requirements - **CPU**: 8+ cores (16+ recommended for high-frequency L2 processing) - **RAM**: 16GB minimum (32GB+ recommended) - **Storage**: 100GB SSD (fast I/O for L2 message processing) - **Network**: High-bandwidth, low-latency connection to Arbitrum nodes ### Required Services - **Arbitrum RPC Provider**: Alchemy, Infura, or QuickNode (WebSocket support required) - **Monitoring**: Prometheus + Grafana (optional but recommended) - **Alerting**: Slack/Discord webhooks for real-time alerts ## Quick Start ### 1. Clone and Build ```bash git clone https://github.com/your-org/mev-beta.git cd mev-beta go build -o mev-bot ./cmd/mev-bot/main.go ``` ### 2. Environment Setup ```bash # Create environment file cat > .env << EOF # Arbitrum L2 Configuration ARBITRUM_RPC_ENDPOINT="wss://arb-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY" ARBITRUM_WS_ENDPOINT="wss://arb-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY" # Performance Tuning BOT_MAX_WORKERS=25 BOT_CHANNEL_BUFFER_SIZE=1000 BOT_POLLING_INTERVAL=0.25 # Monitoring METRICS_ENABLED=true METRICS_PORT=9090 # Alerting SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK" DISCORD_WEBHOOK="https://discord.com/api/webhooks/YOUR/DISCORD/WEBHOOK" EOF ``` ### 3. Production Configuration ```bash # Copy production config cp config/config.production.yaml config/config.yaml # Update with your API keys sed -i 's/YOUR_ALCHEMY_KEY/your-actual-key/g' config/config.yaml sed -i 's/YOUR_INFURA_KEY/your-actual-key/g' config/config.yaml ``` ### 4. Start the Bot ```bash # With environment variables source .env && ./mev-bot start # Or with direct flags METRICS_ENABLED=true ./mev-bot start ``` ## Advanced Deployment ### Docker Deployment ```dockerfile # Dockerfile FROM golang:1.24-alpine AS builder WORKDIR /app COPY . . RUN go build -o mev-bot ./cmd/mev-bot/main.go FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=builder /app/mev-bot . COPY --from=builder /app/config ./config CMD ["./mev-bot", "start"] ``` ```bash # Build and run docker build -t mev-bot . docker run -d \ --name mev-bot-production \ -p 9090:9090 \ -p 8080:8080 \ -e ARBITRUM_RPC_ENDPOINT="wss://your-endpoint" \ -e METRICS_ENABLED=true \ -v $(pwd)/data:/data \ -v $(pwd)/logs:/var/log/mev-bot \ mev-bot ``` ### Docker Compose (Recommended) The project includes a production-ready `docker-compose.yml` with auto-restart capabilities: ```bash # 1. Copy the environment template cp .env.example .env # 2. Edit .env with your configuration nano .env # 3. Build and start the container docker compose up -d # 4. Check status docker compose ps docker compose logs -f mev-bot ``` The `docker-compose.yml` includes: - `restart: always` - Container restarts automatically on failure or reboot - Health checks - Monitors container health - Resource limits - Prevents resource exhaustion - Security hardening - Runs with minimal privileges #### Auto-Start on Boot with systemd To ensure the Docker container starts on boot, create a systemd service: ```bash # 1. Copy the service file sudo cp scripts/mev-bot.service /etc/systemd/system/ # 2. Reload systemd sudo systemctl daemon-reload # 3. Enable auto-start on boot sudo systemctl enable mev-bot.service # 4. Start the service sudo systemctl start mev-bot.service # 5. Check status sudo systemctl status mev-bot.service ``` The systemd service will: - Start automatically on system boot - Pull latest image on start (if configured) - Restart on failure - Integrate with system logs (`journalctl -u mev-bot`) #### Managing the Production Container ```bash # View logs docker compose logs -f mev-bot # Restart the container docker compose restart mev-bot # Or using systemd sudo systemctl restart mev-bot # Stop the container docker compose down # Or using systemd sudo systemctl stop mev-bot # Update and restart git pull origin master docker compose up -d --build # Or using systemd sudo systemctl reload mev-bot # View container stats docker stats mev-bot-production ``` ### Advanced Docker Compose Configuration For a full monitoring stack with Prometheus and Grafana: ```yaml # docker-compose.full.yml (example) version: '3.8' services: mev-bot: build: . container_name: mev-bot-l2 restart: always ports: - "9090:9090" # Metrics - "8080:8080" # Health checks environment: - ARBITRUM_RPC_ENDPOINT=${ARBITRUM_RPC_ENDPOINT} - ARBITRUM_WS_ENDPOINT=${ARBITRUM_WS_ENDPOINT} - BOT_MAX_WORKERS=25 - BOT_CHANNEL_BUFFER_SIZE=1000 - METRICS_ENABLED=true - LOG_LEVEL=info volumes: - ./data:/data - ./logs:/var/log/mev-bot - ./config:/app/config networks: - mev-network healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"] interval: 30s timeout: 10s retries: 3 prometheus: image: prom/prometheus:latest container_name: mev-prometheus restart: unless-stopped ports: - "9091:9090" volumes: - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml - prometheus_data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.console.libraries=/etc/prometheus/console_libraries' - '--web.console.templates=/etc/prometheus/consoles' networks: - mev-network grafana: image: grafana/grafana:latest container_name: mev-grafana restart: unless-stopped ports: - "3000:3000" environment: - GF_SECURITY_ADMIN_PASSWORD=admin volumes: - grafana_data:/var/lib/grafana - ./monitoring/grafana-dashboard.json:/etc/grafana/provisioning/dashboards/mev-dashboard.json networks: - mev-network volumes: prometheus_data: grafana_data: networks: mev-network: driver: bridge ``` ### Monitoring Setup #### Prometheus Configuration ```yaml # monitoring/prometheus.yml global: scrape_interval: 15s scrape_configs: - job_name: 'mev-bot' static_configs: - targets: ['mev-bot:9090'] scrape_interval: 5s metrics_path: '/metrics/prometheus' ``` #### Grafana Dashboard ```json { "dashboard": { "title": "MEV Bot L2 Monitoring", "panels": [ { "title": "L2 Messages/Second", "type": "graph", "targets": [ { "expr": "mev_bot_l2_messages_per_second" } ] }, { "title": "Net Profit (ETH)", "type": "singlestat", "targets": [ { "expr": "mev_bot_net_profit_eth" } ] }, { "title": "Error Rate", "type": "graph", "targets": [ { "expr": "mev_bot_error_rate" } ] } ] } } ``` ## Performance Optimization ### L2 Message Processing Tuning #### High-Frequency Settings ```yaml # config/config.yaml bot: polling_interval: 0.1 # 100ms polling max_workers: 50 # High worker count channel_buffer_size: 2000 # Large buffers arbitrum: rate_limit: requests_per_second: 100 # Aggressive rate limits max_concurrent: 50 burst: 200 ``` #### Memory Optimization ```bash # System tuning echo 'vm.swappiness=1' >> /etc/sysctl.conf echo 'net.core.rmem_max=134217728' >> /etc/sysctl.conf echo 'net.core.wmem_max=134217728' >> /etc/sysctl.conf sysctl -p ``` ### Database Optimization ```yaml database: max_open_connections: 100 max_idle_connections: 50 connection_max_lifetime: "1h" ``` ## Security Configuration ### Network Security ```bash # Firewall rules ufw allow 22/tcp # SSH ufw allow 9090/tcp # Metrics (restrict to monitoring IPs) ufw allow 8080/tcp # Health checks (restrict to load balancer) ufw deny 5432/tcp # Block database access ufw enable ``` ### API Key Security ```bash # Use environment variables, never hardcode export ARBITRUM_RPC_ENDPOINT="wss://..." export ALCHEMY_API_KEY="..." # Or use secrets management kubectl create secret generic mev-bot-secrets \ --from-literal=rpc-endpoint="wss://..." \ --from-literal=api-key="..." ``` ### Process Security ```bash # Run as non-root user useradd -r -s /bin/false mevbot chown -R mevbot:mevbot /app sudo -u mevbot ./mev-bot start ``` ## Monitoring and Alerting ### Health Checks ```bash # Simple health check curl http://localhost:8080/health # Detailed metrics curl http://localhost:9090/metrics | grep mev_bot ``` ### Alert Rules ```yaml # alertmanager.yml groups: - name: mev-bot-alerts rules: - alert: HighErrorRate expr: mev_bot_error_rate > 0.05 for: 5m labels: severity: critical annotations: summary: "MEV Bot error rate is high" - alert: L2MessageLag expr: mev_bot_l2_message_lag_ms > 1000 for: 2m labels: severity: critical annotations: summary: "L2 message processing lag detected" - alert: LowProfitability expr: mev_bot_net_profit_eth < 0 for: 10m labels: severity: warning annotations: summary: "Bot is losing money" ``` ## Troubleshooting ### Common Issues #### L2 Message Subscription Failures ```bash # Check WebSocket connectivity wscat -c wss://arb-mainnet.g.alchemy.com/v2/YOUR_KEY # Verify endpoints curl -X POST -H "Content-Type: application/json" \ --data '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' \ https://arb-mainnet.g.alchemy.com/v2/YOUR_KEY ``` #### High Memory Usage ```bash # Monitor memory htop # Check for memory leaks pprof http://localhost:9090/debug/pprof/heap ``` #### Rate Limiting Issues ```bash # Check rate limit status grep "rate limit" /var/log/mev-bot/mev-bot.log # Adjust rate limits in config ``` ### Logs Analysis ```bash # Real-time log monitoring tail -f /var/log/mev-bot/mev-bot.log | grep "L2 message" # Error analysis grep "ERROR" /var/log/mev-bot/mev-bot.log | tail -20 # Performance metrics grep "processing_latency" /var/log/mev-bot/mev-bot.log ``` ## Backup and Recovery ### Data Backup ```bash # Backup database cp /data/mev-bot-production.db /backup/mev-bot-$(date +%Y%m%d).db # Backup configuration tar -czf /backup/mev-bot-config-$(date +%Y%m%d).tar.gz config/ ``` ### Disaster Recovery ```bash # Quick recovery systemctl stop mev-bot cp /backup/mev-bot-YYYYMMDD.db /data/mev-bot-production.db systemctl start mev-bot ``` ## Scaling ### Horizontal Scaling ```yaml # kubernetes deployment apiVersion: apps/v1 kind: Deployment metadata: name: mev-bot-l2 spec: replicas: 3 selector: matchLabels: app: mev-bot template: metadata: labels: app: mev-bot spec: containers: - name: mev-bot image: mev-bot:latest resources: requests: cpu: 2000m memory: 4Gi limits: cpu: 4000m memory: 8Gi ``` ### Load Balancing ```nginx # nginx.conf upstream mev-bot { server mev-bot-1:8080; server mev-bot-2:8080; server mev-bot-3:8080; } server { listen 80; location /health { proxy_pass http://mev-bot; } } ``` ## Performance Benchmarks ### Expected Performance - **L2 Messages/Second**: 500-1000 msgs/sec - **Processing Latency**: <100ms average - **Memory Usage**: 2-4GB under load - **CPU Usage**: 50-80% with 25 workers ### Optimization Targets - **Error Rate**: <1% - **Uptime**: >99.9% - **Profit Margin**: >10% after gas costs ## Support and Maintenance ### Regular Maintenance ```bash # Weekly tasks ./scripts/health-check.sh ./scripts/performance-report.sh ./scripts/profit-analysis.sh # Monthly tasks ./scripts/log-rotation.sh ./scripts/database-cleanup.sh ./scripts/config-backup.sh ``` ### Updates ```bash # Update bot git pull origin main go build -o mev-bot ./cmd/mev-bot/main.go systemctl restart mev-bot # Update dependencies go mod tidy go mod vendor ``` --- ## ⚡ Quick Commands ```bash # Start with monitoring METRICS_ENABLED=true ./mev-bot start # Check health curl localhost:8080/health # View metrics curl localhost:9090/metrics # Check logs tail -f logs/mev-bot.log # Stop gracefully pkill -SIGTERM mev-bot ``` **Your MEV bot is now ready for production deployment with full L2 message processing capabilities!** 🚀