#!/usr/bin/env bash set -euo pipefail # Calculate optimal gas price for ChainID 138 deployments # Uses RPC gas price API and respects minimum gas price from config # # Usage: # GAS_PRICE=$(bash scripts/calculate-chain138-gas-price.sh) # echo "Gas price: $GAS_PRICE wei" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" # Load environment (suppress errors) set +e if [ -f "$PROJECT_ROOT/.env" ]; then source "$PROJECT_ROOT/.env" 2>/dev/null || true fi if [ -f "$PROJECT_ROOT/smom-dbis-138/.env" ]; then source "$PROJECT_ROOT/smom-dbis-138/.env" 2>/dev/null || true fi set -e # RPC URL for ChainID 138 RPC_URL="${RPC_URL_138:-http://192.168.11.211:8545}" # Minimum gas price from config (1 gwei = 1,000,000,000 wei) MIN_GAS_PRICE_WEI="${MIN_GAS_PRICE_WEI:-1000000000}" # Safety multiplier (10% buffer) SAFETY_MULTIPLIER="${SAFETY_MULTIPLIER:-1.1}" # Get current gas price from RPC get_rpc_gas_price() { local rpc_url=$1 cast gas-price --rpc-url "$rpc_url" 2>/dev/null || echo "" } # Calculate optimal gas price calculate_optimal_gas_price() { # Try to get current gas price from RPC local current_gas_price=$(get_rpc_gas_price "$RPC_URL") # Use minimum if RPC call failed or returned 0 if [ -z "$current_gas_price" ] || [ "$current_gas_price" = "0" ]; then current_gas_price="$MIN_GAS_PRICE_WEI" fi # Use the higher of current or minimum local base_gas_price=$current_gas_price if [ "$current_gas_price" -lt "$MIN_GAS_PRICE_WEI" ]; then base_gas_price="$MIN_GAS_PRICE_WEI" fi # Apply safety multiplier (using integer math: * 11 / 10 = 1.1) local optimal_gas_price=$((base_gas_price * 11 / 10)) echo "$optimal_gas_price" } # Output gas price calculate_optimal_gas_price