Skip to content

Commit

Permalink
feat: updated forge version command to be more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed Jun 3, 2024
1 parent 529ecac commit df9f90b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 21 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,8 @@ You must also add `ffi = true` to your `foundry.toml` to use this feature.
### FoundryZkSync Functions:
- `is_foundry_zksync`: Returns true if you are on `foundry-zksync`
# Contributing
PRs are welcome!
```
git clone https://github.com/Cyfrin/foundry-devops
cd foundry-devops
forge install
```
## Testing
# Testing
For testing on vanilla foundry, run:
Expand All @@ -158,3 +149,16 @@ For testing with `foundry-zksync`, run:
```bash
make test-zksync
```
# Limitations
- You cannot deploy a contract with `FoundryZkSyncChainChecker` or `ZkSyncChainChecker` because `foundry-zksync` gets confused by a lot of cheatcodes, and doesn't recognize cheatcodes after compiling to the EraVM.
# Contributing
PRs are welcome!
```
git clone https://github.com/Cyfrin/foundry-devops
cd foundry-devops
make
```
14 changes: 12 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

DEFAULT_ANVIL_KEY := 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

all: clean remove install update build

clean :; forge clean

remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules"

install :; forge install

update:; forge update

build :; forge build

anvil:; anvil -m 'test test test test test test test test test test test junk' --steps-tracing --block-time 1

deploy:; forge script script/DeployStuff.s.sol:DeployStuff --broadcast --rpc-url http://localhost:8545 --private-key $(DEFAULT_ANVIL_KEY) -vvvv

interact:; forge script script/InteractWithStuff.s.sol:InteractWithStuff --broadcast --rpc-url http://localhost:8545 --private-key $(DEFAULT_ANVIL_KEY) -vvvv

test-zksync :; foundryup-zksync && forge test --zksync
zktest :; foundryup-zksync && forge test --zksync && foundryup

test :; foundryup && forge test
test :; forge test
41 changes: 32 additions & 9 deletions src/FoundryZkSyncChecker.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {Script, console2} from "forge-std/Script.sol";
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 {
bytes constant FORGE_VERSION_0_0_2 = hex"666f72676520302e302e32";
bytes constant FORGE_VERSION_0_2_0 = hex"666f72676520302e322e30";
uint256 constant PREFIX_LENGTH = 11;

error FoundryZkSyncChecker__UnknownFoundryVersion();

abstract contract FoundryZkSyncChecker is Script {
/**
* @notice returns the current version of foundry.
*/
function is_foundry_zksync() internal returns (bool) {
string[] memory command = new string[](2);
command[0] = "bash";
command[1] = "src/is_foundry_zksync.sh";
bytes memory retData = vm.ffi(command);
bool isFoundryZkSync = uint256(bytes32(retData)) > 0;
return isFoundryZkSync;
function is_foundry_zksync() public returns (bool) {
string[] memory forgeVersionCommand = new string[](2);
forgeVersionCommand[0] = "forge";
forgeVersionCommand[1] = "--version";
bytes memory retData = vm.ffi(forgeVersionCommand);
console2.logBytes(retData);

bytes memory forgeVersionPrefixed = new bytes(PREFIX_LENGTH);
for (uint256 i = 0; i < PREFIX_LENGTH; i++) {
forgeVersionPrefixed[i] = retData[i];
}
string memory forgePrefixedStr = string(forgeVersionPrefixed);
console2.log("Got forge version:", forgePrefixedStr);

if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_2_0)) {
console2.log("This is Vanilla Foundry");
return false;
} else if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_0_2)) {
console2.log("This is Foundry ZkSync");
return true;
}
console2.log("Unknown forge version");
revert FoundryZkSyncChecker__UnknownFoundryVersion();
}

modifier onlyFoundryZkSync() {
Expand Down
32 changes: 32 additions & 0 deletions test/FoundryZkSyncCheckerTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {Test, console2} from "forge-std/Test.sol";
import {FoundryZkSyncChecker} from "src/FoundryZkSyncChecker.sol";
import {StringUtils} from "src/StringUtils.sol";

contract FoundryZkSyncCheckerTest is Test, FoundryZkSyncChecker {
using StringUtils for string;

string public constant RELATIVE_SCRIPT_PATH = "test/is_foundry_zksync.sh";

function setUp() public {}

function testVersion() public {
bool isZkFoundryBashScript = is_foundry_zksync_bash_script();
bool isZkFoundryChecker = is_foundry_zksync();
assertEq(isZkFoundryBashScript, isZkFoundryChecker);
}

/*//////////////////////////////////////////////////////////////
HELPER
//////////////////////////////////////////////////////////////*/
function is_foundry_zksync_bash_script() public returns (bool) {
string[] memory command = new string[](2);
command[0] = "bash";
command[1] = RELATIVE_SCRIPT_PATH;
bytes memory retData = vm.ffi(command);
bool isFoundryZkSync = uint256(bytes32(retData)) > 0;
return isFoundryZkSync;
}
}
File renamed without changes.

0 comments on commit df9f90b

Please sign in to comment.