#!/bin/bash # Staging Deployment Script for MEV Bot # This script deploys the MEV bot to a staging environment for testing set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Script information echo -e "${BLUE}๐Ÿš€ MEV Bot Staging Deployment Script${NC}" echo -e "${BLUE}====================================${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.staging exists if [ ! -f ".env.staging" ]; then echo -e "${RED}โŒ Error: .env.staging file not found${NC}" echo -e "${YELLOW}Please create .env.staging file with staging configuration${NC}" exit 1 fi # Load staging environment variables echo -e "${BLUE}๐Ÿ”ง Loading staging environment variables...${NC}" source .env.staging # 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" ) 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.staging${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/staging logs/staging config keys # Build the application echo -e "${BLUE}๐Ÿ”จ Building MEV bot application...${NC}" go build -o bin/mev-bot-staging 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 # Check if Docker is available if ! command -v docker &> /dev/null; then echo -e "${RED}โŒ Error: Docker is not installed or not in PATH${NC}" exit 1 fi if ! command -v docker-compose &> /dev/null; then echo -e "${RED}โŒ Error: docker-compose is not installed or not in PATH${NC}" exit 1 fi echo -e "${GREEN}โœ… Docker and docker-compose are available${NC}" # Stop any existing containers echo -e "${BLUE}โน๏ธ Stopping any existing staging containers...${NC}" docker-compose -f docker-compose.staging.yaml down --remove-orphans 2>/dev/null || true # Pull latest images echo -e "${BLUE}โฌ‡๏ธ Pulling latest images...${NC}" docker-compose -f docker-compose.staging.yaml pull # Build images echo -e "${BLUE}๐Ÿ”จ Building staging images...${NC}" docker-compose -f docker-compose.staging.yaml build # Start services echo -e "${BLUE}๐Ÿš€ Starting staging services...${NC}" docker-compose -f docker-compose.staging.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-staging" "mev-bot-redis-staging" "mev-bot-postgres-staging" "mev-bot-prometheus-staging" "mev-bot-grafana-staging" "mev-bot-fluentd-staging") for service in "${SERVICES[@]}"; do if docker 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 staging services started successfully!${NC}" echo "" echo -e "${BLUE}๐Ÿ“Š Monitoring endpoints:${NC}" echo -e " - MEV Bot Metrics: http://localhost:${METRICS_PORT:-9091}/metrics" echo -e " - MEV Bot Health: http://localhost:${HEALTH_PORT:-8081}/health" echo -e " - Prometheus: http://localhost:${PROMETHEUS_PORT:-9092}" echo -e " - Grafana: http://localhost:${GRAFANA_PORT:-3001}" echo "" echo -e "${BLUE}๐Ÿ“ Logs:${NC}" echo -e " - MEV Bot: docker logs mev-bot-arbitrum-staging" echo -e " - Redis: docker logs mev-bot-redis-staging" echo -e " - PostgreSQL: docker logs mev-bot-postgres-staging" echo "" echo -e "${YELLOW}โš ๏ธ Remember to monitor the staging environment closely during testing${NC}" echo -e "${YELLOW}โš ๏ธ Staging uses real funds but with reduced position sizes for safety${NC}" else echo -e "${RED}โŒ Some staging services failed to start${NC}" echo -e "${YELLOW}Check logs with: docker-compose -f docker-compose.staging.yaml logs${NC}" exit 1 fi echo "" echo -e "${GREEN}โœ… Staging deployment completed successfully!${NC}"