Skip to content

Commit

Permalink
refactor(scripts/foundry): moves more variables to constants.sh
Browse files Browse the repository at this point in the history
Simplifies maintenance and readability of scripts.
  • Loading branch information
arthurgousset committed Jul 4, 2024
1 parent 1b90853 commit 4a7606b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
36 changes: 25 additions & 11 deletions packages/protocol/scripts/foundry/constants.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
# Foundry accounts
# Anvil accounts
export FROM_ACCOUNT_NO_ZERO="f39Fd6e51aad88F6F4ce6aB8827279cffFb92266" # Anvil default account (1)
export FROM_ACCOUNT="0x$FROM_ACCOUNT_NO_ZERO" # Anvil default account (1)
export FROM_ACCOUNT_PRIVATE_KEY_NO_ZERO="ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" # Anvil default account (1)
export FROM_ACCOUNT_PRIVATE_KEY="0x$FROM_ACCOUNT_PRIVATE_KEY_NO_ZERO" # Anvil default account (1)

# Foundry directories
TMP_FOLDER="$PWD/.tmp"
ANVIL_FOLDER="$TMP_FOLDER/devchain"

# Foundry configurations
# Anvil configurations (Source: https://book.getfoundry.sh/reference/anvil/)
export ANVIL_PORT=8546
export ANVIL_RPC_URL="http://127.0.0.1:$ANVIL_PORT"
export GAS_LIMIT=50000000
export CODE_SIZE_LIMIT=245760 # EIP-170: Contract code size limit in bytes. Useful to increase for tests. [default: 0x6000 (~25kb)]
export BALANCE=60000 # Set the balance of the accounts. [default: 10000]
export STATE_INTERVAL=1 # Interval in seconds at which the state and block environment is to be dumped to disk.
export STEPS_TRACING="--steps-tracing" # Steps tracing used for debug calls returning geth-style traces. Enable: "--steps-tracing" / Disable: ""

# Forge migration script configurations (Source: https://book.getfoundry.sh/reference/forge/forge-script)
export MIGRATION_SCRIPT_PATH="migrations_sol/Migration.s.sol" # Path to migration script
export TARGET_CONTRACT="Migration" # The name of the contract you want to run.
export BROADCAST="--broadcast" # Broadcasts the transactions. Enable: "--broadcast" / Disable: ""
export SKIP_SIMULATION="" # Skips on-chain simulation. Enable: "--skip-simulation" / Disable: ""
export NON_INTERACTIVE="--non-interactive" # Remove interactive prompts which appear if the contract is near the EIP-170 size limit.
export VERBOSITY_LEVEL="-vvv" # Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
export REGISTRY_OWNER_ADDRESS=$FROM_ACCOUNT_NO_ZERO

# Foundry migration script configurations
export BROADCAST="--broadcast" # Helper to disable broadcast and simulation. On: BROADCAST="--broadcast" / Off: BROADCAST=""
export SKIP_SIMULATION="--skip-simulation" # Helper to disable simulation. On: SKIP_SIMULATION="--skip-simulation" / Off: SKIP_SIMULATION=""
# Foundry directories and file names
export DEVCHAIN_FILE_NAME="devchain.json" # Name of the file that will be published to NPM
export TMP_FOLDER="$PWD/.tmp"
export TEMP_DIR="$PWD/.tmp/libraries"
export ANVIL_FOLDER="$TMP_FOLDER/devchain"

# Contract addresses
export REGISTRY_ADDRESS="0x000000000000000000000000000000000000ce10"
export PROXY_ADMIN_ADDRESS='0x4200000000000000000000000000000000000018' # This address is defined in `IsL2Check.sol`

# Contract bytecode from the Foundry build artifacts
export PROXY_BYTECODE=`cat ./out/Proxy.sol/Proxy.json | jq -r '.deployedBytecode.object'`
export REGISTRY_BYTECODE=$(jq -r '.bytecode' build/contracts/Registry.json)
export PROXY_DEPLOYED_BYTECODE=`cat ./out/Proxy.sol/Proxy.json | jq -r '.deployedBytecode.object'`
# export PROXY_DEPLOYED_BYTECODE=$(jq -r '.deployedBytecode' build/contracts/Proxy.json)

# Contract configurations
export COMMUNITY_REWARD_FRACTION="100000000000000000000" # 0.01 in fixidity format
export CARBON_OFFSETTING_PARTNER="0x22579CA45eE22E2E16dDF72D955D6cf4c767B0eF"
export CARBON_OFFSETTING_FRACTION="10000000000000000000" # 0.001 in fixidity format
export CARBON_OFFSETTING_FRACTION="10000000000000000000" # 0.001 in fixidity format
export REGISTRY_STORAGE_LOCATION="0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103" # Position is bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1);
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,38 @@ time forge build $LIBRARY_FLAGS
# Deploy precompile contracts
source $PWD/scripts/foundry/deploy_precompiles.sh

