How to submit batch transactions
One benefit of Smart Contract Accounts is that it is possible to batch transactions in one UserOperation
(UO). Not all Smart Contract Accounts support batching. But, if the SmartContractAccount
implementation you are using has the encodeBatchExecute
method, then implementations of SmartAccountClient
will allow you to make those calls.
There are two ways you can batch transactions using SmartAccountClient
:
- via
sendUserOperation
- via
sendTransactions
Batching using sendUserOperation
The SmartAccountClient
supports passing either a single UO or an array of UOs to sendUserOperation
. If you pass an array, the client will batch the transactions into a single User Operation and submit it to the bundler. Let's see an example:
import { smartAccountClient } from "./smartAccountClient";
// the hash returned here is the hash of the User Operation
const { hash } = await smartAccountClient.sendUserOperation({
uo: [
{
target: "0x...",
data: "0xcallDataTransacation1",
},
{
target: "0x...",
data: "0xcallDataTransacation2",
},
],
});
Batching using sendTransactions
The SmartAccountClient
supports sending UOs and waiting for them to be mined in a transaction via the sendTransaction
and sendTransactions
methods. The latter allows for batching in the same way sendUserOperation
:
import { smartAccountClient } from "./smartAccountClient";
// the hash returned here is the hash of the mined Tx that includes the UserOperation
const hash = await smartAccountClient.sendTransactions({
requests: [
{
to: "0x...",
data: "0xcallDataTransacation1",
},
{
to: "0x...",
data: "0xcallDataTransacation2",
},
],
});