EIP-1014 · Final · Core · 2018-04-20
EIP-1014: Skinny CREATE2
Adds the CREATE2 opcode, which deploys a contract at an address computed from the deployer, a salt and the init code, so the address is known before deployment.
What problem does it solve?
With the original CREATE opcode, a new contract's address is keccak256(rlp([sender, nonce])). It depends on how many contracts the sender has already deployed, so you cannot reliably know or reserve an address in advance.
Many designs need exactly that. State channels need to refer to a contract that will only be deployed if there is a dispute ("counterfactual" contracts). Wallets and factories want to give a user an address that can receive funds before any code is deployed there. CREATE2, added in the Constantinople hard fork (2019), makes this possible.
How it works
CREATE2 (0xf5) takes four stack arguments: value, memory_start, memory_length, salt. It works like CREATE, except for how the address is computed:
address = keccak256( 0xff ++ deployer ++ salt ++ keccak256(init_code) )[12:]
0xff: one byte. RLP-encodedCREATEpreimages never start with it in practice, so the two schemes cannot collide.deployer: the 20-byte address runningCREATE2.salt: any 32-byte value chosen by the deployer.init_code: the creation bytecode (constructor plus constructor arguments), not the runtime code.
The preimage is always 85 bytes. Gas is the same as CREATE, plus a hashing cost of 6 gas per 32-byte word of init code (the same rate as SHA3/KECCAK256), charged up front.
If the computed address already has code or a nonzero nonce, the creation fails. That rule is EIP-684.
Sample code
Deploying with a salt in Solidity, and predicting the address:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Child { uint256 public x; constructor(uint256 _x) { x = _x; } }
contract Factory {
function deploy(bytes32 salt, uint256 x) external returns (address) {
return address(new Child{salt: salt}(x)); // uses CREATE2
}
function predict(bytes32 salt, uint256 x) external view returns (address) {
bytes32 h = keccak256(abi.encodePacked(
bytes1(0xff), address(this), salt,
keccak256(abi.encodePacked(type(Child).creationCode, abi.encode(x)))
));
return address(uint160(uint256(h)));
}
}
Off-chain with ethers v6, checked against Example 0 from the spec:
import { getCreate2Address, keccak256, ZeroAddress, ZeroHash } from "ethers";
getCreate2Address(ZeroAddress, ZeroHash, keccak256("0x00"));
// "0x4D1A2e2bB4F88F0250f26Ffff098B0b30B26BF38"
Things to know
- Changing the constructor arguments changes the init code, and so changes the address. Treat the arguments as part of the "address recipe".
CREATE2is the foundation of counterfactual smart accounts (for example in ERC-4337), minimal-proxy clone factories (ERC-1167), and deterministic cross-chain deployers such as ERC-2470, which is Stagnant.- Combined with
SELFDESTRUCT, it once allowed "metamorphic" contracts that redeploy different code at the same address. EIP-6780 largely shut this down. - Anyone can deploy to your predicted address only if they can run the same factory with the same salt and init code. Factories should bind the salt to the caller or to the owner when that matters.