1

enter image description here

This is the UniswapV2Factory contract createPair function. I know that create2 opcode takes 4 arguments (value, offset , size , salt)

How add(bytecode,32) can give the offset ?

How mload(bytecode) can give the size of the actual bytecode ?

"bytecode" argument is the actual bytecode, isn't it ?

1
  • mload opcode loads 32 byte from the given location, which is bytecode in this case. So bytecode is location.
    – Emrah
    Commented Jun 2, 2022 at 13:16

1 Answer 1

3

How mload(bytecode) can give the size of the actual bytecode ?

Memory arrays have a specific layout. The length is written on 32 bytes at the address of the variable, which is then followed by the data.

Take this example :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Test {

    function test() public pure returns (uint256 length) {

        // bytes array containing 2 bytes
        bytes memory array =  '\xFF\xFF';

        // mload from `array` which is an address containing the length
        // of the array as a 32 bytes value
        assembly {
            length := mload(array)
        }

        // returns 2
    }
}

How add(bytecode,32) can give the offset ?

The offset in that case can be translated to "the address at which the data starts". Since we have seen above that the length is encoded on 32 bytes at the beginning, and that what follows it is the data. add(bytecode, 32) skips the 32 bytes holding the length value, to get the address where the array data.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Test {

    // the bytes32 type is only for convenience / the example.
    function test() public pure returns (bytes32 data) {

        // bytes array containing 2 bytes
        bytes memory array =  '\xFF\xFF';

        // mload from `array` + 32 which is the address of the data.
        // Skipping the length field encoded on the first 32 bytes
        assembly {
            data := mload(add(array, 32))
        }

        // returns 0xffff000000000000000000000000000000000000000000000000000000000000
    }

}

"bytecode" argument is the actual bytecode, isn't it ?

It should be clear now that the answer is no. the bytecode argument is actually an address of a memory location at which the length of the actual bytecode is written on 32 bytes, which is then followed by the bytecode.

I hope that answers your questions.

3
  • Thanks a lot. This was very explanatory. Do you have any articles,example codes about like this subtle things ? Commented Jun 2, 2022 at 13:54
  • The documentation usually have everything you might be looking for (docs.soliditylang.org/en/v0.8.14/index.html) plus playing around with the language helps quite a bit !
    – hroussille
    Commented Jun 2, 2022 at 15:12
  • @hroussille this staement is not clear: " the bytecode argument is actually an address of a memory location at which the length of the actual bytecode is written on 32 bytes, which is then followed by the bytecode". bytecode is the reference which is then followed by the bytecode.
    – Yilmaz
    Commented Nov 18, 2022 at 14:30

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.