I am trying to get the bytecode from a deployed contract, then by using that bytecode, use the CREATE opcode to deploy a new instance of that contract.
Here is the code I'm trying out (but to no avail)
Implementation contract (already deployed) :
contract Implementation {
address public owner;
constructor(address _owner) {
owner = _owner;
}
}
Deployer contract :
contract Deployer {
address public implementation;
constructor(address _implementation) {
implementation = _implementation;
}
function deployContract() public returns (address deployed) {
bytes memory bytecode = abi.encodePacked(implementation.code, abi.encode(msg.sender));
assembly{
deployed := create(0, add(bytecode, 0x20), mload(bytecode))
if iszero(extcodesize(deployed)) {
revert(0, 0)
}
}
}
After deploying both contracts (Implementation contract first) then setting the Implementation contract as the address public implementation
on the deployer contract,
calling deployContract()
will always revert.
Any pointers would be much appreciated.