chore: sync submodule state (parent ref update)

Made-with: Cursor
This commit is contained in:
defiQUG
2026-03-02 12:14:09 -08:00
parent 50ab378da9
commit 5efe36b1e0
1100 changed files with 155024 additions and 8674 deletions

View File

@@ -0,0 +1,208 @@
#!/usr/bin/env bash
# Complete Setup Script for Token Aggregation Service
# This script runs database migrations and prepares the service for deployment
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SERVICE_DIR="$SCRIPT_DIR/.."
MIGRATIONS_DIR="$PROJECT_ROOT/explorer-monorepo/backend/database/migrations"
echo "=========================================="
echo "Token Aggregation Service - Complete Setup"
echo "=========================================="
echo ""
# Load environment variables
if [[ -f "$SERVICE_DIR/.env" ]]; then
echo "Loading environment from .env file..."
set -a
source "$SERVICE_DIR/.env"
set +a
fi
# Database configuration
DATABASE_URL="${DATABASE_URL:-postgresql://postgres:postgres@localhost:5432/explorer_db}"
echo "Database URL: ${DATABASE_URL%%@*}" # Show without password
echo ""
# Step 1: Verify migrations exist
echo "Step 1: Verifying migration files..."
MIGRATION_0011="$MIGRATIONS_DIR/0011_token_aggregation_schema.up.sql"
MIGRATION_0012="$MIGRATIONS_DIR/0012_admin_config_schema.up.sql"
if [[ ! -f "$MIGRATION_0011" ]]; then
echo "❌ Migration 0011 not found: $MIGRATION_0011"
exit 1
fi
if [[ ! -f "$MIGRATION_0012" ]]; then
echo "❌ Migration 0012 not found: $MIGRATION_0012"
exit 1
fi
echo "✅ Migration files found"
echo ""
# Step 2: Test database connection
echo "Step 2: Testing database connection..."
if psql "$DATABASE_URL" -c "SELECT 1;" > /dev/null 2>&1; then
echo "✅ Database connection successful"
else
echo "❌ Database connection failed"
echo "Please verify:"
echo " - PostgreSQL is running"
echo " - DATABASE_URL is correct in .env file"
echo " - Database credentials are valid"
exit 1
fi
echo ""
# Step 3: Check if migrations already applied
echo "Step 3: Checking migration status..."
MIGRATION_0011_APPLIED=$(psql "$DATABASE_URL" -tAc "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'token_market_data');" 2>/dev/null || echo "false")
MIGRATION_0012_APPLIED=$(psql "$DATABASE_URL" -tAc "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'api_keys');" 2>/dev/null || echo "false")
if [[ "$MIGRATION_0011_APPLIED" == "t" ]]; then
echo "⚠️ Migration 0011 (token aggregation) already applied"
else
echo "📋 Migration 0011 (token aggregation) needs to be applied"
fi
if [[ "$MIGRATION_0012_APPLIED" == "t" ]]; then
echo "⚠️ Migration 0012 (admin config) already applied"
else
echo "📋 Migration 0012 (admin config) needs to be applied"
fi
echo ""
# Step 4: Run migrations
echo "Step 4: Running database migrations..."
if [[ "$MIGRATION_0011_APPLIED" != "t" ]]; then
echo "Running migration 0011: Token Aggregation Schema..."
if psql "$DATABASE_URL" -f "$MIGRATION_0011"; then
echo "✅ Migration 0011 completed successfully"
else
echo "❌ Migration 0011 failed"
exit 1
fi
echo ""
else
echo "⏭️ Skipping migration 0011 (already applied)"
echo ""
fi
if [[ "$MIGRATION_0012_APPLIED" != "t" ]]; then
echo "Running migration 0012: Admin Configuration Schema..."
if psql "$DATABASE_URL" -f "$MIGRATION_0012"; then
echo "✅ Migration 0012 completed successfully"
else
echo "❌ Migration 0012 failed"
exit 1
fi
echo ""
else
echo "⏭️ Skipping migration 0012 (already applied)"
echo ""
fi
# Step 5: Verify migrations
echo "Step 5: Verifying migrations..."
TOKEN_TABLES=$(psql "$DATABASE_URL" -tAc "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('token_market_data', 'liquidity_pools', 'token_ohlcv');" 2>/dev/null || echo "0")
ADMIN_TABLES=$(psql "$DATABASE_URL" -tAc "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('api_keys', 'api_endpoints', 'admin_users');" 2>/dev/null || echo "0")
echo "Token aggregation tables: $TOKEN_TABLES/3"
echo "Admin configuration tables: $ADMIN_TABLES/3"
if [[ "$TOKEN_TABLES" -ge "3" && "$ADMIN_TABLES" -ge "3" ]]; then
echo "✅ All migrations verified successfully"
else
echo "⚠️ Some tables may be missing"
fi
echo ""
# Step 6: Check for admin users
echo "Step 6: Checking for admin users..."
ADMIN_COUNT=$(psql "$DATABASE_URL" -tAc "SELECT COUNT(*) FROM admin_users WHERE is_active = true;" 2>/dev/null || echo "0")
if [[ "$ADMIN_COUNT" -eq "0" ]]; then
echo "⚠️ No admin users found"
echo ""
echo "Next step: Create an admin user"
echo " Run: ./scripts/create-admin-user.sh"
echo ""
else
echo "✅ Found $ADMIN_COUNT active admin user(s)"
echo ""
fi
# Step 7: Verify service files
echo "Step 7: Verifying service files..."
if [[ -f "$SERVICE_DIR/src/index.ts" ]]; then
echo "✅ Backend service files found"
else
echo "❌ Backend service files missing"
exit 1
fi
if [[ -f "$SERVICE_DIR/frontend/src/App.tsx" ]]; then
echo "✅ Frontend files found"
else
echo "❌ Frontend files missing"
exit 1
fi
if [[ -f "$SERVICE_DIR/package.json" ]]; then
echo "✅ Service configuration found"
else
echo "❌ Service configuration missing"
exit 1
fi
echo ""
# Summary
echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo "✅ Database migrations: Applied"
echo "✅ Service files: Verified"
echo ""
if [[ "$ADMIN_COUNT" -eq "0" ]]; then
echo "📋 Next Steps:"
echo ""
echo "1. Create admin user:"
echo " cd $SERVICE_DIR"
echo " ./scripts/create-admin-user.sh"
echo ""
echo "2. Deploy to Proxmox (if on Proxmox host):"
echo " ./scripts/deploy-to-proxmox.sh"
echo ""
echo "3. Or run locally:"
echo " npm install"
echo " npm run build"
echo " npm start"
echo ""
else
echo "📋 Next Steps:"
echo ""
echo "1. Deploy to Proxmox (if on Proxmox host):"
echo " cd $SERVICE_DIR"
echo " ./scripts/deploy-to-proxmox.sh"
echo ""
echo "2. Or run locally:"
echo " npm install"
echo " npm run build"
echo " npm start"
echo ""
echo "3. Access control panel:"
echo " http://localhost:3000 (API)"
echo " http://localhost:3001 (Frontend in dev mode)"
echo ""
fi
echo "=========================================="

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# Create admin user for Token Aggregation Control Panel
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load environment
if [[ -f "$PROJECT_ROOT/smom-dbis-138/services/token-aggregation/.env" ]]; then
source "$PROJECT_ROOT/smom-dbis-138/services/token-aggregation/.env"
fi
DATABASE_URL="${DATABASE_URL:-postgresql://postgres:postgres@localhost:5432/explorer_db}"
echo "Creating admin user for Token Aggregation Control Panel"
echo ""
read -p "Username: " USERNAME
read -sp "Password: " PASSWORD
echo ""
read -p "Email (optional): " EMAIL
read -p "Role (super_admin/admin/operator/viewer) [admin]: " ROLE
ROLE="${ROLE:-admin}"
# Hash password using Node.js (with error handling)
if ! command -v node &> /dev/null; then
echo "❌ Node.js not found. Please install Node.js to create admin users."
exit 1
fi
PASSWORD_HASH=$(node -e "const bcrypt = require('bcrypt'); bcrypt.hash('$PASSWORD', 10).then(h => console.log(h)).catch(e => {console.error('Error:', e.message); process.exit(1);})" 2>&1)
if [[ $? -ne 0 ]] || [[ -z "$PASSWORD_HASH" ]]; then
echo "❌ Failed to hash password. Make sure bcrypt is installed: npm install bcrypt"
exit 1
fi
# Insert into database
psql "$DATABASE_URL" <<EOF
INSERT INTO admin_users (username, email, password_hash, role, is_active)
VALUES ('$USERNAME', '$EMAIL', '$PASSWORD_HASH', '$ROLE', true)
ON CONFLICT (username) DO UPDATE SET
password_hash = EXCLUDED.password_hash,
role = EXCLUDED.role,
updated_at = NOW();
EOF
echo ""
echo "Admin user created successfully!"
echo "Username: $USERNAME"
echo "Role: $ROLE"

