---
eip: 897
label: ERC-897
title: DelegateProxy
status: Stagnant
type: Standards Track
category: ERC
created: 2018-02-21
official: https://eips.ethereum.org/EIPS/eip-897
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-897.md
canonical: https://eips.huatuo.xyz/zh/eip-897/
lang: zh
tldr: 一个早期且已停滞的接口，让代理合约能够报告自己是代理、是固定型还是可升级型，以及逻辑所在的位置，使工具能够看穿它们；这一角色后来由 ERC-1967 的存储槽接替。
tags: [proxy, upgradeability, delegatecall, interface, stagnant]
---

# ERC-897: DelegateProxy

> Proxy contracts are being increasingly used as both as an upgradeability mechanism and a way to save gas when deploying many instances of a particular contract. This standard proposes a set of interfaces for proxies to signal how they work and what their main implementation is.

**TL;DR:** 一个早期且已停滞的接口，让代理合约能够报告自己是代理、是固定型还是可升级型，以及逻辑所在的位置，使工具能够看穿它们；这一角色后来由 ERC-1967 的存储槽接替。

## 它解决什么问题？

到 2018 年，**委托代理（delegate proxy）**已经很常见。一个小合约用 `DELEGATECALL`（[EIP-7](/eip-7/)）把每次调用转发给持有真正逻辑的实现合约。项目使用它们有两个原因：在不改变地址的情况下升级逻辑，以及部署同一个合约的大量廉价克隆。

每个项目构建代理的方式都不同。区块浏览器或钱包查看某个代理时，只能看到转发代码，没有标准方式找到真正合约的代码和 ABI。EIP-897 由 Aragon 和 Zeppelin 的作者撰写，它并不规定代理内部如何工作，而是规定代理如何**描述自己**。

## 它如何运作

符合标准的代理会公开两个函数：

```solidity
interface ERCProxy {
    function proxyType() external pure returns (uint256 proxyTypeId);
    function implementation() external view returns (address codeAddr);
}
```

- `implementation()` 返回代理此刻针对这条消息将要委托的地址。
- `proxyType()` 是 `pure` 函数，所以它的返回值永远不会变。它表明这是哪种代理：
  - **`1`，转发代理。** 一旦返回了非零的实现地址，该地址就永远不会改变。这适合克隆。
  - **`2`，可升级代理。** 实现可以根据代理或实现中的逻辑发生变化。
- 如果某个合约不响应 `proxyType()`，或返回 `0`，就将其视为**不是代理**。

规范点明的主要好处是**源码验证**。浏览器可以为代理地址显示实现的代码和 ABI，而不只是转发的桩代码。

## 示例代码

一个实现了该接口的最小可升级代理（没有加强访问控制，仅作演示）：

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

contract UpgradeableProxy {
    address private _impl;  // real proxies keep this in a collision-safe slot (ERC-1967)
    address private _admin;

    constructor(address impl) { _impl = impl; _admin = msg.sender; }

    function proxyType() external pure returns (uint256) { return 2; }
    function implementation() external view returns (address) { return _impl; }

    function upgradeTo(address impl) external {
        require(msg.sender == _admin, "not admin");
        _impl = impl;
    }

    fallback() external payable {
        address impl = _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()) }
        }
    }
}
```

在链下检测代理（ethers v6）：

```ts
import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const p = new Contract(addr, [
  "function proxyType() view returns (uint256)",
  "function implementation() view returns (address)",
], provider);
const kind = await p.proxyType().catch(() => 0n);   // 0 = not a proxy
if (kind !== 0n) console.log("logic at", await p.implementation());
```

## 需要注意

- **该提案状态为 Stagnant。** 广泛使用的代理库（例如 OpenZeppelin 的）转而把实现地址存放在 [ERC-1967](/eip-1967/) 定义的标准存储槽中，工具可以直接读取。
- 代理上的公开函数可能与实现中的函数**冲突**，因为选择器一旦匹配，调用就不会被转发。ERC-1967 风格的代理正是出于这个原因不添加公开函数。
- 对于不可升级的克隆，请参阅 [ERC-1167](/eip-1167/) 最小代理。对于把升级逻辑放在实现中的方案，请参阅 [ERC-1822](/eip-1822/)（UUPS）。
- 像示例那样把 `_impl` 存放在普通存储槽中，可能与实现自身的存储发生冲突。生产环境请使用经过审计的库。


---

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