// SPDX-License-Identifier: undefined
pragma solidity 0.6.6;
contract Foo{
address public owner;
constructor(address _owner) public{
owner = _owner;
}
}
contract AddressFinder{
function getAddress(address _factory, bytes32 _salt, bytes calldata _bytecode) external pure returns (address){
return address(uint(keccak256(abi.encodePacked(
hex'ff',
_factory,
_salt,
keccak256(_bytecode)
))));
}
}
contract Factory {
event deployed(address addr, uint256 salt);
function deploy(bytes memory _bytecode, uint _salt) public {
address addr;
assembly {
addr := create2(0, add(_bytecode, 0x20), mload(_bytecode), _salt)
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
emit deployed(addr, _salt);
}
function getCreationBytecode(address _owner) public pure returns (bytes memory) {
bytes memory bytecode = type(Foo).creationCode;
return abi.encodePacked(bytecode, abi.encode(_owner));
}
}
context
How to depoly a contract with address have leading zeros like: 0x00000000 ?
Foois the contract I want to deploy, but I want get it’s address before deploy.- deploy
Factorycontract first, get it’s address - call
getCreationBytecodeget bytecode ofFoocontract - deploy
AddressFindercontract - call
AddressFinder.getAddressby pass factory address andFoobytecode, and give a randomsalt, you will get an address in return. - use the same
saltcallFactory.deploy, you will get aFoocontract deployed in the same address you got at step5. Magic! - repeat step5, with other random
salt, until find an address you like.






最新评论