Skip to content

Commit

Permalink
Adds example create2 factory
Browse files Browse the repository at this point in the history
  • Loading branch information
nitaliano committed Apr 5, 2024
1 parent 04f0e57 commit 0048573
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/contracts-ecosystem/src/Create2Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity ^0.8.24;

import {StdUtils} from "forge-std/StdUtils.sol";

contract Create2Factory {
event DeployedContract(address indexed addr);

function deploy(bytes memory bytecode, bytes32 salt) public {
address deployedAddress;

assembly {
deployedAddress := create2(0, add(bytecode, 0x20), mload(bytecode), salt)

if iszero(extcodesize(deployedAddress)) { revert(0, 0) }
}

emit DeployedContract(deployedAddress);
}

function computeAddress(bytes memory bytecode, bytes32 salt) public view returns (address) {
bytes32 hash = keccak256(abi.encodePacked(bytes1(0xff), address(this), salt, keccak256(bytecode)));

return address(uint160(uint256(hash)));
}
}

0 comments on commit 0048573

Please sign in to comment.