ERC-7638 · Draft · ERC · 2024-02-26
ERC-7638: Batch Calls Encoding in SCA
A draft compact byte encoding for packing many (to, value, data) calls into one bytes blob, so a smart contract account can execute them in a single, optionally atomic, call with less calldata.
What problem does it solve?
Many user actions are really several calls: approve then swap, or a payment plus a fee to a relayer. With an EOA each step is a separate transaction, and if a later step fails the earlier ones remain (for example, a dangling approval).
Smart contract accounts (SCAs) can run several calls in one transaction and revert all of them if any fails. The usual way to pass them is an ABI-encoded array of (address, uint256, bytes) structs, which carries offsets and padding. ERC-7638 ("Batch Calls Encoding", BCE) defines a tighter, tightly packed format.
How it works
Each call is the concatenation of four fields:
| Field | Size | Meaning |
|---|---|---|
to |
20 bytes | Target address |
value |
32 bytes | Wei to send |
data length |
32 bytes | Length of data in bytes |
data |
variable | Calldata for the target |
A batch is just calls placed back to back. The wallet walks the bytes with calldata slices: read 20 + 32 + 32 bytes of header, then len bytes of data, call, advance by 84 + len.
The ERC only standardises the encoding. Function names, signatures, replay protection and whether a failure reverts everything (atomic) or is skipped (non-atomic) are left to each wallet. The spec's example wallet takes a signed batch from a bundler, checks the owner's signature plus a deadline, executes every call, and reverts the whole batch if any call fails. One call in the batch can pay the bundler in an ERC-20, giving gas sponsorship without ERC-4337, though both can coexist.
Sample code
Encoding a batch in TypeScript (ethers v6):
import { solidityPacked, concat, Interface, parseUnits } from "ethers";
type Call = { to: string; value: bigint; data: string };
function encodeBatch(calls: Call[]): string {
return concat(
calls.map(c => {
const len = BigInt((c.data.length - 2) / 2);
return solidityPacked(["address", "uint256", "uint256", "bytes"], [c.to, c.value, len, c.data]);
}),
);
}
const erc20 = new Interface(["function approve(address,uint256)", "function transfer(address,uint256)"]);
const batch = encodeBatch([
{ to: token, value: 0n, data: erc20.encodeFunctionData("approve", [router, parseUnits("100", 6)]) },
{ to: router, value: 0n, data: swapCalldata },
{ to: token, value: 0n, data: erc20.encodeFunctionData("transfer", [bundler, parseUnits("1", 6)]) },
]);
Decoding loop inside the account (atomic version):
uint256 i;
while (i < calls.length) {
address to = address(bytes20(calls[i:i + 20]));
uint256 val = uint256(bytes32(calls[i + 20:i + 52]));
uint256 len = uint256(bytes32(calls[i + 52:i + 84]));
(bool ok, bytes memory ret) = to.call{value: val}(calls[i + 84:i + 84 + len]);
if (!ok) assembly { revert(add(ret, 32), mload(ret)) }
i += 84 + len;
}
Things to know
- Status: Draft.
- The gas savings claim in the spec is based on the authors' own testing; measure for your case.
- Signature checks, nonces or used-hash tracking, deadlines and chain ID binding are entirely the wallet's responsibility. The encoding has no replay protection of its own.
- Wallet-to-dapp batching is also covered at the RPC level by EIP-5792 (
wallet_sendCalls), and EOAs can gain batching via EIP-7702. Neither requires this byte format. - The
Bundler.solexample in the spec references an undefined variable and is illustrative only.