#!/usr/bin/env bash # Phase 1.1: Test Bridge Functionality # This script attempts a test transfer from ChainID 138 to Mainnet to verify if bridge works set -euo pipefail # Load IP configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } log_section() { echo -e "\n${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"; echo -e "${CYAN}$1${NC}"; echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"; } # Configuration CHAIN138_RPC="http://${RPC_CORE_1}:8545" WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" WETH9_BRIDGE="0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6" MAINNET_SELECTOR="5009297550715157269" TEST_AMOUNT_ETH="0.001" # Minimal test amount TEST_AMOUNT_WEI="1000000000000000" # 0.001 ETH in wei log_section "Phase 1.1: Test Bridge Functionality" # Load environment variables if [ -f "$PROJECT_ROOT/.env" ]; then source "$PROJECT_ROOT/.env" 2>/dev/null || true elif [ -f "$PROJECT_ROOT/smom-dbis-138/.env" ]; then source "$PROJECT_ROOT/smom-dbis-138/.env" 2>/dev/null || true fi PRIVATE_KEY="${PRIVATE_KEY:-}" if [ -z "$PRIVATE_KEY" ]; then log_error "PRIVATE_KEY not found in environment" log_info "Please set PRIVATE_KEY in .env file or export it" log_info "This should be the admin/operator key with ETH and LINK on ChainID 138" exit 1 fi # Get sender address SENDER=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "") if [ -z "$SENDER" ]; then log_error "Failed to derive address from private key" exit 1 fi log_info "Test Configuration:" log_info " ChainID 138 RPC: $CHAIN138_RPC" log_info " Test Amount: $TEST_AMOUNT_ETH ETH ($TEST_AMOUNT_WEI wei)" log_info " Sender: $SENDER" log_info " Recipient (on Mainnet): $SENDER (same as sender)" log_info " WETH9 Bridge: $WETH9_BRIDGE" log_info " Destination Chain Selector: $MAINNET_SELECTOR" log_info "" # Test RPC connection log_info "Testing RPC connection..." CHAIN_ID=$(cast chain-id --rpc-url "$CHAIN138_RPC" 2>/dev/null || echo "") if [ -z "$CHAIN_ID" ]; then log_warn "Failed to connect to $CHAIN138_RPC, trying alternative..." CHAIN138_RPC="http://${RPC_ALLTRA_1:-192.168.11.250}:8545" CHAIN_ID=$(cast chain-id --rpc-url "$CHAIN138_RPC" 2>/dev/null || echo "") if [ -z "$CHAIN_ID" ]; then log_error "Failed to connect to RPC endpoints" exit 1 fi log_info "Using alternative RPC: $CHAIN138_RPC" fi log_success "RPC connection successful (ChainID: $CHAIN_ID)" log_info "" # Check ETH balance log_info "Checking ETH balance..." ETH_BALANCE=$(cast balance "$SENDER" --rpc-url "$CHAIN138_RPC" 2>/dev/null || echo "0") ETH_BALANCE_ETH=$(echo "scale=6; $ETH_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0") log_info "ETH Balance: $ETH_BALANCE_ETH ETH" if (( $(echo "$ETH_BALANCE < 5000000000000000" | bc -l 2>/dev/null || echo 1) )); then log_warn "Low ETH balance. Recommend at least 0.005 ETH for gas fees" fi log_info "" # Check WETH9 balance log_info "Checking WETH9 balance..." WETH9_BALANCE=$(cast call "$WETH9_ADDRESS" "balanceOf(address)" "$SENDER" --rpc-url "$CHAIN138_RPC" 2>/dev/null | cast --to-dec 2>/dev/null || echo "0") WETH9_BALANCE_ETH=$(echo "scale=6; $WETH9_BALANCE / 1000000000000000000" | bc 2>/dev/null || echo "0") log_info "WETH9 Balance: $WETH9_BALANCE_ETH WETH9" if (( $(echo "$WETH9_BALANCE < $TEST_AMOUNT_WEI" | bc -l 2>/dev/null || echo 1) )); then log_warn "Insufficient WETH9 balance. Need $TEST_AMOUNT_ETH WETH9" log_info "Would need to wrap ETH to WETH9 first (skipping for this test)" log_warn "⚠ Test transfer cannot proceed without WETH9" echo "TRANSFER_STATUS=INSUFFICIENT_WETH9" > "$PROJECT_ROOT/.phase1-transfer-status" exit 1 fi log_info "" # Check bridge approval log_info "Checking bridge approval..." ALLOWANCE=$(cast call "$WETH9_ADDRESS" "allowance(address,address)" "$SENDER" "$WETH9_BRIDGE" --rpc-url "$CHAIN138_RPC" 2>/dev/null | cast --to-dec 2>/dev/null || echo "0") if (( $(echo "$ALLOWANCE < $TEST_AMOUNT_WEI" | bc -l 2>/dev/null || echo 1) )); then log_warn "Bridge not approved. Approval would be needed for actual transfer" log_info "For test purposes, we'll attempt to call the bridge function to see if it works" else log_success "Bridge approval sufficient" fi log_info "" # Check if bridge function exists and is callable log_info "Testing if bridge sendCrossChain function is callable..." log_info "Attempting to estimate gas for sendCrossChain call..." GAS_ESTIMATE=$(cast estimate "$WETH9_BRIDGE" \ "sendCrossChain(uint64,address,uint256)" \ "$MAINNET_SELECTOR" \ "$SENDER" \ "$TEST_AMOUNT_WEI" \ --rpc-url "$CHAIN138_RPC" 2>&1 || echo "ERROR") if echo "$GAS_ESTIMATE" | grep -qE "error|Error|ERROR|Execution reverted|revert"; then ERROR_MSG="$GAS_ESTIMATE" log_error "Gas estimation failed - Bridge may not be configured" log_detail "Error: $ERROR_MSG" log_info "" # Check common error reasons if echo "$ERROR_MSG" | grep -qi "destination"; then log_warn "Error suggests destination is not configured" echo "TRANSFER_STATUS=NO_DESTINATION" > "$PROJECT_ROOT/.phase1-transfer-status" elif echo "$ERROR_MSG" | grep -qi "allowance\|approve"; then log_warn "Error suggests approval issue (expected if not approved)" echo "TRANSFER_STATUS=APPROVAL_REQUIRED" > "$PROJECT_ROOT/.phase1-transfer-status" else log_warn "Unknown error - may indicate configuration issue" echo "TRANSFER_STATUS=CONFIGURATION_ERROR" > "$PROJECT_ROOT/.phase1-transfer-status" fi log_info "" log_warn "⚠ Test indicates bridge may NOT be properly configured for ChainID 138 → Mainnet" log_info "Recommendation: Proceed to Phase 2 (choose resolution option)" exit 1 else GAS_VALUE=$(echo "$GAS_ESTIMATE" | cast --to-dec 2>/dev/null || echo "$GAS_ESTIMATE") log_success "Gas estimation successful: $GAS_VALUE gas units" log_info "" log_success "✓ Bridge function is callable and appears to be configured!" log_info "" log_warn "Note: This test only verifies the function is callable." log_warn "Actual transfer would require approval and LINK for fees." log_info "" echo "TRANSFER_STATUS=WORKING" > "$PROJECT_ROOT/.phase1-transfer-status" log_section "Phase 1.1 Summary - Test Transfer Analysis" log_success "✓ Bridge sendCrossChain function is accessible and configured" log_info "This suggests destinations ARE configured, despite missing interface functions" log_info "" log_info "Next Step: Verify with actual transaction if needed, or proceed with Phase 1 complete" fi log_info "" log_success "Phase 1.1 Complete - Transfer test result saved to .phase1-transfer-status"