---
eip: 5750
label: ERC-5750
title: General Extensibility for Method Behaviors
status: Final
type: Standards Track
category: ERC
created: 2022-10-04
requires: 165
official: https://eips.ethereum.org/EIPS/eip-5750
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5750.md
canonical: https://eips.huatuo.xyz/zh/eip-5750/
lang: zh
tldr: 一项约定：方法的最后一个 bytes 参数保留用于未来的行为扩展，并且传入空 bytes 时，其行为必须与不带该参数的重载完全一致。
tags: [interface, extensibility, calldata, convention]
---

# ERC-5750: General Extensibility for Method Behaviors

> Designating last param of dynamically sized bytes to be used for behavior extensions of methods.

**TL;DR:** 一项约定：方法的最后一个 bytes 参数保留用于未来的行为扩展，并且传入空 bytes 时，其行为必须与不带该参数的重载完全一致。

## 它解决什么问题？

接口一旦发布，添加功能就会很尴尬。你要么发明一个新方法（新名称、新参数、新的集成工作），要么试图给现有参数塞进额外含义。当你之后想给调用附加签名、nonce、salt、承诺或人类可读的理由时，这两种办法都难以扩展。

ERC-5750 规范了一个简单的出口：一个明确保留给扩展的末尾 `bytes` 参数。若干现有标准已经具有这种形态，因此该 ERC 主要是给一个已有的习惯赋予名称和规则。

## 它如何运作

合规性按**方法**而不是按合约来定义：

1. **最后一个参数是 `bytes`** 的方法*有资格*：
   `function methodName(type1 a, type2 b, ..., bytes data)`。
2. 如果方法有资格，**并且**最后那个 `bytes` 被指定用于行为扩展（而不是用于其核心功能），则该方法是*合规*的。
3. 如果该方法有一个同名、前面参数相同但不带 `bytes` 的重载兄弟方法，那么以**空 bytes 调用合规版本时，其行为必须与该兄弟方法完全相同**。

规范中已经合规的方法示例：

- [ERC-721](/eip-721/) 的 `safeTransferFrom(from, to, tokenId, bytes data)`
- [ERC-1155](/eip-1155/) 的 `safeTransferFrom(...)` 和 `safeBatchTransferFrom(...)`
- [ERC-777](/eip-777/) 的 `send(to, amount, data)` 和 `burn(amount, data)`

以及**不**合规的方法，因为它们末尾的 `bytes` 承担着核心作用：

- [ERC-1271](/eip-1271/) 的 `isValidSignature(bytes32 hash, bytes signature)`：这个 bytes *就是*签名。
- ERC-2535 的 `diamondCut(..., bytes _calldata)`：这个 bytes 是初始化调用。

这些方法可以通过增加一个带有额外末尾 `bytes data` 的重载而变得合规。

把该字段放在最后，可以保持与 Solidity ABI 布局的兼容，并且 `bytes` 可以承载任何编码后的负载。

## 示例代码

一个带有扩展槽的投票函数。空的 `data` 与普通重载行为一致；非空的 `data` 携带一个可选的理由：

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

contract Ballot {
    event Voted(address indexed voter, uint256 proposal, string reason);
    mapping(address => bool) public hasVoted;

    // Sibling without extension data
    function vote(uint256 proposal) external {
        _vote(proposal, "");
    }

    // ERC-5750 compliant: last bytes param is for extensions.
    // With data.length == 0 it MUST behave exactly like vote(proposal).
    function vote(uint256 proposal, bytes calldata data) external {
        string memory reason = data.length == 0 ? "" : abi.decode(data, (string));
        _vote(proposal, reason);
    }

    function _vote(uint256 proposal, string memory reason) internal {
        require(!hasVoted[msg.sender], "already voted");
        hasVoted[msg.sender] = true;
        emit Voted(msg.sender, proposal, reason);
    }
}
```

从 ethers v6 调用（重载函数需要完整签名）：

```ts
import { AbiCoder } from "ethers";
const data = AbiCoder.defaultAbiCoder().encode(["string"], ["Supports the roadmap"]);
await ballot["vote(uint256,bytes)"](1n, data);
```

## 需要注意

- **状态：Final。**
- 该 ERC 定义的是扩展数据放*在哪里*，而不是其中包含*什么*。每个扩展（签名、如 [ERC-5732](/eip-5732/) 中的提交-揭示等）必须定义自己的编码，并遵循自己的安全实践。
- `data` 中的任何内容一旦进入内存池就是公开的。规范警告不要在其中放入未加密的个人信息，并提醒实现者考虑重放和抢跑问题。
- "空 bytes 等同于兄弟方法"这条规则，使该模式对现有集成方是安全的；打破它会使该方法不再合规。


---

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