EIPs Explained

ERC-6224 · Last Call · ERC · 2022-12-27

ERC-6224: Contracts Dependencies Registry

A standard ContractsRegistry plus a Dependant mix-in that give a protocol one on-chain place to register, upgrade and wire together its contracts through dependency injection.

What problem does it solve?

A real protocol is many contracts that point at each other: a vault needs the oracle, the oracle needs the price feed, and so on. Teams usually hard-code those addresses in constructors or initializers, then struggle when one contract is replaced or upgraded: every contract that depends on it must be updated by hand.

ERC-6224 proposes a standard architecture: one ContractsRegistry that knows every contract of the protocol (optionally deploying upgradeable proxies for them), and a Dependant base contract that lets the registry push dependencies into each contract on demand.

How it works

ContractsRegistry (IContractsRegistry):

  • Contracts are identified by a human-readable string name.
  • addContract(name, addr) registers a plain, non-upgradeable contract.
  • addProxyContract(name, impl) / addProxyContractAndCall(name, impl, data) deploy a proxy over an implementation, so the registry can later upgradeContract / upgradeContractAndCall it.
  • justAddProxyContract(name, proxy) adopts an existing proxy (e.g. when migrating to a new registry, which must then hold upgrade rights).
  • getContract(name) reverts if nothing is registered; hasContract(name) checks presence; removeContract(name) deletes.
  • Registering address(0) must revert.
  • injectDependencies(name) / injectDependenciesWithData(name, data) call setDependencies(address(this), data) on the named contract.

Dependant (IDependant):

  • Every standalone protocol contract inherits it and reads its dependencies in setDependencies, not in a constructor or initializer.
  • Only the current injector may call setDependencies and setInjector. The injector starts as zero, so the first call is allowed; the reference implementation then records the caller as injector.
  • The injector address may live in the slot bytes32(uint256(keccak256("eip6224.dependant.slot")) - 1), an ERC-1967-style storage slot.

The extra bytes data argument follows the ERC-5750 pattern for extensible method behaviour.

Sample code

A dependant contract pulling two dependencies from the registry:

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

interface IContractsRegistry {
    function getContract(string memory name) external view returns (address);
}

/// `Dependant` as in the ERC's reference implementation (stores the injector,
/// exposes the `dependant` modifier that checks and then records msg.sender).
import {Dependant} from "./Dependant.sol";

contract Vault is Dependant {
    address public oracle;
    address public treasury;

    function setDependencies(address registry, bytes memory) public override dependant {
        IContractsRegistry r = IContractsRegistry(registry);
        oracle = r.getContract("ORACLE");
        treasury = r.getContract("TREASURY");
    }
}

Deployment flow (pseudocode):

registry.addContract("ORACLE", oracleAddr)
registry.addProxyContract("TREASURY", treasuryImpl)
registry.addProxyContract("VAULT", vaultImpl)
registry.injectDependencies("VAULT")       // Vault reads ORACLE and TREASURY
// later: swap the oracle and re-inject
registry.addContract("ORACLE", newOracle)
registry.injectDependencies("VAULT")

Things to know

  • Status is Last Call: close to final, but details could still change.
  • The registry owner controls every upgrade. Losing or leaking that key compromises the whole protocol; use a multisig or governance.
  • The registry does no compatibility checks between implementations; storage-layout safety on upgrade is your responsibility.
  • Because the injector starts as zero, the first setDependencies call can be front-run. Inject dependencies right after deployment, ideally in the same transaction.
  • Events use non-indexed string names on purpose, trading filterability for readability.

#registry #upgradeability #proxy #dependency-injection #architecture

Details

Authors
Artem Chystiakov (@arvolear)
Status
Last Call
Type
Standards Track / ERC
Created
2022-12-27
Requires

Machine-readable: Markdown · JSON