Skip to content

Commit

Permalink
feat: added support for foundry 0.3.0 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC authored Jan 8, 2025
1 parent 626dbcf commit 90b6dde
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
85 changes: 64 additions & 21 deletions src/DevOpsTools.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ library DevOpsTools {
using stdJson for string;
using StringUtils for string;

Vm public constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
Vm public constant vm =
Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

string public constant RELATIVE_BROADCAST_PATH = "./broadcast";

function get_most_recent_deployment(string memory contractName, uint256 chainId) internal view returns (address) {
return get_most_recent_deployment(contractName, chainId, RELATIVE_BROADCAST_PATH);
function get_most_recent_deployment(
string memory contractName,
uint256 chainId
) internal view returns (address) {
return
get_most_recent_deployment(
contractName,
chainId,
RELATIVE_BROADCAST_PATH
);
}

function get_most_recent_deployment(
Expand All @@ -33,27 +42,36 @@ library DevOpsTools {
for (uint256 i = 0; i < entries.length; i++) {
string memory normalizedPath = normalizePath(entries[i].path);
if (
normalizedPath.contains(string.concat("/", vm.toString(chainId), "/"))
&& normalizedPath.contains(".json") && !normalizedPath.contains("dry-run")
normalizedPath.contains(
string.concat("/", vm.toString(chainId), "/")
) &&
normalizedPath.contains(".json") &&
!normalizedPath.contains("dry-run")
) {
string memory json = vm.readFile(normalizedPath);
uint256 timestamp = vm.parseJsonUint(json, ".timestamp");
latestAddress = processRun(json, contractName, latestAddress);
}
}
for (uint256 i = 0; i < entries.length; i++) {
Vm.DirEntry memory entry = entries[i];
if (
entry.path.contains(string.concat("/", vm.toString(chainId), "/")) && entry.path.contains(".json")
&& !entry.path.contains("dry-run")
entry.path.contains(
string.concat("/", vm.toString(chainId), "/")
) &&
entry.path.contains(".json") &&
!entry.path.contains("dry-run")
) {
runProcessed = true;
string memory json = vm.readFile(entry.path);

uint256 timestamp = vm.parseJsonUint(json, ".timestamp");

if (timestamp > lastTimestamp) {
latestAddress = processRun(json, contractName, latestAddress);
latestAddress = processRun(
json,
contractName,
latestAddress
);

// If we have found some deployed contract, update the timestamp
// Otherwise, the earliest deployment may have been before `lastTimestamp` and we should not update
Expand All @@ -73,32 +91,57 @@ library DevOpsTools {
} else {
revert(
string.concat(
"No contract named ", "'", contractName, "'", " has been deployed on chain ", vm.toString(chainId)
"No contract named ",
"'",
contractName,
"'",
" has been deployed on chain ",
vm.toString(chainId)
)
);
}
}

function processRun(string memory json, string memory contractName, address latestAddress)
internal
view
returns (address)
{
for (uint256 i = 0; vm.keyExistsJson(json, string.concat("$.transactions[", vm.toString(i), "]")); i++) {
string memory contractNamePath = string.concat("$.transactions[", vm.toString(i), "].contractName");
function processRun(
string memory json,
string memory contractName,
address latestAddress
) internal view returns (address) {
for (
uint256 i = 0;
vm.keyExistsJson(
json,
string.concat("$.transactions[", vm.toString(i), "]")
);
i++
) {
string memory contractNamePath = string.concat(
"$.transactions[",
vm.toString(i),
"].contractName"
);
if (vm.keyExistsJson(json, contractNamePath)) {
string memory deployedContractName = json.readString(contractNamePath);
string memory deployedContractName = json.readString(
contractNamePath
);
if (deployedContractName.isEqualTo(contractName)) {
latestAddress =
json.readAddress(string.concat("$.transactions[", vm.toString(i), "].contractAddress"));
latestAddress = json.readAddress(
string.concat(
"$.transactions[",
vm.toString(i),
"].contractAddress"
)
);
}
}
}

return latestAddress;
}

function normalizePath(string memory path) internal pure returns (string memory) {
function normalizePath(
string memory path
) internal pure returns (string memory) {
// Replace backslashes with forward slashes
bytes memory b = bytes(path);
for (uint256 i = 0; i < b.length; i++) {
Expand Down
13 changes: 11 additions & 2 deletions src/FoundryZkSyncChecker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import {Test, console2} from "forge-std/Test.sol";

// We have to label it a test so foundry-zksync doesn't get confused
abstract contract FoundryZkSyncChecker is Test {
// cast from-utf8 "forge 0.0.2"
bytes constant FORGE_VERSION_0_0_2 = hex"666f72676520302e302e32";
// cast from-utf8 "forge 0.2.0"
bytes constant FORGE_VERSION_0_2_0 = hex"666f72676520302e322e30";
// cast from-utf8 "forge 0.3.0"
bytes constant FORGE_VERSION_0_3_0 = hex"666f72676520302e332e30";
uint256 constant PREFIX_LENGTH = 11;

error FoundryZkSyncChecker__UnknownFoundryVersion();
Expand All @@ -28,10 +32,15 @@ abstract contract FoundryZkSyncChecker is Test {
string memory forgePrefixedStr = string(forgeVersionPrefixed);
console2.log("Got forge version:", forgePrefixedStr);

if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_2_0)) {
if (
bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_2_0) ||
bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_3_0)
) {
console2.log("This is Vanilla Foundry");
return false;
} else if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_0_2)) {
} else if (
bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_0_2)
) {
console2.log("This is Foundry ZkSync");
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion test/is_foundry_zksync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

output=$(forge --version)

if [[ $output == forge\ 0.2.0* ]]; then
if [[ $output == forge\ 0.2.0* || $output == forge\ 0.3.0* ]]; then
echo 0x00 # This is false
else
echo 0x01 # This is true
Expand Down

0 comments on commit 90b6dde

Please sign in to comment.