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:
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
.
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:
- remove the
mstore
- 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