View File

@@ -0,0 +1,195 @@
#!/usr/bin/env bash
# Deploy Token Aggregation Service to Proxmox VM
# This script creates an LXC container and deploys the service with control panel
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SERVICE_DIR="$PROJECT_ROOT/smom-dbis-138/services/token-aggregation"
# Load common functions if available
if [[ -f "$PROJECT_ROOT/smom-dbis-138-proxmox/lib/common.sh" ]]; then
source "$PROJECT_ROOT/smom-dbis-138-proxmox/lib/common.sh"
fi
# Configuration
VMID="${VMID:-3600}"
HOSTNAME="${HOSTNAME:-token-aggregation}"
MEMORY="${MEMORY:-4096}"
CORES="${CORES:-2}"
DISK="${DISK:-40}"
IP_ADDRESS="${IP_ADDRESS:-}"
log_info "Deploying Token Aggregation Service to Proxmox VMID: $VMID"
# Check if running on Proxmox host
if ! command_exists pct 2>/dev/null; then
log_error "This script must be run on Proxmox host (pct command not found)"
exit 1
fi
# Check if container exists
if pct list | grep -q "^\s*$VMID\s"; then
log_warn "Container $VMID already exists"
read -p "Do you want to recreate it? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
log_info "Stopping and destroying existing container..."
pct stop $VMID 2>/dev/null || true
pct destroy $VMID || true
else
log_info "Using existing container $VMID"
pct start $VMID 2>/dev/null || true
exit 0
fi
fi
# Create container
log_info "Creating LXC container $VMID..."
OS_TEMPLATE="${CONTAINER_OS_TEMPLATE:-local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst}"
if [[ -n "$IP_ADDRESS" ]]; then
NETWORK_CONFIG="bridge=vmbr0,name=eth0,ip=$IP_ADDRESS/24,gw=192.168.11.1"
else
NETWORK_CONFIG="bridge=vmbr0,name=eth0,ip=dhcp"
fi
pct create "$VMID" \
"$OS_TEMPLATE" \
--storage local-lvm \
--hostname "$HOSTNAME" \
--memory "$MEMORY" \
--cores "$CORES" \
--rootfs local-lvm:"$DISK" \
--net0 "$NETWORK_CONFIG" \
--unprivileged 1 \
--swap 1024 \
--onboot 1 \
--features nesting=1,keyctl=1
log_success "Container $VMID created"
# Start container
log_info "Starting container..."
pct start "$VMID"
sleep 5
# Wait for container to be ready
log_info "Waiting for container to be ready..."
for i in {1..30}; do
if pct exec "$VMID" -- test -f /etc/os-release 2>/dev/null; then
break
fi
sleep 2
done
# Update container
log_info "Updating container..."
pct exec "$VMID" -- bash -c "export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get upgrade -y"
# Install dependencies
log_info "Installing dependencies..."
pct exec "$VMID" -- bash -c "export DEBIAN_FRONTEND=noninteractive && apt-get install -y curl wget git build-essential postgresql-client"
# Install Node.js 20
log_info "Installing Node.js 20..."
pct exec "$VMID" -- bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs"
# Install nginx for frontend
log_info "Installing nginx..."
pct exec "$VMID" -- bash -c "export DEBIAN_FRONTEND=noninteractive && apt-get install -y nginx"
# Create service user
log_info "Creating service user..."
pct exec "$VMID" -- bash -c "useradd -m -s /bin/bash token-aggregation || true"
# Create service directory
log_info "Setting up service directory..."
pct exec "$VMID" -- bash -c "mkdir -p /opt/token-aggregation && chown token-aggregation:token-aggregation /opt/token-aggregation"
# Copy service files
log_info "Copying service files..."
pct push "$VMID" "$SERVICE_DIR" /opt/token-aggregation --recursive
# Install service dependencies
log_info "Installing service dependencies..."
pct exec "$VMID" -- bash -c "cd /opt/token-aggregation && npm install"
# Build service
log_info "Building service..."
pct exec "$VMID" -- bash -c "cd /opt/token-aggregation && npm run build"
# Build frontend
log_info "Building frontend..."
pct exec "$VMID" -- bash -c "cd /opt/token-aggregation/frontend && npm install && npm run build"
# Create systemd service
log_info "Creating systemd service..."
pct exec "$VMID" -- bash -c "cat > /etc/systemd/system/token-aggregation.service << 'EOF'
[Unit]
Description=Token Aggregation Service
After=network.target
[Service]
Type=simple
User=token-aggregation
WorkingDirectory=/opt/token-aggregation
Environment=NODE_ENV=production
ExecStart=/usr/bin/node /opt/token-aggregation/dist/index.js
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
"
# Configure nginx for frontend
log_info "Configuring nginx..."
pct exec "$VMID" -- bash -c "cat > /etc/nginx/sites-available/token-aggregation << 'EOF'
server {
listen 80;
server_name _;
# Frontend
location / {
root /opt/token-aggregation/frontend/dist;
try_files \$uri \$uri/ /index.html;
}
# API proxy
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
}
EOF
"
pct exec "$VMID" -- bash -c "ln -sf /etc/nginx/sites-available/token-aggregation /etc/nginx/sites-enabled/ && rm -f /etc/nginx/sites-enabled/default"
# Enable and start services
log_info "Enabling services..."
pct exec "$VMID" -- systemctl daemon-reload
pct exec "$VMID" -- systemctl enable token-aggregation
pct exec "$VMID" -- systemctl enable nginx
pct exec "$VMID" -- systemctl restart nginx
# Get container IP
CONTAINER_IP=$(pct exec "$VMID" -- hostname -I | awk '{print $1}')
log_success "Container IP: $CONTAINER_IP"
log_info "Deployment complete!"
log_info "Service will be available at: http://$CONTAINER_IP"
log_info "Control Panel: http://$CONTAINER_IP"
log_info ""
log_info "Next steps:"
log_info "1. Configure .env file in /opt/token-aggregation"
log_info "2. Run database migration"
log_info "3. Start service: systemctl start token-aggregation"
log_info "4. Create admin user via API or database"

