#!/bin/bash # Complete setup and deployment for local development # This script will: # 1. Check prerequisites # 2. Start Anvil # 3. Deploy contracts # 4. Start the oracle set -e # Colors for output GREEN='\033[0;32m' BLUE='\033[0;34m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${BLUE}════════════════════════════════════════════════════════${NC}" echo -e "${BLUE} Natural Language Agreement Oracle - Quick Setup${NC}" echo -e "${BLUE}════════════════════════════════════════════════════════${NC}\n" # Check prerequisites echo -e "${BLUE}📋 Checking prerequisites...${NC}\n" # Load .env file if it exists if [ -f ".env" ]; then echo -e "${BLUE}📄 Loading .env file...${NC}" export $(cat .env | grep -v '^#' | xargs) echo -e "${GREEN}✅ Environment variables loaded${NC}" fi # Check Bun if ! command -v bun &> /dev/null; then echo -e "${RED}❌ Bun is not installed${NC}" echo "Please install it: https://bun.sh" exit 1 fi echo -e "${GREEN}✅ Bun installed${NC}" # Check Foundry if ! command -v forge &> /dev/null; then echo -e "${RED}❌ Foundry (forge) is not installed${NC}" echo "Please install it: https://book.getfoundry.sh/getting-started/installation" exit 1 fi echo -e "${GREEN}✅ Foundry installed${NC}" # Check Anvil if ! command -v anvil &> /dev/null; then echo -e "${RED}❌ Anvil is not installed${NC}" echo "Please install Foundry: https://book.getfoundry.sh/getting-started/installation" exit 1 fi echo -e "${GREEN}✅ Anvil installed${NC}" # Check alkahest if [ ! -d "../alkahest" ]; then echo -e "${RED}❌ alkahest repository not found${NC}" echo "Please clone it in the parent directory:" echo " cd .. && git clone https://github.com/arkhai-io/alkahest.git" exit 1 fi echo -e "${GREEN}✅ alkahest repository found${NC}" # Check LLM API keys if [ -z "$OPENAI_API_KEY" ] && [ -z "$ANTHROPIC_API_KEY" ] && [ -z "$OPENROUTER_API_KEY" ]; then echo -e "${RED}❌ No LLM provider API key set${NC}" echo "Please add at least one API key to your .env file:" echo " OPENAI_API_KEY=sk-..." echo " ANTHROPIC_API_KEY=sk-ant-..." echo " OPENROUTER_API_KEY=sk-or-..." exit 1 fi if [ -n "$OPENAI_API_KEY" ]; then echo -e "${GREEN}✅ OpenAI API key configured${NC}" fi if [ -n "$ANTHROPIC_API_KEY" ]; then echo -e "${GREEN}✅ Anthropic API key configured${NC}" fi if [ -n "$OPENROUTER_API_KEY" ]; then echo -e "${GREEN}✅ OpenRouter API key configured${NC}" fi if [ -n "$PERPLEXITY_API_KEY" ]; then echo -e "${GREEN}✅ Perplexity API key configured${NC}" fi echo "" # Install dependencies echo -e "${BLUE}📦 Installing dependencies...${NC}\n" bun install # Check if Anvil is already running if lsof -Pi :8545 -sTCP:LISTEN -t >/dev/null ; then echo -e "${YELLOW}⚠️ Anvil is already running on port 8545${NC}" read -p "Do you want to kill it and start fresh? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then pkill -f anvil || true sleep 2 else echo -e "${BLUE}Using existing Anvil instance${NC}\n" fi fi # Start Anvil in background if not running if ! lsof -Pi :8545 -sTCP:LISTEN -t >/dev/null ; then echo -e "${BLUE}🔨 Starting Anvil...${NC}" anvil > anvil.log 2>&1 & ANVIL_PID=$! echo $ANVIL_PID > .anvil.pid echo -e "${GREEN}✅ Anvil started (PID: $ANVIL_PID)${NC}" echo " Logs: tail -f anvil.log" sleep 3 fi # Deploy contracts echo -e "\n${BLUE}📝 Deploying contracts...${NC}\n" bun run cli/server/deploy.ts --network localhost --rpc-url http://localhost:8545 # Start oracle echo -e "\n${BLUE}🚀 Starting oracle...${NC}\n" bun run cli/server/oracle.ts --deployment ./cli/deployments/localhost.json # Cleanup function cleanup() { echo -e "\n${YELLOW}🛑 Shutting down...${NC}" if [ -f .anvil.pid ]; then ANVIL_PID=$(cat .anvil.pid) kill $ANVIL_PID 2>/dev/null || true rm .anvil.pid echo -e "${GREEN}✅ Anvil stopped${NC}" fi exit 0 } trap cleanup SIGINT SIGTERM