Skip to content

Commit

Permalink
feat: add l2 teardown script
Browse files Browse the repository at this point in the history
  • Loading branch information
parketh committed Nov 11, 2024
1 parent 85202d3 commit 052368d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ L1_EXPLORER_URL=https://sepolia.etherscan.io/
L1_CHAIN_ID=11155111
L1_CHAIN_NAME=Sepolia
L1_BLOCK_TIME=12
L1_FUNDED_ADDRESS=<DEPLOYER_ADDRESS>
L1_FUNDED_PRIVATE_KEY=<DEPLOYER_PRIVATE_KEY>
# Amount to fund the ADMIN, BATCHER and PROPOSER addresses
# Specified as a string with a unit type. e.g. 1ether, 10gwei, 0.01ether
Expand Down
62 changes: 62 additions & 0 deletions scripts/l2/l2-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
set -euo pipefail

# Path to the .env file
ENV_FILE="$(pwd)/.env"

# Source the .env file
source "$ENV_FILE"

# Function to send out funds from an address
drain_address() {
local address=$1
local private_key=$2

# Get existing balance
balance=$(cast balance --rpc-url "$L2_RPC_URL" "$address")
echo "Existing balance: $balance"

if [[ "$balance" == "0" ]]; then
echo "Balance is 0, skipping..."
return
fi

# Estimate gas
gas_cost=$(cast estimate --rpc-url "$L2_RPC_URL" \
--from "$address" \
--value "$balance" \
"$L1_FUNDED_ADDRESS" | \
xargs cast --to-eth --pad 18)

# Add 20% buffer
gas_cost_with_buffer=$(echo "$gas_cost * 1.2" | bc)
amount=$(echo "$balance - $gas_cost_with_buffer" | bc)

# Send out funds if amount is greater than 0
if [[ "$amount" -gt 0 ]]; then
echo "Sending out $amount to $L1_FUNDED_ADDRESS"
cast send --private-key "$private_key" --rpc-url "$L2_RPC_URL" \
--value "$amount" "$L1_FUNDED_ADDRESS"
else
echo "Gas cost exceeds balance, skipping..."
fi
}

# Fund the generated addresses
for role in ADMIN BATCHER PROPOSER; do
address_var="GS_${role}_ADDRESS"
private_key_var="GS_${role}_PRIVATE_KEY"
address="${!address_var}"
private_key="${!private_key_var}"

if [[ -n "$address" ]]; then
echo "Draining $role address: $address"
drain_address "$address" "$private_key"
echo
else
echo "Warning: $role address not found in .env file"
echo
fi
done

echo "Draining complete."

0 comments on commit 052368d

Please sign in to comment.