View File

@@ -0,0 +1,64 @@
#!/usr/bin/env bash
# Run Database Migrations for Token Aggregation Service
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SERVICE_DIR="$SCRIPT_DIR/.."
MIGRATIONS_DIR="$PROJECT_ROOT/explorer-monorepo/backend/database/migrations"
# Load environment
if [[ -f "$SERVICE_DIR/.env" ]]; then
set -a
source "$SERVICE_DIR/.env"
set +a
fi
DATABASE_URL="${DATABASE_URL:-postgresql://postgres:postgres@localhost:5432/explorer_db}"
echo "Running database migrations for Token Aggregation Service"
echo "Database: ${DATABASE_URL%%@*}"
echo ""
# Migration files
MIGRATION_0011="$MIGRATIONS_DIR/0011_token_aggregation_schema.up.sql"
MIGRATION_0012="$MIGRATIONS_DIR/0012_admin_config_schema.up.sql"
MIGRATION_0013="$MIGRATIONS_DIR/0013_update_token_logos_ipfs.up.sql"
# Check if migrations exist
if [[ ! -f "$MIGRATION_0011" ]]; then
echo "❌ Migration 0011 not found: $MIGRATION_0011"
exit 1
fi
if [[ ! -f "$MIGRATION_0012" ]]; then
echo "❌ Migration 0012 not found: $MIGRATION_0012"
exit 1
fi
# Test connection
if ! psql "$DATABASE_URL" -c "SELECT 1;" > /dev/null 2>&1; then
echo "❌ Database connection failed"
exit 1
fi
# Run migrations
echo "Running migration 0011: Token Aggregation Schema..."
psql "$DATABASE_URL" -f "$MIGRATION_0011"
echo "✅ Migration 0011 completed"
echo ""
echo "Running migration 0012: Admin Configuration Schema..."
psql "$DATABASE_URL" -f "$MIGRATION_0012"
echo "✅ Migration 0012 completed"
if [[ -f "$MIGRATION_0013" ]]; then
echo ""
echo "Running migration 0013: Update token logos to IPFS..."
psql "$DATABASE_URL" -f "$MIGRATION_0013"
echo "✅ Migration 0013 completed"
fi
echo ""
echo "✅ All migrations completed successfully"

