The AbstractClient includes a sendTransactionBatch method that can be used to sign and submit multiple transactions in a single call using the connected Abstract Global Wallet.

YouTube Tutorial: Send Batch Transactions with AGW

Watch our video tutorials to learn more about building on Abstract.

Usage

import { useAbstractClient } from "@abstract-foundation/agw-react";
import { getGeneralPaymasterInput } from "viem/zksync";
import { encodeFunctionData, parseUnits } from "viem";
import { ROUTER_ADDRESS, TOKEN_ADDRESS, WETH_ADDRESS, PAYMASTER_ADDRESS, routerAbi, erc20Abi } from "./config";

export default function SendTransactionBatch() {
  const { data: agwClient } = useAbstractClient();

  async function sendTransactionBatch() {
    if (!agwClient) return;

    // Batch an approval and a swap in a single call
    const hash = await agwClient.sendTransactionBatch({
      calls: [
        // 1 - Approval
        {
          to: TOKEN_ADDRESS,
          args: [ROUTER_ADDRESS, parseUnits("100", 18)],
          data: encodeFunctionData({
            abi: erc20Abi,
            functionName: "approve",
            args: [ROUTER_ADDRESS, parseUnits("100", 18)],
          }),
        },
        // 2 - Swap
        {
          to: ROUTER_ADDRESS,
          data: encodeFunctionData({
            abi: routerAbi,
            functionName: "swapExactTokensForETH",
            args: [
              parseUnits("100", 18),
              BigInt(0),
              [TOKEN_ADDRESS, WETH_ADDRESS],
              agwClient.account.address,
              BigInt(Math.floor(Date.now() / 1000) + 60 * 20),
            ],
          }),
        },
      ],
      paymaster: PAYMASTER_ADDRESS,
      paymasterInput: getGeneralPaymasterInput({
        innerInput: "0x",
      }),
    });
  }
}

Parameters

calls
Array<TransactionRequest>

An array of transaction requests. Each transaction request can include the following fields:

paymaster
Account | Address

Address of the paymaster smart contract that will pay the gas fees of the transaction batch.

paymasterInput
Hex

Input data to the paymaster.

Returns

Returns a Promise<Hex> containing the transaction hash of the submitted transaction batch.