Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 66 additions & 31 deletions deploy/scripts/deploy_exchange.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,83 @@
#!/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
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"
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing line continuation breaks forge script command

The forge script command line continuation is broken. Line 57 ends with --slow # comment without a backslash, which terminates the command prematurely instead of continuing to the -s flag on line 65. This causes the deployment to fail because the actual script function and its arguments are never executed.

Fix in Cursor Fix in Web


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!"