Skip to content

Commit

Permalink
Extend ScriptTools and Domain to support export and loading of addres…
Browse files Browse the repository at this point in the history
…s arrays
  • Loading branch information
telome committed Aug 21, 2024
1 parent a99c543 commit c6f921f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ScriptTools.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ library ScriptTools {
_doExport(name, json);
}

/**
* @notice Used to export important contracts to higher level deploy scripts. Specifically,
* this function exports an array of contracts under the same key (label).
* Note waiting on Foundry to have better primitives, but roll our own for now.
* @dev Set FOUNDRY_EXPORTS_NAME to override the name of the json file.
* @param name The name to give the json file.
* @param label The label of the address.
* @param addr The addresses to export.
*/
function exportContracts(string memory name, string memory label, address[] memory addr) internal {
name = vm.envOr("FOUNDRY_EXPORTS_NAME", name);
string memory json = vm.serializeAddress(string(abi.encodePacked(EXPORT_JSON_KEY, "_", name)), label, addr);
_doExport(name, json);
}

/**
* @notice Used to export important values to higher level deploy scripts.
* Note waiting on Foundry to have better primitives, but roll our own for now.
Expand Down
9 changes: 9 additions & 0 deletions src/domains/Domain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ contract Domain {
vm.makePersistent(address(this));
}

function hasConfigKey(string memory key) internal view returns (bool) {
bytes memory raw = config.parseRaw(string.concat(".domains.", _details.chainAlias, ".", key));
return raw.length > 0;
}

function readConfigString(string memory key) public view returns (string memory) {
return config.readString(string.concat(".domains.", _details.chainAlias, ".", key));
}
Expand All @@ -47,6 +52,10 @@ contract Domain {
return config.readAddress(string.concat(".domains.", _details.chainAlias, ".", key));
}

function readConfigAddresses(string memory key) internal view returns (address[] memory) {
return config.readAddressArray(string.concat(".domains.", _details.chainAlias, ".", key));
}

function readConfigUint(string memory key) public view returns (uint256) {
return config.readUint(string.concat(".domains.", _details.chainAlias, ".", key));
}
Expand Down
5 changes: 5 additions & 0 deletions src/tests/ScriptToolsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ contract ScriptToolTest is DssTest {
// Export some contracts and write to output
ScriptTools.exportContract("myExports", "addr1", address(0x1));
ScriptTools.exportContract("myExports", "addr2", address(0x2));
address[] memory addr34 = new address[](2);
addr34[0] = address(0x3);
addr34[1] = address(0x4);
ScriptTools.exportContracts("myExports", "addr34", addr34);

// Simulate a subsequent run loading a previously written file (use latest deploy)
loadedExports = ScriptTools.readOutput("myExports", 1);
assertEq(stdJson.readAddress(loadedExports, ".addr1"), address(0x1));
assertEq(stdJson.readAddress(loadedExports, ".addr2"), address(0x2));
assertEq(stdJson.readAddressArray(loadedExports, ".addr34"), addr34);

// Export some values and write to output
ScriptTools.exportValue("myExports", "label1", 1);
Expand Down

0 comments on commit c6f921f

Please sign in to comment.