2

Below is the contract that is used to deploy other contracts using only bytecode.

I tried to deploy the contract using deployBytecode function, the contract gets deployed but only 0x is showing as bytecode. Pls, anyone help to solve this issue.

pragma solidity ^0.5.5;

contract DeployBytecode {
    
    // Create contract from bytecode
    function deployBytecode(bytes memory bytecode) public returns (address) {
        address retval;
        assembly{
            mstore(0x0, bytecode)
            retval := create(0,0xa0, calldatasize)
        }
        return retval;
   }
}

Deployed contract bytecode

1 Answer 1

2

First of all: it might be helpful to be more specific on your question. Is this question about the DeployBytecode contract or about the contract that should be deployed and what bytecode are you using?

Looking at your deployBytecode function (especially the assembly block) there are some things that don't look correct:

  1. mstore is used to put 32-bytes into memory. The first parameter is the target position in memory and the second parameter is the value. In your case you store the pointer to the start of bytecode in memory position 0.
  2. create takes 3 parameters: value, location of deployment code and length of deployment code. In your case you fix the location of your deployment code to 0xa0, which is not always correct. Also you use calldatasize as the length of the deployment code, but you are not actually using calldata, so this is also not correct.

I would propose the following changes:

  1. remove the mstore
  2. Change the create to have the following parameters: 0 (for the value), add(bytecode, 32) (for the data location), mload(bytecode) (for the data length).

Lets take a detailed look at the data location and data length parameter:

To understand these values it is important to understand how a bytes is stored in Solidity. The variable value of bytes is the location of the data in memory. In memory the bytes is stored by having first the length of the bytes and then the data, this results in the following schema: <32-bytes length><data> at the location where bytecode points to.

Now if we want to use the data with create we first point to the start of the raw data, which is after the length. Therefore we add 32 (the space required for the length) to the location stored in the bytecode variable. This is the first parameter. For the second parameter we read the length from memory using mload. As the length is the first 32 bytes at the location of bytecode we can read the length by calling mload(bytecode).

A full example that is used in production can be found here: https://github.com/gnosis/safe-contracts/blob/1.3.0-libs.0/contracts/libraries/CreateCall.sol#L22-L29

More information about assembly/yul can be found in the Solidity docs

3
  • I've updated the DeployBytecode contract with the changes you've suggested, but still I end up with the new deployed contract - bytecode being 0x. I'm trying to deploy new contract with the same exact bytecode as this contract - bscscan.com/address/… Not sure what I'm doing wrong here, contract gets deployed, but no bytecode ? Sorry for not adding more info in the Question. Please help me. Thanks.
    – Sam
    Dec 26, 2021 at 18:11
  • Your deployment bytecode is different from the runtime bytecode. The deployment bytecode is the input of the creation transaction: bscscan.com/tx/…
    – Richard
    Dec 27, 2021 at 8:25
  • I'm having trouble deploying this contract - bscscan.com/tx/… , with other contracts I'm having success. Pls shed some light on this. Thanks
    – Sam
    Dec 29, 2021 at 9:07

Your Answer

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

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