EIPs Explained

ERC-2535 · Final · ERC · 2020-02-22

ERC-2535: Diamonds, Multi-Facet Proxy

The Diamond standard: one proxy contract that routes each function selector to a different implementation contract (facet), giving a single address with no size limit and fine-grained upgrades.

What problem does it solve?

A contract's deployed code is capped at 24 KB, and a normal contract cannot be changed after deployment. Large systems end up split across many addresses, and classic proxies can swap the whole implementation at once but nothing finer.

A diamond is a single contract address whose external functions are supplied by many separate contracts called facets. Functions can be added, replaced or removed individually, there is no practical size limit, and users always interact with the same address. A diamond can also be made immutable later.

How it works

  • The diamond stores a mapping from function selector → facet address.
  • Any call that is not defined directly in the diamond hits its fallback, which looks up the facet for msg.sig and runs it with delegatecall. The facet's code runs against the diamond's storage, and msg.sender / msg.value are unchanged.
  • diamondCut (interface IDiamondCut) changes the mapping. Each FacetCut names a facet, an action (Add, Replace, Remove) and a list of selectors. Adding an existing selector, replacing with the same facet, or removing an unset selector all revert. Optionally _init / _calldata is run via delegatecall afterwards for initialization.
  • A DiamondCut event must be emitted for every change, including immutable functions at deployment, so the full upgrade history is on-chain.
  • Loupe functions (IDiamondLoupe) let tools inspect the diamond: facets(), facetFunctionSelectors(facet), facetAddresses(), facetAddress(selector).
  • Facets share state by agreeing on storage layout. The standard does not mandate one, but points to patterns such as Diamond Storage and AppStorage.
  • Supporting ERC-165 supportsInterface is optional.

Sample code

The fallback router at the heart of a diamond (simplified):

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

contract MiniDiamond {
    mapping(bytes4 => address) internal selectorToFacet;

    fallback() external payable {
        address facet = selectorToFacet[msg.sig];
        require(facet != address(0), "Diamond: function does not exist");
        assembly {
            calldatacopy(0, 0, calldatasize())
            let ok := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch ok
            case 0 { revert(0, returndatasize()) }
            default { return(0, returndatasize()) }
        }
    }
}

Listing a diamond's facets with ethers v6:

import { Contract, JsonRpcProvider } from "ethers";

const loupe = new Contract(
  diamondAddress,
  ["function facets() view returns (tuple(address facetAddress, bytes4[] functionSelectors)[])"],
  new JsonRpcProvider(process.env.RPC_URL),
);
for (const f of await loupe.facets()) {
  console.log(f.facetAddress, f.functionSelectors.length, "selectors");
}

Things to know

  • Access control is your job. diamondCut can run arbitrary code against the diamond's storage; whoever controls it controls everything. The standard does not define ownership.
  • Storage collisions between facets are the main source of bugs. Use a disciplined layout pattern and never reorder shared structs.
  • Avoid selfdestruct in facets.
  • Block explorers cannot show a diamond's ABI from source alone; tools rely on the loupe functions and DiamondCut events.
  • It builds on the withdrawn ERC-1538. For simpler single-implementation upgrades, a standard proxy using the ERC-1967 storage slots is often enough.
  • The spec links a reference implementation; start from an audited implementation rather than the sketch above.

#proxy #upgradeability #diamond #solidity #architecture

Details

Authors
Nick Mudge (@mudgen)
Status
Final
Type
Standards Track / ERC
Created
2020-02-22
Required by

Machine-readable: Markdown · JSON