Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add ScriptTools.exportValue utility #36

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions src/ScriptTools.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { stdJson } from "forge-std/StdJson.sol";

import { WardsAbstract } from "dss-interfaces/Interfaces.sol";

/**
/**
* @title Script Tools
* @dev Contains opinionated tools used in scripts.
*/
library ScriptTools {

VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));

string internal constant DEFAULT_DELIMITER = ",";
string internal constant DELIMITER_OVERRIDE = "DSSTEST_ARRAY_DELIMITER";
string internal constant EXPORT_JSON_KEY = "EXPORTS";
Expand Down Expand Up @@ -57,7 +57,7 @@ library ScriptTools {
string memory chainOutputFolder = string(abi.encodePacked("/script/output/", vm.toString(getRootChainId()), "/"));
return vm.readFile(string(abi.encodePacked(root, chainOutputFolder, name, "-latest.json")));
}

/**
* @notice Use standard environment variables to load config.
* @dev Will first check FOUNDRY_SCRIPT_CONFIG_TEXT for raw json text.
Expand All @@ -72,7 +72,7 @@ library ScriptTools {
config = readInput(vm.envOr("FOUNDRY_SCRIPT_CONFIG", name));
}
}

/**
* @notice Use standard environment variables to load config.
* @dev Will first check FOUNDRY_SCRIPT_CONFIG_TEXT for raw json text.
Expand All @@ -86,7 +86,7 @@ library ScriptTools {
config = readInput(vm.envString("FOUNDRY_SCRIPT_CONFIG"));
}
}

/**
* @notice Use standard environment variables to load dependencies.
* @dev Will first check FOUNDRY_SCRIPT_DEPS_TEXT for raw json text.
Expand All @@ -101,7 +101,7 @@ library ScriptTools {
dependencies = readOutput(vm.envOr("FOUNDRY_SCRIPT_DEPS", name));
}
}

/**
* @notice Use standard environment variables to load dependencies.
* @dev Will first check FOUNDRY_SCRIPT_DEPS_TEXT for raw json text.
Expand All @@ -116,6 +116,17 @@ library ScriptTools {
}
}

/**
* @notice Used to export important contracts to higher level deploy scripts.
* Note waiting on Foundry to have better primitives, but roll our own for now.
* @dev Requires FOUNDRY_EXPORTS_NAME to be set.
* @param label The label of the address.
* @param addr The address to export.
*/
function exportContract(string memory label, address addr) internal {
exportContract(vm.envString("FOUNDRY_EXPORTS_NAME"), label, addr);
}

/**
* @notice Used to export important contracts to higher level deploy scripts.
* Note waiting on Foundry to have better primitives, but roll our own for now.
Expand All @@ -127,23 +138,46 @@ library ScriptTools {
function exportContract(string memory name, string memory label, address addr) internal {
name = vm.envOr("FOUNDRY_EXPORTS_NAME", name);
string memory json = vm.serializeAddress(EXPORT_JSON_KEY, label, addr);
string memory root = vm.projectRoot();
string memory chainOutputFolder = string(abi.encodePacked("/script/output/", vm.toString(getRootChainId()), "/"));
vm.writeJson(json, string(abi.encodePacked(root, chainOutputFolder, name, "-", vm.toString(block.timestamp), ".json")));
if (vm.envOr("FOUNDRY_EXPORTS_OVERWRITE_LATEST", false)) {
vm.writeJson(json, string(abi.encodePacked(root, chainOutputFolder, name, "-latest.json")));
}
_doExport(name, json);
}

/**
* @notice Used to export important contracts to higher level deploy scripts.
* @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.
* @dev Requires FOUNDRY_EXPORTS_NAME to be set.
* @param label The label of the address.
* @param addr The address to export.
* @param val The value to export.
*/
function exportContract(string memory label, address addr) internal {
exportContract(vm.envString("FOUNDRY_EXPORTS_NAME"), label, addr);
function exportValue(string memory label, uint256 val) internal {
exportValue(vm.envString("FOUNDRY_EXPORTS_NAME"), label, val);
}

/**
* @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.
* @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 val The value to export.
*/
function exportValue(string memory name, string memory label, uint256 val) internal {
name = vm.envOr("FOUNDRY_EXPORTS_NAME", name);
string memory json = vm.serializeUint(EXPORT_JSON_KEY, label, val);
_doExport(name, json);
}

/**
* @dev Common logic to export JSON files.
* @param name The name to give the json file
* @param json The serialized json object to export.
*/
function _doExport(string memory name, string memory json) internal {
string memory root = vm.projectRoot();
string memory chainOutputFolder = string(abi.encodePacked("/script/output/", vm.toString(getRootChainId()), "/"));
vm.writeJson(json, string(abi.encodePacked(root, chainOutputFolder, name, "-", vm.toString(block.timestamp), ".json")));
if (vm.envOr("FOUNDRY_EXPORTS_OVERWRITE_LATEST", false)) {
vm.writeJson(json, string(abi.encodePacked(root, chainOutputFolder, name, "-latest.json")));
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/tests/ScriptToolsTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,15 @@ contract ScriptToolTest is DssTest {
assertEq(stdJson.readAddress(loadedExports, ".label2"), address(2));
}

function test_export_values() public {
// Export some values and write to output
ScriptTools.exportValue("myExports", "label1", 1);
ScriptTools.exportValue("myExports", "label2", 2);

// Simulate a subsequent run loading a previously written file (use latest deploy)
loadedExports = ScriptTools.readOutput("myExports", 1);
assertEq(stdJson.readUint(loadedExports, ".label1"), 1);
assertEq(stdJson.readUint(loadedExports, ".label2"), 2);
}

}