EIPs Explained

ERC-6093 · Final(最终) · ERC · 2022-12-06

ERC-6093: Custom errors for commonly-used tokens

一组适用于 ERC-20、ERC-721 和 ERC-1155 代币的共享 Solidity 自定义错误,让钱包和工具能以一致、结构化的方式解码代币调用为何 revert。

它解决什么问题?

ERC-20、ERC-721 和 ERC-1155 在 Solidity 0.8.4 引入自定义错误之前就已定稿,所以它们都没有规定失败的调用应如何报告出了什么问题。每个实现都发明了自己的 revert 字符串("ERC20: transfer amount exceeds balance"、"insufficient balance" 等)。字符串会消耗部署 gas,不携带机器可读的数据,而且每种代币各不相同。

ERC-6093 标准化了错误名称和参数,使钱包或测试框架能够以同样的方式解码任何合规代币的错误,例如"allowance 为 5,需要 10"。

它如何运作

每个错误名称都遵循同一种语法:

<Domain><ErrorPrefix><Subject>(<Arguments>)
  • Domain:ERC20、ERC721 或 ERC1155。这个前缀可以避免同名但参数不同的错误引发 Solidity 声明冲突。
  • ErrorPrefix:为什么失败,例如 Invalid 或 Insufficient。
  • Subject:在代币的两种操作之一中,什么失败了:转账(发送方、接收方、余额)或授权(授权方、操作员/花费者)。
  • Arguments 的顺序是谁、什么、为什么,需要时把 tokenId 放在最后。

例如:ERC20InsufficientBalance(sender, balance, needed)、ERC20InsufficientAllowance(spender, allowance, needed)、ERC721NonexistentToken(tokenId)、ERC721IncorrectOwner(sender, tokenId, owner)、ERC1155MissingApprovalForAll(operator, owner)、ERC1155InvalidArrayLength(idsLength, valuesLength)。

该 EIP 规定的是在某种场景下使用哪个错误,而不是是否 revert;后者仍由各代币标准和实现决定。使用规则使错误保持精确,例如 ERC20InvalidSender 用于发送方为零地址的情况,而余额不足必须使用 ERC20InsufficientBalance。

示例代码

在代币中使用 ERC-20 错误:

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

interface IERC20Errors {
    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);
    error ERC20InvalidSender(address sender);
    error ERC20InvalidReceiver(address receiver);
    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);
    error ERC20InvalidApprover(address approver);
    error ERC20InvalidSpender(address spender);
}

abstract contract Token is IERC20Errors {
    mapping(address => uint256) internal _balances;

    function _transfer(address from, address to, uint256 value) internal {
        if (from == address(0)) revert ERC20InvalidSender(address(0));
        if (to == address(0)) revert ERC20InvalidReceiver(address(0));
        uint256 bal = _balances[from];
        if (bal < value) revert ERC20InsufficientBalance(from, bal, value);
        unchecked { _balances[from] = bal - value; }
        _balances[to] += value;
    }
}

在客户端解码 revert(ethers v6):

try {
  await token.transfer(to, amount);
} catch (e: any) {
  const err = token.interface.parseError(e.data);
  if (err?.name === "ERC20InsufficientBalance") {
    const [sender, balance, needed] = err.args;
    console.log(`${sender} has ${balance}, needs ${needed}`);
  }
}

需要注意

  • 状态为 Final。OpenZeppelin Contracts 5.x 内置了这些错误(IERC20Errors、IERC721Errors、IERC1155Errors),所以大多数基于它构建的新代币都会抛出这些错误。
  • 较旧的和不可升级的代币仍然使用字符串或自己的错误来 revert。客户端应同时兼容两者。
  • 把代币升级为使用这些错误,可能会破坏那些匹配旧 revert 字符串的链下系统。
  • 不要让 UI 盲目地根据来自不受信任合约的解码错误采取行动;恶意合约可以返回任意错误数据,诱导用户做出有害的操作。

#token #errors #solidity #standard

详细信息

作者
Ernesto García (@ernestognw), Francisco Giordano (@frangio), Hadrien Croubois (@Amxx)
状态
Final(最终)
类型
Standards Track / ERC
创建日期
2022-12-06
依赖的提案
被以下提案依赖

机器可读数据: Markdown · JSON