TransactionsSend transactions

Send batch transactions

Using Smart Wallets, you can batch multiple actions (such as token transfers, approvals, or swaps) into a single transaction. Users will no longer need multiple confirmations or pop-ups to handle sequential actions. This helps you speed up how users interact with your app and improve the user experience.

How it works

Smart wallets support running a batch of actions, called “calls”, in a single transaction. These actions are atomic, which means if any of them fail (revert), the entire batched transaction will revert. This protects users from accidentally having part of a batch apply, but not all.

To use this feature, you just need to specify multiple calls to make. The parameter type accepts an array of calls, and each element should contain the action you want the wallet to take.

Prerequisites

Implementation

Required SDK version: ^v4.59.1

See the sendCalls SDK reference for full descriptions of the parameters used in the following example.

You can batch transactions using either the smart wallet client sendCalls or prepareCalls actions.

1import { type Address, erc20Abi, encodeFunctionData, parseUnits } from "viem";
2import { client } from "./client";
3import { swapAbi } from "./swapAbi";
4
5const DEMO_USDC_ADDRESS: Address = "0xCFf7C6dA719408113DFcb5e36182c6d5aa491443";
6
7const DEMO_SWAP_ADDRESS: Address = "0xB0AEC4c25E8332256A91bBaf169E3C32dfC3C33C";
8
9const { preparedCallIds } = await client.sendCalls({
10 calls: [
11 // To batch, simply specify multiple calls in the calls array
12 {
13 // approve 5000 USDC to the swap contract
14 to: DEMO_USDC_ADDRESS,
15 data: encodeFunctionData({
16 abi: erc20Abi,
17 functionName: "approve",
18 args: [DEMO_SWAP_ADDRESS, parseUnits("5000", 6)],
19 }),
20 },
21 {
22 // swap 5000 USDC to 1 WETH
23 to: DEMO_SWAP_ADDRESS,
24 data: encodeFunctionData({
25 abi: swapAbi,
26 functionName: "swapUSDCtoWETH",
27 args: [parseUnits("5000", 6), parseUnits("1", 18)],
28 }),
29 },
30 ],
31});

Next steps

Build more: