Skip to content

executeBatches

Executes batches of call(s) using the "batch of batches" mode on an ERC-7821-compatible contract.

Usage

example.ts
import { parseEther } from 'viem'
import { account, client } from './config'
 
const hash = await client.executeBatches({ 
  account,
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('2')
        },
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
})

Account Hoisting

If you do not wish to pass an account to every sendCalls, you can also hoist the Account on the Wallet Client (see config.ts).

Learn more.

example.ts
import { parseEther } from 'viem'
import { account, client } from './config'
 
const hash = await client.execute({ 
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('2')
        },
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
})

Contract Calls

The calls property also accepts Contract Calls, and can be used via the abi, functionName, and args properties.

example.ts
import { parseEther } from 'viem'
import { account, client } from './config'
 
const abi = parseAbi([
  'function approve(address, uint256) returns (bool)',
  'function transferFrom(address, address, uint256) returns (bool)',
])
 
const hash = await client.execute({ 
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
          abi,
          functionName: 'approve',
          args: [
            '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
            100n
          ],
        },
        {
          to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
          abi,
          functionName: 'transferFrom',
          args: [
            '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
            '0x0000000000000000000000000000000000000000',
            100n
          ],
        },
      ],
    },
  ],
})

Return Value

Hash

A Transaction Hash.

Parameters

account

  • Type: Account | Address | null

Account to invoke the execution of the calls.

Accepts a JSON-RPC Account or Local Account (Private Key, etc). If set to null, it is assumed that the transport will handle filling the sender of the transaction.

const hash = await client.execute({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
})

address

  • Type: 0x${string}

Address of the contract to execute the calls on.

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D', 
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
})

batches

  • Type: { calls: Call[], opData?: Hex }[]

Set of call batches to execute.

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [ 
    { 
      calls: [ 
        { 
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
          value: parseEther('1') 
        }, 
      ] 
    }, 
    { 
      calls: [ 
        { 
          data: '0xdeadbeef', 
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
        }, 
      ], 
    }, 
  ], 
})

batches.calls

  • Type: Call[]

Set of calls in a batch to execute.

// @filename: config.ts
import { createClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { erc7821Actions } from 'viem/experimental'
 
export const account = privateKeyToAccount('0x...')
 
export const client = createClient({
  account,
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
// @filename: config.ts
import { createClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { erc7821Actions } from 'viem/experimental'
 
export const account = privateKeyToAccount('0x...')
 
export const client = createClient({
  account,
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
// @filename: config.ts
import { createClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { erc7821Actions } from 'viem/experimental'
 
export const account = privateKeyToAccount('0x...')
 
export const client = createClient({
  chain: mainnet,
  transport: http(),
}).extend(erc7821Actions())
// @filename: example.js
 
// ---cut---
import { client } from './config'
// ---cut---
const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [ 
        { 
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', 
          value: parseEther('1') 
        }, 
      ] 
    }
    {
      calls: [ 
        { 
          data: '0xdeadbeef', 
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
        }, 
      ], 
    },
  ], 
})
## Errors were thrown in the sample, but not included in an error tag These errors were not marked as being expected: 1005. Expected: // @errors: 1005 Compiler Errors: example.js [1005] 1564 - ',' expected.

batches.opData (optional)

  • Type: Hex

Additional data to pass to execution.

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ],
      opData: '0xdeadbeef', 
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
})

authorizationList (optional)

  • Type: AuthorizationList

Signed EIP-7702 Authorization list.

const authorization = await client.signAuthorization({ 
  contractAddress: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
}) 
 
const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  authorizationList: [authorization], 
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ], 
})

chain (optional)

  • Type: Chain
  • Default: client.chain

Chain to execute the calls on.

import { optimism } from 'viem/chains'
 
const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
  chain: optimism, 
})

gasPrice (optional)

  • Type: bigint

The price (in wei) to pay per gas.

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
  gasPrice: parseGwei('20'), 
})

maxFeePerGas (optional)

  • Type: bigint

Total fee per gas (in wei), inclusive of maxPriorityFeePerGas.

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
  maxFeePerGas: parseGwei('20'), 
})

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas (in wei).

const hash = await client.execute({
  address: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
  batches: [
    {
      calls: [
        {
          to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
          value: parseEther('1')
        },
      ]
    },
    {
      calls: [
        {
          data: '0xdeadbeef',
          to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        },
      ],
    },
  ],
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'), 
})