diff --git a/deploy/scripts/deploy_exchange.sh b/deploy/scripts/deploy_exchange.sh index b541b61..24e79e1 100755 --- a/deploy/scripts/deploy_exchange.sh +++ b/deploy/scripts/deploy_exchange.sh @@ -1,32 +1,46 @@ #!/usr/bin/env bash -LOCAL=.env.local -TESTNET=.env.testnet -MAINNET=.env - -if [ -z $1 ] -then - echo "usage: deploy_exchange.sh [local || testnet || mainnet]" - exit 1 -elif [ $1 == "local" ] -then - ENV=$LOCAL -elif [ $1 == "testnet" ] -then - ENV=$TESTNET -elif [ $1 == "mainnet" ] -then - ENV=$MAINNET -else - echo "usage: deploy_exchange.sh [local || testnet || mainnet]" - exit 1 +# File paths for environment variables +LOCAL_ENV=".env.local" +TESTNET_ENV=".env.testnet" +MAINNET_ENV=".env" + +# --- 1. Argument and Environment Selection --- +if [ -z "$1" ]; then + echo "Usage: deploy_exchange.sh [local | testnet | mainnet]" + exit 1 fi -source $ENV +case "$1" in + "local") + ENV_FILE=$LOCAL_ENV + ;; + "testnet") + ENV_FILE=$TESTNET_ENV + ;; + "mainnet") + ENV_FILE=$MAINNET_ENV + ;; + *) + echo "Error: Invalid argument '$1'." + echo "Usage: deploy_exchange.sh [local | testnet | mainnet]" + exit 1 + ;; +esac + +if [ ! -f "$ENV_FILE" ]; then + echo "Error: Environment file '$ENV_FILE' not found." + exit 1 +fi -echo "Deploying CTF Exchange..." +# CRITICAL SECURITY WARNING: Sourcing private keys directly from environment files +# is highly insecure. For production, use hardware wallets (Ledger/Trezor) or +# dedicated secrets management services (e.g., AWS KMS, Azure Key Vault) via Foundry. +source "$ENV_FILE" -echo "Deploy args: +echo "Deploying CTF Exchange to $1 network..." + +echo "Deploy arguments: Admin: $ADMIN Collateral: $COLLATERAL ConditionalTokensFramework: $CTF @@ -34,15 +48,36 @@ ProxyFactory: $PROXY_FACTORY SafeFactory: $SAFE_FACTORY " -OUTPUT="$(forge script ExchangeDeployment \ - --private-key $PK \ - --rpc-url $RPC_URL \ +# --- 2. Optimized Foundry Command --- +OUTPUT=$(forge script ExchangeDeployment \ + --private-key "$PK" \ + --rpc-url "$RPC_URL" \ --json \ --broadcast \ - --with-gas-price 200000000000 \ - -s "deployExchange(address,address,address,address,address)" $ADMIN $COLLATERAL $CTF $PROXY_FACTORY $SAFE_FACTORY)" + --slow # Use --slow to rely on the RPC URL for fetching dynamic gas price, or remove to rely on default EIP-1559 -EXCHANGE=$(echo "$OUTPUT" | grep "{" | jq -r .returns.exchange.value) -echo "Exchange deployed: $EXCHANGE" + # Removed: --with-gas-price 200000000000 to use dynamic gas price from RPC + # If EIP-1559 is supported, Foundry automatically uses it. + # If legacy pricing is needed for non-EIP-1559 chains (e.g., Polygon pre-London), use: + # --gas-price $(cast gas-price --rpc-url $RPC_URL) + + -s "deployExchange(address,address,address,address,address)" "$ADMIN" "$COLLATERAL" "$CTF" "$PROXY_FACTORY" "$SAFE_FACTORY" +) -echo "Complete!" +# --- 3. Process Output (Requires 'jq') --- +# Extracting the deployed contract address from the JSON output +EXCHANGE=$(echo "$OUTPUT" | jq -r ' + .transactions[] | + select(.transactionType == "CREATE" and .contractName == "CTFExchange") | + .contractAddress +') + +if [ -z "$EXCHANGE" ]; then + echo "Error: Deployment failed or could not retrieve contract address." + echo "Full Forge Output:" + echo "$OUTPUT" + exit 1 +fi + +echo "Exchange deployed: $EXCHANGE" +echo "Deployment Complete!"