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-7751, which standardizes smart contract error reporting through a custom error called WrappedError, making it easier to identify the contracts and functions involved in 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-7751 proposes a standard for handling bubbled-up reverts in Ethereum smart contracts.

Bubbling up occurs when a contract calls a function in another contract and propagates the revert data returned by the failed call.

When a smart contract calls another contract and the called contract reverts, the original revert data can be propagated to the caller.

However, that data alone may not reveal where the error originated or why the operation failed.

ERC-7751 introduces a dedicated custom error, WrappedError, which wraps the original revert data with additional context before propagating it to the next level.

This gives analysis tools such as Etherscan and Tenderly a consistent format for interpreting and displaying errors.

Motivation

When revert data from a failed external call is propagated unchanged, several problems arise.

  • Insufficient context It can be difficult to determine which contract call failed and which layer caused the error.
  • Difficult debugging A failure indication alone may require considerable investigation to locate its origin.
  • Inconvenient tooling Infrastructure tools such as Etherscan and Tenderly may have difficulty identifying the call hierarchy associated with a revert when only the revert data is available.

ERC-7751 addresses these problems through a standardized custom error.

Wrapping revert data in WrappedError offers the following benefits.

  • Developers can identify failed calls more easily, improving debugging efficiency.
  • Infrastructure providers can interpret reverts using a consistent format and present more informative error messages and call traces.

The proposal aims to improve the clarity and usability of revert information throughout the ecosystem.

Specification

Definition of the WrappedError Error

A contract wrapping a revert under this standard must revert using an error with the following signature.

solidity
error WrappedError(address target, bytes4 selector, bytes reason, bytes details);

The error selector is 0x90bfb865. A selector is derived from the hash of a function or error signature and identifies its encoded representation.

Parameters

WrappedError contains four parameters.

Parameter Description
target Address of the called contract associated with the revert.
selector Selector of the called function. For a plain ETH transfer without calldata, use bytes4(0).
reason Raw bytes containing the original revert data.
details Additional context about the revert. This can be empty or contain an ABI-encoded custom error.

The details Field

The optional details field provides additional context.

  • When additional information is unnecessary, use empty bytes.
  • When additional information is useful, include an ABI-encoded custom error defined by the calling contract.

This can communicate details such as which user, amount, or operation was associated with the failure, beyond the original revert reason.

Additional Notes

By including the called contract, function selector, raw revert bytes, and optional context, WrappedError provides richer information about a failed execution.

Instead of reporting only that an operation failed, a wrapper can identify the external call involved and preserve its original revert data.

The standardized format also supports nested bubbled-up reverts. When several contracts call one another and propagate wrapped errors, tools can recursively decode the nested revert data.

Tools such as Etherscan and Foundry can use this format to present human-readable error information, making contract interactions easier to understand and supporting debugging and error handling.

Compatibility

ERC-7751 does not require changes to existing deployed contracts. Developers can adopt the wrapping convention incrementally where appropriate.

Test Cases

The following example tests nested reverts wrapped using WrappedError, involving the Token, Vault, and Router contracts.

solidity
// SPDX-License-Identifier: CC0-1.0
pragma solidity 0.8.26;

contract Token {
    mapping(address => uint256) public balanceOf;

    event Transfer(address indexed sender, address indexed recipient, uint amount);

    function transfer(address to, uint256 amount) external returns (bool) {
        require(balanceOf[msg.sender] >= amount, "insufficient balance");
        balanceOf[msg.sender] -= amount;
        balanceOf[to] += amount;
        emit Transfer(msg.sender, to, amount);
        return true;
    }
}

contract Vault {
    Token token;

    error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
    error ERC20TransferFailed(address recipient);

    constructor(Token token_) {
        token = token_;
    }

    function withdraw(address to, uint256 amount) external {
        try token.transfer(to, amount) {} catch (bytes memory error) {
            revert WrappedError(
                address(token),
                token.transfer.selector,
                error,
                abi.encodeWithSelector(ERC20TransferFailed.selector, to)
            );
        }
    }
}

contract Router {
    Vault vault;

    error WrappedError(address target, bytes4 selector, bytes reason, bytes details);

    constructor(Vault vault_) {
        vault = vault_;
    }

    function withdraw(uint256 amount) external {
        try vault.withdraw(msg.sender, amount) {} catch (bytes memory error) {
            revert WrappedError(address(vault), vault.withdraw.selector, error, "");
        }
    }
}

contract Test {
    function test_BubbledNestedReverts(uint256 amount) external {
        Token token = new Token();
        Vault vault = new Vault(token);
        Router router = new Router(vault);

        try router.withdraw(amount) {} catch (bytes memory thrownError) {
            bytes memory expectedError = abi.encodeWithSelector(
                Router.WrappedError.selector,
                address(vault),
                vault.withdraw.selector,
                abi.encodeWithSelector(
                    Vault.WrappedError.selector,
                    address(token),
                    token.transfer.selector,
                    abi.encodeWithSignature("Error(string)", "insufficient balance"),
                    abi.encodeWithSelector(Vault.ERC20TransferFailed.selector, address(this))
                ),
                ""
            );
            assert(keccak256(thrownError) == keccak256(expectedError));
        }
    }
}

The test illustrates how reverts crossing several contracts can be propagated and identified through nested WrappedError values.

Reference Implementation

The following example catches a revert from an external call and wraps the returned error data.

solidity
contract Foo {

    error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
    error MyCustomError(uint256 x);

    function foo(address to, bytes memory data) external {
        (bool success, bytes memory returnData) = to.call(data);
        if (!success) {
            revert WrappedError(
                to,
                bytes4(data),
                returnData,
                abi.encodeWithSelector(MyCustomError.selector, 42)
            );
        }
    }
}

When the call fails, the code reverts with WrappedError and includes an encoded custom error, MyCustomError, in the details field.

Security

The mechanism has several security considerations.

  • Contracts may intentionally suppress revert data or interrupt its propagation.
  • A contract may fabricate the contents of WrappedError, so the reported information is not necessarily accurate.

Consequently, WrappedError should be treated as information useful for debugging and tooling, not as independently authenticated evidence of what occurred.

Citation

Daniel Gretzke (@gretzke), Sara Reynolds (@snreynolds), Alice Henshaw (@hensha256), Marko Veniger marko.veniger@tenderly.co, Hadrien Croubois (@Amxx), "ERC-7751: Wrapping of bubbled up reverts," Ethereum Improvement Proposals, no. 7751, August 2024. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7751.

Conclusion

ERC-7751 standardizes the wrapping of propagated smart contract reverts through WrappedError, providing additional context about failed external calls.

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

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/