Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-838, which proposes standardizing smart contract error information so users and developers can more easily identify the cause of a failure.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs, BIPs, SLIPs, CAIPs, ENSIPs, RFCs, and ACPs.

Overview

ERC-838 defines how potential error conditions are encoded in a smart contract's JSON ABI (Application Binary Interface). An ABI describes how a smart contract communicates with external programs such as web applications and wallets.

ERC-838 proposes a standardized representation of errors that external applications and libraries such as web3.js can reconstruct.

High-level languages such as Solidity are expected to provide syntax for declaring and throwing errors. A compiler encodes those errors into the reason data of the REVERT opcode, enabling clients to reconstruct them.

The objective is to establish common rules for communicating smart contract errors in a human-readable and machine-processable form.

Motivation

Clearly communicating why an Ethereum transaction failed is important for users and developers.

Historically, a failed transaction could simply return a revert result without explaining the underlying problem. The introduction of the REVERT opcode enabled contract developers to return encoded error reasons as strings.

At the time of the proposal, a Solidity implementation for passing strings to REVERT was under review.

Returning strings alone is insufficient. Without standardized error structures and formats, clients and development tools have difficulty interpreting their meaning.

ERC-838 aims to provide the following benefits.

Objective Description
Standardized error format A common format improves reusability across smart contracts.
Development efficiency Compilers and tools can process error information automatically, without manually parsing strings.
Improved user experience Frontends and wallets can display understandable error information.
Easier debugging Identify the functions or conditions responsible for failures during testing and production use.

Specification

ERC-838 specifies how error information is represented in a smart contract's JSON ABI. A conforming compiler, such as a Solidity compiler, must include error declarations alongside functions and events in the JSON ABI.

Errors represent failures and unmet conditions in a structured form. External tools and clients such as web3.js can interpret this information and communicate the cause to users.

Error Object Structure

Each error is represented in JSON and must contain the following three elements.

Key Type Description
type string Always "error", identifying an error definition.
name string Error name or identifier, such as "InsufficientBalance".
arguments Array of objects Defines the error parameters in the same form as function inputs. Each argument has a name and type.

For example, an insufficient-balance error can be defined as follows.

json
{
  "type": "error",
  "name": "InsufficientBalance",
  "arguments": [
    { "name": "amount", "type": "uint256" }
  ]
}

This allows errors to be treated as structured data.

Error Selector Calculation and Encoding

Like functions and events, errors can have a selector. A selector is part of a hash derived from the error signature, such as InsufficientBalance(), used to identify the error.

The selector must be included in the REVERT reason data. Clients can use it to look up the corresponding error.

The proposal describes encoding error arguments using RLP (Recursive Length Prefix) encoding and compares this with encoding function return values.

The exact encoding of selectors and arguments was left for future definition. The historical Solidity implementation discussed in the proposal reserved room for extensions by prefixing a free-form string with uint256(0).

Example Usage in Solidity

The proposal is designed to be used naturally in high-level languages such as Solidity.

js
contract MyToken {
  error InsufficientFunds(uint256 amount);

  function transfer(address _to, uint256 _amount) {
    if (balances[msg.sender] <= _amount)
       throw InsufficientFunds(_amount);
    ...
  }
  ...
}

The example declares an InsufficientFunds error and throws it from the transfer function when the balance is insufficient. Structured ABI error information allows frontends and libraries to interpret the failure.

Possible Extensions

The specification also considers future extensions.

Defining Error Messages with NatSpec Comments

Solidity uses NatSpec (Ethereum Natural Specification Format) comments for documentation. Adding a NatSpec comment above an error declaration could specify a default error message.

Error arguments could also be interpolated into the message. In the following example, the amount value is inserted automatically.

solidity
/// @notice You don't have enough funds to transfer `amount`.
error InsufficientFunds(uint256 amount);

This could dynamically generate a specific message describing an insufficient transfer amount.

Declaring Errors That a Function May Throw

Functions could explicitly declare which errors callers may encounter. This information could be added to the function's JSON ABI under an errors key.

solidity
function transfer(address _to, uint256 _amount) throws(InsufficientFunds);

Callers, including clients and other contracts, could then discover potential errors before invoking a function. This resembles typed exception handling in API design.

Error Overloading Considerations

Future extensions might support overloading errors with the same name but different arguments. Differences in argument types or order produce different signature hashes and therefore different selectors, subject to selector collisions.

Implementations should consider potential future extensions when designing error names and parameters.

Citation

Federico Bond (@federicobond), Renan Rodrigues de Souza (@RenanSouza2), "ERC-838: ABI specification for REVERT reason string [DRAFT]," Ethereum Improvement Proposals, no. 838, August 2020. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-838.

Conclusion

ERC-838 proposes standardizing smart contract error information to make failures easier for users and developers to understand.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/