Config.sol 6.71 KB
Newer Older
1 2 3
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

4
import { Vm, VmSafe } from "forge-std/Vm.sol";
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
/// @notice Enum representing different ways of outputting genesis allocs.
/// @custom:value NONE    No output, used in internal tests.
/// @custom:value LATEST  Output allocs only for latest fork.
/// @custom:value ALL     Output allocs for all intermediary forks.
enum OutputMode {
    NONE,
    LATEST,
    ALL
}

library OutputModeUtils {
    function toString(OutputMode _mode) internal pure returns (string memory) {
        if (_mode == OutputMode.NONE) {
            return "none";
        } else if (_mode == OutputMode.LATEST) {
            return "latest";
        } else if (_mode == OutputMode.ALL) {
            return "all";
        } else {
            return "unknown";
        }
    }
}

/// @notice Enum of forks available for selection when generating genesis allocs.
enum Fork {
    NONE,
    DELTA,
    ECOTONE,
35 36
    FJORD,
    GRANITE
37 38
}

39
Fork constant LATEST_FORK = Fork.GRANITE;
40 41 42 43 44 45 46 47 48 49 50

library ForkUtils {
    function toString(Fork _fork) internal pure returns (string memory) {
        if (_fork == Fork.NONE) {
            return "none";
        } else if (_fork == Fork.DELTA) {
            return "delta";
        } else if (_fork == Fork.ECOTONE) {
            return "ecotone";
        } else if (_fork == Fork.FJORD) {
            return "fjord";
51 52
        } else if (_fork == Fork.GRANITE) {
            return "granite";
53 54 55 56 57 58
        } else {
            return "unknown";
        }
    }
}

59 60 61 62 63 64 65 66 67
/// @title Config
/// @notice Contains all env var based config. Add any new env var parsing to this file
///         to ensure that all config is in a single place.
library Config {
    /// @notice Foundry cheatcode VM.
    Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

    /// @notice Returns the path on the local filesystem where the deployment artifact is
    ///         written to disk after doing a deployment.
68 69
    function deploymentOutfile() internal view returns (string memory env_) {
        env_ = vm.envOr(
70 71
            "DEPLOYMENT_OUTFILE",
            string.concat(vm.projectRoot(), "/deployments/", vm.toString(block.chainid), "-deploy.json")
72 73 74 75
        );
    }

    /// @notice Returns the path on the local filesystem where the deploy config is
76
    function deployConfigPath() internal view returns (string memory env_) {
77
        if (vm.isContext(VmSafe.ForgeContext.TestGroup)) {
78
            env_ = string.concat(vm.projectRoot(), "/deploy-config/hardhat.json");
79
        } else {
80 81
            env_ = vm.envOr("DEPLOY_CONFIG_PATH", string(""));
            require(bytes(env_).length > 0, "Config: must set DEPLOY_CONFIG_PATH to filesystem path of deploy config");
82
        }
83 84 85 86
    }

    /// @notice Returns the chainid from the EVM context or the value of the CHAIN_ID env var as
    ///         an override.
87 88
    function chainID() internal view returns (uint256 env_) {
        env_ = vm.envOr("CHAIN_ID", block.chainid);
89 90 91 92 93
    }

    /// @notice Returns the value of the env var CONTRACT_ADDRESSES_PATH which is a JSON key/value
    ///         pair of contract names and their addresses. Each key/value pair is passed to `save`
    ///         which then backs the `getAddress` function.
94 95
    function contractAddressesPath() internal view returns (string memory env_) {
        env_ = vm.envOr("CONTRACT_ADDRESSES_PATH", string(""));
96 97 98
    }

    /// @notice The CREATE2 salt to be used when deploying the implementations.
99 100
    function implSalt() internal view returns (string memory env_) {
        env_ = vm.envOr("IMPL_SALT", string("ethers phoenix"));
101 102 103 104
    }

    /// @notice Returns the path that the state dump file should be written to or read from
    ///         on the local filesystem.
105 106
    function stateDumpPath(string memory _suffix) internal view returns (string memory env_) {
        env_ = vm.envOr(
107 108
            "STATE_DUMP_PATH",
            string.concat(vm.projectRoot(), "/state-dump-", vm.toString(block.chainid), _suffix, ".json")
109 110 111 112 113 114
        );
    }

    /// @notice Returns the name of the file that the forge deployment artifact is written to on the local
    ///         filesystem. By default, it is the name of the deploy script with the suffix `-latest.json`.
    ///         This was useful for creating hardhat deploy style artifacts and will be removed in a future release.
115 116
    function deployFile(string memory _sig) internal view returns (string memory env_) {
        env_ = vm.envOr("DEPLOY_FILE", string.concat(_sig, "-latest.json"));
117 118 119
    }

    /// @notice Returns the private key that is used to configure drippie.
120 121
    function drippieOwnerPrivateKey() internal view returns (uint256 env_) {
        env_ = vm.envUint("DRIPPIE_OWNER_PRIVATE_KEY");
122
    }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    /// @notice Returns the OutputMode for genesis allocs generation.
    ///         It reads the mode from the environment variable OUTPUT_MODE.
    ///         If it is unset, OutputMode.ALL is returned.
    function outputMode() internal view returns (OutputMode) {
        string memory modeStr = vm.envOr("OUTPUT_MODE", string("latest"));
        bytes32 modeHash = keccak256(bytes(modeStr));
        if (modeHash == keccak256(bytes("none"))) {
            return OutputMode.NONE;
        } else if (modeHash == keccak256(bytes("latest"))) {
            return OutputMode.LATEST;
        } else if (modeHash == keccak256(bytes("all"))) {
            return OutputMode.ALL;
        } else {
            revert(string.concat("Config: unknown output mode: ", modeStr));
        }
    }

141
    /// @notice Returns true if multithreaded Cannon is used for the deployment.
142 143
    function useMultithreadedCannon() internal view returns (bool enabled_) {
        enabled_ = vm.envOr("USE_MT_CANNON", false);
144 145
    }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    /// @notice Returns the latest fork to use for genesis allocs generation.
    ///         It reads the fork from the environment variable FORK. If it is
    ///         unset, NONE is returned.
    ///         If set to the special value "latest", the latest fork is returned.
    function fork() internal view returns (Fork) {
        string memory forkStr = vm.envOr("FORK", string(""));
        if (bytes(forkStr).length == 0) {
            return Fork.NONE;
        }
        bytes32 forkHash = keccak256(bytes(forkStr));
        if (forkHash == keccak256(bytes("latest"))) {
            return LATEST_FORK;
        } else if (forkHash == keccak256(bytes("delta"))) {
            return Fork.DELTA;
        } else if (forkHash == keccak256(bytes("ecotone"))) {
            return Fork.ECOTONE;
        } else if (forkHash == keccak256(bytes("fjord"))) {
            return Fork.FJORD;
164 165
        } else if (forkHash == keccak256(bytes("granite"))) {
            return Fork.GRANITE;
166 167 168 169
        } else {
            revert(string.concat("Config: unknown fork: ", forkStr));
        }
    }
170
}