#!/usr/bin/env bash # Preflight: RPC reachability and eth_chainId for XDC Zero second pair (parent ↔ Chain 138). # Optional: ETHEREUM_MAINNET_RPC (or other vars) for quick public mainnet access checks. # # Usage: # bash scripts/verify/xdc-zero-chain138-preflight.sh # XDC mainnet: set XDC_PARENTNET_URL or rely on fallbacks (erpc.xinfin.network, Ankr, rpc.xinfin.network). # Apothem: XDC_PARENTNET_URL=https://rpc.apothem.network bash scripts/verify/xdc-zero-chain138-preflight.sh # # Loads repo dotenv via scripts/lib/load-project-env.sh when sourced from repo root. # Avoid nounset while sourcing dotenv (some .env lines reference optional vars). set -eo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # shellcheck source=/dev/null source "${PROJECT_ROOT}/scripts/lib/load-project-env.sh" _rpc_chain_id() { local url="$1" local name="$2" if [[ -z "$url" || "$url" == "devnet" || "$url" == "testnet" ]]; then echo "SKIP $name: empty or alias URL ($url) — resolve to HTTPS RPC for this check." return 0 fi local out if ! out="$(curl -sS --connect-timeout 8 --max-time 20 -X POST "$url" \ -H 'Content-Type: application/json' \ -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>/dev/null)"; then echo "FAIL $name: no HTTP response from $url" return 1 fi local hex hex="$(printf '%s' "$out" | sed -n 's/.*"result":"\([^"]*\)".*/\1/p')" if [[ -z "$hex" ]]; then echo "FAIL $name: bad JSON or error from $url — $out" return 1 fi echo "OK $name ($url) eth_chainId=$hex" } _balance_eth() { local url="$1" local addr="$2" local out if ! out="$(curl -sS --connect-timeout 8 --max-time 20 -X POST "$url" \ -H 'Content-Type: application/json' \ -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"${addr}\",\"latest\"],\"id\":1}" 2>/dev/null)"; then echo "" return 1 fi local hex hex="$(printf '%s' "$out" | sed -n 's/.*"result":"\([^"]*\)".*/\1/p')" [[ -n "$hex" ]] || return 1 cast to-unit "$hex" ether 2>/dev/null || printf '%s' "$hex" } # Echo chainId hex or empty if URL is not a working JSON-RPC endpoint. _eth_chain_id_hex() { local url="$1" local out hex if ! out="$(curl -sS --connect-timeout 8 --max-time 20 -X POST "$url" \ -H 'Content-Type: application/json' \ -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' 2>/dev/null)"; then return 1 fi hex="$(printf '%s' "$out" | sed -n 's/.*"result":"\([^"]*\)".*/\1/p')" [[ -n "$hex" ]] || return 1 printf '%s' "$hex" } # Pick first working XDC parent RPC; sets global PARENT to the URL that answered. _resolve_xdc_parent_rpc() { local primary="${XDC_PARENTNET_URL:-}" [[ -z "$primary" ]] && primary="${PARENTNET_URL:-}" local fallbacks=( "https://erpc.xinfin.network" "https://rpc.ankr.com/xdc" "https://rpc.xinfin.network" ) local candidates=() [[ -n "$primary" ]] && candidates+=("$primary") candidates+=("${fallbacks[@]}") local seen="|" u hex for u in "${candidates[@]}"; do [[ -z "$u" || "$u" == "devnet" || "$u" == "testnet" ]] && continue case "$seen" in *"|$u|"*) continue;; esac seen="${seen}${u}|" if hex="$(_eth_chain_id_hex "$u")"; then echo "OK XDC parent ($u) eth_chainId=$hex" PARENT="$u" return 0 fi done echo "FAIL XDC parent: no working RPC (tried ${#candidates[@]} candidate URLs; set XDC_PARENTNET_URL to a healthy endpoint)" return 1 } echo "=== XDC Zero Chain 138 pair — RPC preflight ===" fail=0 PARENT="" if [[ -n "${XDC_PARENTNET_URL:-${PARENTNET_URL:-}}" ]]; then echo "INFO XDC parent: trying XDC_PARENTNET_URL / PARENTNET_URL first, then public fallbacks if needed" else echo "INFO XDC_PARENTNET_URL and PARENTNET_URL unset — trying public XDC mainnet RPC fallbacks" fi _resolve_xdc_parent_rpc || fail=1 PEER_OPERATOR="${XDC_ZERO_PEER_RPC_URL:-${RPC_URL_138:-${CHAIN138_RPC_URL:-${CHAIN138_RPC:-}}}}" PEER_SERVICE="${CHAIN138_PUBLIC_RPC_URL:-${SUBNET_URL:-${RPC_URL_138_PUBLIC:-}}}" if [[ -n "$PEER_OPERATOR" ]]; then _rpc_chain_id "$PEER_OPERATOR" "Chain138 operator RPC_URL_138" || fail=1 else echo "WARN RPC_URL_138 (or CHAIN138_RPC_URL) unset — set for Chain 138 checks." fi if [[ -n "$PEER_SERVICE" ]]; then _rpc_chain_id "$PEER_SERVICE" "Chain138 service CHAIN138_PUBLIC_RPC_URL / SUBNET_URL / RPC_URL_138_PUBLIC" || fail=1 else echo "WARN CHAIN138_PUBLIC_RPC_URL, SUBNET_URL, and RPC_URL_138_PUBLIC unset — set one for relayer/service checks." fi if [[ -n "${ETHEREUM_MAINNET_RPC:-}" ]]; then _rpc_chain_id "$ETHEREUM_MAINNET_RPC" "ETHEREUM_MAINNET_RPC (optional)" || fail=1 fi if [[ -n "${BSC_RPC_URL:-}" ]]; then _rpc_chain_id "$BSC_RPC_URL" "BSC_RPC_URL (optional)" || fail=1 fi if [[ "$fail" -ne 0 ]]; then echo "=== Preflight finished with failures ===" exit 1 fi if [[ -n "${PRIVATE_KEY:-}" ]]; then DEPLOYER_ADDR="$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null || true)" if [[ -n "$DEPLOYER_ADDR" ]]; then echo "" echo "Deployer readiness" echo " deployer_address: $DEPLOYER_ADDR" if [[ -n "$PARENT" ]]; then parent_bal="$(_balance_eth "$PARENT" "$DEPLOYER_ADDR" || true)" if [[ -n "$parent_bal" ]]; then echo " parent_balance: $parent_bal" if [[ "$parent_bal" == "0.000000000000000000" || "$parent_bal" == "0" || "$parent_bal" == "0x0" ]]; then echo " note: deployer has no native gas on parent; parent Endpoint/CSC deploy and registerChain are blocked until funded." fi fi fi if [[ -n "$PEER_OPERATOR" ]]; then peer_bal="$(_balance_eth "$PEER_OPERATOR" "$DEPLOYER_ADDR" || true)" [[ -n "$peer_bal" ]] && echo " chain138_balance: $peer_bal" fi fi fi echo "=== Preflight finished ===" echo "Next: deploy Endpoint/CSC per docs/03-deployment/CHAIN138_XDC_ZERO_BRIDGE_RUNBOOK.md"