#!/usr/bin/env bash # Compare deployer balance from Blockscout API vs RPC (e.g. cast balance). # Use this to verify Blockscout index matches the current running chain/RPC. # # Usage: ./scripts/verify/check-deployer-balance-blockscout-vs-rpc.sh [RPC_URL] [EXPLORER_API_URL] # Default RPC: RPC_URL_138 or https://rpc-core.d-bis.org # Default Explorer API: https://explorer.d-bis.org/api/v2 (Chain 138 Blockscout) set -euo pipefail DEPLOYER="${DEPLOYER_ADDRESS:-0x4A666F96fC8764181194447A7dFdb7d471b301C8}" RPC="${1:-${RPC_URL_138:-https://rpc-core.d-bis.org}}" EXPLORER_API="${2:-https://explorer.d-bis.org/api/v2}" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "Deployer balance: Blockscout vs RPC (Chain 138)" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "Deployer: $DEPLOYER" echo "RPC: $RPC" echo "Blockscout API: $EXPLORER_API" echo "" # --- 1. Balance from RPC (source of truth for live chain) --- RPC_WEI="" if command -v cast &>/dev/null; then RPC_WEI=$(cast balance "$DEPLOYER" --rpc-url "$RPC" 2>/dev/null) || true fi if [ -z "$RPC_WEI" ]; then RESP=$(curl -sS -X POST "$RPC" \ -H "Content-Type: application/json" \ -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"$DEPLOYER\",\"latest\"],\"id\":1}" 2>/dev/null) || true RPC_HEX=$(echo "$RESP" | jq -r '.result // empty' 2>/dev/null) || true if [ -n "$RPC_HEX" ] && [ "$RPC_HEX" != "null" ]; then RPC_WEI=$(printf "%d" "$RPC_HEX" 2>/dev/null) || true fi fi if [ -z "$RPC_WEI" ]; then echo "RPC balance: (unable to fetch — RPC unreachable or error)" else RPC_ETH=$(awk "BEGIN { printf \"%.6f\", $RPC_WEI / 1e18 }" 2>/dev/null || echo "N/A") echo "RPC balance: $RPC_WEI wei (~ $RPC_ETH ETH)" fi # --- 2. Balance from Blockscout API --- BLOCKSCOUT_JSON=$(curl -sS "${EXPLORER_API}/addresses/${DEPLOYER}" 2>/dev/null || true) BLOCKSCOUT_WEI="" if [ -n "$BLOCKSCOUT_JSON" ]; then # Try common Blockscout v2 field names (coin_balance or balance, often string) BLOCKSCOUT_WEI=$(echo "$BLOCKSCOUT_JSON" | jq -r '.coin_balance // .balance // .coin_balance_hex // empty' 2>/dev/null) || true if [[ "$BLOCKSCOUT_WEI" == 0x* ]]; then BLOCKSCOUT_WEI=$(printf "%d" "$BLOCKSCOUT_WEI" 2>/dev/null) || true fi if [ -z "$BLOCKSCOUT_WEI" ]; then BLOCKSCOUT_WEI=$(echo "$BLOCKSCOUT_JSON" | jq -r '.data.coin_balance // .data.balance // empty' 2>/dev/null) || true fi fi if [ -z "$BLOCKSCOUT_WEI" ]; then echo "Blockscout: (unable to fetch — API unreachable or address not indexed)" else BLOCKSCOUT_ETH=$(awk "BEGIN { printf \"%.6f\", $BLOCKSCOUT_WEI / 1e18 }" 2>/dev/null || echo "N/A") echo "Blockscout: $BLOCKSCOUT_WEI wei (~ $BLOCKSCOUT_ETH ETH)" fi # --- 3. Compare --- echo "" if [ -n "$RPC_WEI" ] && [ -n "$BLOCKSCOUT_WEI" ]; then if [ "$RPC_WEI" -ge "$BLOCKSCOUT_WEI" ]; then DIFF=$((RPC_WEI - BLOCKSCOUT_WEI)) else DIFF=$((BLOCKSCOUT_WEI - RPC_WEI)) fi if [ "$DIFF" -le 1 ]; then echo "Match: RPC and Blockscout balances match (diff <= 1 wei)." else echo "Difference: RPC and Blockscout differ by $DIFF wei. Indexer may be behind or use a different RPC." fi else echo "Comparison skipped (one or both sources unavailable). Run from a host that can reach RPC and Blockscout API." fi echo "" echo "Explorer (UI): ${EXPLORER_API%/api/v2}/address/$DEPLOYER"