A Comprehensive Guide to the CREATE2 Opcode in Solidity

Srinivas Joshi
CoinsBench
Published in
4 min readMar 22, 2023

--

Photo by Arthur Franklin on Unsplash

Introduction

The CREATE2 opcode is a powerful tool in the Solidity language that allows for deterministic contract creation at a specific address. It was introduced in the Byzantium hard fork of the Ethereum network and has become a popular choice for many developers due to its flexibility and cost-saving benefits. In this article, I will provide a comprehensive guide to the CREATE2 opcode, covering the why, the when, the how, and an example.

Prerequisite

When using Remix, you simply click the ‘Compile’ button followed by the ‘Deploy’ button to deploy your smart contract to the blockchain. However, have you ever wondered what occurs behind the scenes during this process?

Transaction is sent to the zero address for contract creation

When you click on the ‘Deploy’ button in Remix and select the contract you want to deploy, Remix creates a new transaction with the CREATE opcode. This opcode is used to create a new contract on the Ethereum network, and it takes the bytecode of the contract as its input.

The CREATE opcode is used in the constructor function of the contract, which is the function that is called when the contract is deployed. The constructor function is responsible for initialising the contract’s state variables and setting its initial values.

When the CREATE opcode is executed, it creates a new contract instance with a unique address on the Ethereum network. The address of the newly created contract is returned by the CREATE opcode, and it is stored in the blockchain.

The address of the newly created contract is returned by the CREATE opcode, and it is stored in the blockchain. Smart contracts can be created both by other contracts (using Solidity’s new keyword) and by regular accounts (such as when using Remix). In both cases, the address for the new contract is computed the same way: as a function of the sender’s own address and a nonce.

new_address = hash(sender, nonce)

Every account has an associated nonce: for regular accounts it is increased on every transaction, while for contract accounts it is increased on every contract creation. Nonces cannot be reused, and they must be sequential.

This means it is possible to predict the address where the next created contract will be deployed, but only if no other transactions happen before then — an undesirable property for counterfactual systems.

What is CREATE2 ?

CREATE2 is a Solidity opcode that is used to deploy smart contracts on the Ethereum blockchain. It is similar to the CREATE opcode, which is also used for contract deployment, but there are some key differences.

The CREATE2 opcode takes four inputs:

  • 0xFF, a constant that prevents collisions with CREATE
  • The sender’s own address
  • A salt (an arbitrary value provided by the sender)
  • The to-be-deployed contract’s bytecode

new_address = hash(0xFF, sender, salt, bytecode)

When executed, CREATE2 creates a new contract with a deterministic address that is calculated based on the salt value and the contract creation code. This means that if the same salt value and contract creation code are used, the contract will always have the same address, regardless of which node is used to deploy it.

This deterministic nature of the CREATE2 opcode can be useful in a number of scenarios. For example, it can make it easier to coordinate between different smart contracts or dApps that need to interact with a specific contract, as the address of the contract can be predicted ahead of time.

More reasons to use CREATE2

  1. Improved Interoperability: The CREATE2 opcode provides improved interoperability between different dApps by allowing them to share the same contract address. This is because the contract address is based on the input parameters, which are the same across different dApps.
  2. Easier State Migration: Because the contract address is deterministic, it is easier to migrate the state of a contract from one blockchain to another. This can be useful in the event of a hard fork or when moving from a test network to the main network.
  3. Reduced Gas Costs: Using CREATE2 can result in reduced gas costs for contract deployment, particularly when multiple contracts are being deployed as it eliminates the need for them to be deployed individually.
  4. Easier Contract Upgrades: Using CREATE2 can make it easier to upgrade a contract without changing its address. This can be done by deploying a new contract with the same creation code and salt value as the old contract, which will result in the new contract having the same address as the old contract. This can be particularly useful for upgrading smart contracts that are already in use.

How do I do it ?

Refer this article. It contains a very simple implementation using solidity.

References :

  1. Youtube video
  2. Using OpenZeppelin and CREATE2

I hope this post has been informative and helpful in understanding CREATE2. Thanks for taking the time to read this post. If you found it useful, please share it with your friends and colleagues 🥳

--

--

Frontend Developer | Learning and sharing solidity,EVM knowledge 🚀