-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestnet_up.sh
executable file
·114 lines (97 loc) · 2.43 KB
/
testnet_up.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash -i
# Example usage:
# ./testnet_up.sh https://arbitrum-sepolia.infura.io/v3/your-api-key --persist
# Vars
forkUrl=$1
persist=false
persistLocation=".testnet.state.log"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--persist)
persist=true
shift
;;
--help)
echo "Usage: ./testnet_up.sh <fork-url> [--persist]"
echo " --persist: Persist the state of the testnet"
exit 0
;;
*)
shift
;;
esac
done
# Check dependencies
hash anvil
if [ $? -eq 1 ]; then
echo "Error: Anvil is not installed. Please install Anvil via foundry 'https://book.getfoundry.sh/getting-started/installation'"
exit 1
fi
hash npm
if [ $? -eq 1 ]; then
echo "Error: npm is not installed. Please install npm via 'https://www.npmjs.com/get-npm'"
exit 1
fi
echo "Starting testnet setup..."
# Add any necessary setup commands below
echo "Generating accounts..."
npx hardhat run scripts/tools/accounts.ts > testnet.accounts.log
if [ $? -eq 1 ]; then
echo "Error: Failed to generate accounts."
exit 1
fi
echo "Starting Anvil testnet..."
echo " - Chain ID: 1337"
echo " - Port: 8545"
if [ $persist = true ]; then
echo " - Persisting state"
anvil --chain-id 1337 --fork-url $forkUrl --state $persistLocation &
else
echo " - Not persisting state"
anvil --chain-id 1337 --fork-url $forkUrl &
fi
ANVIL_PID=$!
function cleanup {
echo "Cleaning up..."
if kill -0 $ANVIL_PID; then
echo "Stopping Anvil..."
kill $ANVIL_PID
fi
}
trap cleanup EXIT
echo "Waiting for Anvil to start..."
sleep 5
# Check if Anvil is running
if ! kill -0 $ANVIL_PID; then
echo "Error: Anvil failed to start."
exit 1
fi
echo "Deploying contracts..."
# only run if persist false or persistLocation doesnt exists
if [[ $persist != true ]] || [[ ! -f $persistLocation ]]; then
npx hardhat run scripts/deploy/deploy.ts --network localhost > testnet.deployments.log
if [ $? -eq 1 ]; then
echo "Error: Failed to deploy contracts."
kill $ANVIL_PID
exit 1
fi
else
echo "Contracts already deployed, skipping..."
fi
if [ $? -eq 0 ]; then
echo ""
echo "🙌 Testnet setup complete!"
echo "- accounts: ./testnet.accounts.log"
echo "- deployments: ./testnet.deployments.log"
if [ $persist = true ]; then
echo "- state: ./$persistLocation"
fi
echo ""
echo "🟢 Network is running. Press Ctrl+C to stop."
echo ""
wait $ANVIL_PID
else
echo "Testnet setup failed."
kill $ANVIL_PID
fi