Skip to content

Commit

Permalink
feat: read addresses from node_modules/mangrovedao/mangrove-core (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMangrove authored Jul 28, 2023
1 parent ea4d5a8 commit e60cbae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
6 changes: 3 additions & 3 deletions script/lib/Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ abstract contract Deployer is Script2 {
line(end ? " }" : " },");
}
line("]");
string memory latestBackupFile = fork.addressesFile("deployed.backup", "-latest");
string memory latestBackupFile = fork.addressesFileRoot("deployed.backup", "-latest");
string memory timestampedBackupFile =
fork.addressesFile("deployed.backup", string.concat("-", vm.toString(block.timestamp), ".backup"));
string memory mainFile = fork.addressesFile("deployed");
fork.addressesFileRoot("deployed.backup", string.concat("-", vm.toString(block.timestamp), ".backup"));
string memory mainFile = fork.addressesFileRoot("deployed");
vm.writeFile(latestBackupFile, out);
vm.writeFile(timestampedBackupFile, out);
if (writeDeploy) {
Expand Down
47 changes: 41 additions & 6 deletions test/lib/forks/Generic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ contract GenericFork is Script {
addr = getNoRevert(name);
if (addr == address(0)) {
revert(
"Fork::get(string name): no contract found for name argument, either in context nor in deployed addresses. Check the appropriate context/<chain>.json and deployed/<chain>.json, and make sure you are doing fork.set(name,address) for all your deployed contracts."
string.concat(
"Fork::get(string name)",
name,
": no contract found for name argument, either in context nor in deployed addresses. Check the appropriate context/<chain>.json and deployed/<chain>.json, and make sure you are doing fork.set(name,address) for all your deployed contracts."
)
);
}
}
Expand All @@ -70,16 +74,47 @@ contract GenericFork is Script {

/* Read addresses from JSON files */

function addressesFile(string memory category, string memory suffix) public view returns (string memory) {
return string.concat(vm.projectRoot(), "/addresses/", category, "/", NETWORK, suffix, ".json");
function addressesFile(string memory root, string memory category, string memory suffix)
public
view
returns (string memory)
{
return string.concat(root, "/addresses/", category, "/", NETWORK, suffix, ".json");
}

function addressesFile(string memory category) public view returns (string memory) {
return addressesFile(category, "");
function addressesFileNodeModules(string memory category, string memory suffix) public view returns (string memory) {
return addressesFile(string.concat(vm.projectRoot(), "/node_modules/@mangrovedao/mangrove-core"), category, suffix);
}

function addressesFileRoot(string memory category, string memory suffix) public view returns (string memory) {
return addressesFile(vm.projectRoot(), category, suffix);
}

function addressesFileNodeModules(string memory category) public view returns (string memory) {
return addressesFileNodeModules(category, "");
}

function addressesFileRoot(string memory category) public view returns (string memory) {
return addressesFileRoot(category, "");
}

function readAddresses(string memory category) internal returns (Record[] memory) {
string memory fileName = addressesFile(category);
string memory fileNameNodeModules = addressesFileNodeModules(category);
Record[] memory recordsFromNodeModules = readAddressesFromFileName(fileNameNodeModules);
string memory fileNameRoot = addressesFileRoot(category);
Record[] memory recordsFromRoot = readAddressesFromFileName(fileNameRoot);
Record[] memory records = new Record[](recordsFromNodeModules.length + recordsFromRoot.length);
uint i = 0;
for (; i < recordsFromNodeModules.length; i++) {
records[i] = recordsFromNodeModules[i];
}
for (uint j = 0; j < recordsFromRoot.length; j++) {
records[i + j] = recordsFromRoot[j];
}
return records;
}

function readAddressesFromFileName(string memory fileName) internal returns (Record[] memory) {
try vm.readFile(fileName) returns (string memory addressesRaw) {
if (bytes(addressesRaw).length == 0) {
return (new Record[](0));
Expand Down

0 comments on commit e60cbae

Please sign in to comment.