View File

@@ -0,0 +1,55 @@
#!/bin/bash
# Token Aggregation Service Setup Script
set -e
echo "🚀 Setting up Token Aggregation Service..."
# Check Node.js version
echo "📦 Checking Node.js version..."
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 20 ]; then
echo "❌ Node.js 20+ is required. Current version: $(node -v)"
exit 1
fi
echo "✅ Node.js version: $(node -v)"
# Install dependencies
echo "📦 Installing dependencies..."
npm install
# Check if .env exists
if [ ! -f .env ]; then
echo "📝 Creating .env file from .env.example..."
cp .env.example .env
echo "⚠️ Please edit .env file with your configuration"
else
echo "✅ .env file exists"
fi
# Build the project
echo "🔨 Building TypeScript..."
npm run build
# Check database connection
echo "🔍 Checking database connection..."
if [ -z "$DATABASE_URL" ]; then
echo "⚠️ DATABASE_URL not set. Please configure it in .env"
else
echo "✅ DATABASE_URL is set"
fi
# Verify database migration
echo "📊 Verifying database migration..."
echo "⚠️ Please ensure migration 0011_token_aggregation_schema.up.sql has been run"
echo " Location: explorer-monorepo/backend/database/migrations/0011_token_aggregation_schema.up.sql"
echo ""
echo "✅ Setup complete!"
echo ""
echo "Next steps:"
echo "1. Edit .env file with your configuration"
echo "2. Run database migration if not already done"
echo "3. Start the service: npm start"
echo "4. Or run in development: npm run dev"