Execute multiple transactions in a single operation using smart account batching
import { encodeFunctionData } from "viem";
async function batchTransactions(
client: any,
operations: Array<{
target: string;
data?: string;
value?: bigint;
}>
) {
try {
const { hash } = await client.sendUserOperation({
uo: operations.map(op => ({
target: op.target,
data: op.data || "0x",
value: op.value || 0n
}))
});
console.log("Batch UserOp Hash:", hash);
const receipt = await client.waitForUserOperationReceipt({
hash
});
console.log("Batch executed:", receipt.receipt.transactionHash);
return receipt;
} catch (error) {
console.error("Batch operation failed:", error);
throw error;
}
}
import { encodeFunctionData, parseAbi } from "viem";
const erc20Abi = parseAbi([
"function transfer(address to, uint256 amount) returns (bool)",
"function approve(address spender, uint256 amount) returns (bool)"
]);
async function batchTokenOperations(
client: any,
tokenAddress: string,
operations: Array<{
type: "transfer" | "approve";
to: string;
amount: bigint;
}>
) {
const batchOps = operations.map(op => ({
target: tokenAddress,
data: encodeFunctionData({
abi: erc20Abi,
functionName: op.type,
args: [op.to, op.amount]
})
}));
const { hash } = await client.sendUserOperation({
uo: batchOps
});
const receipt = await client.waitForUserOperationReceipt({ hash });
return receipt;
}
import { encodeFunctionData, parseAbi } from "viem";
const nftAbi = parseAbi([
"function mint(address to, uint256 tokenId)"
]);
async function batchMintNFTs(
client: any,
nftAddress: string,
recipients: Array<{ to: string; tokenId: bigint }>
) {
const mintOps = recipients.map(r => ({
target: nftAddress,
data: encodeFunctionData({
abi: nftAbi,
functionName: "mint",
args: [r.to, r.tokenId]
})
}));
const { hash } = await client.sendUserOperation({
uo: mintOps
});
console.log(`Minting ${recipients.length} NFTs...`);
const receipt = await client.waitForUserOperationReceipt({ hash });
console.log("Batch mint completed!");
return receipt;
}