---
eip: 7546
label: ERC-7546
title: Upgradeable Clone for Scalable Contracts
status: Draft
type: Standards Track
category: ERC
created: 2023-10-25
requires: 165, 1967, 7201
official: https://eips.ethereum.org/EIPS/eip-7546
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7546.md
canonical: https://eips.huatuo.xyz/zh/eip-7546/
lang: zh
tldr: 一种草案代理模式，把按函数升级（类似 Diamond）与信标式共享升级结合起来，使大量克隆代理可以在一笔交易中逐函数升级。
tags: [proxy, upgradeability, clone, factory, solidity]
---

# ERC-7546: Upgradeable Clone for Scalable Contracts

> An upgradeable, cloneable, horizontally extensible proxy pattern.

**TL;DR:** 一种草案代理模式，把按函数升级（类似 Diamond）与信标式共享升级结合起来，使大量克隆代理可以在一笔交易中逐函数升级。

## 它解决什么问题？

现有的代理标准各自覆盖了一部分场景：

| 需求 | 现有方案 |
| --- | --- |
| 廉价克隆，无升级 | [ERC-1167](/eip-1167/) 最小代理 |
| 升级整个合约 | UUPS / 透明代理 |
| 一次升级许多克隆 | [ERC-1967](/eip-1967/) 的信标代理 |
| 升级单个函数 | [ERC-2535](/eip-2535/) Diamond |

缺少的是**面向克隆工厂的函数级升级**。Diamond 把选择器到 facet 的映射表存放在每个代理内部，所以升级 1,000 个克隆的 Diamond 就意味着 1,000 次更新。ERC-7546，即 *Upgradeable Clone Standard*（UCS），填补了这个空白。按函数拆分逻辑也有助于应对 24,576 字节的合约大小限制。

## 它如何运作

一个逻辑上的合约被拆成三部分：

1. **Proxy**：保存状态（余额、存储）。它把 Dictionary 的地址存放在 ERC-1967 风格的槽位 `bytes32(uint256(keccak256('erc7546.proxy.dictionary')) - 1)` 中。每次调用时，它向 Dictionary 查询 `getImplementation(msg.sig)`，并对结果执行 delegatecall。为避免选择器冲突，它不应定义自己的 external 函数，并在 Dictionary 变更时触发 `DictionaryUpgraded`。
2. **Dictionary**：一个共享注册表，把函数选择器映射到实现地址，提供 `getImplementation`，通常还有 `setImplementation`，并触发 `ImplementationUpgraded`。它也可以为整个集合回答 ERC-165 的 `supportsInterface`。
3. **Function contracts**：逻辑部分，每个包含一个或多个函数。它们通过 delegatecall 在代理的存储中运行。

由于许多代理可以指向同一个 Dictionary，一次 `setImplementation` 调用就能同时为**所有**代理升级一个函数，就像信标，但粒度是函数级。

## 示例代码

一个最小的 UCS 代理（从规范的设计简化而来）：

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

interface IDictionary {
    function getImplementation(bytes4 selector) external view returns (address);
}

contract UCSProxy {
    // bytes32(uint256(keccak256("erc7546.proxy.dictionary")) - 1)
    bytes32 internal constant DICTIONARY_SLOT =
        0x267691be3525af8a813d30db0c9e2bad08f63baecf6dceb85e2cf3676cff56f4;

    event DictionaryUpgraded(address dictionary);

    constructor(address dictionary, bytes memory initData) {
        assembly { sstore(DICTIONARY_SLOT, dictionary) }
        emit DictionaryUpgraded(dictionary);
        if (initData.length > 0) {
            (bool ok, ) = address(this).call(initData);
            require(ok, "init failed");
        }
    }

    fallback() external payable {
        address dict;
        assembly { dict := sload(DICTIONARY_SLOT) }
        address impl = IDictionary(dict).getImplementation(msg.sig);
        require(impl != address(0), "no impl");
        assembly {
            calldatacopy(0, 0, calldatasize())
            let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch ok case 0 { revert(0, returndatasize()) } default { return(0, returndatasize()) }
        }
    }
}
```

## 需要注意

- 状态为 **Draft**。
- 所有 function contract 共享代理的存储，因此布局冲突是真实存在的风险。规范没有规定具体方法，但建议使用带命名空间的存储，例如 [ERC-7201](/eip-7201/)。
- 谁控制 Dictionary，谁就控制所有相连代理的代码。不要把代理接到由不受信任的管理员运营的 Dictionary 上，并考虑允许切换到另一个 Dictionary。
- 把 Dictionary 地址硬编码（immutable）可能会让你永久无法变更，如果 Dictionary 的管理员与你不是同一方。
- 只在真正实现了某个选择器的 function contract 上注册该选择器；不匹配会导致调用失败。代理并不打算通过 `DELEGATECALL` 来调用。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7546
