Files
mev-beta/orig/scripts/apply-critical-fixes.sh
Administrator c54c569f30 refactor: move all remaining files to orig/ directory
Completed clean root directory structure:
- Root now contains only: .git, .env, docs/, orig/
- Moved all remaining files and directories to orig/:
  - Config files (.claude, .dockerignore, .drone.yml, etc.)
  - All .env variants (except active .env)
  - Git config (.gitconfig, .github, .gitignore, etc.)
  - Tool configs (.golangci.yml, .revive.toml, etc.)
  - Documentation (*.md files, @prompts)
  - Build files (Dockerfiles, Makefile, go.mod, go.sum)
  - Docker compose files
  - All source directories (scripts, tests, tools, etc.)
  - Runtime directories (logs, monitoring, reports)
  - Dependency files (node_modules, lib, cache)
  - Special files (--delete)

- Removed empty runtime directories (bin/, data/)

V2 structure is now clean:
- docs/planning/ - V2 planning documents
- orig/ - Complete V1 codebase preserved
- .env - Active environment config (not in git)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 10:53:05 +01:00

378 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
# Critical Fixes Application Script
# Date: 2025-10-30
# Purpose: Apply all critical fixes identified in log analysis
set -euo pipefail # Exit on error, undefined vars, pipe failures
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
echo "========================================="
echo "MEV Bot Critical Fixes - Application Script"
echo "========================================="
echo "Date: $(date)"
echo "Project: $PROJECT_ROOT"
echo ""
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Backup function
create_backup() {
local file=$1
local backup_dir="$PROJECT_ROOT/backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$backup_dir"
if [ -f "$file" ]; then
cp "$file" "$backup_dir/$(basename "$file").backup"
log_info "Backed up: $file -> $backup_dir"
fi
}
echo "========================================"
echo "FIX 1: Log Manager Script Bug (Line 188)"
echo "========================================"
LOG_MANAGER_SCRIPT="$PROJECT_ROOT/scripts/log-manager.sh"
if [ -f "$LOG_MANAGER_SCRIPT" ]; then
log_info "Found log-manager.sh, applying fix..."
create_backup "$LOG_MANAGER_SCRIPT"
# Fix: Replace the problematic line with proper variable quoting
awk '
{
if (/recent_health_trend.*recent_errors/) {
print " \"recent_health_trend\": \"$([ -n \"${recent_errors}\" ] && [ \"${recent_errors}\" -lt 10 ] 2>/dev/null && echo good || echo concerning)\"";
} else {
print;
}
}
' "$LOG_MANAGER_SCRIPT" > "$LOG_MANAGER_SCRIPT.tmp"
if [ -f "$LOG_MANAGER_SCRIPT.tmp" ]; then
mv "$LOG_MANAGER_SCRIPT.tmp" "$LOG_MANAGER_SCRIPT"
chmod +x "$LOG_MANAGER_SCRIPT"
log_info "✅ Log manager script fixed"
else
log_error "Failed to create fixed script"
fi
else
log_warn "Log manager script not found, skipping..."
fi
echo ""
echo "========================================="
echo "FIX 2: Add Zero Address Validation Checks"
echo "========================================="
# Create a validation helper file
VALIDATION_HELPER="$PROJECT_ROOT/pkg/utils/address_validation.go"
log_info "Creating address validation helper..."
mkdir -p "$PROJECT_ROOT/pkg/utils"
cat > "$VALIDATION_HELPER" << 'GOEOF'
package utils
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
)
// ValidateAddress ensures an address is not zero
func ValidateAddress(addr common.Address, name string) error {
if addr == (common.Address{}) {
return fmt.Errorf("%s cannot be zero address", name)
}
return nil
}
// ValidateAddresses validates multiple addresses
func ValidateAddresses(addrs map[string]common.Address) error {
for name, addr := range addrs {
if err := ValidateAddress(addr, name); err != nil {
return err
}
}
return nil
}
// IsZeroAddress checks if address is zero
func IsZeroAddress(addr common.Address) bool {
return addr == (common.Address{})
}
GOEOF
log_info "✅ Created address validation helper"
echo ""
echo "========================================="
echo "FIX 3: Update RPC Configuration"
echo "========================================="
ENV_FILE="$PROJECT_ROOT/.env"
ENV_PRODUCTION="$PROJECT_ROOT/.env.production"
log_info "Updating RPC configuration for conservative rate limiting..."
# Update .env if it exists
if [ -f "$ENV_FILE" ]; then
create_backup "$ENV_FILE"
# Ensure proper RPC configuration
if ! grep -q "ARBITRUM_RPC_RATE_LIMIT" "$ENV_FILE"; then
cat >> "$ENV_FILE" << 'ENVEOF'
# RPC Rate Limiting (Conservative Settings)
ARBITRUM_RPC_RATE_LIMIT=5
ARBITRUM_RPC_BURST=10
ARBITRUM_RPC_MAX_RETRIES=3
ARBITRUM_RPC_BACKOFF_SECONDS=1
ENVEOF
log_info "Added rate limiting config to .env"
fi
fi
# Update .env.production if it exists
if [ -f "$ENV_PRODUCTION" ]; then
create_backup "$ENV_PRODUCTION"
if ! grep -q "ARBITRUM_RPC_RATE_LIMIT" "$ENV_PRODUCTION"; then
cat >> "$ENV_PRODUCTION" << 'ENVEOF'
# RPC Rate Limiting (Production Settings)
ARBITRUM_RPC_RATE_LIMIT=10
ARBITRUM_RPC_BURST=20
ARBITRUM_RPC_MAX_RETRIES=5
ARBITRUM_RPC_BACKOFF_SECONDS=2
ENVEOF
log_info "Added rate limiting config to .env.production"
fi
fi
log_info "✅ RPC configuration updated"
echo ""
echo "========================================="
echo "FIX 4: Create Pre-Run Validation Script"
echo "========================================="
VALIDATION_SCRIPT="$PROJECT_ROOT/scripts/pre-run-validation.sh"
cat > "$VALIDATION_SCRIPT" << 'VALEOF'
#!/bin/bash
# Pre-Run Validation Script
# Validates environment before starting MEV bot
set -e
echo "========================================="
echo "MEV Bot Pre-Run Validation"
echo "========================================="
ERRORS=0
# Check RPC endpoints
echo "[1/5] Checking RPC endpoints..."
if [ -z "$ARBITRUM_RPC_ENDPOINT" ]; then
echo "❌ ARBITRUM_RPC_ENDPOINT not set"
ERRORS=$((ERRORS + 1))
else
echo "✅ ARBITRUM_RPC_ENDPOINT: $ARBITRUM_RPC_ENDPOINT"
fi
# Check for wss:// or https:// prefix
echo "[2/5] Validating endpoint format..."
if [[ "$ARBITRUM_RPC_ENDPOINT" == wss://* ]] || [[ "$ARBITRUM_RPC_ENDPOINT" == https://* ]]; then
echo "✅ Endpoint format valid"
else
echo "❌ Endpoint must start with wss:// or https://"
ERRORS=$((ERRORS + 1))
fi
# Check log directory
echo "[3/5] Checking log directory..."
if [ -d "logs" ]; then
echo "✅ Log directory exists"
# Check for excessive zero addresses in recent logs
if [ -f "logs/liquidity_events_$(date +%Y-%m-%d).jsonl" ]; then
ZERO_COUNT=$(grep -c "0x0000000000000000000000000000000000000000" "logs/liquidity_events_$(date +%Y-%m-%d).jsonl" 2>/dev/null || echo 0)
echo "Zero addresses in today's events: $ZERO_COUNT"
if [ "$ZERO_COUNT" -gt 10 ]; then
echo "⚠️ WARNING: High zero address count detected"
fi
fi
else
mkdir -p logs
echo "✅ Created log directory"
fi
# Check binary exists
echo "[4/5] Checking binary..."
if [ -f "./mev-bot" ] || [ -f "./bin/mev-bot" ]; then
echo "✅ MEV bot binary found"
else
echo "❌ MEV bot binary not found. Run 'make build' first"
ERRORS=$((ERRORS + 1))
fi
# Check for port conflicts
echo "[5/5] Checking for port conflicts..."
if lsof -Pi :9090 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "⚠️ WARNING: Port 9090 (metrics) already in use"
fi
if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "⚠️ WARNING: Port 8080 (dashboard) already in use"
fi
echo ""
echo "========================================="
if [ $ERRORS -eq 0 ]; then
echo "✅ Validation PASSED - Safe to start"
exit 0
else
echo "❌ Validation FAILED - $ERRORS error(s) found"
exit 1
fi
VALEOF
chmod +x "$VALIDATION_SCRIPT"
log_info "✅ Created pre-run validation script"
echo ""
echo "========================================="
echo "FIX 5: Archive Old Logs"
echo "========================================="
log_info "Archiving old logs to reduce disk usage..."
cd "$PROJECT_ROOT/logs"
# Archive logs older than 1 day
find . -name "*.log" -type f -mtime +1 -size +10M -exec gzip {} \; 2>/dev/null || true
# Move very old archives
if [ -d "archived" ]; then
ARCHIVE_COUNT=$(find archived/ -name "*.log" -type f | wc -l)
if [ "$ARCHIVE_COUNT" -gt 5 ]; then
log_info "Found $ARCHIVE_COUNT old archive files"
find archived/ -name "*.log" -type f -mtime +7 -delete 2>/dev/null || true
log_info "Cleaned up old archives"
fi
fi
cd "$PROJECT_ROOT"
log_info "✅ Log archiving complete"
echo ""
echo "========================================="
echo "FIX 6: Create Quick Test Script"
echo "========================================="
TEST_SCRIPT="$PROJECT_ROOT/scripts/quick-test.sh"
cat > "$TEST_SCRIPT" << 'TESTEOF'
#!/bin/bash
# Quick Test Script - Validates fixes are working
set -e
echo "========================================="
echo "MEV Bot Quick Test"
echo "========================================="
# Run pre-validation
echo "[1/3] Running pre-run validation..."
./scripts/pre-run-validation.sh
# Build
echo "[2/3] Building..."
make build 2>&1 | tail -10
# Run for 30 seconds
echo "[3/3] Running bot for 30 seconds..."
timeout 30 ./mev-bot start 2>&1 | tee test-run.log || true
echo ""
echo "========================================="
echo "Analyzing Test Run..."
echo "========================================="
# Check for critical errors
WSS_ERRORS=$(grep -c "unsupported protocol scheme" test-run.log 2>/dev/null || echo 0)
ZERO_ADDR=$(grep -c "0x00000000000000000000000000000000000000000" test-run.log 2>/dev/null || echo 0)
RATE_LIMITS=$(grep -c "Too Many Requests" test-run.log 2>/dev/null || echo 0)
echo "WebSocket errors: $WSS_ERRORS"
echo "Zero addresses: $ZERO_ADDR"
echo "Rate limit errors: $RATE_LIMITS"
if [ "$WSS_ERRORS" -eq 0 ] && [ "$ZERO_ADDR" -lt 10 ] && [ "$RATE_LIMITS" -lt 10 ]; then
echo ""
echo "✅ TEST PASSED - Fixes appear to be working"
exit 0
else
echo ""
echo "⚠️ TEST WARNINGS - Some issues remain:"
[ "$WSS_ERRORS" -gt 0 ] && echo " - WebSocket errors still present"
[ "$ZERO_ADDR" -ge 10 ] && echo " - High zero address count"
[ "$RATE_LIMITS" -ge 10 ] && echo " - Rate limiting issues"
exit 1
fi
TESTEOF
chmod +x "$TEST_SCRIPT"
log_info "✅ Created quick test script"
echo ""
echo "========================================="
echo "Summary of Applied Fixes"
echo "========================================="
echo ""
echo "✅ Fixed log manager script bug (line 188)"
echo "✅ Created address validation helper"
echo "✅ Updated RPC configuration with rate limiting"
echo "✅ Created pre-run validation script"
echo "✅ Archived old logs"
echo "✅ Created quick test script"
echo ""
echo "========================================="
echo "Next Steps"
echo "========================================="
echo ""
echo "1. Review changes: git diff"
echo "2. Run validation: ./scripts/pre-run-validation.sh"
echo "3. Build: make build"
echo "4. Quick test: ./scripts/quick-test.sh"
echo "5. Full test: timeout 60 ./mev-bot start"
echo ""
echo "Backup location: $PROJECT_ROOT/backups/$(date +%Y%m%d_%H%M%S)"
echo ""
echo "========================================="
echo "Fixes Applied Successfully!"
echo "========================================="