# CREATE2 Factory

By [m0t0k1ch1](https://paragraph.com/@m0t0k1ch1) · 2021-12-13

---

    pragma solidity ^0.8.10;
    
    contract Factory {
        event Created(address indexed addr);
    
        function create(
            bytes memory code,
            uint256 salt,
            bytes calldata data
        ) external returns (address) {
            address addr;
    
            assembly {
                addr := create2(0, add(code, 0x20), mload(code), salt)
                if iszero(extcodesize(addr)) {
                    revert(0, 0)
                }
            }
    
            if (data.length > 0) {
                (bool success, ) = addr.call(data);
                if (!success) {
                    assembly {
                        returndatacopy(0, 0, returndatasize())
                        revert(0, returndatasize())
                    }
                }
            }
    
            emit Created(addr);
    
            return addr;
        }
    }

---

*Originally published on [m0t0k1ch1](https://paragraph.com/@m0t0k1ch1/create2-factory)*
