0

I have a contract that has a function deployContract(bytes memory runtimeBytecode) I want this function to be able to deploy the bytes sent to it as a new smart contract. I understand that I will have to have the creation byte code inside that function adapts to the runtimeBytecode sent. May be I could use some assembly with the create or create2 opcodes? Any advice is welcome Thank you

1 Answer 1

2
function deploy(bytes memory _data) public returns (address pointer) {

    bytes memory code = abi.encodePacked(
        hex"63",
        uint32(_data.length),
        hex"80_60_0E_60_00_39_60_00_F3",
        _data
    );

    assembly { 
        pointer := create(0, add(code, 32), mload(code)) 
    }
    }


/*
  0x00    0x63         0x63XXXXXX  PUSH4 _data.length  size
  0x01    0x80         0x80        DUP1                size size
  0x02    0x60         0x600e      PUSH1 14            14 size size
  0x03    0x60         0x6000      PUSH1 00            0 14 size size
  0x04    0x39         0x39        CODECOPY            size
  0x05    0x60         0x6000      PUSH1 00            0 size
  0x06    0xf3         0xf3        RETURN
  <CODE>
*/

in this example _data is your runtime code

reference: https://github.com/0xsequence/sstore2/blob/master/contracts/utils/Bytecode.sol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.