Skip to content

Commit df9f90b

Browse files
committed
feat: updated forge version command to be more resilient
1 parent 529ecac commit df9f90b

5 files changed

+90
-21
lines changed

README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,8 @@ You must also add `ffi = true` to your `foundry.toml` to use this feature.
135135
### FoundryZkSync Functions:
136136
- `is_foundry_zksync`: Returns true if you are on `foundry-zksync`
137137
138-
# Contributing
139138
140-
PRs are welcome!
141-
142-
```
143-
git clone https://github.com/Cyfrin/foundry-devops
144-
cd foundry-devops
145-
forge install
146-
```
147-
148-
## Testing
139+
# Testing
149140
150141
For testing on vanilla foundry, run:
151142
@@ -158,3 +149,16 @@ For testing with `foundry-zksync`, run:
158149
```bash
159150
make test-zksync
160151
```
152+
153+
# Limitations
154+
- 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.
155+
156+
# Contributing
157+
158+
PRs are welcome!
159+
160+
```
161+
git clone https://github.com/Cyfrin/foundry-devops
162+
cd foundry-devops
163+
make
164+
```

makefile

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
DEFAULT_ANVIL_KEY := 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
44

5+
all: clean remove install update build
6+
7+
clean :; forge clean
8+
9+
remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules"
10+
11+
install :; forge install
12+
513
update:; forge update
614

15+
build :; forge build
16+
717
anvil:; anvil -m 'test test test test test test test test test test test junk' --steps-tracing --block-time 1
818

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

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

13-
test-zksync :; foundryup-zksync && forge test --zksync
23+
zktest :; foundryup-zksync && forge test --zksync && foundryup
1424

15-
test :; foundryup && forge test
25+
test :; forge test

src/FoundryZkSyncChecker.sol

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.18;
33

4-
import {Script, console2} from "forge-std/Script.sol";
4+
import {Test, console2} from "forge-std/Test.sol";
5+
6+
// We have to label it a test so foundry-zksync doesn't get confused
7+
abstract contract FoundryZkSyncChecker is Test {
8+
bytes constant FORGE_VERSION_0_0_2 = hex"666f72676520302e302e32";
9+
bytes constant FORGE_VERSION_0_2_0 = hex"666f72676520302e322e30";
10+
uint256 constant PREFIX_LENGTH = 11;
11+
12+
error FoundryZkSyncChecker__UnknownFoundryVersion();
513

6-
abstract contract FoundryZkSyncChecker is Script {
714
/**
815
* @notice returns the current version of foundry.
916
*/
10-
function is_foundry_zksync() internal returns (bool) {
11-
string[] memory command = new string[](2);
12-
command[0] = "bash";
13-
command[1] = "src/is_foundry_zksync.sh";
14-
bytes memory retData = vm.ffi(command);
15-
bool isFoundryZkSync = uint256(bytes32(retData)) > 0;
16-
return isFoundryZkSync;
17+
function is_foundry_zksync() public returns (bool) {
18+
string[] memory forgeVersionCommand = new string[](2);
19+
forgeVersionCommand[0] = "forge";
20+
forgeVersionCommand[1] = "--version";
21+
bytes memory retData = vm.ffi(forgeVersionCommand);
22+
console2.logBytes(retData);
23+
24+
bytes memory forgeVersionPrefixed = new bytes(PREFIX_LENGTH);
25+
for (uint256 i = 0; i < PREFIX_LENGTH; i++) {
26+
forgeVersionPrefixed[i] = retData[i];
27+
}
28+
string memory forgePrefixedStr = string(forgeVersionPrefixed);
29+
console2.log("Got forge version:", forgePrefixedStr);
30+
31+
if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_2_0)) {
32+
console2.log("This is Vanilla Foundry");
33+
return false;
34+
} else if (bytes32(forgeVersionPrefixed) == bytes32(FORGE_VERSION_0_0_2)) {
35+
console2.log("This is Foundry ZkSync");
36+
return true;
37+
}
38+
console2.log("Unknown forge version");
39+
revert FoundryZkSyncChecker__UnknownFoundryVersion();
1740
}
1841

1942
modifier onlyFoundryZkSync() {

test/FoundryZkSyncCheckerTest.t.sol

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.18;
3+
4+
import {Test, console2} from "forge-std/Test.sol";
5+
import {FoundryZkSyncChecker} from "src/FoundryZkSyncChecker.sol";
6+
import {StringUtils} from "src/StringUtils.sol";
7+
8+
contract FoundryZkSyncCheckerTest is Test, FoundryZkSyncChecker {
9+
using StringUtils for string;
10+
11+
string public constant RELATIVE_SCRIPT_PATH = "test/is_foundry_zksync.sh";
12+
13+
function setUp() public {}
14+
15+
function testVersion() public {
16+
bool isZkFoundryBashScript = is_foundry_zksync_bash_script();
17+
bool isZkFoundryChecker = is_foundry_zksync();
18+
assertEq(isZkFoundryBashScript, isZkFoundryChecker);
19+
}
20+
21+
/*//////////////////////////////////////////////////////////////
22+
HELPER
23+
//////////////////////////////////////////////////////////////*/
24+
function is_foundry_zksync_bash_script() public returns (bool) {
25+
string[] memory command = new string[](2);
26+
command[0] = "bash";
27+
command[1] = RELATIVE_SCRIPT_PATH;
28+
bytes memory retData = vm.ffi(command);
29+
bool isFoundryZkSync = uint256(bytes32(retData)) > 0;
30+
return isFoundryZkSync;
31+
}
32+
}
File renamed without changes.

0 commit comments

Comments
 (0)