CREATE2 Deployments

In the Ethereum Virtual Machine, CREATE2 is an opcode that allows for the deployment of smart contracts with deterministic addresses. This is particularly useful in complex decentralized applications (dApps) where the ability to predict the address of a contract before it is deployed can simplify interactions between multiple contracts and improve security.

It is especially valuable in the context of L2, as it enables consistent contract addresses across L1 and L2, simplifying cross-layer interactions.

To facilitate safe usage of the CREATE2 opcode, there is a create2deployer Preinstall deployed on Zircuit, which can be used for deterministic deployment addresses of smart contracts. The Preinstalls are accessible on the addresses specified on Contract Addresses.

create2deployer provides two functions. computeAddress(bytes32 salt, bytes32 codeHash) returns the address where a contract will be stored if deployed through the deploy function. The salt is a 32-byte value used to add additional entropy to the contract creation process and prevent collisions with the default CREATE opcode. The codeHash is the keccak256 hash of the contract's bytecode that will be deployed. This includes the compiled bytecode of the contract itself, potentially combined with any constructor arguments encoded into it. Any change in salt or codeHash will result in a new address. After computing the address, the deploy(uint256 value, bytes32 salt, bytes memory code) function can be used to deploy the contract on the address computed before. The value field in the deploy function is used to specify the amount of Ether (in wei) that should be sent along with the contract deployment transaction. This parameter serves several purposes:

  1. Funding the new contract: If the contract being deployed needs initial Ether to function, this value will be transferred to the new contract's balance upon creation.

  2. Payable constructors: If the contract being deployed has a payable constructor, this value can be used to send Ether to the constructor function during deployment.

The code parameter in the deploy function should be the complete bytecode including encoded constructor arguments.

Apart from this, some smart contract development tools like Foundry automatically make use of a CREATE2 Proxy like this and the steps above do not have to be done manually necessarily.

Last updated