pragma solidity >=0.4.22 <0.7.0; /* 1. deploy Factory 2. call Factory.deployTarget() 3. call getAddr(0); 4. call getAddrWithAddr(deployer_address, 0); 5. compare 3 address a. events from step 2 b. output[12:] from step 3 c. output[12:] from step 4 */ contract Target { address public owner; constructor() public { owner = msg.sender; } function foo() public pure returns (string memory) { return "foo()"; } } contract Factory { uint public salt; address public owner; constructor() public { owner = msg.sender; } event Deployed(address addr, uint256 salt); function deployTarget() public { bytes memory code = type(Target).creationCode; address addr; uint _salt = salt; assembly { addr := create2(0, add(code, 0x20), mload(code), _salt) if iszero(extcodesize(addr)) { revert(0, 0) } } emit Deployed(addr, salt); salt +=1 ; } function getAddrWithAddr(address addr, uint256 salt) public view returns (bytes32) { if (addr == address(0x00)) { addr = address(this); } bytes1 prefix = 0xff; bytes32 codeHash = keccak256(type(Target).creationCode); return keccak256(abi.encodePacked( prefix, addr, salt, codeHash )); } function getAddr(uint256 salt) public view returns (bytes32) { return getAddrWithAddr(address(0x00), salt); } }
0.5.5