#!/usr/bin/env bash # Production Deployment Script for MEV Bot # This script deploys the MEV bot to a production environment for live trading # Supports: Podman (preferred) and Docker set -euo pipefail # Exit on any error, undefined vars, pipe failures # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Script information echo -e "${PURPLE}๐Ÿš€ MEV Bot Production Deployment Script${NC}" echo -e "${PURPLE}=====================================${NC}" echo "" # Check if running from project root if [ ! -f "go.mod" ]; then echo -e "${RED}โŒ Error: This script must be run from the project root directory${NC}" exit 1 fi # Check if .env.production exists if [ ! -f ".env.production" ]; then echo -e "${RED}โŒ Error: .env.production file not found${NC}" echo -e "${YELLOW}Please create .env.production file with production configuration${NC}" exit 1 fi # Load production environment variables echo -e "${BLUE}๐Ÿ”ง Loading production environment variables...${NC}" source .env.production # Check required environment variables echo -e "${BLUE}๐Ÿ” Checking required environment variables...${NC}" REQUIRED_VARS=( "ARBITRUM_RPC_ENDPOINT" "ETHEREUM_PRIVATE_KEY" "ETHEREUM_ACCOUNT_ADDRESS" "CONTRACT_ARBITRAGE_EXECUTOR" "CONTRACT_FLASH_SWAPPER" "POSTGRES_PASSWORD" "MEV_BOT_ENCRYPTION_KEY" ) MISSING_VARS=() for var in "${REQUIRED_VARS[@]}"; do if [ -z "${!var}" ]; then MISSING_VARS+=("$var") fi done if [ ${#MISSING_VARS[@]} -ne 0 ]; then echo -e "${RED}โŒ Error: Missing required environment variables:${NC}" for var in "${MISSING_VARS[@]}"; do echo -e "${RED} - $var${NC}" done echo -e "${YELLOW}Please set these variables in .env.production${NC}" exit 1 fi echo -e "${GREEN}โœ… All required environment variables are set${NC}" # Create required directories echo -e "${BLUE}๐Ÿ“ Creating required directories...${NC}" mkdir -p data/production logs/production config keys # Build the application echo -e "${BLUE}๐Ÿ”จ Building MEV bot application...${NC}" GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/mev-bot-production cmd/mev-bot/main.go if [ $? -eq 0 ]; then echo -e "${GREEN}โœ… Application built successfully${NC}" else echo -e "${RED}โŒ Error: Failed to build application${NC}" exit 1 fi # Run tests to ensure application is working echo -e "${BLUE}๐Ÿงช Running tests...${NC}" go test -v ./pkg/... -short if [ $? -eq 0 ]; then echo -e "${GREEN}โœ… Tests passed${NC}" else echo -e "${RED}โŒ Error: Tests failed${NC}" exit 1 fi # Load container runtime detection source "$(dirname "$0")/container-runtime.sh" init if [[ -z "$CONTAINER_RUNTIME" ]]; then echo -e "${RED}โŒ Error: No container runtime found (podman or docker required)${NC}" exit 1 fi if [[ -z "$COMPOSE_CMD" ]]; then echo -e "${RED}โŒ Error: No compose command available${NC}" exit 1 fi echo -e "${GREEN}โœ… Container runtime available: $CONTAINER_RUNTIME${NC}" echo -e "${GREEN}โœ… Compose command: $COMPOSE_CMD${NC}" # Stop any existing containers echo -e "${BLUE}โน๏ธ Stopping any existing production containers...${NC}" $COMPOSE_CMD -f docker-compose.production.yaml down --remove-orphans 2>/dev/null || true # Pull latest images echo -e "${BLUE}โฌ‡๏ธ Pulling latest images...${NC}" $COMPOSE_CMD -f docker-compose.production.yaml pull # Build images echo -e "${BLUE}๐Ÿ”จ Building production images...${NC}" $COMPOSE_CMD -f docker-compose.production.yaml build # Start services echo -e "${BLUE}๐Ÿš€ Starting production services...${NC}" $COMPOSE_CMD -f docker-compose.production.yaml up -d # Wait for services to start echo -e "${BLUE}โณ Waiting for services to start...${NC}" sleep 30 # Check service status echo -e "${BLUE}๐Ÿ” Checking service status...${NC}" SERVICES_RUNNING=true SERVICES=("mev-bot-arbitrum" "mev-bot-redis" "mev-bot-postgres" "mev-bot-prometheus" "mev-bot-grafana" "mev-bot-fluentd") for service in "${SERVICES[@]}"; do if $CONTAINER_RUNTIME ps | grep -q "$service"; then echo -e "${GREEN}โœ… $service is running${NC}" else echo -e "${RED}โŒ $service is not running${NC}" SERVICES_RUNNING=false fi done if [ "$SERVICES_RUNNING" = true ]; then echo -e "${GREEN}๐ŸŽ‰ All production services started successfully!${NC}" echo "" echo -e "${BLUE}๐Ÿ“Š Monitoring endpoints:${NC}" echo -e " - MEV Bot Metrics: http://localhost:${METRICS_PORT:-9090}/metrics" echo -e " - MEV Bot Health: http://localhost:${HEALTH_PORT:-8080}/health" echo -e " - Prometheus: http://localhost:${PROMETHEUS_PORT:-9091}" echo -e " - Grafana: http://localhost:${GRAFANA_PORT:-3000}" echo "" echo -e "${BLUE}๐Ÿ“ Logs:${NC}" echo -e " - MEV Bot: $CONTAINER_RUNTIME logs mev-bot-arbitrum" echo -e " - Redis: $CONTAINER_RUNTIME logs mev-bot-redis" echo -e " - PostgreSQL: $CONTAINER_RUNTIME logs mev-bot-postgres" echo "" echo -e "${YELLOW}โš ๏ธ Remember to monitor the production environment closely during initial deployment${NC}" echo -e "${YELLOW}โš ๏ธ Start with small position sizes to validate everything works correctly${NC}" else echo -e "${RED}โŒ Some production services failed to start${NC}" echo -e "${YELLOW}Check logs with: $COMPOSE_CMD -f docker-compose.production.yaml logs${NC}" exit 1 fi echo "" echo -e "${GREEN}โœ… Production deployment completed successfully!${NC}" echo -e "${CYAN}๐Ÿš€ MEV Bot is now running in production mode${NC}" echo -e "${CYAN} Monitoring Arbitrum for profitable opportunities...${NC}"