Skip to content

Instantly share code, notes, and snippets.

@Cordtus
Created October 14, 2025 23:23
Show Gist options
  • Select an option

  • Save Cordtus/113e0f717ad707a356948d7989febbf3 to your computer and use it in GitHub Desktop.

Select an option

Save Cordtus/113e0f717ad707a356948d7989febbf3 to your computer and use it in GitHub Desktop.
Mantrachain Explorer Setup (No Nginx) - For use with Caddy or other reverse proxy
#!/bin/bash
#
# Mantrachain Explorer Setup Script (Simple - No Nginx)
# For Debian 12 LXC Container
#
# This script installs:
# - PostgreSQL database
# - PostgREST API server (port 3000)
# - Yaci blockchain indexer
# - Explorer UI (static files only)
#
# Chain: mantrachain (mantra-1)
# gRPC: mantra-grpc.polkachu.com:25190 (non-TLS)
#
# Route through your own Caddy server
#
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
POSTGRES_PASSWORD="mantra_$(openssl rand -hex 16)"
POSTGRES_USER="yaci"
POSTGRES_DB="yaci"
CHAIN_GRPC="mantra-grpc.polkachu.com:25190"
CHAIN_ID="mantra-1"
CHAIN_NAME="mantrachain"
API_PORT="3000"
STATIC_FILES_DIR="/var/www/mantrachain-explorer"
echo_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
echo_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
echo_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
if [ "$EUID" -ne 0 ]; then
echo_error "Please run as root"
exit 1
fi
echo_info "Starting Mantrachain Explorer Setup (Simple)"
echo ""
echo_warn "Generated PostgreSQL password: $POSTGRES_PASSWORD"
echo_warn "SAVE THIS! Writing to /root/mantra-credentials.txt"
echo ""
cat > /root/mantra-credentials.txt << EOF
Mantrachain Explorer Credentials
================================
PostgreSQL User: $POSTGRES_USER
PostgreSQL Password: $POSTGRES_PASSWORD
PostgreSQL Database: $POSTGRES_DB
Chain: $CHAIN_NAME ($CHAIN_ID)
gRPC Endpoint: $CHAIN_GRPC
API Port: $API_PORT
Static Files: $STATIC_FILES_DIR
Generated: $(date)
EOF
chmod 600 /root/mantra-credentials.txt
read -p "Press Enter to continue or Ctrl+C to abort..."
echo_info "Step 1: Installing dependencies..."
export DEBIAN_FRONTEND=noninteractive
apt update
apt upgrade -y
apt install -y \
postgresql postgresql-contrib \
git build-essential wget curl jq netcat-openbsd ca-certificates
echo_info "Step 2: Installing Go 1.22..."
if [ ! -d "/usr/local/go" ]; then
cd /tmp
wget -q https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin:/root/go/bin' >> /root/.bashrc
export PATH=$PATH:/usr/local/go/bin:/root/go/bin
fi
echo_info "Step 3: Installing Node.js 20..."
if ! command -v node &> /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
fi
echo_info "Step 4: Setting up PostgreSQL..."
systemctl start postgresql
systemctl enable postgresql
sleep 3
sudo -u postgres psql << EOF
DROP DATABASE IF EXISTS $POSTGRES_DB;
DROP USER IF EXISTS $POSTGRES_USER;
CREATE DATABASE $POSTGRES_DB;
CREATE USER $POSTGRES_USER WITH ENCRYPTED PASSWORD '$POSTGRES_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER;
\c $POSTGRES_DB
GRANT ALL ON SCHEMA public TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO $POSTGRES_USER;
EOF
echo_info "Step 5: Installing PostgREST..."
if [ ! -f "/usr/local/bin/postgrest" ]; then
cd /tmp
wget -q https://github.com/PostgREST/postgrest/releases/download/v12.0.2/postgrest-v12.0.2-linux-static-x64.tar.xz
tar xf postgrest-v12.0.2-linux-static-x64.tar.xz
mv postgrest /usr/local/bin/
chmod +x /usr/local/bin/postgrest
fi
mkdir -p /etc/postgrest
cat > /etc/postgrest/config.conf << EOF
db-uri = "postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB"
db-schemas = "api"
db-anon-role = "$POSTGRES_USER"
db-pool = 10
server-host = "0.0.0.0"
server-port = $API_PORT
EOF
chmod 600 /etc/postgrest/config.conf
echo_info "Step 6: Building Yaci indexer..."
cd /opt
if [ ! -d "yaci" ]; then
git clone https://github.com/Cordtus/yaci.git
fi
cd yaci
git pull
make build
cp bin/yaci /usr/local/bin/yaci
chmod +x /usr/local/bin/yaci
echo_info "Step 7: Applying database migrations..."
cd /opt/yaci/internal/output/postgresql/migrations
sudo -u postgres psql -d $POSTGRES_DB << 'EOF'
CREATE SCHEMA IF NOT EXISTS api;
GRANT ALL ON SCHEMA api TO yaci;
EOF
for migration in $(ls -1 *.up.sql | sort -V); do
echo_info "Applying: $migration"
sudo -u postgres psql -d $POSTGRES_DB -f "$migration"
done
sudo -u postgres psql -d $POSTGRES_DB << EOF
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA api TO $POSTGRES_USER;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA api TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA api GRANT ALL ON TABLES TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA api GRANT ALL ON SEQUENCES TO $POSTGRES_USER;
EOF
echo_info "Step 8: Building Explorer UI..."
cd /opt
if [ ! -d "yaci-explorer" ]; then
git clone https://github.com/Cordtus/yaci-explorer.git
fi
cd yaci-explorer
git pull
npm install
npm run build
echo_info "Step 9: Installing static files..."
mkdir -p $STATIC_FILES_DIR
cp -r build/client/* $STATIC_FILES_DIR/
chown -R www-data:www-data $STATIC_FILES_DIR 2>/dev/null || chown -R root:root $STATIC_FILES_DIR
echo_info "Step 10: Creating systemd services..."
cat > /etc/systemd/system/postgrest.service << EOF
[Unit]
Description=PostgREST API Server
After=postgresql.service
Requires=postgresql.service
[Service]
Type=simple
ExecStart=/usr/local/bin/postgrest /etc/postgrest/config.conf
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/yaci.service << EOF
[Unit]
Description=Yaci Blockchain Indexer for Mantrachain
After=postgresql.service postgrest.service
Requires=postgresql.service
[Service]
Type=simple
ExecStart=/usr/local/bin/yaci extract postgres $CHAIN_GRPC \\
-p postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB \\
--live \\
--max-concurrency 100 \\
-k \\
--enable-prometheus \\
--prometheus-addr 0.0.0.0:2112 \\
-l info
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
echo_info "Step 11: Starting services..."
systemctl enable postgresql postgrest yaci
systemctl start postgresql
sleep 2
systemctl start postgrest
sleep 2
systemctl start yaci
echo_info "Step 12: Creating helper scripts..."
cat > /usr/local/bin/mantra-status << 'EOFSTATUS'
#!/bin/bash
echo "=== Mantrachain Explorer Status ==="
echo ""
systemctl status postgresql postgrest yaci --no-pager | grep "Active:"
echo ""
echo "Database blocks:"
sudo -u postgres psql -d yaci -t -c "SELECT COUNT(*) FROM api.blocks_raw;" 2>/dev/null || echo "0"
echo ""
echo "Latest indexed block:"
sudo -u postgres psql -d yaci -t -c "SELECT MAX((data->'block'->'header'->>'height')::int) FROM api.blocks_raw;" 2>/dev/null || echo "none"
EOFSTATUS
cat > /usr/local/bin/mantra-logs << 'EOFLOGS'
#!/bin/bash
SERVICE=${1:-yaci}
journalctl -u $SERVICE -f
EOFLOGS
chmod +x /usr/local/bin/mantra-status
chmod +x /usr/local/bin/mantra-logs
echo ""
echo_info "=========================================="
echo_info "Installation Complete!"
echo_info "=========================================="
echo ""
echo_info "Services Running:"
echo_info " PostgREST API: http://0.0.0.0:$API_PORT"
echo_info " Prometheus Metrics: http://0.0.0.0:2112/metrics"
echo_info " Static Files: $STATIC_FILES_DIR"
echo ""
echo_info "Exposed Ports:"
echo_info " - 3000 (PostgREST API)"
echo_info " - 2112 (Prometheus metrics)"
echo ""
echo_info "Configure your Caddy server to:"
echo_info " 1. Serve static files from: $STATIC_FILES_DIR"
echo_info " 2. Proxy /api/* to: http://localhost:3000/"
echo ""
echo_info "Example Caddyfile snippet:"
echo_info " mantra.yourdomain.com {"
echo_info " root * $STATIC_FILES_DIR"
echo_info " handle /api/* {"
echo_info " uri strip_prefix /api"
echo_info " reverse_proxy localhost:3000"
echo_info " }"
echo_info " file_server"
echo_info " try_files {path} /index.html"
echo_info " }"
echo ""
echo_info "Credentials: /root/mantra-credentials.txt"
echo ""
echo_info "Useful Commands:"
echo_info " mantra-status - Check services"
echo_info " mantra-logs yaci - View indexer logs"
echo ""
echo_info "Setup complete! 🚀"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment