> ## Documentation Index
> Fetch the complete documentation index at: https://developers.circle.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Batch and parallel user operations

> Combine multiple actions into a single user operation, or run independent operations in parallel using 2D nonces.

A user operation can carry one call or many. Batching packs several calls into
one operation that the smart account executes atomically. Parallelism uses 2D
nonces to submit independent operations concurrently so a slow one doesn't block
the others. These are two independent patterns, applicable on their own or
together.

The examples below use the `smartAccount` and `bundlerClient` returned by
[Create a modular wallet](/wallets/modular/create-a-modular-wallet).

## Batch calls in a single user operation

Pass multiple `calls` to `sendUserOperation`. The user signs once and the smart
account executes the entire array atomically. A common pattern is to bundle an
ERC-20 `approve` with the call that spends the allowance, so the user never has
a granted-but-unused approval sitting onchain.

<CodeGroup>
  ```typescript Web SDK (TypeScript) theme={null}
  import { encodeFunctionData, parseUnits } from "viem";

  const USDC = "0x..."; // USDC on Arc Testnet
  const spender = "0x..."; // contract that will pull USDC

  const userOpHash = await bundlerClient.sendUserOperation({
    calls: [
      {
        to: USDC,
        data: encodeFunctionData({
          abi: erc20Abi,
          functionName: "approve",
          args: [spender, parseUnits("100", 6)],
        }),
      },
      {
        to: spender,
        data: encodeFunctionData({
          abi: spenderAbi,
          functionName: "pullAndDeposit",
          args: [parseUnits("100", 6)],
        }),
      },
    ],
    paymaster: true,
  });
  ```

  ```swift iOS SDK (Swift) theme={null}
  Task {
      do {
          let userOpHash = try await bundlerClient.sendUserOperation(
              account: smartAccount,
              calls: [
                  EncodeCallDataArg(
                      to: USDC,
                      value: BigInt(0),
                      abiJson: ERC20_ABI,
                      functionName: "approve",
                      args: [spender, BigInt(100_000_000)]
                  ),
                  EncodeCallDataArg(
                      to: spender,
                      value: BigInt(0),
                      abiJson: SPENDER_ABI,
                      functionName: "pullAndDeposit",
                      args: [BigInt(100_000_000)]
                  ),
              ],
              paymaster: Paymaster.True()
          )
      } catch {
          print(error)
      }
  }
  ```

  ```kotlin Android SDK (Kotlin) theme={null}
  CoroutineScope(Dispatchers.IO).launch {
      try {
          val userOpHash = bundlerClient.sendUserOperation(
              context,
              smartAccount,
              arrayOf(
                  EncodeCallDataArg(
                      to = USDC,
                      value = BigInteger.ZERO,
                      functionName = "approve",
                      abiJson = ABI_ERC20,
                      args = arrayOf(spender, parseUnits("100", 6)),
                  ),
                  EncodeCallDataArg(
                      to = spender,
                      value = BigInteger.ZERO,
                      functionName = "pullAndDeposit",
                      abiJson = SPENDER_ABI,
                      args = arrayOf(parseUnits("100", 6)),
                  ),
              ),
              paymaster = Paymaster.True(),
          )
      } catch (e: Exception) {
          e.printStackTrace()
      }
  }
  ```

  ```java Android SDK (Java) theme={null}
  try {
      EncodeCallDataArg approveCall = new EncodeCallDataArg(
          USDC,
          BigInteger.ZERO,
          null,
          AbiConstantsKt.getABI_ERC20(),
          new Object[] { spender, parseUnits("100", 6) },
          "approve"
      );
      EncodeCallDataArg depositCall = new EncodeCallDataArg(
          spender,
          BigInteger.ZERO,
          null,
          SPENDER_ABI,
          new Object[] { parseUnits("100", 6) },
          "pullAndDeposit"
      );

      CompletableFuture<String> userOpHashFuture = new CompletableFuture<>();
      bundlerClient.sendUserOperation(
          context,
          smartAccount,
          new EncodeCallDataArg[] { approveCall, depositCall },
          new UserOperationV07(),
          new Paymaster.True(),
          new CustomContinuation<>(userOpHashFuture)
      );
      String userOpHash = userOpHashFuture.join();
  } catch (Exception e) {
      e.printStackTrace();
  }
  ```
</CodeGroup>

## Run independent operations in parallel

Externally owned accounts process transactions sequentially because each one
consumes the next integer nonce. Smart accounts use 2D nonces, a `nonceKey` plus
a nonce value. Operations with different `nonceKey` values are independent and
can land in any order, so a slow operation doesn't block a fast one.

Use parallel execution when:

* The user operations don't depend on each other's state.
* You want to submit multiple operations without waiting for each receipt.
* The operations are safe to land in any order.

For sequential dependencies (for example, swap A to B then swap B to C), keep
the default linear nonce by omitting `nonceKey` or passing `0`.

<Note>
  To submit operations in parallel, sign each with a distinct `nonceKey` greater
  than `0`, then send them concurrently. Don't `await` each receipt before
  sending the next, or you lose the benefit.
</Note>

