|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {Script} from "forge-std/Script.sol"; |
| 5 | +import {console} from "forge-std/console.sol"; |
| 6 | +import {AddressWhitelist} from "../src/common/implementation/AddressWhitelist.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @title Redeployment script for AddressWhitelist |
| 10 | + * @notice Deploys a new AddressWhitelist contract duplicating the configuration from a previous deployment |
| 11 | + * |
| 12 | + * Environment variables: |
| 13 | + * - MNEMONIC: Required. The mnemonic phrase for the deployer wallet |
| 14 | + * - PREVIOUS_ADDRESS_WHITELIST: Required. The address of the previous AddressWhitelist contract to duplicate |
| 15 | + */ |
| 16 | +contract RedeployAddressWhitelist is Script { |
| 17 | + function run() external { |
| 18 | + // Load environment variables from .env file (if it exists) |
| 19 | + // This will automatically load variables from .env file in the project root |
| 20 | + |
| 21 | + // Get required environment variables |
| 22 | + string memory mnemonic = vm.envString("MNEMONIC"); |
| 23 | + address previousWhitelist = vm.envAddress("PREVIOUS_ADDRESS_WHITELIST"); |
| 24 | + |
| 25 | + // Derive the 0 index address from mnemonic |
| 26 | + uint256 deployerPrivateKey = vm.deriveKey(mnemonic, 0); |
| 27 | + address deployer = vm.addr(deployerPrivateKey); |
| 28 | + |
| 29 | + console.log("Previous AddressWhitelist:", previousWhitelist); |
| 30 | + console.log("Deployer:", deployer); |
| 31 | + |
| 32 | + // Get the previous contract instance |
| 33 | + AddressWhitelist previousWhitelistContract = AddressWhitelist(previousWhitelist); |
| 34 | + |
| 35 | + // Get the previous owner |
| 36 | + address previousOwner = previousWhitelistContract.owner(); |
| 37 | + console.log("Previous Owner:", previousOwner); |
| 38 | + |
| 39 | + // Get all whitelisted addresses from the previous contract |
| 40 | + address[] memory whitelistedAddresses = previousWhitelistContract.getWhitelist(); |
| 41 | + console.log("Number of whitelisted addresses to copy:", whitelistedAddresses.length); |
| 42 | + |
| 43 | + // Start broadcasting transactions with the derived private key |
| 44 | + vm.startBroadcast(deployerPrivateKey); |
| 45 | + |
| 46 | + // Deploy the new contract with deployer as owner (always safe) |
| 47 | + AddressWhitelist newWhitelist = new AddressWhitelist(); |
| 48 | + console.log("New AddressWhitelist deployed at:", address(newWhitelist)); |
| 49 | + |
| 50 | + // Copy all whitelisted addresses from the previous contract |
| 51 | + if (whitelistedAddresses.length > 0) { |
| 52 | + console.log("Copying whitelisted addresses..."); |
| 53 | + for (uint256 i = 0; i < whitelistedAddresses.length; i++) { |
| 54 | + address addr = whitelistedAddresses[i]; |
| 55 | + newWhitelist.addToWhitelist(addr); |
| 56 | + console.log("Added to whitelist:", addr); |
| 57 | + } |
| 58 | + console.log("Finished copying whitelisted addresses"); |
| 59 | + } |
| 60 | + |
| 61 | + // Handle ownership logic |
| 62 | + if (previousOwner == address(0)) { |
| 63 | + // If the previous contract had no owner (burned), burn ownership on the new contract |
| 64 | + console.log("Previous contract had no owner, burning ownership on new contract..."); |
| 65 | + newWhitelist.renounceOwnership(); |
| 66 | + console.log("Ownership burned to zero address"); |
| 67 | + } else if (previousOwner != deployer) { |
| 68 | + // If the previous owner is different from deployer, transfer ownership to the same address |
| 69 | + console.log("Transferring ownership to previous owner:", previousOwner); |
| 70 | + newWhitelist.transferOwnership(previousOwner); |
| 71 | + console.log("Ownership transferred to:", previousOwner); |
| 72 | + } else { |
| 73 | + // If the previous owner is the same as deployer, keep deployer as owner |
| 74 | + console.log("Keeping deployer as owner:", deployer); |
| 75 | + } |
| 76 | + |
| 77 | + vm.stopBroadcast(); |
| 78 | + |
| 79 | + // Output deployment summary |
| 80 | + console.log("\n=== Redeployment Summary ==="); |
| 81 | + console.log("New Contract:", address(newWhitelist)); |
| 82 | + console.log("Previous Contract:", previousWhitelist); |
| 83 | + console.log("Chain ID:", block.chainid); |
| 84 | + console.log("Deployer:", deployer); |
| 85 | + console.log("Previous Owner:", previousOwner); |
| 86 | + console.log("Final Owner:", newWhitelist.owner()); |
| 87 | + console.log("Whitelisted Addresses Copied:", whitelistedAddresses.length); |
| 88 | + |
| 89 | + // Verify the whitelist was copied correctly |
| 90 | + address[] memory newWhitelistAddresses = newWhitelist.getWhitelist(); |
| 91 | + console.log("New Contract Whitelist Count:", newWhitelistAddresses.length); |
| 92 | + |
| 93 | + if (whitelistedAddresses.length == newWhitelistAddresses.length) { |
| 94 | + console.log("[SUCCESS] Whitelist copy verification: SUCCESS"); |
| 95 | + } else { |
| 96 | + console.log("[FAILED] Whitelist copy verification: FAILED"); |
| 97 | + console.log("Expected:", whitelistedAddresses.length); |
| 98 | + console.log("Actual:", newWhitelistAddresses.length); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments