EIPs Explained

ERC-1167 · Final · ERC · 2018-06-22

ERC-1167: Minimal Proxy Contract

A tiny, fixed 45-byte bytecode that forwards every call to one implementation contract, letting you deploy cheap, immutable clones that tools can recognize at a glance.

What problem does it solve?

Many applications deploy the same contract over and over: one wallet per user, one vault per pool, one escrow per deal. Deploying the full bytecode each time is expensive. A proxy that DELEGATECALLs to a shared implementation is much cheaper, but if every project writes its own proxy, explorers and auditors cannot tell what a given address will actually run.

ERC-1167 standardizes one exact proxy bytecode. Because the bytes are fixed (apart from the embedded target address), a tool such as a block explorer can read the code at an address, recognize it as a minimal proxy, and know that its behavior is exactly that of the implementation it points to, including any verified source or audit of that implementation.

How it works

The runtime code is:

363d3d373d3d3d363d73 <20-byte implementation address> 5af43d82803e903d91602b57fd5bf3

In plain terms it:

  1. Copies the full calldata into memory.
  2. DELEGATECALLs the implementation with all remaining gas.
  3. Copies the return data back.
  4. Returns it on success, or reverts with the same data on failure, so revert messages bubble up.

Because it uses DELEGATECALL, the clone keeps its own storage and balance while running the implementation's code. The bytecode relies on EIP-211: RETURNDATASIZE returns 0 before any call, and it is used as a cheaper way to push zero.

The standard deliberately covers only the deployed (runtime) code; how the clone is deployed is left to the factory.

Sample code

A minimal factory using the widely used 55-byte creation code (10-byte constructor + 45-byte runtime):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract CloneFactory {
    event Cloned(address instance);

    function clone(address implementation) external returns (address instance) {
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
            instance := create(0, ptr, 0x37)
        }
        require(instance != address(0), "create failed");
        emit Cloned(instance);
    }
}

In practice, OpenZeppelin's Clones library wraps this, including a CREATE2 variant (EIP-1014) for predictable addresses:

import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";

address wallet = Clones.cloneDeterministic(walletImpl, keccak256(abi.encode(user)));
IWallet(wallet).initialize(user);

Things to know

  • Constructors do not run for clones. Put setup logic in an initialize function and make sure it can be called only once, ideally in the same transaction as the clone is created, or someone else can initialize it first.
  • Clones are not upgradeable: the implementation address is baked into the bytecode. That is by design, so users can trust that the code never changes.
  • Every call pays a small extra cost for the DELEGATECALL hop. Clones save on deployment, not on per-call gas.
  • If the implementation can SELFDESTRUCT or be otherwise disabled, every clone that points to it is affected. Lock down the implementation contract itself.
  • ERC-3448 (MetaProxy) extends this idea by appending immutable metadata to the clone's bytecode.

#proxy #clone #factory #delegatecall #gas

Details

Authors
Peter Murray (@yarrumretep), Nate Welch (@flygoing), Joe Messerman (@JAMesserman)
Status
Final
Type
Standards Track / ERC
Created
2018-06-22
Requires
Required by

Machine-readable: Markdown · JSON