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-6357, which proposes enabling an externally owned account (EOA) to execute multiple calls to a supporting contract in a single transaction.

This explanation translates, summarizes, and expands on the following specification.

ERC-6357 was in Review status as of October 24, 2023.

For other ERCs, see https://cardene.dev/proposals.

Overview

This proposal standardizes a multicall interface. An EOA can invoke multiple functions of a supporting smart contract in one transaction. If any call fails, all calls are reverted.

Motivation

Without batching, transferring multiple ERC-721 NFTs individually requires one transaction per transfer. Each transaction incurs the 21,000-gas intrinsic base cost, in addition to execution costs. Batching avoids repeating that base cost unnecessarily.

For ERC-721, see the following article.

https://chaldene.net/erc721

Specification

Contracts implementing the proposal must implement the following interface, with the payable extension optional.

javascript
pragma solidity ^0.8.0;

interface IMulticall {
    /// @notice           Takes an array of abi-encoded call data, delegatecalls itself with each calldata, and returns the abi-encoded result
    /// @dev              Reverts if any delegatecall reverts
    /// @param    data    The abi-encoded data
    /// @returns  results The abi-encoded return values
    function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results);

    /// @notice           OPTIONAL. Takes an array of abi-encoded call data, delegatecalls itself with each calldata, and returns the abi-encoded result
    /// @dev              Reverts if any delegatecall reverts
    /// @param    data    The abi-encoded data
    /// @param    values  The effective msg.values. These must add up to at most msg.value
    /// @returns  results The abi-encoded return values
    function multicallPayable(bytes[] calldata data, uint256[] values) external payable virtual returns (bytes[] memory results);
}

multicall

solidity
function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results)

Overview

Accepts an array of ABI-encoded calldata, delegatecalls the contract itself using each element, and returns ABI-encoded results.

For delegatecall, see the following article.

https://chaldene.net/solidity-delegatecall

Details

The calldata entries are processed in order through delegatecalls to the contract itself. Each result is ABI-encoded and returned in an array. If any delegatecall reverts, multicall also reverts.

Parameters

  • data: an array of ABI-encoded calldata.

Return Value

  • results: an array of ABI-encoded return values.

multicallPayable

solidity
function multicallPayable(bytes[] calldata data, uint256[] values) external payable virtual returns (bytes[] memory results)

Overview

Accepts an array of ABI-encoded calldata and corresponding Ether values, delegatecalls the contract itself for each entry, and returns ABI-encoded results.

Details

The calldata entries are processed sequentially. If any delegatecall reverts, the entire function reverts. Effective Ether values must be associated with the corresponding calls. The payable extension is optional.

Parameters

  • data: an array of ABI-encoded calldata.
  • values: an array of effective Ether values, one per call, whose sum must not exceed msg.value.

Return Value

  • results: an array of ABI-encoded return values.

Additional Considerations

multicallPayable is optional because splitting the effective msg.value between calls is not always implementable.

Backward Compatibility

The proposal is compatible with most existing multicall implementations.

Tests

The following JavaScript example uses Ethers to atomically transfer amt units of an ERC-20 token to both addressA and addressB.

amt specifies the amount of ERC-20 tokens to transfer.

For ERC-20, see the following article.

https://chaldene.net/erc20

javascript
await token.multicall(await Promise.all([
    token.interface.encodeFunctionData('transfer', [ addressA, amt ]),
    token.interface.encodeFunctionData('transfer', [ addressB, amt ]),
]));

Reference Implementation

javascript
pragma solidity ^0.8.0;

/// Derived from OpenZeppelin's implementation
abstract contract Multicall is IMulticall {
    function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
        results = new bytes[](data.length);
        for (uint256 i = 0; i < data.length; i++) {
            (bool success, bytes memory returndata) = address(this).delegatecall(data);
            require(success);
            results[i] = returndata;
        }
        return results;
    }
}

Security Considerations

multicallPayable should only be used when the contract can safely support it. A naive implementation may allow a payable function to account for the same Ether more than once, creating security vulnerabilities. Appropriate safeguards are essential.

Citation

Gavin John (@Pandapip1), "ERC-6357: Single-contract Multi-delegatecall [DRAFT]," Ethereum Improvement Proposals, no. 6357, January 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6357.

Conclusion

ERC-6357 standardizes atomic batching of function calls to a supporting contract through a single transaction.

Further reading and author links: