---
eip: 6093
label: ERC-6093
title: Custom errors for commonly-used tokens
status: Final
type: Standards Track
category: ERC
created: 2022-12-06
requires: 20, 721, 1155
official: https://eips.ethereum.org/EIPS/eip-6093
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-6093.md
canonical: https://eips.huatuo.xyz/ja/eip-6093/
lang: ja
tldr: ERC-20、ERC-721、ERC-1155 トークンで共通に使える Solidity カスタムエラーのセット。ウォレットやツールが、トークン呼び出しが revert した理由を、一貫した構造化された方法でデコードできるようにする。
tags: [token, errors, solidity, standard]
---

# ERC-6093: Custom errors for commonly-used tokens

> Lists custom errors for common token implementations

**TL;DR:** ERC-20、ERC-721、ERC-1155 トークンで共通に使える Solidity カスタムエラーのセット。ウォレットやツールが、トークン呼び出しが revert した理由を、一貫した構造化された方法でデコードできるようにする。

## 何を解決するのか

[ERC-20](/eip-20/)、[ERC-721](/eip-721/)、[ERC-1155](/eip-1155/) は、Solidity 0.8.4 でカスタムエラーが導入される前に確定したため、失敗した呼び出しが何が悪かったのかをどう報告すべきかについて、どれも何も定めていません。実装ごとに独自の revert 文字列（"ERC20: transfer amount exceeds balance"、"insufficient balance" など）が作られました。文字列はデプロイ時のガスがかかり、機械可読なデータを持たず、トークンごとに異なります。

ERC-6093 はエラー名と引数を標準化します。これにより、ウォレットやテストフレームワークは、たとえば「allowance は 5 で、必要なのは 10 だった」という情報を、準拠するすべてのトークンで同じ方法でデコードできます。

## 仕組み

すべてのエラー名は、1 つの文法に従います。

```
<Domain><ErrorPrefix><Subject>(<Arguments>)
```

- **Domain**: `ERC20`、`ERC721`、`ERC1155` のいずれか。引数が異なる同名のエラーによる Solidity の宣言の衝突を、このプレフィックスで避けます。
- **ErrorPrefix**: *なぜ*失敗したか。たとえば `Invalid` や `Insufficient`。
- **Subject**: トークンの 2 つのアクション（転送: 送信者・受信者・残高、承認: 承認者・オペレーター/スペンダー）のうち、*何が*失敗したか。
- **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 のエラーを使う例です。

```solidity
// 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）。

```ts
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`）を同梱しているため、それを使って作られた新しいトークンの多くがこれらを emit します。
- 古いトークンやアップグレード不可能なトークンは、依然として文字列や独自のエラーで revert します。クライアントは両方を許容すべきです。
- トークンをこれらのエラーにアップグレードすると、古い revert 文字列に一致させているオフチェーンのシステムが壊れる可能性があります。
- 信頼できないコントラクトからデコードしたエラーに、UI が盲目的に反応してはいけません。悪意のあるコントラクトは任意のエラーデータを返して、ユーザーを有害な操作に誘導できます。


---

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