// 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
?
Foo
is the contract I want to deploy, but I want get it’s address before deploy.- deploy
Factory
contract first, get it’s address - call
getCreationBytecode
get bytecode ofFoo
contract - deploy
AddressFinder
contract - call
AddressFinder.getAddress
by pass factory address andFoo
bytecode, and give a randomsalt
, you will get an address in return. - use the same
salt
callFactory.deploy
, you will get aFoo
contract deployed in the same address you got at step5. Magic! - repeat step5, with other random
salt
, until find an address you like.
最新评论