Skip to content

Commit cf2d162

Browse files
committed
add anvil
1 parent a214833 commit cf2d162

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

scripts/node-env.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fi
2222
# Polkadot SDK directory path (can be overridden by environment variable)
2323
POLKADOT_SDK_DIR="${POLKADOT_SDK_DIR:-$HOME/polkadot-sdk}"
2424

25+
# anvil directory path (can be overridden by environment variable)
26+
FOUNDRY_DIR="${FOUNDRY_DIR:-$HOME/github/foundry-polkadot}"
27+
2528
# Define the revive-differential-tests directory path (can be overridden by environment variable)
2629
RETESTER_DIR="${RETESTER_DIR:-$HOME/github/revive-differential-tests}"
2730

@@ -1306,3 +1309,105 @@ function westend_stack() {
13061309
# Wait for eth-rpc to be ready
13071310
wait_for_eth_rpc
13081311
}
1312+
1313+
# Runs anvil (Ethereum node) in development mode
1314+
# Useful for testing contracts against standard Ethereum
1315+
# Usage: anvil-dev [proxy|run] [--port <port>]
1316+
# Examples:
1317+
# anvil-dev - Use default run mode on port 8545
1318+
# anvil-dev run --port 8546 - Run on custom port 8546
1319+
# anvil-dev proxy - Run with mitmproxy (8545->8546)
1320+
# anvil-dev proxy --port 9000 - Run with mitmproxy (9000->9001)
1321+
function anvil-dev() {
1322+
# Parse arguments
1323+
local mode="run"
1324+
local port="8545"
1325+
1326+
# Check if first arg is mode
1327+
if [[ "$1" =~ ^(proxy|run)$ ]]; then
1328+
mode="$1"
1329+
shift
1330+
fi
1331+
1332+
# Parse remaining arguments
1333+
while [[ $# -gt 0 ]]; do
1334+
case "$1" in
1335+
--port)
1336+
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
1337+
port="$2"
1338+
shift 2
1339+
else
1340+
echo "Error: --port requires a numeric argument"
1341+
return 1
1342+
fi
1343+
;;
1344+
--build)
1345+
cargo build --manifest-path $FOUNDRY_DIR/Cargo.toml --release -p anvil-polkadot
1346+
;;
1347+
*)
1348+
echo "Unknown argument: $1"
1349+
return 1
1350+
;;
1351+
esac
1352+
done
1353+
1354+
# Helper function to start anvil
1355+
start_anvil() {
1356+
local port="$1"
1357+
set -x
1358+
"$FOUNDRY_DIR/target/release/anvil-polkadot" -p "$port"
1359+
{ set +x; } 2>/dev/null
1360+
}
1361+
1362+
# Execute based on mode
1363+
case "$mode" in
1364+
proxy)
1365+
# Calculate server port (port + 1)
1366+
local server_port=$((port + 1))
1367+
1368+
# Kill any existing mitmproxy instances
1369+
pkill -f mitmproxy
1370+
1371+
# Start mitmproxy
1372+
start_mitmproxy "${port}:${server_port}"
1373+
1374+
# Start anvil on server port
1375+
start_anvil "$server_port"
1376+
;;
1377+
run)
1378+
# Start anvil directly
1379+
start_anvil "$port"
1380+
;;
1381+
esac
1382+
}
1383+
1384+
# Runs anvil in a new tmux window
1385+
# Provides a quick way to start an Ethereum development node
1386+
# Usage: anvil_stack [--proxy] [--build]
1387+
# Examples:
1388+
# anvil_stack - Run anvil without proxy
1389+
# anvil_stack --proxy - Run anvil with proxy
1390+
# anvil_stack --build - Build then Run anvil
1391+
function anvil_stack() {
1392+
# Kill existing 'servers' window if it exists
1393+
tmux kill-window -t servers 2>/dev/null
1394+
1395+
# Parse arguments
1396+
mode="run"
1397+
build_flag=""
1398+
1399+
for arg in "$@"; do
1400+
case "$arg" in
1401+
--proxy)
1402+
mode="proxy"
1403+
;;
1404+
--build)
1405+
build_flag="--build"
1406+
;;
1407+
esac
1408+
done
1409+
1410+
# Create new 'servers' window in detached mode
1411+
# Source shell config and run anvil with specified mode
1412+
tmux new-window -d -n servers "$CURRENT_SHELL -c 'source $SHELL_RC; anvil-dev $mode $build_flag; exec \$SHELL'"
1413+
}

0 commit comments

Comments
 (0)