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)});

  // estimate transactions added to the batch and get the fee data for the UserOp
  const op = await primeSdk.estimate();
  console.log(`Estimate UserOp: ${await printOp(op)}`);

  // sign the UserOp and sending to the bundler...
  const uoHash = await primeSdk.send(op);
  console.log(`UserOpHash: ${uoHash}`);

  // get transaction hash...
  console.log('Waiting for transaction...');
  let userOpsReceipt = null;
  const timeout = Date.now() + 60000; // 1 minute timeout
  while((userOpsReceipt == null) && (Date.now() < timeout)) {
    await sleep(2);
    userOpsReceipt = await primeSdk.getUserOpReceipt(uoHash);
  }
  console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
}

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