Files
proxmox/scripts/deployment/preflight-chain138-deploy.sh
defiQUG b3a8fe4496
Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
chore: sync all changes to Gitea
- Config, docs, scripts, and backup manifests
- Submodule refs unchanged (m = modified content in submodules)

Made-with: Cursor
2026-03-02 11:37:34 -08:00

94 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Pre-flight checks before Chain 138 deployment: correct RPC, dotenv, nonce (no stuck txs).
# Optionally run gas/cost estimate for deployment cost.
#
# Usage: ./scripts/deployment/preflight-chain138-deploy.sh [--cost]
# --cost Also run cost estimate (smom-dbis-138/scripts/deployment/calculate-costs-consolidated.sh).
#
# Requires: run from repo root. Uses smom-dbis-138/.env only (PRIVATE_KEY, RPC_URL_138).
# See: docs/03-deployment/DEPLOYMENT_ORDER_OF_OPERATIONS.md § Deployment safety
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SMOM="${PROJECT_ROOT}/smom-dbis-138"
RUN_COST=""
for a in "$@"; do [[ "$a" == "--cost" ]] && RUN_COST=1; done
echo "=== Chain 138 deployment pre-flight ==="
echo ""
# 1) Dotenv: must exist and be the canonical one
if [[ ! -f "$SMOM/.env" ]]; then
echo "FAIL: $SMOM/.env not found. Use smom-dbis-138/.env only for deployment secrets." >&2
exit 1
fi
echo "OK Dotenv: $SMOM/.env exists (canonical for Chain 138 deploy)."
# 2) Env check script (keys only, no values)
if [[ -f "$SMOM/scripts/deployment/check-env-required.sh" ]]; then
echo ""
bash "$SMOM/scripts/deployment/check-env-required.sh" || { echo "FAIL: env check failed. Fix MISS entries in $SMOM/.env." >&2; exit 1; }
else
echo "WARN: $SMOM/scripts/deployment/check-env-required.sh not found; skipping env key check." >&2
fi
# 3) Load env for RPC and nonce checks (no secrets printed)
[[ -f "${PROJECT_ROOT}/config/ip-addresses.conf" ]] && source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
set -a
source "$SMOM/.env"
set +a
RPC="${RPC_URL_138:-http://192.168.11.211:8545}"
if [[ -z "${PRIVATE_KEY:-}" ]]; then
echo "FAIL: PRIVATE_KEY not set in $SMOM/.env." >&2
exit 1
fi
# 4) RPC: must be Core (chainId 138 = 0x8a)
echo ""
chain_id_hex=$(curl -s -m 10 -X POST "$RPC" -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>/dev/null | sed -n 's/.*"result":"\([^"]*\)".*/\1/p') || true
if [[ "$chain_id_hex" != "0x8a" ]]; then
if [[ -n "$chain_id_hex" ]]; then
echo "FAIL: RPC returned chainId $chain_id_hex (expected 0x8a for Chain 138). Use Core RPC only (RPC_URL_138 = VMID 2101, e.g. http://192.168.11.211:8545)." >&2
else
echo "FAIL: RPC unreachable: $RPC. Fix Core RPC or network." >&2
fi
echo " See docs/04-configuration/RPC_ENDPOINTS_MASTER.md." >&2
exit 1
fi
echo "OK RPC (Core): $RPC (chainId 138)."
# 5) Nonce: warn if pending > latest (stuck txs)
DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null) || { echo "FAIL: cast wallet address failed. Check PRIVATE_KEY in .env." >&2; exit 1; }
NONCE_PENDING=$(cast nonce "$DEPLOYER" --rpc-url "$RPC" --block pending 2>/dev/null) || true
NONCE_LATEST=$(cast nonce "$DEPLOYER" --rpc-url "$RPC" --block latest 2>/dev/null) || true
# Normalize to decimal (cast may return hex 0xN or decimal N)
[[ -z "${NONCE_LATEST//[0-9a-fA-Fx]/}" && -n "$NONCE_LATEST" ]] || NONCE_LATEST=0
[[ -z "${NONCE_PENDING//[0-9a-fA-Fx]/}" && -n "$NONCE_PENDING" ]] || NONCE_PENDING="$NONCE_LATEST"
NONCE_PENDING=$((NONCE_PENDING))
NONCE_LATEST=$((NONCE_LATEST))
echo " Deployer: $DEPLOYER"
echo " Nonce (pending): $NONCE_PENDING (latest: $NONCE_LATEST)"
if [[ $NONCE_PENDING -gt $NONCE_LATEST ]]; then
echo ""
echo "WARN: Pending nonce ($NONCE_PENDING) > latest ($NONCE_LATEST) — possible stuck transaction(s)." >&2
echo " Do NOT deploy until resolved: run ./scripts/clear-all-transaction-pools.sh then wait ~60s." >&2
echo " See docs/03-deployment/DEPLOYMENT_ORDER_OF_OPERATIONS.md § Do not deploy when stuck." >&2
exit 1
fi
echo "OK No stuck transactions (nonce consistent)."
# 6) Optional: cost estimate
if [[ -n "$RUN_COST" ]] && [[ -f "$SMOM/scripts/deployment/calculate-costs-consolidated.sh" ]]; then
echo ""
echo "=== Cost estimate (--cost) ==="
(cd "$SMOM" && bash scripts/deployment/calculate-costs-consolidated.sh) || true
fi
echo ""
echo "Pre-flight passed. Proceed with deployment using scripts that source $SMOM/.env and use RPC_URL_138 (Core only)."