Skip to content
Verified contractExactMIT

Create2Factory

Address0xddbbda6b76a27cf22950dac71b4251f84f85d5be
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.34

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.34+commit.80d5c536
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,429,317
Creation transaction
0xaec3e1cde4…7bf24aeefa

factory.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// @title SingleUserCreate2Factory
/// @notice Deterministic contract deployment using CREATE2.
/// @dev The factory uses its own address, supplied salt and init code.
///      Intended for one designated user.
contract Create2Factory {

    address public immutable owner;

    error Unauthorized();
    error EmptyBytecode();
    error DeploymentFailed();
    error EmptyRuntimeCode();

    event ContractDeployed(
        address indexed deployedAddress,
        bytes32 indexed salt,
        bytes32 initCodeHash
    );

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        if (msg.sender != owner) revert Unauthorized();
        _;
    }

    /// @notice Calculate the counterfactual deployment address.
    function predictAddress(
        bytes32 salt,
        bytes calldata initCode
    ) external view onlyOwner returns (address predicted) {

        if (initCode.length == 0) revert EmptyBytecode();

        bytes32 hash = keccak256(
            abi.encodePacked(
                bytes1(0xff),
                address(this),
                salt,
                keccak256(initCode)
            )
        );

        predicted = address(uint160(uint256(hash)));
    }

    /// @notice Deploy contract at its deterministic CREATE2 address.
    /// @param salt User-provided CREATE2 salt.
    /// @param initCode Complete contract creation bytecode,
    /// including ABI-encoded constructor arguments.
    function deploy(
        bytes32 salt,
        bytes calldata initCode
    )
        external
        payable
        onlyOwner
        returns (address deployedAddress)
    {
        if (initCode.length == 0) revert EmptyBytecode();

        bytes memory code = initCode;

        assembly ("memory-safe") {
            deployedAddress := create2(
                callvalue(),
                add(code, 0x20),
                mload(code),
                salt
            )
        }

        if (deployedAddress == address(0)) {
            revert DeploymentFailed();
        }

        if (deployedAddress.code.length == 0) {
            revert EmptyRuntimeCode();
        }

        emit ContractDeployed(
            deployedAddress,
            salt,
            keccak256(initCode)
        );
    }

    /// @notice Predict address and deploy in one transaction.
    function predictAndDeploy(
        bytes32 salt,
        bytes calldata initCode
    )
        external
        payable
        onlyOwner
        returns (
            address predictedAddress,
            address deployedAddress
        )
    {
        if (initCode.length == 0) revert EmptyBytecode();

        bytes32 initCodeHash = keccak256(initCode);

        bytes32 hash = keccak256(
            abi.encodePacked(
                bytes1(0xff),
                address(this),
                salt,
                initCodeHash
            )
        );

        predictedAddress = address(uint160(uint256(hash)));

        bytes memory code = initCode;

        assembly ("memory-safe") {
            deployedAddress := create2(
                callvalue(),
                add(code, 0x20),
                mload(code),
                salt
            )
        }

        if (deployedAddress == address(0)) {
            revert DeploymentFailed();
        }

        if (deployedAddress.code.length == 0) {
            revert EmptyRuntimeCode();
        }

        assert(deployedAddress == predictedAddress);

        emit ContractDeployed(
            deployedAddress,
            salt,
            initCodeHash
        );
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.