---
eip: 5568
label: ERC-5568
title: Well-Known Format for Required Actions
status: Review
type: Standards Track
category: ERC
created: 2022-08-31
requires: 140
official: https://eips.ethereum.org/EIPS/eip-5568
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5568.md
canonical: https://eips.huatuo.xyz/zh/eip-5568/
lang: zh
tldr: 一种让合约通过一个众所周知的 view 函数和自定义 revert 错误，告诉钱包“用户必须先做某件事”（批准代币、发起 HTTP 请求、轮换密钥）的标准方法。
tags: [wallet, revert, ux, interface]
---

# ERC-5568: Well-Known Format for Required Actions

> Signal to wallets that an action is needed through a well-known function and revert reason

**TL;DR:** 一种让合约通过一个众所周知的 view 函数和自定义 revert 错误，告诉钱包“用户必须先做某件事”（批准代币、发起 HTTP 请求、轮换密钥）的标准方法。

## 它解决什么问题？

交易常常会失败，除非用户先做某件事：批准 [ERC-20](/eip-20/) 的 allowance、从服务器获取数据、签署某些内容。目前这些知识存在于每个 dApp 的前端中，前端必须硬编码每一个前置条件。

ERC-5568 让**合约本身**以紧凑、机器可读的形式告诉钱包需要什么操作，从而使钱包能够通用地处理。

## 它如何运作

分为两部分：

1. **检测函数**，钱包在发送前会模拟调用它：

   ```solidity
   function walletSignal24(bytes32 selector, bytes function_data)
       view returns (uint24 instruction_id, bytes instruction_data);
   ```

   `instruction_id = 0` 表示不需要任何操作。非零值指明一条指令，`instruction_data` 保存其参数。

2. **自定义 revert 错误**，在未执行所需操作时使用（revert 数据依赖 [EIP-140](/eip-140/) 的 `REVERT`）：

   ```solidity
   error WalletSignal24(uint24 instruction_id, bytes instruction_data);
   ```

规则：

- 指令的 `instruction_id` 应当是定义它的 ERC 的编号，每个 ERC 最多定义一条指令。该 ERC 同时定义数据布局。
- 钱包在广播之前必须在本地模拟 `walletSignal24`，并把它视为可能改变状态。
- 如果钱包不支持该指令，必须显示错误。否则它会执行该指令并重新评估交易，除非指令声明不需要这样做。
- 无效或无法解析的指令必须产生错误，且不重新评估。

不使用 ERC-165；钱包只需调用该函数即可检测是否支持。

## 示例代码

钱包端在发送交易前的检查（ethers v6）：

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

const provider = new JsonRpcProvider(process.env.RPC_URL);
const abi = [
  "function walletSignal24(bytes32 selector, bytes function_data) view returns (uint24, bytes)",
];

async function requiredAction(to: string, data: string) {
  const c = new Contract(to, abi, provider);
  const selector = data.slice(0, 10).padEnd(66, "0"); // 4-byte selector, right-padded to bytes32
  try {
    const [id, payload] = await c.walletSignal24.staticCall(selector, "0x" + data.slice(10));
    return id === 0n ? null : { id: Number(id), payload };
  } catch {
    return null; // contract does not implement ERC-5568
  }
}
```

规范没有明确说明选择器如何打包进 `bytes32`，也没有说明 `function_data` 是否包含它；这个示例只是一种合理的解读。

## 需要注意

- 状态为 **Review**；格式仍可能变化。
- 这个 ERC 只定义信号传递的外壳。具体的指令（及其数据格式）必须由其他 ERC 定义。
- [ERC-3668](/eip-3668/)（CCIP Read）用另一种基于 revert 的机制解决一个相关问题，即获取链下数据；两者可以共存。
- 规范认为与现有自定义错误发生冲突的可能性不大，且大多无害。


---

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