Commit 14e7889e authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

contracts-bedrock: cleanup `Artifacts` (#9312)

The `Artifacts` contract can be inherited by any contract
that wants to operate with a deploy artifact JSON file.
It was originally created to work with hardhat deploy artifacts,
which no longer exist. This cleans up some of the code to make
it more obvious that it isn't for hardhat deploy anymore.

This has no functionality changes, just renames and cleans up.
parent a6ec5b02
...@@ -32,8 +32,8 @@ abstract contract Artifacts { ...@@ -32,8 +32,8 @@ abstract contract Artifacts {
Deployment[] internal _newDeployments; Deployment[] internal _newDeployments;
/// @notice Path to the directory containing the hh deploy style artifacts /// @notice Path to the directory containing the hh deploy style artifacts
string internal deploymentsDir; string internal deploymentsDir;
/// @notice The path to the temp deployments file /// @notice The path to the deployment artifact that is being written to.
string internal tempDeploymentsPath; string internal deployArtifactPath;
/// @notice The namespace for the deployment. Can be set with the env var DEPLOYMENT_CONTEXT. /// @notice The namespace for the deployment. Can be set with the env var DEPLOYMENT_CONTEXT.
string internal deploymentContext; string internal deploymentContext;
...@@ -49,12 +49,12 @@ abstract contract Artifacts { ...@@ -49,12 +49,12 @@ abstract contract Artifacts {
vm.createDir(deploymentsDir, true); vm.createDir(deploymentsDir, true);
} }
tempDeploymentsPath = vm.envOr("DEPLOYMENT_OUTFILE", string.concat(deploymentsDir, "/.deploy")); deployArtifactPath = vm.envOr("DEPLOYMENT_OUTFILE", string.concat(deploymentsDir, "/.deploy"));
try vm.readFile(tempDeploymentsPath) returns (string memory) { } try vm.readFile(deployArtifactPath) returns (string memory) { }
catch { catch {
vm.writeJson("{}", tempDeploymentsPath); vm.writeJson("{}", deployArtifactPath);
} }
console.log("Storing temp deployment data in %s", tempDeploymentsPath); console.log("Using deploy artifact %s", deployArtifactPath);
try vm.createDir(deploymentsDir, true) { } catch (bytes memory) { } try vm.createDir(deploymentsDir, true) { } catch (bytes memory) { }
...@@ -189,8 +189,7 @@ abstract contract Artifacts { ...@@ -189,8 +189,7 @@ abstract contract Artifacts {
} }
} }
/// @notice Writes a deployment to disk as a temp deployment so that the /// @notice Appends a deployment to disk as a JSON deploy artifact.
/// hardhat deploy artifact can be generated afterwards.
/// @param _name The name of the deployment. /// @param _name The name of the deployment.
/// @param _deployed The address of the deployment. /// @param _deployed The address of the deployment.
function save(string memory _name, address _deployed) public { function save(string memory _name, address _deployed) public {
...@@ -201,17 +200,18 @@ abstract contract Artifacts { ...@@ -201,17 +200,18 @@ abstract contract Artifacts {
revert InvalidDeployment("AlreadyExists"); revert InvalidDeployment("AlreadyExists");
} }
console.log("Saving %s: %s", _name, _deployed);
Deployment memory deployment = Deployment({ name: _name, addr: payable(_deployed) }); Deployment memory deployment = Deployment({ name: _name, addr: payable(_deployed) });
_namedDeployments[_name] = deployment; _namedDeployments[_name] = deployment;
_newDeployments.push(deployment); _newDeployments.push(deployment);
_writeTemp(_name, _deployed); _appendDeployment(_name, _deployed);
} }
/// @notice Reads the temp deployments from disk that were generated /// @notice Reads the deployment artifact from disk that were generated
/// by the deploy script. /// by the deploy script.
/// @return An array of deployments. /// @return An array of deployments.
function _getTempDeployments() internal returns (Deployment[] memory) { function _getDeployments() internal returns (Deployment[] memory) {
string memory json = vm.readFile(tempDeploymentsPath); string memory json = vm.readFile(deployArtifactPath);
string[] memory cmd = new string[](3); string[] memory cmd = new string[](3);
cmd[0] = Executables.bash; cmd[0] = Executables.bash;
cmd[1] = "-c"; cmd[1] = "-c";
...@@ -229,8 +229,8 @@ abstract contract Artifacts { ...@@ -229,8 +229,8 @@ abstract contract Artifacts {
} }
/// @notice Adds a deployment to the temp deployments file /// @notice Adds a deployment to the temp deployments file
function _writeTemp(string memory _name, address _deployed) internal { function _appendDeployment(string memory _name, address _deployed) internal {
vm.writeJson({ json: stdJson.serialize("", _name, _deployed), path: tempDeploymentsPath }); vm.writeJson({ json: stdJson.serialize("", _name, _deployed), path: deployArtifactPath });
} }
/// @notice Reads the artifact from the filesystem by name and returns the address. /// @notice Reads the artifact from the filesystem by name and returns the address.
...@@ -246,8 +246,8 @@ abstract contract Artifacts { ...@@ -246,8 +246,8 @@ abstract contract Artifacts {
function _getExistingDeployment(string memory _name) internal view returns (Deployment memory) { function _getExistingDeployment(string memory _name) internal view returns (Deployment memory) {
string memory path = string.concat(deploymentsDir, "/", _name, ".json"); string memory path = string.concat(deploymentsDir, "/", _name, ".json");
try vm.readFile(path) returns (string memory json) { try vm.readFile(path) returns (string memory json) {
bytes memory addr = stdJson.parseRaw(json, "$.address"); address addr = stdJson.readAddress(json, "$.address");
return Deployment({ addr: abi.decode(addr, (address)), name: _name }); return Deployment({ addr: payable(addr), name: _name });
} catch { } catch {
return Deployment({ addr: payable(address(0)), name: "" }); return Deployment({ addr: payable(address(0)), name: "" });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment