1

I'm trying to return the revert reason of a revert that is triggered during a CREATE opcode.

I'm currently using

    function deployCode(bytes memory bytecode) internal returns (address payable addr) {
        assembly {
            addr := create(0, add(bytecode, 0x20), mload(bytecode))

            if iszero(extcodesize(addr)) {
                returndatacopy(0, 0, returndatasize())
                revert(0, returndatasize())
            }
        }
    }

But no matter what, returndatasize() is always 0. I am stepping through the opcodes with a debugger and see that in the createcode call, there is an intermediary frame. The last steps are the following (with the first 4 bytes in memory showing my exact revert reason!)

Create opcodes

The next few codes are the following

0 returndatasize

returndatasize seems to be 0. Does that mean that it's not possible to retrieve any returndata from a create call if it is unsuccessful? Or am I missing something obvious?

1 Answer 1

1

Well, it ain't pretty, but... for testing purposes I decided to encode the revert message in the returned bytecode during contract deployment. I made it clear by returning the STOP opcode 00 followed by the revert message and read it out as follows and then bubbled up the reason.

    function deployCode(bytes memory bytecode) internal returns (address payable addr) {
        assembly {
            addr := create(0, add(bytecode, 0x20), mload(bytecode))

            // can't get any custom messages out of here...

            let ext_code_size := extcodesize(addr)

            if iszero(ext_code_size) {
                revert(0, 0)
            }

            // encode reason in bytecode just so I can run my tests...
            // by checking if first byte equals 00 (STOP)

            extcodecopy(addr, 0, 0, 1)

            if iszero(mload(0)) {
                let revert_size := sub(ext_code_size, 1)
                extcodecopy(addr, 0, 1, revert_size)
                revert(0, revert_size)
            }
        }
    }

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.