Introduction
In Ethereum's ecosystem, both Externally Owned Accounts (EOAs) and smart contracts can create new accounts. This article explores the two primary methods smart contracts use to create accounts: CREATE and CREATE2.
Understanding CREATE
The CREATE method involves directly instantiating a new contract using the new keyword. The address calculation follows this formula:
New Address = hash(creator's address, nonce)Key characteristics:
noncerepresents the total number of transactions sent by the creatorThe syntax in Solidity:
Contract x = new Contract{value: _value}(params)- Payable constructors can receive ETH during creation
- Requires knowledge of the target contract's code
Exploring CREATE2
CREATE2 enables deterministic address calculation independent of future blockchain events. The formula is:
New Address = hash("0xFF", creator's address, salt, hash(bytecode))Components:
0xFF: Constant to prevent collision with CREATEsalt: Arbitrary value chosen by creatorbytecode: Target contract's bytecode
Implementation example:
predictedAddress = address(
uint160(
uint(
keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
salt,
keccak256(type(Pair).creationCode)
)
)
)
)
);Usage options:
Solidity syntax:
Contract x = new Contract{salt: _salt, value: _value}(params)Inline assembly:
assembly { addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt) }
CREATE vs CREATE2: Key Differences
- Address Predictability: CREATE2 allows pre-computation of deployment addresses
Dependence Factors:
- CREATE depends on creator's nonce
- CREATE2 depends on bytecode and salt
- Functionality: Both methods work identically post-deployment
๐ Learn more about smart contract deployment strategies
Practical Applications
- Upgradeable Contracts: CREATE2 enables seamless contract upgrades
- State Channels: Predictable addresses for off-chain transactions
- Contract Factories: Efficient deployment of multiple contract instances
Security Considerations
- Salt Management: Ensure unique salts to prevent address collision
- Bytecode Verification: Validate bytecode matches precomputed hash
- Deployment Safety: Verify successful deployment through return checks
FAQ Section
Why would I use CREATE2 instead of CREATE?
CREATE2 provides address determinism, allowing you to compute contract addresses before deployment and ensuring consistent addresses regardless of blockchain state changes.
Can I change a contract's bytecode after using CREATE2?
No, the CREATE2-generated address is permanently tied to the specific bytecode hash used in its calculation. Any bytecode change requires a new salt.
How do I choose an appropriate salt value?
The salt can be any arbitrary value, but best practice suggests using:
- Unique identifiers
- Random numbers
- Hashes of relevant data
- Sequential numbers in factory patterns
Is CREATE2 more expensive than CREATE?
Gas costs are nearly identical, with CREATE2 requiring only slightly more computation for the additional hash operations.
Can CREATE2 addresses collide with CREATE addresses?
The 0xFF prefix prevents collisions between CREATE and CREATE2 generated addresses.
๐ Explore advanced contract deployment techniques
Conclusion
Both CREATE and CREATE2 serve vital roles in Ethereum's contract deployment ecosystem. While CREATE remains simpler for basic deployments, CREATE2's deterministic addressing enables sophisticated patterns like upgradeable contracts and state channels. Understanding both methods empowers developers to choose the right tool for their specific use case.