CREATE2
Create2¶
CREATE2 is an opcode introduced in Ethereum's Constantinople upgrade (EIP-1014) that allows the deterministic computation of a contract's address before deployment.
The Problem It Solves¶
When deploying a contract using the traditional CREATE opcode, the new contract's address is determined by the sender's address (sender) and the sender's current nonce (keccak256(sender, nonce)). This means that if the nonce changes (e.g., from sending other transactions), the predicted address becomes invalid. CREATE2 makes contract address generation independent of the nonce, instead relying on the contract's initialization code, the sender's address, and a user-provided "salt" value. This allows users to safely send funds to or interact with that future address even before the contract is deployed (counterfactual interaction).
Implementation Mechanisms and Principles¶
The CREATE2 address calculation formula is as follows:
0xff: A constant used to prevent collisions with addresses generated by the CREATE opcode. * senderAddress: The address of the factory contract (or caller) deploying the contract. * salt: A 32-byte random number or specific value specified by the developer. * init_code: The initialization bytecode of the contract to be deployed (including constructor logic). As long as these three parameters are determined, the contract's address is fixed regardless of when deployment occurs.
Key Features¶
- Deterministic Address: The contract address can be pre-computed off-chain.
- Counterfactual Instantiation: Allows interaction with contracts that do not yet exist on-chain (e.g., depositing funds), as long as there is confidence the contract will be correctly deployed in the future.
- State Channels: In state channels, participants can sign state updates claiming that a specific arbitration contract will be deployed if a dispute arises.
CREATE2guarantees the consistency of that arbitration contract address. - Metamorphic Contracts: By combining
SELFDESTRUCTandCREATE2, it is possible to redeploy contracts with different code at the same address (by changing the logic ininit_codewhile keepingkeccak256(init_code)unchanged, typically using proxy patterns or specific techniques), enabling "in-place upgrades" or code modifications.
Recommended Reading¶
- EIP-1014: Skinny CREATE2
- Metamorphic Smart Contracts
Related Concepts¶
- Nonce: Transaction counter, a key parameter in traditional contract address generation.
- Factory Pattern: The factory pattern typically uses
CREATE2to deploy child contracts at deterministic addresses. - Salt: Random data used to perturb hash results.