Skip to content

Commit

Permalink
feat: add DeploymentConfig for network specifc deployments
Browse files Browse the repository at this point in the history
To allow deployment to different chains but using the same deployment
scripts, this commit introduces a basic `DeploymentConfig` which can be
extended as necessary in each project.

There's a few things that should be considered:

- `activeNetworkConfig` will be initialized via the constructor, at
  which point it is know what `block.chainid` is
- To add new configuration settings, extend `NetworkConfig`
- To add a new config for a different chain, extend the `if/else` block
  in the constructor so that it creates a `NetworkConfig` for the chain
  in question
  • Loading branch information
0x-r4bbit committed Sep 6, 2023
1 parent 43a1dff commit 4deccd1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ pragma solidity >=0.8.19 <=0.9.0;

import { Foo } from "../src/Foo.sol";
import { BaseScript } from "./Base.s.sol";
import { DeploymentConfig } from "./DeploymentConfig.s.sol";

contract Deploy is BaseScript {
function run() public broadcast returns (Foo foo) {
address private deployer;

function run() public returns (Foo foo) {
DeploymentConfig deploymentConfig = new DeploymentConfig(broadcaster);
(deployer) = deploymentConfig.activeNetworkConfig();
foo = new Foo();
}
}
38 changes: 38 additions & 0 deletions script/DeploymentConfig.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.8.19 <=0.9.0;

import { Script } from "forge-std/Script.sol";

contract DeploymentConfig is Script {
error DeploymentConfig_InvalidDeployerAddress();

struct NetworkConfig {
address deployer;
}

NetworkConfig public activeNetworkConfig;

address private deployer;

constructor(address _broadcaster) {
if (block.chainid == 31_337) {
activeNetworkConfig = getOrCreateAnvilEthConfig();
} else {
revert("no network config for this chain");
}
if (_broadcaster == address(0)) revert DeploymentConfig_InvalidDeployerAddress();
deployer = _broadcaster;
}

function getOrCreateAnvilEthConfig() public view returns (NetworkConfig memory) {
return NetworkConfig({ deployer: deployer });
}

// This function is a hack to have it excluded by `forge coverage` until
// https://github.com/foundry-rs/foundry/issues/2988 is fixed.
// See: https://github.com/foundry-rs/foundry/issues/2988#issuecomment-1437784542
// for more info.
// solhint-disable-next-line
function test() public { }
}

0 comments on commit 4deccd1

Please sign in to comment.