60 lines
1.9 KiB
Solidity
60 lines
1.9 KiB
Solidity
|
|
// SPDX-License-Identifier: MIT
|
||
|
|
pragma solidity ^0.8.19;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @title CREATE2 Factory
|
||
|
|
* @notice Factory for deploying contracts at deterministic addresses using CREATE2
|
||
|
|
* @dev Based on the canonical CREATE2 factory pattern
|
||
|
|
*/
|
||
|
|
contract CREATE2Factory {
|
||
|
|
event Deployed(address addr, uint256 salt);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @notice Deploy a contract using CREATE2
|
||
|
|
* @param bytecode Contract bytecode
|
||
|
|
* @param salt Salt for deterministic address generation
|
||
|
|
* @return addr Deployed contract address
|
||
|
|
*/
|
||
|
|
function deploy(bytes memory bytecode, uint256 salt) public returns (address addr) {
|
||
|
|
assembly {
|
||
|
|
addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
|
||
|
|
if iszero(extcodesize(addr)) {
|
||
|
|
revert(0, 0)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
emit Deployed(addr, salt);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @notice Compute the address that will be created with CREATE2
|
||
|
|
* @param bytecode Contract bytecode
|
||
|
|
* @param salt Salt for deterministic address generation
|
||
|
|
* @return addr Predicted contract address
|
||
|
|
*/
|
||
|
|
function computeAddress(bytes memory bytecode, uint256 salt) public view returns (address addr) {
|
||
|
|
bytes32 hash = keccak256(
|
||
|
|
abi.encodePacked(bytes1(0xff), address(this), salt, keccak256(bytecode))
|
||
|
|
);
|
||
|
|
addr = address(uint160(uint256(hash)));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @notice Compute the address that will be created with CREATE2 (with deployer)
|
||
|
|
* @param deployer Deployer address
|
||
|
|
* @param bytecode Contract bytecode
|
||
|
|
* @param salt Salt for deterministic address generation
|
||
|
|
* @return addr Predicted contract address
|
||
|
|
*/
|
||
|
|
function computeAddressWithDeployer(
|
||
|
|
address deployer,
|
||
|
|
bytes memory bytecode,
|
||
|
|
uint256 salt
|
||
|
|
) public pure returns (address addr) {
|
||
|
|
bytes32 hash = keccak256(
|
||
|
|
abi.encodePacked(bytes1(0xff), deployer, salt, keccak256(bytecode))
|
||
|
|
);
|
||
|
|
addr = address(uint160(uint256(hash)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|