ERC-6357 · Last Call · ERC · 2023-01-18
ERC-6357: Single-contract Multi-delegatecall
Standardizes a multicall(bytes[]) function that lets an EOA run several functions of one contract in a single, all-or-nothing transaction.
What problem does it solve?
An externally owned account can only make one call per transaction. Sending five NFTs or paying three people from a token contract means five or three transactions, each paying the 21,000 base gas cost and each needing a separate signature. If one fails halfway, you are left in a partial state.
Many contracts (Uniswap's routers, OpenZeppelin's Multicall) already solved this with a multicall function. ERC-6357 standardizes that function so wallets and tools can rely on a single shape.
How it works
The contract exposes:
function multicall(bytes[] calldata data) external returns (bytes[] memory results);
Each element of data is ABI-encoded calldata for one of the contract's own functions. The contract delegatecalls itself with each item in order. Because it is a delegatecall to itself, msg.sender stays the original caller, so transfer and similar functions behave as if you had called them directly. The return data of each call is collected into results. If any call reverts, the whole multicall reverts.
An optional multicallPayable(bytes[] data, uint256[] values) assigns a share of msg.value to each call (the values must add up to at most msg.value). It is optional because splitting ETH safely is hard to get right.
It only batches calls to the same contract. Batching across contracts is the job of smart accounts or wallet APIs such as EIP-5792.
Sample code
Reference-style implementation (derived from OpenZeppelin):
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
abstract contract Multicall {
function multicall(bytes[] calldata data) external returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
(bool ok, bytes memory ret) = address(this).delegatecall(data[i]);
require(ok, "multicall: call failed");
results[i] = ret;
}
}
}
Two ERC-20 transfers in one transaction (ethers v6):
const calls = [
token.interface.encodeFunctionData("transfer", [addressA, amt]),
token.interface.encodeFunctionData("transfer", [addressB, amt]),
];
await (await token.multicall(calls)).wait(); // both succeed or both revert
Things to know
- Status: Last Call (deadline listed in the spec: 2023-11-10). It has not been marked Final.
- A naive
multicallPayable, or a payablemulticallthat delegatecalls payable functions, lets each sub-call see the fullmsg.value, so an attacker could "spend" the same ETH several times. Don't makemulticallpayable unless you handle this. - Many existing contracts already have a compatible
multicall(bytes[]), so the selector is widely recognized. - Like any delegatecall-based helper, it should only be used for the contract calling itself, never with user-supplied target addresses.