|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/** |
| 5 | + * @title ICreateX |
| 6 | + * @notice Interface for the CreateX factory contract that provides CREATE3 deployment functionality |
| 7 | + * @dev CreateX is a factory contract that enables deterministic contract deployment using CREATE3 |
| 8 | + * https://github.com/pcaversaccio/createx/tree/main |
| 9 | + */ |
| 10 | +interface ICreateX { |
| 11 | + struct Values { |
| 12 | + uint256 constructorAmount; |
| 13 | + uint256 initCallAmount; |
| 14 | + } |
| 15 | + |
| 16 | + function deployCreate3(bytes32 salt, bytes memory initCode) external payable returns (address); |
| 17 | + function computeCreate3Address( |
| 18 | + bytes32 salt |
| 19 | + ) external view returns (address computedAddress); |
| 20 | + function deployCreate3AndInit( |
| 21 | + bytes32 salt, |
| 22 | + bytes memory initCode, |
| 23 | + bytes memory data, |
| 24 | + Values memory values |
| 25 | + ) external payable returns (address newContract); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @title Create3Library |
| 30 | + * @notice Library providing convenient wrapper functions for CREATE3 deployments via CreateX factory |
| 31 | + * @dev This library simplifies CREATE3 deployments by handling salt generation and factory interactions |
| 32 | + */ |
| 33 | +library Create3Library { |
| 34 | + /// @notice Address of the CreateX factory contract used for CREATE3 deployments |
| 35 | + /// @dev This is the canonical CreateX factory address deployed on multiple chains |
| 36 | + address public constant CREATEX_FACTORY = 0xba5Ed099633D3B313e4D5F7bdc1305d3c28ba5Ed; |
| 37 | + |
| 38 | + /** |
| 39 | + * @notice Deploys a contract using CREATE3 with a deployer-specific salt |
| 40 | + * @dev Combines the deployer address with the provided salt to create a unique deployment salt |
| 41 | + * @param deployer The address of the deployer (used in salt generation) |
| 42 | + * @param salt An 11-byte salt value for deterministic address generation |
| 43 | + * @param code The contract bytecode to deploy |
| 44 | + * @return The address of the deployed contract |
| 45 | + */ |
| 46 | + function deployCreate3(address deployer, bytes11 salt, bytes memory code) public returns (address) { |
| 47 | + return ICreateX(CREATEX_FACTORY).deployCreate3(getSaltForCreate3(salt, deployer), code); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @notice Deploys a contract using CREATE3 and calls an initialization function |
| 52 | + * @dev Combines deployment and initialization in a single transaction |
| 53 | + * @param deployer The address of the deployer (used in salt generation) |
| 54 | + * @param salt An 11-byte salt value for deterministic address generation |
| 55 | + * @param code The contract bytecode to deploy |
| 56 | + * @param data The calldata for the initialization function call |
| 57 | + * @param values The ETH values to send during deployment and initialization |
| 58 | + * @return The address of the deployed and initialized contract |
| 59 | + */ |
| 60 | + function deployCreate3AndInit( |
| 61 | + address deployer, |
| 62 | + bytes11 salt, |
| 63 | + bytes memory code, |
| 64 | + bytes memory data, |
| 65 | + ICreateX.Values memory values |
| 66 | + ) public returns (address) { |
| 67 | + return ICreateX(CREATEX_FACTORY).deployCreate3AndInit(getSaltForCreate3(salt, deployer), code, data, values); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @notice Computes the deterministic address for a CREATE3 deployment |
| 72 | + * @dev Useful for predicting contract addresses before deployment |
| 73 | + * @param salt An 11-byte salt value for address computation |
| 74 | + * @param deployer The address of the deployer (used in salt generation) |
| 75 | + * @return The computed address where the contract would be deployed |
| 76 | + */ |
| 77 | + function computeCreate3Address(bytes11 salt, address deployer) public view returns (address) { |
| 78 | + return ICreateX(CREATEX_FACTORY).computeCreate3Address(getSaltForCreate3(salt, deployer)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @notice Generates a 32-byte salt for CREATE3 deployment by combining deployer address and salt |
| 83 | + * @dev The salt format is: [160-bit deployer address][8-bit zero padding][88-bit salt] |
| 84 | + * @param salt An 11-byte (88-bit) salt value |
| 85 | + * @param deployer The deployer's address (160-bit) |
| 86 | + * @return A 32-byte salt suitable for CREATE3 deployment |
| 87 | + */ |
| 88 | + function getSaltForCreate3(bytes11 salt, address deployer) public pure returns (bytes32) { |
| 89 | + return bytes32(uint256(uint160(deployer)) << 96 | uint256(0x00) << 88 | uint256(uint88(salt))); |
| 90 | + } |
| 91 | +} |
0 commit comments