-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate2Deployer.sol
More file actions
34 lines (30 loc) · 992 Bytes
/
Create2Deployer.sol
File metadata and controls
34 lines (30 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
//import {CREATE3} from "solady/utils/CREATE3.sol";
contract SimpleStore {
uint256 public value;
constructor(uint256 _value) {
value = _value;
}
}
contract Create2Deployer {
event Deployed(address addr);
// 使用 CREATE2 部署 SimpleStore 合约
function deploy(bytes32 salt, bytes memory bytecode) public returns (address addr) {
assembly {
addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
if iszero(extcodesize(addr)) { revert(0, 0) }
}
emit Deployed(addr);
}
// 预测 SimpleStore 的 CREATE2 地址
function predictAddress(bytes32 salt, bytes memory bytecode) public view returns (address) {
bytes32 hash = keccak256(bytecode);
return address(uint160(uint(keccak256(abi.encodePacked(
bytes1(0xff),
address(this),
salt,
hash
)))));
}
}