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 ERC7821, which proposes a mechanism for batch transactions using EIP7702.

This article translates, summarizes, and expands on the specification linked below.

I have also covered a range of other EIPs.

Overview

ERC7821 proposes a minimal batch execution interface for delegation. Delegation is a mechanism through which one contract provides execution logic for another. Introducing this interface standardizes batch execution across contracts and makes it more efficient.

Motivation

EIP7702 makes atomic batch execution possible for EOAs. Previously, an EOA would typically initiate a single operation per transaction. With EIP7702, multiple operations can be grouped into a single transaction.

For more on EIP7702, see the following article.

[EIP7702] How EOAs Gain Contract Account Functionality

EIP7702 is expected to lead to different delegation implementations from various providers. If these implementations proliferate without a common interface, the way each delegation is used may differ, creating compatibility issues. A uniform execution interface helps ensure compatibility and interoperability across delegation implementations.

Delegation under EIP7702 also calls for caution. Frequently switching delegations can increase security risks and incur additional gas costs. A common execution interface helps minimize how often delegations need to change.

Relationship to EIP5792

ERC7821 is also related to the wallet_sendCalls API defined in EIP5792. EIP5792 defines how applications can request that wallets send batches of calls. ERC7821 makes it possible to detect whether an EOA supports atomic batch execution and construct the corresponding calldata (the request data sent to a contract).

For more on EIP5792, see the following article.

[EIP5792] JSON-RPC Methods for Batch Transactions

Benefits of atomic batch execution

Batch execution can significantly reduce transaction latency and costs. Traditionally, multiple operations had to be submitted separately, incurring gas costs for each transaction and increasing the time needed to complete the workflow. With EIP7702 delegation, these operations can instead be grouped into a single transaction, reducing both latency and costs.

Specification

IERC7821.sol
/// @dev Interface for minimal batch executor.
interface IERC7821 {
    /// @dev Call struct for the `execute` function.
    struct Call {
        address target; // Replaced with `address(this)` if `address(0)`.
        uint256 value; // Amount of native currency (i.e. Ether) to send.
        bytes data; // Calldata to send with the call.
    }

    /// @dev Executes the calls in `executionData`.
    /// Reverts and bubbles up error if any call fails.
    ///
    /// `executionData` encoding (single batch):
    /// - If `opData` is empty, `executionData` is simply `abi.encode(calls)`.
    /// - Else, `executionData` is `abi.encode(calls, opData)`.
    ///   See: https://eips.ethereum.org/EIPS/eip-7579
    ///
    /// `executionData` encoding (batch of batches):
    /// - `executionData` is `abi.encode(bytes[])`, where each element in `bytes[]`
    ///   is an `executionData` for a single batch.
    ///
    /// Supported modes:
    /// - `0x01000000000000000000...`: Single batch. Does not support optional `opData`.
    /// - `0x01000000000078210001...`: Single batch. Supports optional `opData`.
    /// - `0x01000000000078210002...`: Batch of batches. The mode is optional.
    ///
    /// For the "batch of batches" mode, each batch will be recursively passed into
    /// `execute` internally with mode `0x01000000000078210001...`.
    /// Useful for passing in batches signed by different signers.
    ///
    /// Authorization checks:
    /// - If `opData` is empty, the implementation SHOULD require that
    ///   `msg.sender == address(this)`.
    /// - If `opData` is not empty, the implementation SHOULD use the signature
    ///   encoded in `opData` to determine if the caller can perform the execution.
    /// - If `msg.sender` is an authorized entry point, then `execute` MAY accept
    ///   calls from the entry point, and MAY use `opData` for specialized logic.
    ///
    /// `opData` may be used to store additional data for authentication,
    /// paymaster data, gas limits, etc.
    ///
    /// For calldata compression efficiency, if a Call.target is `address(0)`,
    /// it will be replaced with `address(this)`.
    function execute(bytes32 mode, bytes calldata executionData)
        external
        payable;

