EIPs Explained

ERC-2470 · Stagnant · ERC · 2020-01-15

ERC-2470: Singleton Factory

A keyless, permissionless CREATE2 factory deployed at the same address on every chain, so any contract can be given the same deterministic address everywhere.

What problem does it solve?

Some contracts should exist exactly once, at the same address on every chain: registries like ERC-1820, shared libraries, or wallet infrastructure. Deploying them with an ordinary account gives an address that depends on that account's nonce, which differs from chain to chain.

ERC-2470 proposes one shared Singleton Factory: a tiny contract that wraps CREATE2. If the factory itself sits at the same address on every chain, then anything it deploys with the same init code and salt also lands at the same address everywhere.

How it works

The factory has a single function:

function deploy(bytes memory _initCode, bytes32 _salt) public returns (address payable createdContract);

It calls create2(0, initCode, salt). No ETH can be forwarded, so the result depends only on the code and the salt.

Getting the factory to the same address everywhere uses Nick's method (keyless deployment):

  1. A deployment transaction is built with nonce 0, a fixed 100 gwei gas price and a 247,000 gas limit, and without EIP-155 chain-ID protection so it is valid on any chain.
  2. Its signature is not real: r and s are the human-chosen values 0x247000 and 0x2470. Recovering the "sender" gives an account whose private key nobody can know.
  3. Anyone funds that one-time sender with 0.0247 ETH and broadcasts the raw transaction. The factory appears at the same address on that chain, and since the sender's nonce is now used, no other code can ever be put there.

The spec publishes the raw transaction, the single-use sender 0xBb6e024b9cFFACB947A71991E386681B1Cd1477D, and the resulting factory address 0xce0042B868300000d44A59004Da54A005ffdcf9f.

The address of anything deployed through it follows the CREATE2 rule:

address = keccak256(0xff ++ factory ++ salt ++ keccak256(initCode))[12:]

Sample code

Predict and deploy through the factory (ethers v6):

import { Contract, Wallet, JsonRpcProvider, getCreate2Address, keccak256, ZeroHash } from "ethers";

const FACTORY = "0xce0042B868300000d44A59004Da54A005ffdcf9f";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);

const initCode = "0x..."; // contract bytecode + ABI-encoded constructor args
const salt = ZeroHash;     // spec suggests zero salt unless you want a vanity address

const predicted = getCreate2Address(FACTORY, salt, keccak256(initCode));
console.log("will deploy to", predicted);

if ((await provider.getCode(predicted)) === "0x") {
  const factory = new Contract(FACTORY, ["function deploy(bytes,bytes32) returns (address)"], wallet);
  await (await factory.deploy(initCode, salt)).wait();
}

Things to know

  • Status is Stagnant. The idea is sound, but the factory must first be deployed on each chain by someone paying 0.0247 ETH to the one-time sender.
  • Because the deploy transaction lacks EIP-155 protection and has a fixed gas price and gas limit, it can fail on chains or RPC nodes that reject unprotected transactions, or whose gas pricing differs. Check with getCode before relying on the factory on a given chain.
  • Contracts deployed through it must not use msg.sender in the constructor: the factory would become their owner. Pass the owner as a constructor argument instead.
  • Same address does not mean same state. A contract that must behave differently per chain can read the chain ID (block.chainid).
  • Anyone can call deploy, so anyone can front-run your deployment with the same init code and salt. The result is identical code at the expected address, which is usually fine, but constructor arguments are part of the init code and are public.

#create2 #factory #deployment #singleton #deterministic

Details

Authors
Ricardo Guilherme Schmidt (@3esmit)
Status
Stagnant
Type
Standards Track / ERC
Created
2020-01-15
Requires

Machine-readable: Markdown · JSON