Deployer.sol 22.1 KB
Newer Older
Mark Tyneway's avatar
Mark Tyneway committed
1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.0;
Mark Tyneway's avatar
Mark Tyneway committed
3 4 5 6

import { Script } from "forge-std/Script.sol";
import { stdJson } from "forge-std/StdJson.sol";
import { console2 as console } from "forge-std/console2.sol";
7
import { Executables } from "./Executables.sol";
8
import { Chains } from "./Chains.sol";
Mark Tyneway's avatar
Mark Tyneway committed
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

/// @notice store the new deployment to be saved
struct Deployment {
    string name;
    address payable addr;
}

/// @notice A `hardhat-deploy` style artifact
struct Artifact {
    string abi;
    address addr;
    string[] args;
    bytes bytecode;
    bytes deployedBytecode;
    string devdoc;
    string metadata;
    uint256 numDeployments;
    string receipt;
    bytes32 solcInputHash;
    string storageLayout;
    bytes32 transactionHash;
    string userdoc;
}

/// @title Deployer
/// @author tynes
/// @notice A contract that can make deploying and interacting with deployments easy.
///         When a contract is deployed, call the `save` function to write its name and
///         contract address to disk. Then the `sync` function can be called to generate
///         hardhat deploy style artifacts. Forked from `forge-deploy`.
Mark Tyneway's avatar
Mark Tyneway committed
39
abstract contract Deployer is Script {
Mark Tyneway's avatar
Mark Tyneway committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    /// @notice The set of deployments that have been done during execution.
    mapping(string => Deployment) internal _namedDeployments;
    /// @notice The same as `_namedDeployments` but as an array.
    Deployment[] internal _newDeployments;
    /// @notice The namespace for the deployment. Can be set with the env var DEPLOYMENT_CONTEXT.
    string internal deploymentContext;
    /// @notice Path to the deploy artifact generated by foundry
    string internal deployPath;
    /// @notice Path to the directory containing the hh deploy style artifacts
    string internal deploymentsDir;
    /// @notice The name of the deploy script that sends the transactions.
    ///         Can be modified with the env var DEPLOY_SCRIPT
    string internal deployScript;
    /// @notice The path to the temp deployments file
    string internal tempDeploymentsPath;
    /// @notice Error for when attempting to fetch a deployment and it does not exist
56

Mark Tyneway's avatar
Mark Tyneway committed
57 58 59 60 61
    error DeploymentDoesNotExist(string);
    /// @notice Error for when trying to save an invalid deployment
    error InvalidDeployment(string);
    /// @notice The storage slot that holds the address of the implementation.
    ///        bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1)
62 63

    bytes32 internal constant IMPLEMENTATION_KEY = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
Mark Tyneway's avatar
Mark Tyneway committed
64 65
    /// @notice The storage slot that holds the address of the owner.
    ///        bytes32(uint256(keccak256('eip1967.proxy.admin')) - 1)
66
    bytes32 internal constant OWNER_KEY = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
Mark Tyneway's avatar
Mark Tyneway committed
67

68 69 70 71 72 73 74
    /// @notice Create the global variables and set up the filesystem.
    ///         Forge script will create a file where the prefix is the
    ///         name of the function that runs with the suffix `-latest.json`.
    ///         By default, `run()` is called. Allow the user to use the SIG
    ///         env var to specify what function signature was called so that
    ///         the `sync()` method can be used to create hardhat deploy style
    ///         artifacts.
Mark Tyneway's avatar
Mark Tyneway committed
75 76
    function setUp() public virtual {
        string memory root = vm.projectRoot();
Mark Tyneway's avatar
Mark Tyneway committed
77
        deployScript = vm.envOr("DEPLOY_SCRIPT", name());
Mark Tyneway's avatar
Mark Tyneway committed
78 79

        deploymentContext = _getDeploymentContext();
80
        string memory sig = vm.envOr("SIG", string("run"));
81
        string memory deployFile = vm.envOr("DEPLOY_FILE", string.concat(sig, "-latest.json"));
Mark Tyneway's avatar
Mark Tyneway committed
82 83
        uint256 chainId = vm.envOr("CHAIN_ID", block.chainid);
        deployPath = string.concat(root, "/broadcast/", deployScript, ".s.sol/", vm.toString(chainId), "/", deployFile);
Mark Tyneway's avatar
Mark Tyneway committed
84 85

        deploymentsDir = string.concat(root, "/deployments/", deploymentContext);
86
        try vm.createDir(deploymentsDir, true) { } catch (bytes memory) { }
Mark Tyneway's avatar
Mark Tyneway committed
87 88

        string memory chainIdPath = string.concat(deploymentsDir, "/.chainId");
Mark Tyneway's avatar
Mark Tyneway committed
89 90
        try vm.readFile(chainIdPath) returns (string memory localChainId) {
            if (vm.envOr("STRICT_DEPLOYMENT", true)) {
91 92 93 94
                require(
                    vm.parseUint(localChainId) == chainId,
                    string.concat("Misconfigured networks: ", localChainId, " != ", vm.toString(chainId))
                );
Mark Tyneway's avatar
Mark Tyneway committed
95
            }
Mark Tyneway's avatar
Mark Tyneway committed
96
        } catch {
Mark Tyneway's avatar
Mark Tyneway committed
97
            vm.writeFile(chainIdPath, vm.toString(chainId));
Mark Tyneway's avatar
Mark Tyneway committed
98
        }
Mark Tyneway's avatar
Mark Tyneway committed
99
        console.log("Connected to network with chainid %s", chainId);
Mark Tyneway's avatar
Mark Tyneway committed
100 101

        tempDeploymentsPath = string.concat(deploymentsDir, "/.deploy");
102 103
        try vm.readFile(tempDeploymentsPath) returns (string memory) { }
        catch {
Mark Tyneway's avatar
Mark Tyneway committed
104 105 106 107 108 109 110 111 112 113
            vm.writeJson("{}", tempDeploymentsPath);
        }
        console.log("Storing temp deployment data in %s", tempDeploymentsPath);
    }

    /// @notice Call this function to sync the deployment artifacts such that
    ///         hardhat deploy style artifacts are created.
    function sync() public {
        Deployment[] memory deployments = _getTempDeployments();
        console.log("Syncing %s deployments", deployments.length);
114
        console.log("Using deployment artifact %s", deployPath);
Mark Tyneway's avatar
Mark Tyneway committed
115 116 117 118 119 120

        for (uint256 i; i < deployments.length; i++) {
            address addr = deployments[i].addr;
            string memory deploymentName = deployments[i].name;

            string memory deployTx = _getDeployTransactionByContractAddress(addr);
121 122 123 124
            if (bytes(deployTx).length == 0) {
                console.log("Deploy Tx not found for %s skipping deployment artifact generation", deploymentName);
                continue;
            }
125
            string memory contractName = _getContractNameFromDeployTransaction(deployTx);
126
            console.log("Syncing deployment %s: contract %s", deploymentName, contractName);
Mark Tyneway's avatar
Mark Tyneway committed
127 128

            string[] memory args = getDeployTransactionConstructorArguments(deployTx);
129 130
            bytes memory code = _getCode(contractName);
            bytes memory deployedCode = _getDeployedCode(contractName);
Mark Tyneway's avatar
Mark Tyneway committed
131 132 133 134 135 136 137 138
            string memory receipt = _getDeployReceiptByContractAddress(addr);

            string memory artifactPath = string.concat(deploymentsDir, "/", deploymentName, ".json");

            uint256 numDeployments = 0;
            try vm.readFile(artifactPath) returns (string memory res) {
                numDeployments = stdJson.readUint(string(res), "$.numDeployments");
                vm.removeFile(artifactPath);
139
            } catch { }
140
            numDeployments++;
Mark Tyneway's avatar
Mark Tyneway committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

            Artifact memory artifact = Artifact({
                abi: getAbi(contractName),
                addr: addr,
                args: args,
                bytecode: code,
                deployedBytecode: deployedCode,
                devdoc: getDevDoc(contractName),
                metadata: getMetadata(contractName),
                numDeployments: numDeployments,
                receipt: receipt,
                solcInputHash: bytes32(0),
                storageLayout: getStorageLayout(contractName),
                transactionHash: stdJson.readBytes32(deployTx, "$.hash"),
                userdoc: getUserDoc(contractName)
            });

            string memory json = _serializeArtifact(artifact);

160
            vm.writeJson({ json: json, path: artifactPath });
Mark Tyneway's avatar
Mark Tyneway committed
161 162 163 164 165 166
        }

        console.log("Synced temp deploy files, deleting %s", tempDeploymentsPath);
        vm.removeFile(tempDeploymentsPath);
    }

Mark Tyneway's avatar
Mark Tyneway committed
167 168
    /// @notice Returns the name of the deployment script. Children contracts
    ///         must implement this to ensure that the deploy artifacts can be found.
169 170
    ///         This should be the same as the name of the script and is used as the file
    ///         name inside of the `broadcast` directory when looking up deployment artifacts.
171
    function name() public pure virtual returns (string memory);
Mark Tyneway's avatar
Mark Tyneway committed
172

Mark Tyneway's avatar
Mark Tyneway committed
173 174 175 176 177 178 179 180 181 182 183 184 185
    /// @notice Returns all of the deployments done in the current context.
    function newDeployments() external view returns (Deployment[] memory) {
        return _newDeployments;
    }

    /// @notice Returns whether or not a particular deployment exists.
    /// @param _name The name of the deployment.
    /// @return Whether the deployment exists or not.
    function has(string memory _name) public view returns (bool) {
        Deployment memory existing = _namedDeployments[_name];
        if (existing.addr != address(0)) {
            return bytes(existing.name).length > 0;
        }
AKABABA-ETH's avatar
AKABABA-ETH committed
186
        return _getExistingDeploymentAddress(_name) != address(0);
Mark Tyneway's avatar
Mark Tyneway committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200
    }

    /// @notice Returns the address of a deployment.
    /// @param _name The name of the deployment.
    /// @return The address of the deployment. May be `address(0)` if the deployment does not
    ///         exist.
    function getAddress(string memory _name) public view returns (address payable) {
        Deployment memory existing = _namedDeployments[_name];
        if (existing.addr != address(0)) {
            if (bytes(existing.name).length == 0) {
                return payable(address(0));
            }
            return existing.addr;
        }
AKABABA-ETH's avatar
AKABABA-ETH committed
201
        return _getExistingDeploymentAddress(_name);
Mark Tyneway's avatar
Mark Tyneway committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    }

    /// @notice Returns the address of a deployment and reverts if the deployment
    ///         does not exist.
    /// @return The address of the deployment.
    function mustGetAddress(string memory _name) public view returns (address payable) {
        address addr = getAddress(_name);
        if (addr == address(0)) {
            revert DeploymentDoesNotExist(_name);
        }
        return payable(addr);
    }

    /// @notice Returns a deployment that is suitable to be used to interact with contracts.
    /// @param _name The name of the deployment.
    /// @return The deployment.
    function get(string memory _name) public view returns (Deployment memory) {
        Deployment memory deployment = _namedDeployments[_name];
        if (deployment.addr != address(0)) {
            return deployment;
        } else {
            return _getExistingDeployment(_name);
        }
    }

    /// @notice Writes a deployment to disk as a temp deployment so that the
    ///         hardhat deploy artifact can be generated afterwards.
    /// @param _name The name of the deployment.
    /// @param _deployed The address of the deployment.
    function save(string memory _name, address _deployed) public {
        if (bytes(_name).length == 0) {
            revert InvalidDeployment("EmptyName");
        }
        if (bytes(_namedDeployments[_name].name).length > 0) {
            revert InvalidDeployment("AlreadyExists");
        }

239
        Deployment memory deployment = Deployment({ name: _name, addr: payable(_deployed) });
Mark Tyneway's avatar
Mark Tyneway committed
240 241 242 243 244 245 246 247 248 249 250
        _namedDeployments[_name] = deployment;
        _newDeployments.push(deployment);
        _writeTemp(_name, _deployed);
    }

    /// @notice Reads the temp deployments from disk that were generated
    ///         by the deploy script.
    /// @return An array of deployments.
    function _getTempDeployments() internal returns (Deployment[] memory) {
        string memory json = vm.readFile(tempDeploymentsPath);
        string[] memory cmd = new string[](3);
251
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
252
        cmd[1] = "-c";
253
        cmd[2] = string.concat(Executables.jq, " 'keys' <<< '", json, "'");
Mark Tyneway's avatar
Mark Tyneway committed
254 255 256 257 258
        bytes memory res = vm.ffi(cmd);
        string[] memory names = stdJson.readStringArray(string(res), "");

        Deployment[] memory deployments = new Deployment[](names.length);
        for (uint256 i; i < names.length; i++) {
Mark Tyneway's avatar
Mark Tyneway committed
259 260
            string memory contractName = names[i];
            address addr = stdJson.readAddress(json, string.concat("$.", contractName));
261
            deployments[i] = Deployment({ name: contractName, addr: payable(addr) });
Mark Tyneway's avatar
Mark Tyneway committed
262 263 264 265 266 267 268
        }
        return deployments;
    }

    /// @notice Returns the json of the deployment transaction given a contract address.
    function _getDeployTransactionByContractAddress(address _addr) internal returns (string memory) {
        string[] memory cmd = new string[](3);
269
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
270
        cmd[1] = "-c";
271 272 273 274 275 276
        cmd[2] = string.concat(
            Executables.jq,
            " -r '.transactions[] | select(.contractAddress == ",
            '"',
            vm.toString(_addr),
            '"',
277 278
            ') | select(.transactionType == "CREATE"',
            ' or .transactionType == "CREATE2"',
279 280 281
            ")' < ",
            deployPath
        );
Mark Tyneway's avatar
Mark Tyneway committed
282
        bytes memory res = vm.ffi(cmd);
283 284 285
        return string(res);
    }

286
    /// @notice Returns the contract name from a deploy transaction.
287
    function _getContractNameFromDeployTransaction(string memory _deployTx) internal pure returns (string memory) {
288 289 290
        return stdJson.readString(_deployTx, ".contractName");
    }

291
    /// @notice Wrapper for vm.getCode that handles semver in the name.
Mark Tyneway's avatar
Mark Tyneway committed
292
    function _getCode(string memory _name) internal returns (bytes memory) {
293 294 295 296 297
        string memory fqn = _getFullyQualifiedName(_name);
        bytes memory code = vm.getCode(fqn);
        return code;
    }

298
    /// @notice Wrapper for vm.getDeployedCode that handles semver in the name.
Mark Tyneway's avatar
Mark Tyneway committed
299
    function _getDeployedCode(string memory _name) internal returns (bytes memory) {
300 301 302 303 304
        string memory fqn = _getFullyQualifiedName(_name);
        bytes memory code = vm.getDeployedCode(fqn);
        return code;
    }

305
    /// @notice Removes the semantic versioning from a contract name. The semver will exist if the contract is compiled
Mark Tyneway's avatar
Mark Tyneway committed
306
    /// more than once with different versions of the compiler.
307
    function _stripSemver(string memory _name) internal returns (string memory) {
308 309 310
        string[] memory cmd = new string[](3);
        cmd[0] = Executables.bash;
        cmd[1] = "-c";
311 312 313
        cmd[2] = string.concat(
            Executables.echo, " ", _name, " | ", Executables.sed, " -E 's/[.][0-9]+\\.[0-9]+\\.[0-9]+//g'"
        );
314
        bytes memory res = vm.ffi(cmd);
Mark Tyneway's avatar
Mark Tyneway committed
315 316 317 318 319 320
        return string(res);
    }

    /// @notice Returns the constructor arguent of a deployment transaction given a transaction json.
    function getDeployTransactionConstructorArguments(string memory _transaction) internal returns (string[] memory) {
        string[] memory cmd = new string[](3);
321
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
322
        cmd[1] = "-c";
323
        cmd[2] = string.concat(Executables.jq, " -r '.arguments' <<< '", _transaction, "'");
Mark Tyneway's avatar
Mark Tyneway committed
324 325 326 327 328 329 330 331 332 333
        bytes memory res = vm.ffi(cmd);

        string[] memory args = new string[](0);
        if (keccak256(bytes("null")) != keccak256(res)) {
            args = stdJson.readStringArray(string(res), "");
        }
        return args;
    }

    /// @notice Builds the fully qualified name of a contract. Assumes that the
334
    ///         file name is the same as the contract name but strips semver for the file name.
335
    function _getFullyQualifiedName(string memory _name) internal returns (string memory) {
336 337
        string memory sanitized = _stripSemver(_name);
        return string.concat(sanitized, ".sol:", _name);
Mark Tyneway's avatar
Mark Tyneway committed
338 339 340 341 342 343
    }

    /// @notice Returns the filesystem path to the artifact path. Assumes that the name of the
    ///         file matches the name of the contract.
    function _getForgeArtifactPath(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
344
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
345
        cmd[1] = "-c";
Mark Tyneway's avatar
Mark Tyneway committed
346
        cmd[2] = string.concat(Executables.forge, " config --json | ", Executables.jq, " -r .out");
Mark Tyneway's avatar
Mark Tyneway committed
347
        bytes memory res = vm.ffi(cmd);
348
        string memory contractName = _stripSemver(_name);
349 350
        string memory forgeArtifactPath =
            string.concat(vm.projectRoot(), "/", string(res), "/", contractName, ".sol/", _name, ".json");
Mark Tyneway's avatar
Mark Tyneway committed
351 352 353 354 355 356 357 358 359 360 361 362 363
        return forgeArtifactPath;
    }

    /// @notice Returns the forge artifact given a contract name.
    function _getForgeArtifact(string memory _name) internal returns (string memory) {
        string memory forgeArtifactPath = _getForgeArtifactPath(_name);
        string memory forgeArtifact = vm.readFile(forgeArtifactPath);
        return forgeArtifact;
    }

    /// @notice Returns the receipt of a deployment transaction.
    function _getDeployReceiptByContractAddress(address addr) internal returns (string memory) {
        string[] memory cmd = new string[](3);
364
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
365
        cmd[1] = "-c";
366 367 368 369 370 371 372 373 374
        cmd[2] = string.concat(
            Executables.jq,
            " -r '.receipts[] | select(.contractAddress == ",
            '"',
            vm.toString(addr),
            '"',
            ")' < ",
            deployPath
        );
Mark Tyneway's avatar
Mark Tyneway committed
375 376 377 378 379 380 381 382
        bytes memory res = vm.ffi(cmd);
        string memory receipt = string(res);
        return receipt;
    }

    /// @notice Returns the devdoc for a deployed contract.
    function getDevDoc(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
383
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
384
        cmd[1] = "-c";
385
        cmd[2] = string.concat(Executables.jq, " -r '.devdoc' < ", _getForgeArtifactPath(_name));
Mark Tyneway's avatar
Mark Tyneway committed
386 387 388 389 390 391 392
        bytes memory res = vm.ffi(cmd);
        return string(res);
    }

    /// @notice Returns the storage layout for a deployed contract.
    function getStorageLayout(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
393
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
394
        cmd[1] = "-c";
395
        cmd[2] = string.concat(Executables.jq, " -r '.storageLayout' < ", _getForgeArtifactPath(_name));
Mark Tyneway's avatar
Mark Tyneway committed
396 397 398 399 400 401 402
        bytes memory res = vm.ffi(cmd);
        return string(res);
    }

    /// @notice Returns the abi for a deployed contract.
    function getAbi(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
403
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
404
        cmd[1] = "-c";
405
        cmd[2] = string.concat(Executables.jq, " -r '.abi' < ", _getForgeArtifactPath(_name));
Mark Tyneway's avatar
Mark Tyneway committed
406 407 408 409 410 411 412
        bytes memory res = vm.ffi(cmd);
        return string(res);
    }

    /// @notice Returns the userdoc for a deployed contract.
    function getUserDoc(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
413
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
414
        cmd[1] = "-c";
415
        cmd[2] = string.concat(Executables.jq, " -r '.userdoc' < ", _getForgeArtifactPath(_name));
Mark Tyneway's avatar
Mark Tyneway committed
416 417 418 419 420 421 422
        bytes memory res = vm.ffi(cmd);
        return string(res);
    }

    /// @notice
    function getMetadata(string memory _name) internal returns (string memory) {
        string[] memory cmd = new string[](3);
423
        cmd[0] = Executables.bash;
Mark Tyneway's avatar
Mark Tyneway committed
424
        cmd[1] = "-c";
425
        cmd[2] = string.concat(Executables.jq, " '.metadata | tostring' < ", _getForgeArtifactPath(_name));
Mark Tyneway's avatar
Mark Tyneway committed
426 427 428 429 430
        bytes memory res = vm.ffi(cmd);
        return string(res);
    }

    /// @notice Adds a deployment to the temp deployments file
Mark Tyneway's avatar
Mark Tyneway committed
431
    function _writeTemp(string memory _name, address _deployed) internal {
432
        vm.writeJson({ json: stdJson.serialize("", _name, _deployed), path: tempDeploymentsPath });
Mark Tyneway's avatar
Mark Tyneway committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    }

    /// @notice Turns an Artifact into a json serialized string
    /// @param _artifact The artifact to serialize
    /// @return The json serialized string
    function _serializeArtifact(Artifact memory _artifact) internal returns (string memory) {
        string memory json = "";
        json = stdJson.serialize("", "address", _artifact.addr);
        json = stdJson.serialize("", "abi", _artifact.abi);
        json = stdJson.serialize("", "args", _artifact.args);
        json = stdJson.serialize("", "bytecode", _artifact.bytecode);
        json = stdJson.serialize("", "deployedBytecode", _artifact.deployedBytecode);
        json = stdJson.serialize("", "devdoc", _artifact.devdoc);
        json = stdJson.serialize("", "metadata", _artifact.metadata);
        json = stdJson.serialize("", "numDeployments", _artifact.numDeployments);
        json = stdJson.serialize("", "receipt", _artifact.receipt);
        json = stdJson.serialize("", "solcInputHash", _artifact.solcInputHash);
        json = stdJson.serialize("", "storageLayout", _artifact.storageLayout);
        json = stdJson.serialize("", "transactionHash", _artifact.transactionHash);
        json = stdJson.serialize("", "userdoc", _artifact.userdoc);
        return json;
    }

    /// @notice The context of the deployment is used to namespace the artifacts.
    ///         An unknown context will use the chainid as the context name.
    function _getDeploymentContext() private returns (string memory) {
        string memory context = vm.envOr("DEPLOYMENT_CONTEXT", string(""));
        if (bytes(context).length > 0) {
            return context;
        }

Mark Tyneway's avatar
Mark Tyneway committed
464
        uint256 chainid = vm.envOr("CHAIN_ID", block.chainid);
465
        if (chainid == Chains.Mainnet) {
Mark Tyneway's avatar
Mark Tyneway committed
466
            return "mainnet";
467
        } else if (chainid == Chains.Goerli) {
Mark Tyneway's avatar
Mark Tyneway committed
468
            return "goerli";
469
        } else if (chainid == Chains.OPGoerli) {
Mark Tyneway's avatar
Mark Tyneway committed
470
            return "optimism-goerli";
471
        } else if (chainid == Chains.OPMainnet) {
Mark Tyneway's avatar
Mark Tyneway committed
472
            return "optimism-mainnet";
473
        } else if (chainid == Chains.LocalDevnet || chainid == Chains.GethDevnet) {
Mark Tyneway's avatar
Mark Tyneway committed
474
            return "devnetL1";
475
        } else if (chainid == Chains.Hardhat) {
Mark Tyneway's avatar
Mark Tyneway committed
476
            return "hardhat";
477
        } else if (chainid == Chains.Sepolia) {
478
            return "sepolia";
479
        } else if (chainid == Chains.OPSepolia) {
480
            return "optimism-sepolia";
Mark Tyneway's avatar
Mark Tyneway committed
481 482 483 484 485 486 487 488
        } else {
            return vm.toString(chainid);
        }
    }

    /// @notice Reads the artifact from the filesystem by name and returns the address.
    /// @param _name The name of the artifact to read.
    /// @return The address of the artifact.
AKABABA-ETH's avatar
AKABABA-ETH committed
489
    function _getExistingDeploymentAddress(string memory _name) internal view returns (address payable) {
Mark Tyneway's avatar
Mark Tyneway committed
490 491 492 493 494 495 496 497 498 499
        return _getExistingDeployment(_name).addr;
    }

    /// @notice Reads the artifact from the filesystem by name and returns the Deployment.
    /// @param _name The name of the artifact to read.
    /// @return The deployment corresponding to the name.
    function _getExistingDeployment(string memory _name) internal view returns (Deployment memory) {
        string memory path = string.concat(deploymentsDir, "/", _name, ".json");
        try vm.readFile(path) returns (string memory json) {
            bytes memory addr = stdJson.parseRaw(json, "$.address");
500
            return Deployment({ addr: abi.decode(addr, (address)), name: _name });
Mark Tyneway's avatar
Mark Tyneway committed
501
        } catch {
502
            return Deployment({ addr: payable(address(0)), name: "" });
Mark Tyneway's avatar
Mark Tyneway committed
503 504 505
        }
    }
}