<CodeGroup>
  ```typescript Web SDK (TypeScript) theme={null}
  // Each partial user operation specifies the calls it carries. Build the
  // array however your app produces them.
  type PartialUserOperation = Parameters<
    typeof bundlerClient.sendUserOperation
  >[0];
  const userOps: PartialUserOperation[] = [
    {
      calls: [{ to: "0x...", data: "0x..." }],
    },
    {
      calls: [{ to: "0x...", data: "0x..." }],
    },
  ];

  // Sign every user op first. Each gets a distinct nonceKey so they're
  // independent.
  const signedUserOps = await Promise.all(
    userOps.map(async (userOp, index) => {
      const nonce = await smartAccount.getNonce({ key: BigInt(index + 1) });
      const signature = await smartAccount.signUserOperation({
        ...userOp,
        nonce,
      });
      return { ...userOp, nonce, signature };
    }),
  );

  // Submit the signed ops concurrently. Don't await each receipt before
  // sending the next one.
  const hashes = await Promise.all(
    signedUserOps.map((userOp) =>
      bundlerClient.sendUserOperation({ smartAccount, ...userOp }),
    ),
  );
  ```

  ```swift iOS SDK (Swift) theme={null}
  Task {
      var signedUserOps: [UserOperationV07] = []

      for (index, userOp) in userOps.enumerated() {
          do {
              let nonceKey = userOp.nonce ?? BigInt(index + 1)
              let nonce = try await smartAccount.getNonce(key: nonceKey)
              var signedUserOp = userOp
              signedUserOp.nonce = nonce
              signedUserOp.signature = try await smartAccount.signUserOperation(
                  chainId: smartAccount.client.chain.chainId,
                  userOp: signedUserOp
              )
              signedUserOps.append(signedUserOp)
          } catch {
              print("Error signing user operation: \(error)")
          }
      }

      await withTaskGroup(of: Void.self) { group in
          for userOp in signedUserOps {
              group.addTask {
                  do {
                      let sendResult = try await bundlerClient.sendUserOperation(
                          account: smartAccount,
                          calls: nil,
                          partialUserOp: userOp
                      )
                      print(sendResult)
                  } catch {
                      print("Error sending user operation: \(error)")
                  }
              }
          }
      }
  }
  ```

  ```kotlin Android SDK (Kotlin) theme={null}
  val signedUserOps = mutableListOf<UserOperationV07>()

  userOps.forEachIndexed { index, userOp ->
      CoroutineScope(Dispatchers.IO).launch {
          try {
              val chain = ArcTestnet
              val nonceKey = userOp.nonce ?: BigInteger("${index + 1}")
              val nonce = smartAccount.getNonce(key = nonceKey)
              val signature = smartAccount.signUserOperation(
                  context,
                  chain.chainId,
                  userOp.copy(nonce = nonce),
              )
              signedUserOps.add(userOp.copy(nonce = nonce, signature = signature))
          } catch (error: Exception) {
              Log.e(TAG, "Error signing user operation at index $index: ${error.message}")
          }
      }
  }

  CoroutineScope(Dispatchers.IO).launch {
      val sendOperations = signedUserOps.map { userOp ->
          async {
              try {
                  bundlerClient.sendUserOperation(
                      context,
                      smartAccount,
                      calls = null,
                      partialUserOp = userOp,
                  )
              } catch (error: Exception) {
                  Log.e(TAG, "Error sending user operation: ${error.message}")
              }
          }
      }
      sendOperations.awaitAll()
  }
  ```

  ```java Android SDK (Java) theme={null}
  List<UserOperationV07> signedUserOps = new ArrayList<>();

  for (int i = 0; i < userOps.size(); i++) {
      try {
          UserOperationV07 userOp = userOps.get(i);
          BigInteger nonceKey = userOp.getNonce() == null
              ? BigInteger.valueOf(i + 1)
              : userOp.getNonce();

          CompletableFuture<BigInteger> nonceFuture = new CompletableFuture<>();
          smartAccount.getNonce(nonceKey, new CustomContinuation<>(nonceFuture));
          BigInteger nonce = nonceFuture.join();
          userOp.setNonce(nonce);

          CompletableFuture<String> signatureFuture = new CompletableFuture<>();
          smartAccount.signUserOperation(
              context,
              ArcTestnet.INSTANCE.getChainId(),
              userOp,
              new CustomContinuation<>(signatureFuture)
          );
          userOp.setSignature(signatureFuture.join());
          signedUserOps.add(userOp);
      } catch (Exception error) {
          Log.e(TAG, "Error signing user operation at index " + i + ": " + error.getMessage());
      }
  }

  List<CompletableFuture<String>> sendOperations = signedUserOps.stream()
      .map(userOp -> {
          CompletableFuture<String> userOpHashFuture = new CompletableFuture<>();
          try {
              bundlerClient.sendUserOperation(
                  context,
                  smartAccount,
                  null,                       // calls
                  userOp,                     // partialUserOp
                  new Paymaster.True(),
                  new CustomContinuation<>(userOpHashFuture)
              );
          } catch (Exception e) {
              userOpHashFuture.completeExceptionally(e);
          }
          return userOpHashFuture;
      })
      .collect(Collectors.toList());

  CompletableFuture.allOf(sendOperations.toArray(new CompletableFuture[0])).join();
  ```
</CodeGroup>
