-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate2.sol
More file actions
31 lines (25 loc) · 895 Bytes
/
Copy pathCreate2.sol
File metadata and controls
31 lines (25 loc) · 895 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
contract Create2Factory {
event ContractDeployed(address addr, uint256 salt);
function deploy(bytes memory bytecode, uint256 salt) public returns (address) {
address addr;
require(bytecode.length != 0, "Bytecode cannot be empty");
assembly {
addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
}
require(addr != address(0), "CREATE2 failed");
emit ContractDeployed(addr, salt);
return addr;
}
function computeAddress(bytes memory bytecode, uint256 salt) public view returns (address) {
bytes32 hash = keccak256(bytecode);
bytes32 data = keccak256(abi.encodePacked(
bytes1(0xff),
address(this),
salt,
hash
));
return address(uint160(uint256(data)));
}
}