diff --git a/.env.example b/.env.example index 35cbf01..3d31d2a 100644 --- a/.env.example +++ b/.env.example @@ -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= L1_FUNDED_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 diff --git a/scripts/l2/l2-teardown.sh b/scripts/l2/l2-teardown.sh new file mode 100644 index 0000000..477bdcd --- /dev/null +++ b/scripts/l2/l2-teardown.sh @@ -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." \ No newline at end of file