-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/static-logs
- Loading branch information
Showing
9 changed files
with
223 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.18; | ||
|
||
import "forge-std/Test.sol"; | ||
import {L2ContractHelper} from "era-contracts/l2-contracts/contracts/L2ContractHelper.sol"; // =0.8.20 | ||
|
||
import {Greeter} from "../src/Greeter.sol"; | ||
import {CustomNumber} from "../src/CustomNumber.sol"; | ||
|
||
import {Create2Utils} from "../src/Create2Utils.sol"; | ||
|
||
contract Create2Test is Test { | ||
function getBytecodeHash(string memory path) internal returns (bytes32 bytecodeHash) { | ||
string memory artifact = vm.readFile(path); | ||
bytecodeHash = vm.parseJsonBytes32(artifact, ".hash"); | ||
} | ||
|
||
function testCanDeployViaCreate2() public { | ||
bytes32 bytecodeHash = getBytecodeHash("zkout/Greeter.sol/Greeter.json"); | ||
address sender = address(0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496); | ||
bytes32 salt = "12345"; | ||
bytes32 constructorInputHash = keccak256(abi.encode()); | ||
|
||
address computedAddress = | ||
Create2Utils.computeCreate2Address(sender, salt, bytes32(bytecodeHash), constructorInputHash); | ||
|
||
// deploy via create2 | ||
address actualAddress = address(new Greeter{salt: salt}()); | ||
|
||
assertEq(actualAddress, computedAddress); | ||
} | ||
|
||
function testComputeCreate2WithNoArgs() external { | ||
bytes32 salt = bytes32(0x0); | ||
|
||
bytes32 bytecodeHash = getBytecodeHash("zkout/Greeter.sol/Greeter.json"); | ||
|
||
address computedAddress = | ||
Create2Utils.computeCreate2Address(address(this), salt, bytes32(bytecodeHash), keccak256(abi.encode())); | ||
address expectedAddress = | ||
L2ContractHelper.computeCreate2Address(address(this), salt, bytes32(bytecodeHash), keccak256(abi.encode())); | ||
|
||
address actualAddress = address(new Greeter{salt: salt}()); | ||
assertEq(actualAddress, expectedAddress); | ||
assertEq(computedAddress, expectedAddress); | ||
} | ||
|
||
function testComputeCreate2WithArgs() external { | ||
bytes32 salt = bytes32(0x0); | ||
uint8 value = 42; | ||
|
||
bytes32 bytecodeHash = getBytecodeHash("zkout/CustomNumber.sol/CustomNumber.json"); | ||
|
||
address computedAddress = | ||
Create2Utils.computeCreate2Address(address(this), salt, bytecodeHash, keccak256(abi.encode(value))); | ||
address expectedAddress = | ||
L2ContractHelper.computeCreate2Address(address(this), salt, bytecodeHash, keccak256(abi.encode(value))); | ||
|
||
CustomNumber num = new CustomNumber{salt: salt}(value); | ||
assertEq(address(num), expectedAddress); | ||
assertEq(computedAddress, expectedAddress); | ||
assertEq(num.number(), value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use foundry_test_utils::{forgetest_async, util, ZkSyncNode}; | ||
|
||
forgetest_async!(forge_zk_can_deploy_erc20, |prj, cmd| { | ||
util::initialize(prj.root()); | ||
prj.add_source("ERC20.sol", include_str!("../../../../../testdata/zk/ERC20.sol")).unwrap(); | ||
|
||
let node = ZkSyncNode::start(); | ||
let url = node.url(); | ||
|
||
let private_key = | ||
ZkSyncNode::rich_wallets().next().map(|(_, pk, _)| pk).expect("No rich wallets available"); | ||
|
||
cmd.forge_fuse().args([ | ||
"create", | ||
"--zk-startup", | ||
"./src/ERC20.sol:MyToken", | ||
"--rpc-url", | ||
url.as_str(), | ||
"--private-key", | ||
private_key, | ||
]); | ||
|
||
let (stdout, _) = cmd.output_lossy(); | ||
assert!(stdout.contains("Deployer: ")); | ||
assert!(stdout.contains("Deployed to: ")); | ||
}); | ||
|
||
forgetest_async!(forge_zk_can_deploy_token_receiver, |prj, cmd| { | ||
util::initialize(prj.root()); | ||
prj.add_source( | ||
"TokenReceiver.sol", | ||
include_str!("../../../../../testdata/zk/TokenReceiver.sol"), | ||
) | ||
.unwrap(); | ||
|
||
let node = ZkSyncNode::start(); | ||
let url = node.url(); | ||
|
||
let private_key = | ||
ZkSyncNode::rich_wallets().next().map(|(_, pk, _)| pk).expect("No rich wallets available"); | ||
|
||
cmd.forge_fuse().args([ | ||
"create", | ||
"--zk-startup", | ||
"./src/TokenReceiver.sol:TokenReceiver", | ||
"--rpc-url", | ||
url.as_str(), | ||
"--private-key", | ||
private_key, | ||
]); | ||
|
||
let (stdout, _) = cmd.output_lossy(); | ||
assert!(stdout.contains("Deployer: ")); | ||
assert!(stdout.contains("Deployed to: ")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
mod basic; | ||
mod cheats; | ||
mod contracts; | ||
mod create; | ||
mod factory; | ||
mod factory_deps; | ||
mod fork; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
|
||
pragma solidity >=0.8.7 <0.9.0; | ||
|
||
library Create2Utils { | ||
function computeCreate2Address(address sender, bytes32 salt, bytes32 creationCodeHash, bytes32 constructorInputHash) | ||
internal | ||
pure | ||
returns (address) | ||
{ | ||
bytes32 zksync_create2_prefix = keccak256("zksyncCreate2"); | ||
bytes32 address_hash = keccak256( | ||
bytes.concat( | ||
zksync_create2_prefix, bytes32(uint256(uint160(sender))), salt, creationCodeHash, constructorInputHash | ||
) | ||
); | ||
|
||
return address(uint160(uint256(address_hash))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.7 <0.9.0; | ||
|
||
contract CustomNumber { | ||
uint8 value; | ||
|
||
constructor(uint8 _value) { | ||
value = _value; | ||
} | ||
|
||
function number() public view returns (uint8) { | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
contract MyToken { | ||
string public owner; | ||
string public constant name = "MyToken"; | ||
string public constant symbol = "MTK"; | ||
uint8 public constant decimals = 18; | ||
uint256 public totalSupply = 1000000 * (10 ** uint256(decimals)); | ||
mapping(address => uint256) public balanceOf; | ||
|
||
event Transfer(address indexed from, address indexed to, uint256 value); | ||
|
||
constructor() { | ||
balanceOf[msg.sender] = totalSupply; | ||
} | ||
|
||
function setTotalSupply(uint256 amount) public { | ||
totalSupply = amount; | ||
} | ||
|
||
function transfer(address to, uint256 amount) public returns (bool) { | ||
require(balanceOf[msg.sender] >= amount, "Not enough tokens"); | ||
balanceOf[msg.sender] -= amount; | ||
balanceOf[to] += amount; | ||
emit Transfer(msg.sender, to, amount); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0; | ||
|
||
interface IMyToken { | ||
function transfer(address to, uint256 amount) external returns (bool); | ||
} | ||
|
||
contract TokenReceiver { | ||
function receiveAndHoldToken(address token, uint256 amount) external { | ||
IMyToken(token).transfer(msg.sender, amount); | ||
} | ||
} |