DeployOwnership.s.sol 17.4 KB
Newer Older
1 2 3 4 5
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { console2 as console } from "forge-std/console2.sol";

6
import { GnosisSafe as Safe } from "safe-contracts/GnosisSafe.sol";
7
import { GnosisSafeProxyFactory as SafeProxyFactory } from "safe-contracts/proxies/GnosisSafeProxyFactory.sol";
8
import { OwnerManager } from "safe-contracts/base/OwnerManager.sol";
9
import { ModuleManager } from "safe-contracts/base/ModuleManager.sol";
10
import { GuardManager } from "safe-contracts/base/GuardManager.sol";
11
import { Enum as SafeOps } from "safe-contracts/common/Enum.sol";
12

13
import { DeployUtils } from "scripts/libraries/DeployUtils.sol";
14

15 16 17
import { LivenessGuard } from "src/safe/LivenessGuard.sol";
import { LivenessModule } from "src/safe/LivenessModule.sol";
import { DeputyGuardianModule } from "src/safe/DeputyGuardianModule.sol";
18
import { ISuperchainConfig } from "interfaces/L1/ISuperchainConfig.sol";
19 20

import { Deploy } from "./Deploy.s.sol";
21

22
/// @notice Configuration for a Safe
23 24 25 26 27
struct SafeConfig {
    uint256 threshold;
    address[] owners;
}

28
/// @notice Configuration for the Liveness Module
29
struct LivenessModuleConfig {
30 31 32 33
    uint256 livenessInterval;
    uint256 thresholdPercentage;
    uint256 minOwners;
    address fallbackOwner;
34 35
}

36 37 38 39 40 41
/// @notice Configuration for the Security Council Safe.
struct SecurityCouncilConfig {
    SafeConfig safeConfig;
    LivenessModuleConfig livenessModuleConfig;
}

42
/// @notice Configuration for the Deputy Guardian Module
43
struct DeputyGuardianModuleConfig {
44
    address deputyGuardian;
45
    ISuperchainConfig superchainConfig;
46
}
47

48 49
/// @notice Configuration for the Guardian Safe.
struct GuardianConfig {
50 51 52
    SafeConfig safeConfig;
    DeputyGuardianModuleConfig deputyGuardianModuleConfig;
}
53

