ERC-2470 Singleton factory
Stagnant · Other · created 2020-01-15
A universally usable CREATE2 factory standard that lets a contract be deployed to the same address on any chain, given the same bytecode and salt.
Why it is needed
There are registry-style contracts like EIP-1820 or EIP-2429 that need to have the same address on every chain. These contracts are usually deployed using Nick's method: an unsigned-style transaction is created from an account whose private key no one knows, and the same transaction is broadcast identically to every chain. Without a common factory, every project has to redesign this keyless deployment transaction from scratch, and even a small difference in gas value or deployment procedure risks producing a different resulting address on each chain. Also, if deployer-dependent values like msg.sender are used in the constructor, the initial state will differ across chains, causing confusion where the same address behaves differently.
What to implement
function deploy(bytes memory _initCode, bytes32 _salt) public returns (address payable createdContract): It calls CREATE2 with the received initialization code (_initCode) and salt to create the contract at a deterministic address and returns that address.
In depth
How deploy() works
The deploy function executes create2(0, add(_initCode, 0x20), mload(_initCode), _salt) in inline assembly. The first argument, value, is fixed at 0, so no ether can be sent along with creation. add(_initCode, 0x20) and mload(_initCode) are the standard way to get the actual data start position and length of a byte array, respectively. The return value createdContract is the address of the created contract.
How the factory itself is deployed keylessly
The factory contract itself is deployed using Nick's method. The sender address is reverse-derived by putting human-chosen values v=27, r=0x247000, s=0x2470 in place of signature values on a transaction with nonce 0, gasPrice 100 Gwei, value 0, and gasLimit 247000. This transaction does not use EIP-155, so it can be replayed identically on any chain. 0.0247 ether must first be sent to this reverse-derived single-use account (0xBb6e024b9cFFACB947A71991E386681B1Cd1477D), and then the transaction must be broadcast. Because the gasLimit is twice the actual amount needed, the chance of failure from opcode price changes is low, and the remaining roughly 0.01 ETH stays permanently locked in that account.
Formula for computing the resulting address
The address of the contract to be created can also be precomputed for other contracts on the chain using the formula address(keccak256(bytes1(0xff), 0xce0042B868300000d44A59004Da54A005ffdcf9f, _salt, keccak256(_code)) << 96).
Role of the salt
_salt is an arbitrary value that other projects can change in order to produce a desired vanity address even with the same initCode. It is specified that bytes(0) should be used if a vanity address is not needed.
Why msg.sender should not be used in the constructor
Contracts created by the factory must not use msg.sender in the constructor, and all initial values must be passed in as constructor parameters. This is a deliberate design choice to block a problem where, if a post-creation callback were allowed to set initial state, contracts would have the same address on every chain but different initial states.
Current status: Stagnant
This proposal's status is marked Stagnant, meaning it is no longer being actively discussed and has stalled. Its content may change or may never be adopted.
Things to watch out for
- Contracts that need a different address per chain: Contracts that do not support using the same address on every chain, or that need a different address per chain, should be implemented to branch safely in the constructor by comparing the chain ID using EIP-1344.
- Chain ID signaling for account contracts: Account contracts are singletons from the user's perspective, but wallets must use EIP-1191 to signal the intended chain ID.
- Prohibition on using msg.sender in the constructor: Contracts deployed via the factory should not use msg.sender in the constructor and should use constructor parameters instead. Otherwise, the factory contract itself would become the controller or sole owner of that contract.