Skip to main content
Batching transactions involves combining multiple operations into a single transaction. This approach can greatly simplify Web3 interactions for users. For example, instead of first staking tokens and then claiming rewards in two separate transactions, a user can perform both actions at once in a single transaction.

Getting Started

1

Importing Dependencies

import { createGelatoSmartWalletClient, sponsored } from "@gelatonetwork/smartwallet";
import { gelato, kernel, safe, metamask } from "@gelatonetwork/smartwallet/accounts";
import { createWalletClient, createPublicClient, http, type Hex } from "viem";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
2

Setup Smart Account

You can set up a Smart Account as per your needs. In the case of Gelato, the Gelato Smart Account address will be the same as your EOA, enabling EIP-7702 features.

When using a Kernel Account, you have the option to use EIP-7702 and ERC-4337 together. Setting eip7702 parameter to true will make your EOA the sender address. However, if you want to utilize existing Kernel accounts only with ERC-4337 features, set it to false.

For a Safe Account, it defaults to the ERC-4337 standard. You can either use an already deployed Safe Account or create a new one, while enhancing the experience with Gelato’s best-in-class infrastructure.
  • Gelato Smart Account
  • Kernel Account
  • Safe Account
  • Metamask Account
const privateKey = (process.env.PRIVATE_KEY ?? generatePrivateKey()) as Hex;
const owner = privateKeyToAccount(privateKey);

const publicClient = createPublicClient({
  chain: baseSepolia,
  transport: http(),
});

const account = await gelato({
  owner,
  client: publicClient,
});
3

Setup Wallet Client

Quickly get started by creating a wallet client using createWalletClient from viem with local account for your specified network. Checkout supported networks here.
const client = createWalletClient({
  account,
  chain: baseSepolia,
  transport: http()
});
4

Creating a Smart Wallet Client

To create a API Key, visit the Gelato App and navigate to the Paymaster & Bundler > API Keys section. Create a new API Key, select the required networks, and copy the generated API Key.

For detailed instructions, click here to learn more about creating a API Key.
const smartWalletClient = createGelatoSmartWalletClient(client, { apiKey });
5

Batch Multiple Transactions

You can batch multiple transactions to be sent on-chain at once by adding them to the calls array:
 const results = await smartWalletClient.execute({
  payment: sponsored(),
  calls: [
    {
      to: "<token-address>",
      data: encodeFunctionData({
        abi: tokenAbi,
        functionName: "approve",
        args: [targetContractAddress, amount]
      }),
      value: 0n
    },
    {
      to: "<target-contract-address>",
      data: encodeFunctionData({
        abi: targetContractAbi,
        functionName: "stake",
        args: [amount]
      }),
      value: 0n
    },
    {
      to: "<target-contract-address>",
      data: encodeFunctionData({
        abi: targetContractAbi,
        functionName: "claimRewards",
        args: []
      }),
      value: 0n
    }
  ]
});