Contract Creation Within Contracts: A Deep Dive into CREATE and CREATE2

ยท

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:

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:

Implementation example:

predictedAddress = address(
    uint160(
        uint(
            keccak256(
                abi.encodePacked(
                    bytes1(0xff),
                    address(this),
                    salt,
                    keccak256(type(Pair).creationCode)
                )
            )
        )
    )
);

Usage options:

CREATE vs CREATE2: Key Differences

  1. Address Predictability: CREATE2 allows pre-computation of deployment addresses
  2. Dependence Factors:

    • CREATE depends on creator's nonce
    • CREATE2 depends on bytecode and salt
  3. Functionality: Both methods work identically post-deployment

๐Ÿ‘‰ Learn more about smart contract deployment strategies

Practical Applications

Security Considerations

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:

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.