54
/// @title DeployOwnership
55 56 57 58 59 60
/// @notice Script used to deploy and configure the Safe contracts which are used to manage the Superchain,
///         as the ProxyAdminOwner and other roles in the system. Note that this script is not executable in a
///         production environment as some steps depend on having a quorum of signers available. This script is meant to
///         be used as an example to guide the setup and configuration of the Safe contracts.
contract DeployOwnership is Deploy {
    /// @notice Internal function containing the deploy logic.
61
    function _run(bool) internal override {
62
        console.log("start of Ownership Deployment");
63 64
        // The SuperchainConfig is needed as a constructor argument to the Deputy Guardian Module
        deploySuperchainConfig();
65

66 67
        deployFoundationOperationsSafe();
        deployFoundationUpgradeSafe();
68
        deploySecurityCouncilSafe();
69 70
        deployGuardianSafe();
        configureGuardianSafe();
71
        configureSecurityCouncilSafe();
72 73 74 75 76 77 78 79 80 81 82 83 84

        console.log("Ownership contracts completed");
    }

    /// @notice Returns a SafeConfig similar to that of the Foundation Safe on Mainnet.
    function _getExampleFoundationConfig() internal returns (SafeConfig memory safeConfig_) {
        address[] memory exampleFoundationOwners = new address[](7);
        for (uint256 i; i < exampleFoundationOwners.length; i++) {
            exampleFoundationOwners[i] = makeAddr(string.concat("fnd-", vm.toString(i)));
        }
        safeConfig_ = SafeConfig({ threshold: 5, owners: exampleFoundationOwners });
    }

85
    /// @notice Returns a GuardianConfig similar to that of the Guardian Safe on Mainnet.
86
    function _getExampleGuardianConfig() internal view returns (GuardianConfig memory guardianConfig_) {
87
        address[] memory exampleGuardianOwners = new address[](1);
88
        exampleGuardianOwners[0] = artifacts.mustGetAddress("SecurityCouncilSafe");
89 90 91
        guardianConfig_ = GuardianConfig({
            safeConfig: SafeConfig({ threshold: 1, owners: exampleGuardianOwners }),
            deputyGuardianModuleConfig: DeputyGuardianModuleConfig({
92 93
                deputyGuardian: artifacts.mustGetAddress("FoundationOperationsSafe"),
                superchainConfig: ISuperchainConfig(artifacts.mustGetAddress("SuperchainConfigImpl"))
94 95 96 97
            })
        });
    }

98 99 100 101 102 103 104 105 106
    /// @notice Returns a SafeConfig similar to that of the Security Council Safe on Mainnet.
    function _getExampleCouncilConfig() internal returns (SecurityCouncilConfig memory councilConfig_) {
        address[] memory exampleCouncilOwners = new address[](13);
        for (uint256 i; i < exampleCouncilOwners.length; i++) {
            exampleCouncilOwners[i] = makeAddr(string.concat("sc-", vm.toString(i)));
        }
        SafeConfig memory safeConfig = SafeConfig({ threshold: 10, owners: exampleCouncilOwners });
        councilConfig_ = SecurityCouncilConfig({
            safeConfig: safeConfig,
107
            livenessModuleConfig: LivenessModuleConfig({
108
                livenessInterval: 14 weeks,
109 110
                thresholdPercentage: 75,
                minOwners: 8,
111
                fallbackOwner: artifacts.mustGetAddress("FoundationUpgradeSafe")
112
            })
113 114 115
        });
    }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    /// @notice Make a call from the Safe contract to an arbitrary address with arbitrary data
    function _callViaSafe(Safe _safe, address _target, bytes memory _data) internal {
        // This is the signature format used when the caller is also the signer.
        bytes memory signature = abi.encodePacked(uint256(uint160(msg.sender)), bytes32(0), uint8(1));

        _safe.execTransaction({
            to: _target,
            value: 0,
            data: _data,
            operation: SafeOps.Operation.Call,
            safeTxGas: 0,
            baseGas: 0,
            gasPrice: 0,
            gasToken: address(0),
            refundReceiver: payable(address(0)),
            signatures: signature
        });
    }

    /// @notice Deploy the Safe
    function deploySafe(string memory _name) public broadcast returns (address addr_) {
        address[] memory owners = new address[](0);
        addr_ = deploySafe(_name, owners, 1, true);
    }

    /// @notice Deploy a new Safe contract. If the keepDeployer option is used to enable further setup actions, then
    ///         the removeDeployerFromSafe() function should be called on that safe after setup is complete.
    ///         Note this function does not have the broadcast modifier.
    /// @param _name The name of the Safe to deploy.
    /// @param _owners The owners of the Safe.
    /// @param _threshold The threshold of the Safe.
147
    /// @param _keepDeployer Whether or not the deployer address will be added as an owner of the Safe.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    function deploySafe(
        string memory _name,
        address[] memory _owners,
        uint256 _threshold,
        bool _keepDeployer
    )
        public
        returns (address addr_)
    {
        bytes32 salt = keccak256(abi.encode(_name, _implSalt()));
        console.log("Deploying safe: %s with salt %s", _name, vm.toString(salt));
        (SafeProxyFactory safeProxyFactory, Safe safeSingleton) = _getSafeFactory();

        if (_keepDeployer) {
            address[] memory expandedOwners = new address[](_owners.length + 1);
            // By always adding msg.sender first we know that the previousOwner will be SENTINEL_OWNERS, which makes it
            // easier to call removeOwner later.
            expandedOwners[0] = msg.sender;
            for (uint256 i = 0; i < _owners.length; i++) {
                expandedOwners[i + 1] = _owners[i];
            }
            _owners = expandedOwners;
        }

        bytes memory initData = abi.encodeCall(
            Safe.setup, (_owners, _threshold, address(0), hex"", address(0), address(0), 0, payable(address(0)))
        );
        addr_ = address(safeProxyFactory.createProxyWithNonce(address(safeSingleton), initData, uint256(salt)));

177
        artifacts.save(_name, addr_);
178 179 180 181 182 183
        console.log("New safe: %s deployed at %s\n    Note that this safe is owned by the deployer key", _name, addr_);
    }

    /// @notice If the keepDeployer option was used with deploySafe(), this function can be used to remove the deployer.
    ///         Note this function does not have the broadcast modifier.
    function removeDeployerFromSafe(string memory _name, uint256 _newThreshold) public {
184
        Safe safe = Safe(artifacts.mustGetAddress(_name));
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

        // The sentinel address is used to mark the start and end of the linked list of owners in the Safe.
        address sentinelOwners = address(0x1);

        // Because deploySafe() always adds msg.sender first (if keepDeployer is true), we know that the previousOwner
        // will be sentinelOwners.
        _callViaSafe({
            _safe: safe,
            _target: address(safe),
            _data: abi.encodeCall(OwnerManager.removeOwner, (sentinelOwners, msg.sender, _newThreshold))
        });
        console.log("Removed deployer owner from ", _name);
    }

    /// @notice Gets the address of the SafeProxyFactory and Safe singleton for use in deploying a new GnosisSafe.
    function _getSafeFactory() internal returns (SafeProxyFactory safeProxyFactory_, Safe safeSingleton_) {
201
        if (artifacts.getAddress("SafeProxyFactory") != address(0)) {
202
            // The SafeProxyFactory is already saved, we can just use it.
203 204
            safeProxyFactory_ = SafeProxyFactory(artifacts.getAddress("SafeProxyFactory"));
            safeSingleton_ = Safe(artifacts.getAddress("SafeSingleton"));
205 206 207 208 209 210 211 212 213 214 215 216 217 218
            return (safeProxyFactory_, safeSingleton_);
        }

        // These are the standard create2 deployed contracts. First we'll check if they are deployed,
        // if not we'll deploy new ones, though not at these addresses.
        address safeProxyFactory = 0xa6B71E26C5e0845f74c812102Ca7114b6a896AB2;
        address safeSingleton = 0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552;

        safeProxyFactory.code.length == 0
            ? safeProxyFactory_ = new SafeProxyFactory()
            : safeProxyFactory_ = SafeProxyFactory(safeProxyFactory);

        safeSingleton.code.length == 0 ? safeSingleton_ = new Safe() : safeSingleton_ = Safe(payable(safeSingleton));

219 220
        artifacts.save("SafeProxyFactory", address(safeProxyFactory_));
        artifacts.save("SafeSingleton", address(safeSingleton_));
221 222
    }

