> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Execute Batches \[erc7821.executeBatches]

Executes batches of calls using "batch of batches" mode on an [ERC-7821-compatible contract](https://eips.ethereum.org/EIPS/eip-7821).

Each batch carries its own calls and optional `opData`.
The contract executes every batch atomically in one transaction.

Before sending, Viem checks the `batchOfBatches` execution mode with
[`Actions.erc7821.supportsExecutionMode`](/docs/actions/erc7821/supportsExecutionMode).

## Usage

This example executes batches of call(s) using "batch of batches" mode on an [ERC-7821-compatible contract](https://eips.ethereum.org/EIPS/eip-7821).

:::code-group
```ts twoslash [example.ts]
import { Value } from 'viem/utils'
import { client } from './viem.config'

const hash = await client.erc7821.executeBatches({
  address: client.account.address,
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: Value.fromEther('1'),
        },
      ],
    },
    {
      calls: [
        { data: '0xdeadbeef', to: '0xcb98643b8786950F0461f3B0edf99D88F274574D' },
      ],
      opData: '0xcafebabe',
    },
  ],
})
// @log: '0x9d2478f5cb1d21ac4153dbe5da9700e106a06d1cf1b957554fcd51de26eec4ae'
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Account, Client, erc7821Actions, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  account: Account.fromPrivateKey('0x...'),
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
```
:::

### Standalone Action

Call `Actions.erc7821.executeBatches` directly by passing the Client as the first argument.

:::code-group
```ts twoslash [example.ts]
import { Actions } from 'viem'
import { Value } from 'viem/utils'
import { client } from './viem.config'

const hash = await Actions.erc7821.executeBatches(client, {
  address: client.account.address,
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: Value.fromEther('1'),
        },
      ],
    },
    {
      calls: [
        { data: '0xdeadbeef', to: '0xcb98643b8786950F0461f3B0edf99D88F274574D' },
      ],
      opData: '0xcafebabe',
    },
  ],
})
// @log: '0x9d2478f5cb1d21ac4153dbe5da9700e106a06d1cf1b957554fcd51de26eec4ae'
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Account, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  account: Account.fromPrivateKey('0x...'),
  chain: mainnet,
  transport: http(),
})
```
:::

## Recipes

### Batch Contract Functions

Group contract function calls into batches when the ERC-7821 contract must execute multiple
operation groups in one transaction.

:::code-group
```ts twoslash [example.ts]
import { Abi } from 'viem/utils'
import { client } from './viem.config'

const hash = await client.erc7821.executeBatches({
  address: client.account.address,
  batches: [ // [!code focus]
    { // [!code focus]
      calls: [ // [!code focus]
        { // [!code focus]
          abi: Abi.from(['function mint()']), // [!code focus]
          functionName: 'mint', // [!code focus]
          to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
        }, // [!code focus]
      ], // [!code focus]
    }, // [!code focus]
    { // [!code focus]
      calls: [ // [!code focus]
        { // [!code focus]
          abi: Abi.from(['function claim()']), // [!code focus]
          functionName: 'claim', // [!code focus]
          to: '0xcb98643b8786950F0461f3B0edf99D88F274574D', // [!code focus]
        }, // [!code focus]
      ], // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
})
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Account, Client, erc7821Actions, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  account: Account.fromPrivateKey('0x...'),
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
```
:::

### Include Data for One Batch

Attach `opData` to the batch that needs operation-specific metadata. Other batches can use the
default execution data.

:::code-group
```ts twoslash [example.ts]
import { Hex } from 'viem/utils'
import { client } from './viem.config'

export function executeWithBatchData(opData: Hex.Hex) {
  return client.erc7821.executeBatches({
    address: client.account.address,
    batches: [
      {
        calls: [
          {
            to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
            value: 1n,
          },
        ],
      },
      {
        calls: [
          {
            to: '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC',
            value: 1n,
          },
        ],
        opData, // [!code focus]
      },
    ],
  })
}
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Account, Client, erc7821Actions, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  account: Account.fromPrivateKey('0x...'),
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
```
:::

## Return Value

`Hex`

The transaction hash. Wait for inclusion with [`Actions.transaction.waitForReceipt`](/docs/actions/public/transaction/waitForReceipt).

## Parameters

Also accepts the remaining
[`Actions.transaction.send` options](/docs/actions/wallet/transaction/send#parameters), including
`authorizationList`, `nonce`, and fee options.

### account

* **Type:** `Account | Address`
* **Default:** `client.account`

The Account sending the transaction.

```ts twoslash
import { Account, Actions } from 'viem'
// [!include ~/snippets/docs/reference-client.ts:setup]
// ---cut---
const account = Account.fromPrivateKey('0x...')

const hash = await Actions.erc7821.executeBatches(client, {
  account, // [!code focus]
  address: account.address,
  batches: [
    { calls: [{ to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1n }] },
  ],
})
```

### address

* **Type:** `Address`

The address of the ERC-7821-compatible contract that executes the batches (for example, a delegated account).

```ts twoslash
import { Actions } from 'viem'
// [!include ~/snippets/docs/reference-client.ts:setup]
// ---cut---
const hash = await Actions.erc7821.executeBatches(client, {
  address: client.account.address, // [!code focus]
  batches: [
    { calls: [{ to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1n }] },
  ],
})
```

### batches

* **Type:** `{ calls: Call[]; opData?: Hex }[]`

The batches to execute. Each batch holds its `calls` (raw call data or contract function parameters) and optional `opData`.

```ts twoslash
import { Actions } from 'viem'
// [!include ~/snippets/docs/reference-client.ts:setup]
// ---cut---
const hash = await Actions.erc7821.executeBatches(client, {
  address: client.account.address,
  batches: [ // [!code focus]
    { // [!code focus]
      calls: [{ to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1n }], // [!code focus]
    }, // [!code focus]
    { // [!code focus]
      calls: [ // [!code focus]
        { data: '0xdeadbeef', to: '0xcb98643b8786950F0461f3B0edf99D88F274574D' }, // [!code focus]
      ], // [!code focus]
      opData: '0xcafebabe', // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `Actions.erc7821.Errors.ExecuteUnsupportedError` | The target contract does not support the `batchOfBatches` execution mode. |
| `ContractError.ContractFunctionExecutionError` | A call reverted while executing the batches. |