    /// @dev Provided for execution mode support detection.
    /// Only returns true for:
    /// - `0x01000000000000000000...`: Single batch. Does not support optional `opData`.
    /// - `0x01000000000078210001...`: Single batch. Supports optional `opData`.
    /// - `0x01000000000078210002...`: Batch of batches. The mode is optional.
    function supportsExecutionMode(bytes32 mode) external view returns (bool);
}

The ERC7821 interface, IERC7821, provides a mechanism for contracts to execute multiple calls together.

Call

The interface uses the Call struct to specify what to execute.

  • target
    • Specifies the address to call.
    • If address(0) is specified, it is automatically replaced with address(this), making the executing contract itself the target.
  • value
    • Specifies the amount of native currency to send.
  • data
    • Contains the calldata needed for the contract function call.

This makes it possible to group multiple calls into a single operation.

execute

The main function for batch execution. Passing executionData to this function allows multiple operations to be executed together.

There are two ways to encode executionData.

  • Single batch execution (one batch)

If opData (optional operation data) is absent, the encoding is abi.encode(calls). If opData is present, the encoding is abi.encode(calls, opData).

  • Executing multiple batches together (batch-of-batches mode)

executionData is encoded as abi.encode(bytes[]), with each element of the bytes[] representing the executionData for an individual batch. Each batch is passed recursively to execute for processing.

Execution modes

Three modes are defined.

  • 0x01000000000000000000...
    • Processes a single batch.
    • Does not support opData.
  • 0x01000000000078210001...
    • Processes a single batch and supports optional opData.
  • 0x01000000000078210002...
    • Processes multiple batches together.
    • This mode is optional; some implementations may not support it.

Authorization

The interface calls for different authorization checks depending on whether opData is present.

  • If opData is absent, the implementation should require msg.sender to be address(this).
    • This check allows execution only when the contract itself calls execute.
  • If opData is present, the implementation should verify the signature data in opData to determine whether the caller is authorized to execute the batch.
  • If msg.sender is a trusted entry point (a designated address authorized to access the contract externally), execution through that entry point may also be permitted.

In addition to authentication data, opData can contain Paymaster data (for sponsoring gas fees), gas limits, and other information.

supportsExecutionMode

This function determines whether a particular execution mode is supported. A return value of true indicates that the mode can be executed.

The function is used to check support for the following three modes.

  • 0x01000000000000000000... (single batch, no opData)
  • 0x01000000000078210001... (single batch, optional opData)
  • 0x01000000000078210002... (multiple batches)

Support for the batch-of-batches mode (0x01000000000078210002...) is optional. If it is not supported, the function must return false.

Frontend recommendations

Wallet and application frontends using this interface should first call supportsExecutionMode(bytes32(0x0100000000000000000000000000000000000000000000000000000000000000)) and confirm that the return value is true.

They can then submit a batch transaction by calling execute(bytes32(0x0100000000000000000000000000000000000000000000000000000000000000), abi.encode(calls)).

This lets an EOA use delegation to execute a token approval (approve) and a swap (swap) efficiently as part of one workflow.

Additional notes

Goals and design rationale

ERC7821 aims to keep the specification as simple as possible to encourage widespread adoption. A minimal design lowers the barrier to implementation. In particular, the proposal seeks to enable decentralized exchanges (DEXs) to support the EOA approval (approve) plus swap (swap) workflow quickly.

Why use execute and supportsExecutionMode?

ERC7821 adopts the functions execute and supportsExecutionMode. This improves compatibility with ERC7579.

To keep the specification minimal, it does not require support for ERC165 or implementation of the executeFromExecutor function from ERC7579.

For more on ERC165, see the following article.

[ERC165] Checking Whether a Contract Supports an Interface

For more on ERC7579, see the following article.

[ERC7579] Minimal Modular Smart Accounts

