Create2 Factory

Deterministic contract deployment factory using the CREATE2 opcode

The Create2 Factory is a minimal proxy contract that enables deterministic contract deployment using the CREATE2 opcode. This allows developers to deploy contracts to addresses that can be computed in advance, regardless of the deployer's nonce or transaction order.

Contract Address: 0x4e59b44847b379578588920ca78fbf26c0b4956c Deployment Status: Default preinstall Gas Cost: Deployment cost + ~32,000 gas overhead

Key Features

  • Deterministic Addresses: Compute contract addresses before deployment

  • Cross-chain Consistency: Same address on all chains with the same bytecode and salt

  • Minimal Implementation: Only 45 bytes of optimized assembly code

  • No Storage or State: Pure function contract with no storage variables

How It Works

The CREATE2 opcode computes addresses using:

address = keccak256(0xff ++ deployerAddress ++ salt ++ keccak256(bytecode))[12:]

This formula ensures that the same inputs always produce the same address, enabling:

  • Pre-funding of contract addresses before deployment

  • Cross-chain address consistency

  • Gasless contract deployment patterns

Usage

Deploy a Contract

```javascript "Ethers.js Create2 Contract Deployment" expandable import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider("YOUR_RPC_URL"); const signer = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);

// Create2 Factory address const CREATE2_FACTORY = "0x4e59b44847b379578588920ca78fbf26c0b4956c";

// Your contract bytecode (including constructor args) const bytecode = "0x608060405234801561001057600080fd5b50..."; // Your compiled bytecode

// Choose a salt (32 bytes) const salt = ethers.id("my-unique-salt-v1"); // Or use ethers.randomBytes(32)

// Deploy using Create2 async function deployWithCreate2() { // Compute the deployment address const deployAddress = ethers.getCreate2Address( CREATE2_FACTORY, salt, ethers.keccak256(bytecode) ); console.log("Contract will be deployed to:", deployAddress); // Send deployment transaction const tx = await signer.sendTransaction({ to: CREATE2_FACTORY, data: salt + bytecode.slice(2), // Concatenate salt and bytecode gasLimit: 3000000, // Adjust based on your contract }); console.log("Deployment tx:", tx.hash); await tx.wait(); console.log("Contract deployed to:", deployAddress); return deployAddress;

}

deployWithCreate2();

Compute Deployment Address

You can calculate the deployment address without actually deploying:

Common Use Cases

Deploy contracts to the same address across multiple chains:

Interact with contracts before they're deployed:

Predictable upgrade addresses using versioned salts:

Build factory contracts with deterministic child addresses:

Implementation Details

The Create2 factory contract is extremely minimal:

This assembly code:

  1. Reads the salt (32 bytes) and bytecode from calldata

  2. Uses the CREATE2 opcode to deploy the contract

  3. Returns the deployed contract address

Best Practices

**Security Considerations**:

  • Always verify bytecode integrity before deployment

  • Be aware that anyone can deploy to a CREATE2 address if they have the bytecode and salt

  • Consider using access controls in your factory contracts

Salt Selection

Choose salts carefully for your use case:

Gas Optimization

  • The Create2 factory adds ~32,000 gas overhead

  • Batch deployments in a single transaction when possible

  • Pre-compute addresses to avoid on-chain calculations

Comparison with CREATE

Aspect
CREATE
CREATE2

Address Calculation

Based on deployer + nonce

Based on deployer + salt + bytecode

Predictability

Requires knowing nonce

Fully deterministic

Cross-chain

Different addresses

Same address possible

Gas Cost

Baseline

~32,000 gas overhead

Use Case

Standard deployments

Deterministic deployments

Troubleshooting

Common issues and solutions:

Issue
Solution

"Create2 deployment failed"

Ensure sufficient gas and correct bytecode format

Address mismatch

Verify salt and bytecode are identical to computation

Contract already deployed

CREATE2 can't deploy to the same address twice

Invalid bytecode

Ensure bytecode includes constructor arguments if needed

Example: Multi-chain Token Deployment

Deploy an ERC20 token to the same address across multiple chains:

Further Reading