echo "Setting Registry Proxy"
echo "Debugging $REGISTRY_ADDRESS ${PROXY_DEPLOYED_BYTECODE:0:10}"

echo "Setting Registry Proxy"
cast rpc anvil_setCode $REGISTRY_ADDRESS $PROXY_DEPLOYED_BYTECODE --rpc-url http://127.0.0.1:$ANVIL_PORT

cast rpc anvil_setCode --rpc-url http://127.0.0.1:$ANVIL_PORT $REGISTRY_ADDRESS $PROXY_BYTECODE
echo "Debugging"

echo "Setting Registry owner"
# Sets the storage of the registry so that it has an owner we control
# position is bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1);
echo "Setting Registry owner"
cast rpc \
anvil_setStorageAt \
$REGISTRY_ADDRESS 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103 "0x000000000000000000000000$REGISTRY_OWNER_ADDRESS" \
$REGISTRY_ADDRESS $REGISTRY_STORAGE_LOCATION "0x000000000000000000000000$REGISTRY_OWNER_ADDRESS" \
--rpc-url http://127.0.0.1:$ANVIL_PORT

# run migrations
# Run migrations
echo "Running migration script... "
time forge script migrations_sol/Migration.s.sol --tc Migration --rpc-url http://127.0.0.1:$ANVIL_PORT -vvv $BROADCAST --non-interactive --sender $FROM_ACCOUNT --unlocked $LIBRARY_FLAGS || echo "Migration script failed"
forge script \
$MIGRATION_SCRIPT_PATH \
--target-contract $TARGET_CONTRACT \
--sender $FROM_ACCOUNT \
--unlocked \
$VERBOSITY_LEVEL \
$BROADCAST \
$SKIP_SIMULATION \
$NON_INTERACTIVE \
$LIBRARY_FLAGS \
--rpc-url http://127.0.0.1:$ANVIL_PORT || echo "Migration script failed"

# Keeping track of the finish time to measure how long it takes to run the script entirely
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Total elapsed time: $ELAPSED_TIME seconds"

# Rename devchain artifact and remove unused directory
mv $TMP_FOLDER/devchain/state.json $TMP_FOLDER/devchain.json
rm -rf $TMP_FOLDER/devchain
mv $ANVIL_FOLDER/state.json $TMP_FOLDER/$DEVCHAIN_FILE_NAME
rm -rf $ANVIL_FOLDER
4 changes: 0 additions & 4 deletions packages/protocol/scripts/foundry/deploy_libraries.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail

# Name of temporary directory
# TEMP_DIR_NAME=".tmp/libraries"
TEMP_DIR="$PWD/.tmp/libraries"

# Create a temporary directory or remove it first it if exists
if [ -d "$TEMP_DIR" ]; then
echo "Removing existing temporary folder..."
Expand Down
13 changes: 11 additions & 2 deletions packages/protocol/scripts/foundry/start_anvil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ if nc -z localhost $ANVIL_PORT; then
echo "Killed previous Anvil"
fi

anvil --port $ANVIL_PORT --gas-limit 50000000 --steps-tracing --code-size-limit 245760 --balance 60000 --dump-state $ANVIL_FOLDER --state-interval 1 &
# Start anvil
anvil \
--port $ANVIL_PORT \
--dump-state $ANVIL_FOLDER \
--state-interval $STATE_INTERVAL \
--gas-limit $GAS_LIMIT \
--code-size-limit $CODE_SIZE_LIMIT \
--balance $BALANCE \
--steps-tracing &
# For context "&" tells the shell to start a command as a background process.
# This allows you to continue executing other commands without waiting for the background command to finish.

# alternatively:
# ANVIL_PID=`lsof -i tcp:8545 | tail -n 1 | awk '{print $2}'`
Expand All @@ -29,7 +39,6 @@ export ANVIL_PID=$!

echo "Waiting Anvil to launch on $ANVIL_PORT..."


while ! nc -z localhost $ANVIL_PORT; do
sleep 0.1 # wait for 1/10 of the second before check again
done
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/scripts/foundry/stop_anvil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set -euo pipefail

# A small script to terminate any instance of anvil currently serving at localhost.

ANVIL_PORT=8546
# Read environment variables and constants
source $PWD/scripts/foundry/constants.sh

if nc -z localhost $ANVIL_PORT; then
kill $(lsof -i tcp:$ANVIL_PORT | tail -n 1 | awk '{print $2}')
Expand Down

0 comments on commit 4a7606b

Please sign in to comment.