223
    /// @notice Deploys a Safe with a configuration similar to that of the Foundation Safe on Mainnet.
224
    function deployFoundationOperationsSafe() public broadcast returns (address addr_) {
225 226
        SafeConfig memory exampleFoundationConfig = _getExampleFoundationConfig();
        addr_ = deploySafe({
227 228 229 230 231 232 233 234 235 236 237 238
            _name: "FoundationOperationsSafe",
            _owners: exampleFoundationConfig.owners,
            _threshold: exampleFoundationConfig.threshold,
            _keepDeployer: false
        });
    }

    /// @notice Deploys a Safe with a configuration similar to that of the Foundation Safe on Mainnet.
    function deployFoundationUpgradeSafe() public broadcast returns (address addr_) {
        SafeConfig memory exampleFoundationConfig = _getExampleFoundationConfig();
        addr_ = deploySafe({
            _name: "FoundationUpgradeSafe",
239 240 241 242 243 244 245 246 247
            _owners: exampleFoundationConfig.owners,
            _threshold: exampleFoundationConfig.threshold,
            _keepDeployer: false
        });
    }

    /// @notice Deploy a LivenessGuard for use on the Security Council Safe.
    ///         Note this function does not have the broadcast modifier.
    function deployLivenessGuard() public returns (address addr_) {
248
        Safe councilSafe = Safe(payable(artifacts.mustGetAddress("SecurityCouncilSafe")));
249 250
        addr_ = address(new LivenessGuard(councilSafe));

251
        artifacts.save("LivenessGuard", address(addr_));
252 253 254 255 256 257
        console.log("New LivenessGuard deployed at %s", address(addr_));
    }

    /// @notice Deploy a LivenessModule for use on the Security Council Safe
    ///         Note this function does not have the broadcast modifier.
    function deployLivenessModule() public returns (address addr_) {
258 259
        Safe councilSafe = Safe(payable(artifacts.mustGetAddress("SecurityCouncilSafe")));
        address guard = artifacts.mustGetAddress("LivenessGuard");
260
        LivenessModuleConfig memory livenessModuleConfig = _getExampleCouncilConfig().livenessModuleConfig;
261 262 263 264 265

        addr_ = address(
            new LivenessModule({
                _safe: councilSafe,
                _livenessGuard: LivenessGuard(guard),
266 267 268 269
                _livenessInterval: livenessModuleConfig.livenessInterval,
                _thresholdPercentage: livenessModuleConfig.thresholdPercentage,
                _minOwners: livenessModuleConfig.minOwners,
                _fallbackOwner: livenessModuleConfig.fallbackOwner
270 271 272
            })
        );

273
        artifacts.save("LivenessModule", address(addr_));
274 275 276
        console.log("New LivenessModule deployed at %s", address(addr_));
    }

277 278 279
    /// @notice Deploy a DeputyGuardianModule for use on the Security Council Safe.
    ///         Note this function does not have the broadcast modifier.
    function deployDeputyGuardianModule() public returns (address addr_) {
280
        Safe guardianSafe = Safe(payable(artifacts.mustGetAddress("GuardianSafe")));
281 282
        DeputyGuardianModuleConfig memory deputyGuardianModuleConfig =
            _getExampleGuardianConfig().deputyGuardianModuleConfig;
283 284
        addr_ = address(
            new DeputyGuardianModule({
285
                _safe: guardianSafe,
286 287
                _superchainConfig: deputyGuardianModuleConfig.superchainConfig,
                _deputyGuardian: deputyGuardianModuleConfig.deputyGuardian
288 289 290
            })
        );

291
        artifacts.save("DeputyGuardianModule", addr_);
292 293 294
        console.log("New DeputyGuardianModule deployed at %s", addr_);
    }

