EIPs Explained

ERC-6093 · Final · ERC · 2022-12-06

ERC-6093: Custom errors for commonly-used tokens

A shared set of Solidity custom errors for ERC-20, ERC-721 and ERC-1155 tokens, so wallets and tools can decode why a token call reverted in a consistent, structured way.

What problem does it solve?

ERC-20, ERC-721 and ERC-1155 were finalized before Solidity 0.8.4 introduced custom errors, so none of them say how a failed call should report what went wrong. Every implementation invented its own revert strings ("ERC20: transfer amount exceeds balance", "insufficient balance", ...). Strings cost deployment gas, carry no machine-readable data, and differ from token to token.

ERC-6093 standardizes the error names and arguments, so a wallet or test framework can decode, say, "allowance was 5, needed 10" the same way for every compliant token.

How it works

Every error name follows one grammar:

<Domain><ErrorPrefix><Subject>(<Arguments>)
  • Domain: ERC20, ERC721 or ERC1155. The prefix avoids Solidity declaration clashes between same-named errors with different arguments.
  • ErrorPrefix: why it failed, e.g. Invalid or Insufficient.
  • Subject: what failed in one of the two token actions: transfer (sender, receiver, balance) or approval (approver, operator/spender).
  • Arguments are ordered who, what, why, with tokenId at the end when needed.

Examples: ERC20InsufficientBalance(sender, balance, needed), ERC20InsufficientAllowance(spender, allowance, needed), ERC721NonexistentToken(tokenId), ERC721IncorrectOwner(sender, tokenId, owner), ERC1155MissingApprovalForAll(operator, owner), ERC1155InvalidArrayLength(idsLength, valuesLength).

The EIP defines which error to use in a scenario, not whether to revert; that remains up to each token standard and implementation. Usage rules keep errors precise, e.g. ERC20InvalidSender is for a zero-address sender, while a too-low balance must use ERC20InsufficientBalance.

Sample code

Using the ERC-20 errors in a token:

// 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;
    }
}

Decoding the revert in a client (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}`);
  }
}

Things to know

  • Status is Final. OpenZeppelin Contracts 5.x ships these errors (IERC20Errors, IERC721Errors, IERC1155Errors), so most new tokens built on it emit them.
  • Older and non-upgradeable tokens still revert with strings or their own errors. Clients should tolerate both.
  • Upgrading a token to these errors can break off-chain systems that match on old revert strings.
  • Don't let a UI blindly act on decoded errors from untrusted contracts; a malicious contract can return any error data to nudge users into harmful actions.

#token #errors #solidity #standard

Details

Authors
Ernesto García (@ernestognw), Francisco Giordano (@frangio), Hadrien Croubois (@Amxx)
Status
Final
Type
Standards Track / ERC
Created
2022-12-06
Requires
Required by

Machine-readable: Markdown · JSON