Skip to content

Commit

Permalink
Update zksync flow (#901)
Browse files Browse the repository at this point in the history
* Update zksync flow

* Use darwin and not mac
  • Loading branch information
ezynda3 authored Dec 11, 2024
1 parent 2e885fc commit 5ce2653
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 36 deletions.
2 changes: 2 additions & 0 deletions foundry-zksync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
forge
cast
12 changes: 0 additions & 12 deletions foundry-zksync/Dockerfile

This file was deleted.

11 changes: 5 additions & 6 deletions script/deploy/deploySingleContract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,10 @@ deploySingleContract() {
rm -fr ./out
rm -fr ./zkout
# Clean zksync cache
docker run --rm -it --volume .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge cache clean
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge cache clean

# Run zksync specific fork of forge from Docker so as not to pollute the
# local foundry forge setup
docker run --rm -it --volume .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge build --zksync
# Run zksync specific fork of forge
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge build --zksync
fi

# execute script
Expand All @@ -224,8 +223,8 @@ deploySingleContract() {
doNotContinueUnlessGasIsBelowThreshold "$NETWORK"

if [[ $NETWORK == "zksync" ]]; then
# Deploy zksync scripts using the zksync specific fork of forge from Docker
RAW_RETURN_DATA=$(docker run --rm -it --volume .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync -e DEPLOYSALT=$DEPLOYSALT -e NETWORK=$NETWORK -e FILE_SUFFIX=$FILE_SUFFIX -e PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") foundry-zksync forge script "$FULL_SCRIPT_PATH" -f $NETWORK --json --silent --broadcast --skip-simulation --slow --zksync)
# Deploy zksync scripts using the zksync specific fork of forge
RAW_RETURN_DATA=$(FOUNDRY_PROFILE=zksync DEPLOYSALT=$DEPLOYSALT NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") ./foundry-zksync/forge script "$FULL_SCRIPT_PATH" -f $NETWORK --json --broadcast --skip-simulation --slow --zksync)
else
# try to execute call
RAW_RETURN_DATA=$(DEPLOYSALT=$DEPLOYSALT CREATE3_FACTORY_ADDRESS=$CREATE3_FACTORY_ADDRESS NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX DEFAULT_DIAMOND_ADDRESS_DEPLOYSALT=$DEFAULT_DIAMOND_ADDRESS_DEPLOYSALT DEPLOY_TO_DEFAULT_DIAMOND_ADDRESS=$DEPLOY_TO_DEFAULT_DIAMOND_ADDRESS PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") DIAMOND_TYPE=$DIAMOND_TYPE forge script "$FULL_SCRIPT_PATH" -f $NETWORK --json --broadcast --skip-simulation --legacy)
Expand Down
158 changes: 149 additions & 9 deletions script/helperFunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1427,17 +1427,17 @@ function verifyContract() {
# only show output if DEBUG flag is activated
if [[ "$DEBUG" == *"true"* ]]; then
if [[ $NETWORK == "zksync" ]]; then
# Verify using foundry-zksync from docker image
docker run --rm -it -v .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge verify-contract --zksync --watch --chain 324 "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}"
# Verify using foundry-zksync
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge verify-contract --zksync --watch --chain 324 "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}"
else
forge verify-contract --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}"
fi

# TODO: add code that automatically identifies blockscout verification
else
if [[ $NETWORK == "zksync" ]]; then
# Verify using foundry-zksync from docker image
docker run --rm -it -v .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
# Verify using foundry-zksync
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
else
forge verify-contract --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
fi
Expand All @@ -1446,17 +1446,17 @@ function verifyContract() {
# only show output if DEBUG flag is activated
if [[ "$DEBUG" == *"true"* ]]; then
if [[ $NETWORK == "zksync" ]]; then
# Verify using foundry-zksync from docker image
docker run --rm -it -v .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}"
# Verify using foundry-zksync
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}"
else
forge verify-contract --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}"
fi
else
if [[ $NETWORK == "zksync" ]]; then
# Verify using foundry-zksync from docker image
docker run --rm -it -v .:/foundry -u $(id -u):$(id -g) -e FOUNDRY_PROFILE=zksync foundry-zksync forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
# Verify using foundry-zksync
FOUNDRY_PROFILE=zksync ./foundry-zksync/forge verify-contract --zksync --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
else
forge verify-contract --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-ar --zksyncgs $ARGS --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
forge verify-contract --watch --chain "$CHAIN_ID" "$ADDRESS" "$FULL_PATH" --constructor-args $ARGS --skip-is-verified-check -e "${!API_KEY}" >/dev/null 2>&1
fi
fi
fi
Expand Down Expand Up @@ -3677,6 +3677,146 @@ function updateDiamondLogs() {
done
playNotificationSound
}

# Function: install_foundry_zksync
# Description: Downloads and installs the zkSync version of foundry tools (forge and cast)
# Arguments:
# $1 - Installation directory (optional, defaults to ./foundry-zksync)
# FOUNDRY_ZKSYNC_VERSION - Environment variable to specify version
# Example Versions:
# FOUNDRY_ZKSYNC_VERSION="nightly-082b6a3610be972dd34aff9439257f4d85ddbf15"
# Returns:
# 0 - Success
# 1 - Failure (with error message)
install_foundry_zksync() {
# Foundry ZKSync version
local FOUNDRY_ZKSYNC_VERSION="nightly-082b6a3610be972dd34aff9439257f4d85ddbf15"
# Allow custom installation directory or use default
local install_dir="${1:-./foundry-zksync}"

# Verify that FOUNDRY_ZKSYNC_VERSION is set
if [ -z "${FOUNDRY_ZKSYNC_VERSION}" ]; then
echo "Error: FOUNDRY_ZKSYNC_VERSION is not set"
return 1
fi

echo "Using Foundry zkSync version: ${FOUNDRY_ZKSYNC_VERSION}"

# Check if binaries already exist and are executable
# -x tests if a file exists and has execute permissions
if [ -x "${install_dir}/forge" ] && [ -x "${install_dir}/cast" ]; then
echo "forge and cast binaries already exist in ${install_dir} and are executable"
echo "Skipping download and installation"
return 0
fi

# Detect operating system
# $OSTYPE is a bash variable that contains the operating system type
local os
if [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
else
echo "Unsupported operating system"
return 1
fi

# Detect CPU architecture
# uname -m returns the machine hardware name
local arch
case $(uname -m) in
x86_64) # Intel/AMD 64-bit
arch="amd64"
;;
arm64|aarch64) # ARM 64-bit (e.g., Apple Silicon, AWS Graviton)
arch="arm64"
;;
*)
echo "Unsupported architecture: $(uname -m)"
return 1
;;
esac

# Construct download URL using the specified version
local base_url="https://github.com/matter-labs/foundry-zksync/releases/download/${FOUNDRY_ZKSYNC_VERSION}"
local filename="foundry_nightly_${os}_${arch}.tar.gz"
local download_url="${base_url}/${filename}"

# Create installation directory if it doesn't exist
# -p flag creates parent directories if needed
mkdir -p "$install_dir"

# Print detection results
echo "Detected OS: $os"
echo "Detected Architecture: $arch"
echo "Downloading from: $download_url"
echo "Installing to: $install_dir"

# Download the file using curl or wget, whichever is available
# command -v checks if a command exists
# &> /dev/null redirects both stdout and stderr to null
if command -v curl &> /dev/null; then
# -L flag follows redirects, -o specifies output file
curl -L -o "${install_dir}/${filename}" "$download_url"
elif command -v wget &> /dev/null; then
# -O specifies output file
wget -O "${install_dir}/${filename}" "$download_url"
else
echo "Neither curl nor wget is installed"
return 1
fi

# Check if download was successful
# $? contains the return status of the last command
if [ $? -ne 0 ]; then
echo "Download failed"
return 1
fi

echo "Download completed successfully"

# Extract the archive
# -x extract, -z gzip, -f file
echo "Extracting files..."
tar -xzf "${install_dir}/${filename}" -C "$install_dir"

if [ $? -ne 0 ]; then
echo "Extraction failed"
return 1
fi

# Make binaries executable
# +x adds execute permission
echo "Setting executable permissions..."
chmod +x "${install_dir}/forge" "${install_dir}/cast"

if [ $? -ne 0 ]; then
echo "Failed to set executable permissions"
return 1
fi

# Clean up by removing the downloaded archive
echo "Cleaning up..."
rm "${install_dir}/${filename}"

if [ $? -ne 0 ]; then
echo "Cleanup failed"
return 1
fi

# Verify that binaries are executable
# This is a final check to ensure everything worked
if [ ! -x "${install_dir}/forge" ] || [ ! -x "${install_dir}/cast" ]; then
echo "Installation completed but binaries are not executable. Please check permissions."
return 1
fi

echo "Installation completed successfully"
echo "Binaries are executable and ready to use"
return 0
}

# <<<<<< helpers to set/update deployment files/logs/etc

# test cases for helper functions
Expand Down
11 changes: 2 additions & 9 deletions script/scriptMaster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,8 @@ scriptMaster() {
if [[ $NETWORK == "zksync" ]]; then
# Use zksync specific scripts
DEPLOY_SCRIPT_DIRECTORY="script/deploy/zksync/"
# Check if the foundry-zksync docker image exists
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q '^foundry-zksync:latest$'; then
echo "The 'foundry-zksync' image already exists. Skipping build."
else
# If it doesn't exist we need to build it
echo "The 'foundry-zksync' image does not exist. Building it now..."
docker build -t foundry-zksync ./foundry-zksync
echo "The 'foundry-zksync' image has been built successfully."
fi
# Check if the foundry-zksync binaries exist, if not fetch them
install_foundry_zksync
fi

# get user-selected deploy script and contract from list
Expand Down

0 comments on commit 5ce2653

Please sign in to comment.