295 296
    /// @notice Deploy a Security Council Safe.
    function deploySecurityCouncilSafe() public broadcast returns (address addr_) {
297 298
        // Deploy the safe with the extra deployer key, and keep the threshold at 1 to allow for further setup.
        SecurityCouncilConfig memory exampleCouncilConfig = _getExampleCouncilConfig();
299 300 301 302 303 304 305
        addr_ = payable(
            deploySafe({
                _name: "SecurityCouncilSafe",
                _owners: exampleCouncilConfig.safeConfig.owners,
                _threshold: 1,
                _keepDeployer: true
            })
306
        );
307
    }
308

309 310 311 312
    /// @notice Deploy Guardian Safe.
    function deployGuardianSafe() public broadcast returns (address addr_) {
        // Config is hardcoded here as the Guardian Safe's configuration is inflexible.
        address[] memory owners = new address[](1);
313
        owners[0] = artifacts.mustGetAddress("SecurityCouncilSafe");
314 315 316 317 318
        addr_ = deploySafe({ _name: "GuardianSafe", _owners: owners, _threshold: 1, _keepDeployer: true });

        console.log("Deployed and configured the Guardian Safe!");
    }

319 320 321 322
    /// @notice Deploy the SuperchainConfig contract
    function deploySuperchainConfig() public broadcast {
        ISuperchainConfig superchainConfig = ISuperchainConfig(
            DeployUtils.create2AndSave({
323
                _save: artifacts,
324 325
                _salt: _implSalt(),
                _name: "SuperchainConfig",
326
                _nick: "SuperchainConfigImpl",
327 328 329 330
                _args: DeployUtils.encodeConstructor(abi.encodeCall(ISuperchainConfig.__constructor__, ()))
            })
        );

331
        require(superchainConfig.guardian() == address(0), "SuperchainConfig: guardian must be address(0)");
332
        bytes32 initialized = vm.load(address(superchainConfig), bytes32(0));
333
        require(initialized != 0, "SuperchainConfig: must be initialized");
334 335
    }

336 337
    /// @notice Configure the Guardian Safe with the DeputyGuardianModule.
    function configureGuardianSafe() public broadcast returns (address addr_) {
338
        addr_ = artifacts.mustGetAddress("GuardianSafe");
339 340
        address deputyGuardianModule = deployDeputyGuardianModule();
        _callViaSafe({
341 342
            _safe: Safe(payable(addr_)),
            _target: addr_,
343 344
            _data: abi.encodeCall(ModuleManager.enableModule, (deputyGuardianModule))
        });
345

346 347
        // Finalize configuration by removing the additional deployer key.
        removeDeployerFromSafe({ _name: "GuardianSafe", _newThreshold: 1 });
348 349 350 351 352 353 354
        console.log("DeputyGuardianModule enabled on GuardianSafe");
    }

    /// @notice Configure the Security Council Safe with the LivenessModule and LivenessGuard.
    function configureSecurityCouncilSafe() public broadcast returns (address addr_) {
        // Deploy and add the Deputy Guardian Module.
        SecurityCouncilConfig memory exampleCouncilConfig = _getExampleCouncilConfig();
355
        Safe safe = Safe(artifacts.mustGetAddress("SecurityCouncilSafe"));
356 357

        // Deploy and add the Liveness Guard.
358 359 360 361
        address guard = deployLivenessGuard();
        _callViaSafe({ _safe: safe, _target: address(safe), _data: abi.encodeCall(GuardManager.setGuard, (guard)) });
        console.log("LivenessGuard setup on SecurityCouncilSafe");

362 363 364 365 366 367 368 369
        // Deploy and add the Liveness Module.
        address livenessModule = deployLivenessModule();
        _callViaSafe({
            _safe: safe,
            _target: address(safe),
            _data: abi.encodeCall(ModuleManager.enableModule, (livenessModule))
        });

370 371 372
        // Finalize configuration by removing the additional deployer key.
        removeDeployerFromSafe({ _name: "SecurityCouncilSafe", _newThreshold: exampleCouncilConfig.safeConfig.threshold });

373 374 375
        address[] memory owners = safe.getOwners();
        require(
            safe.getThreshold() == LivenessModule(livenessModule).getRequiredThreshold(owners.length),
376
            "DeployOwnership: safe threshold must be equal to the LivenessModule's required threshold"
377
        );
378

379 380 381 382
        addr_ = address(safe);
        console.log("Deployed and configured the Security Council Safe!");
    }
}