|
| 1 | +#! /bin/bash |
| 2 | +# upgrade-contract upgrades proxy at $PROXY_ADDRESS to a new deployment of the implementation |
| 3 | +# of the contract at $IMPLEMENTATION_PATH (i.e. src/PDPService.sol:PDPService / src/PDPRecordKeeper.sol:PDPRecordKeeper) |
| 4 | +# Assumption: KEYSTORE, PASSWORD, RPC_URL env vars are set to an appropriate eth keystore path and password |
| 5 | +# and to a valid RPC_URL for the target network. |
| 6 | +# Assumption: forge, cast, jq are in the PATH |
| 7 | +# |
| 8 | +# Set DRY_RUN=false to actually deploy and broadcast transactions (default is dry-run for safety) |
| 9 | +DRY_RUN=${DRY_RUN:-true} |
| 10 | + |
| 11 | +if [ "$DRY_RUN" = "true" ]; then |
| 12 | + echo "🧪 Running in DRY-RUN mode - simulation only, no actual deployment" |
| 13 | +else |
| 14 | + echo "🚀 Running in DEPLOYMENT mode - will actually deploy and upgrade contracts" |
| 15 | +fi |
| 16 | + |
| 17 | +echo "Upgrading contract" |
| 18 | + |
| 19 | +if [ -z "$RPC_URL" ]; then |
| 20 | + echo "Error: RPC_URL is not set" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +if [ -z "$CHAIN_ID" ]; then |
| 25 | + CHAIN_ID=$(cast chain-id --rpc-url "$RPC_URL") |
| 26 | + if [ -z "$CHAIN_ID" ]; then |
| 27 | + echo "Error: Failed to detect chain ID from RPC" |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | +fi |
| 31 | + |
| 32 | +if [ -z "$KEYSTORE" ]; then |
| 33 | + echo "Error: KEYSTORE is not set" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if [ -z "$PROXY_ADDRESS" ]; then |
| 38 | + echo "Error: PROXY_ADDRESS is not set" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +if [ -z "$UPGRADE_DATA" ]; then |
| 43 | + echo "Error: UPGRADE_DATA is not set" |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +if [ -z "$IMPLEMENTATION_PATH" ]; then |
| 48 | + echo "Error: IMPLEMENTATION_PATH is not set (i.e. src/PDPService.sol:PDPService)" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +if [ "$DRY_RUN" = "true" ]; then |
| 53 | + echo "🔍 Simulating deployment of new $IMPLEMENTATION_PATH implementation contract" |
| 54 | + forge create --rpc-url "$RPC_URL" --keystore "$KEYSTORE" --password "$PASSWORD" --compiler-version 0.8.23 --chain-id "$CHAIN_ID" "$IMPLEMENTATION_PATH" |
| 55 | + |
| 56 | + if [ $? -eq 0 ]; then |
| 57 | + echo "✅ Contract compilation and simulation successful!" |
| 58 | + echo "🔍 Simulating proxy upgrade at $PROXY_ADDRESS" |
| 59 | + echo " - Would call: upgradeToAndCall(address,bytes)" |
| 60 | + echo " - With upgrade data: $UPGRADE_DATA" |
| 61 | + echo "✅ Dry run completed successfully!" |
| 62 | + echo "" |
| 63 | + echo "To perform actual deployment, run with: DRY_RUN=false ./tools/upgrade-contract.sh" |
| 64 | + else |
| 65 | + echo "❌ Contract compilation failed during simulation" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +else |
| 69 | + echo "🚀 Deploying new $IMPLEMENTATION_PATH implementation contract" |
| 70 | + # Parse the output of forge create to extract the contract address |
| 71 | + IMPLEMENTATION_ADDRESS=$(forge create --rpc-url "$RPC_URL" --keystore "$KEYSTORE" --password "$PASSWORD" --broadcast --compiler-version 0.8.23 --chain-id "$CHAIN_ID" "$IMPLEMENTATION_PATH" | grep "Deployed to" | awk '{print $3}') |
| 72 | + |
| 73 | + if [ -z "$IMPLEMENTATION_ADDRESS" ]; then |
| 74 | + echo "❌ Error: Failed to extract PDP verifier contract address" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + echo "✅ $IMPLEMENTATION_PATH implementation deployed at: $IMPLEMENTATION_ADDRESS" |
| 78 | + |
| 79 | + echo "🔄 Upgrading proxy at $PROXY_ADDRESS" |
| 80 | + cast send --rpc-url "$RPC_URL" --keystore "$KEYSTORE" --password "$PASSWORD" --chain-id "$CHAIN_ID" "$PROXY_ADDRESS" "upgradeToAndCall(address,bytes)" "$IMPLEMENTATION_ADDRESS" "$UPGRADE_DATA" |
| 81 | + |
| 82 | + if [ $? -eq 0 ]; then |
| 83 | + echo "✅ Contract upgrade completed successfully!" |
| 84 | + echo "📄 You can verify the upgrade by checking the VERSION:" |
| 85 | + echo " cast call $PROXY_ADDRESS \"VERSION()\" --rpc-url $RPC_URL | cast --to-ascii" |
| 86 | + else |
| 87 | + echo "❌ Contract upgrade failed" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +fi |
0 commit comments