Batching transactions is the ability to include a number of transactions within one block to save the user both gas, time, and clicks.

Here we’ll show a code example of how easy it is to batch transactions using the Prime SDK.

We’ll be using the addUserOpsToBatch function for this.

import { ethers } from 'ethers';
import { PrimeSdk } from '@etherspot/prime-sdk';
import { printOp } from '../src/sdk/common/OperationUtils';

const recipient1: string = '0x10a1874E1046B1cc5deFdf4D3153838B72fF94Ac'; // recipient1 wallet address
const recipient2: string = '0x20a1874E1046B1cc5deFdf4D3153838B72fF94Ac'; // recipient2 wallet address
const recipient3: string = '0x30a1874E1046B1cc5deFdf4D3153838B72fF94Ac'; // recipient3 wallet address
const value: string = '0.01'; // transfer value

async function main() {
  // initializating sdk...
  const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID) })

  // add transaction 1 to the batch
  let transactionBatch = await primeSdk.addUserOpsToBatch({to: recipient1, value: ethers.utils.parseEther(value)});

  // add transaction 2 to the batch
  transactionBatch = await primeSdk.addUserOpsToBatch({to: recipient2, value: ethers.utils.parseEther(value)});

  // add transaction 3 to the batch
  transactionBatch = await primeSdk.addUserOpsToBatch({to: recipient3, value: ethers.utils.parseEther(value)});

  // sign transactions added to the batch
  const op = await primeSdk.sign();

  // sending to the bundler...
  const uoHash = await primeSdk.send(op);
}

main()
  .catch(console.error)
  .finally(() => process.exit());