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
109 changes: 78 additions & 31 deletions deploy/scripts/deploy_exchange.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,95 @@
#!/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
# --- ENVIRONMENT CONFIGURATION ---
# Define the environment file names.
LOCAL_ENV=".env.local"
TESTNET_ENV=".env.testnet"
MAINNET_ENV=".env.mainnet" # Renamed from '.env' for clarity/safety
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: Mainnet environment file renamed breaks existing deployments

The mainnet environment file was renamed from .env to .env.mainnet, breaking existing deployments. Users with the previously expected .env file will encounter a "file not found" error when deploying to mainnet, as the script now looks for .env.mainnet instead.

Fix in Cursor Fix in Web


# --- ARGUMENT VALIDATION ---
if [ -z "$1" ]; then
echo "Error: Missing environment argument."
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 environment '$1'."
echo "Usage: deploy_exchange.sh [local | testnet | mainnet]"
exit 1
;;
esac

# Check if the environment file exists
if [ ! -f "$ENV_FILE" ]; then
echo "Error: Environment file not found: $ENV_FILE"
exit 1
fi

echo "Deploying CTF Exchange..."
# Load environment variables (RPC_URL, ADMIN, COLLATERAL, etc.).
# WARNING: Ensure PK (Private Key) is NOT committed to Git.
source "$ENV_FILE"

echo "Deploy args:
# --- DEPLOYMENT PRE-CHECK ---
if [ -z "$PK" ]; then
echo "Error: PK (Private Key) is not set in $ENV_FILE. Aborting for security."
exit 1
fi

echo "--- Deploying CTF Exchange to $1 ---"

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 \
# --- CORE FORGE EXECUTION ---

# Forge script command execution.
# Using standard gas price retrieval (no --with-gas-price) for robustness.
# PK is read from the environment variable (sourced above).
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)"
-s "deployExchange(address,address,address,address,address)" \
"$ADMIN" "$COLLATERAL" "$CTF" "$PROXY_FACTORY" "$SAFE_FACTORY")

EXCHANGE=$(echo "$OUTPUT" | grep "{" | jq -r .returns.exchange.value)
echo "Exchange deployed: $EXCHANGE"
# Check the exit status of the forge script command
if [ $? -ne 0 ]; then
echo "--- DEPLOYMENT FAILED ---"
# Print the full output (including error messages)
echo "$OUTPUT"
exit 1
fi

# --- RESULT PARSING ---

# Extract the deployed address using jq from the raw JSON output.
# Filtering through "grep {" is unnecessary as we expect JSON output.
EXCHANGE=$(echo "$OUTPUT" | jq -r '.returns.exchange.value')

echo "Complete!"
if [ -z "$EXCHANGE" ]; then
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: Null value not detected in exchange address validation

The validation checks if $EXCHANGE is empty, but jq -r returns the string "null" when the JSON path doesn't exist, not an empty string. This allows the script to report success with "Exchange deployed: null" when the deployment actually failed to return a valid address.

Fix in Cursor Fix in Web

echo "Error: Failed to parse deployed exchange address from JSON output."
echo "Full Forge Output:"
echo "$OUTPUT"
exit 1
fi

echo "--- DEPLOYMENT SUCCESS ---"
echo "Exchange deployed: $EXCHANGE"
echo "--- Complete! ---"