Why make opData encoding optional?

Developers can choose whether to encode opData (operation data). Whether executionData contains opData depends on whether it is encoded as abi.encode(calls) or abi.encode(calls, opData).

The benefit is that developers need only use opData when required. opData can include authentication data or Paymaster data (for sponsoring transaction gas fees), but it is not mandatory.

Why replace address(0) with address(this)?

Under ERC7821, when the target field (the destination address) of the Call struct is set to address(0), it is automatically replaced with address(this). This improves the compression efficiency of calldata.

For contract transactions, smaller calldata can reduce gas costs. Using address(0) in place of address(this) reduces the cost of encoding calls to the contract itself, potentially saving gas.

Why make batch-of-batches mode optional?

Batch-of-batches mode is an optional feature that processes multiple batches in a single transaction. This makes it possible to submit batches signed by different signers together. For example, transactions individually signed by different accounts can be processed together in one execute call.

Reference implementation

ERC7821.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.4;

/// @notice Minimal batch executor mixin.
abstract contract ERC7821 {
    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                          STRUCTS                           */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Call struct for the `execute` function.
    struct Call {
        address target; // Replaced with `address(this)` if `address(0)`.
        uint256 value; // Amount of native currency (i.e. Ether) to send.
        bytes data; // Calldata to send with the call.
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                           ERRORS                           */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev The execution mode is not supported.
    error UnsupportedExecutionMode();

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                    EXECUTION OPERATIONS                    */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev Executes the calls in `executionData`.
    /// Reverts and bubbles up error if any call fails.
    ///
    /// `executionData` encoding (single batch):
    /// - If `opData` is empty, `executionData` is simply `abi.encode(calls)`.
    /// - Else, `executionData` is `abi.encode(calls, opData)`.
    ///   See: https://eips.ethereum.org/EIPS/eip-7579
    ///
    /// `executionData` encoding (batch of batches):
    /// - `executionData` is `abi.encode(bytes[])`, where each element in `bytes[]`
    ///   is an `executionData` for a single batch.
    ///
    /// Supported modes:
    /// - `0x01000000000000000000...`: Single batch. Does not support optional `opData`.
    /// - `0x01000000000078210001...`: Single batch. Supports optional `opData`.
    /// - `0x01000000000078210002...`: Batch of batches. The mode is optional.
    ///
    /// For the "batch of batches" mode, each batch will be recursively passed into
    /// `execute` internally with mode `0x01000000000078210001...`.
    /// Useful for passing in batches signed by different signers.
    ///
    /// Authorization checks:
    /// - If `opData` is empty, the implementation SHOULD require that
    ///   `msg.sender == address(this)`.
    /// - If `opData` is not empty, the implementation SHOULD use the signature
    ///   encoded in `opData` to determine if the caller can perform the execution.
    /// - If `msg.sender` is an authorized entry point, then `execute` MAY accept
    ///   calls from the entry point, and MAY use `opData` for specialized logic.
    ///
    /// `opData` may be used to store additional data for authentication,
    /// paymaster data, gas limits, etc.
    ///
    /// For calldata compression efficiency, if a Call.target is `address(0)`,
    /// it will be replaced with `address(this)`.
    function execute(bytes32 mode, bytes memory executionData)
        public
        payable
        virtual
    {
        uint256 id = _executionModeId(mode);
        if (id == 3) {
            mode ^= bytes32(uint256(3 << (22 * 8)));
            bytes[] memory batches = abi.decode(executionData, (bytes[]));
            for (uint256 i; i < batches.length; ++i) {
                execute(mode, batches[i]);
            }
            return;
        }
        if (id == uint256(0)) revert UnsupportedExecutionMode();
        bool tryWithOpData;
        /// @solidity memory-safe-assembly
        assembly {
            let t := gt(mload(add(executionData, 0x20)), 0x3f)
            let executionDataLength := mload(executionData)
            tryWithOpData := and(eq(id, 2), and(gt(executionDataLength, 0x3f), t))
        }
        Call[] memory calls;
        bytes memory opData;
        if (tryWithOpData) {
            (calls, opData) = abi.decode(executionData, (Call[], bytes));
        } else {
            calls = abi.decode(executionData, (Call[]));
        }
        _execute(calls, opData);
    }

    /// @dev Provided for execution mode support detection.
    /// Only returns true for:
    /// - `0x01000000000000000000...`: Single batch. Does not support optional `opData`.
    /// - `0x01000000000078210001...`: Single batch. Supports optional `opData`.
    /// - `0x01000000000078210002...`: Batch of batches. The mode is optional.
    function supportsExecutionMode(bytes32 mode) public view virtual returns (bool result) {
        return _executionModeId(mode) != 0;
    }

    /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
    /*                      INTERNAL HELPERS                      */
    /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

    /// @dev 0: invalid mode, 1: no `opData` support, 2: with `opData` support, 3: batch of batches.
    function _executionModeId(bytes32 mode) internal view virtual returns (uint256 id) {
        // Only supports atomic batched executions.
        // For the encoding scheme, see: https://eips.ethereum.org/EIPS/eip-7579
        // Bytes Layout:
        // - [0]      ( 1 byte )  `0x01` for batch call.
        // - [1]      ( 1 byte )  `0x00` for revert on any failure.
        // - [2..5]   ( 4 bytes)  Reserved by ERC7579 for future standardization.
        // - [6..9]   ( 4 bytes)  `0x00000000` or `0x78210001` or `0x78210002`.
        // - [10..31] (22 bytes)  Unused. Free for use.
        uint256 m = (uint256(mode) >> (22 * 8)) & 0xffff00000000ffffffff;
        if (m == 0x01000000000078210002) id = 3;
        if (m == 0x01000000000078210001) id = 2;
        if (m == 0x01000000000000000000) id = 1;
    }

    /// @dev Executes the calls and returns the results.
    /// Reverts and bubbles up error if any call fails.
    function _execute(Call[] memory calls, bytes memory opData)
        internal
        virtual
    {
        // Very basic auth to only allow this contract to be called by itself.
        // Override this function to perform more complex auth with `opData`.
        if (opData.length == uint256(0)) {
            require(msg.sender == address(this));
            // Remember to return `_execute(calls)` when you override this function.
            return _execute(calls);
        }
        revert(); // In your override, replace this with logic to operate on `opData`.
    }

    /// @dev Executes the calls.
    /// Reverts and bubbles up error if any call fails.
    function _execute(Call[] memory calls) internal virtual {
        for (uint256 i; i < calls.length; ++i) {
            Call memory c = calls[i];
            address target = c.target == address(0) ? address(this) : c.target;
            _execute(target, c.value, c.data);
        }
    }

    /// @dev Executes the call.
    /// Reverts and bubbles up error if the call fails.
    function _execute(address target, uint256 value, bytes memory data)
        internal
        virtual
    {
        (bool success, bytes memory result) = target.call{value: value}(data);
        if (success) return;
        /// @solidity memory-safe-assembly
        assembly {
            // Bubble up the revert if the call reverts.
            revert(add(result, 0x20), mload(result))
        }
    }
}

Security

Execution access control

Implementations must include appropriate access controls.

References

Vectorized (@Vectorized), Jake Moxey (@jxom), Hadrien Croubois (@Amxx), "ERC-7821: Minimal Batch Executor Interface [DRAFT]," Ethereum Improvement Proposals, no. 7821, November 2024. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7821.

Conclusion

This article covered ERC7821, which proposes batch transactions using EIP7702. I hope it helped clarify how the interface works.

If you have any questions, feel free to contact me by direct message on Twitter.

Twitter @cardene777

I also publish on the following platforms.

https://chaldene.net/