Skip to content

Drop and replace failing user operations

In the previous guides, we learned how to send user operations with gas sponsorship, but what happens when a user operation fails to mine? In this guide, we'll cover how to use drop and replace to resend failing user operations and ensure they get mined.

What is drop and replace?

If fees change and your user operation gets stuck in the mempool, you can use drop and replace to resend the user operation with higher fees. This is most useful when used in combination with waitForUserOperationTransaction to ensure the transaction is mined and then resend the user operation with higher fees if waiting times out.

Drop and replace works by resubmitting a user operation with the greater of:

  1. 10% higher fees
  2. The current minimum fees

We export a dropAndReplace function from @aa-sdk/core that you can use to handle this flow for you and is automatically added to the Smart Account Client.

How to drop and replace effectively

Let's run through an example that uses drop and replace if waiting for a user operation to mine times out.

example.ts
import { client } from "./client";
 
// 1. send a user operation
const { hash, request } = await client.sendUserOperation({
  uo: {
    target: "0xTARGET_ADDRESS",
    data: "0x",
    value: 0n,
  },
});
 
try {
  // 2. wait for it to be mined
  const txHash = await client.waitForUserOperationTransaction({ hash });
} catch (e) {
  // 3. if it fails, resubmit the user operation via drop and replace
  const { hash: newHash } = await client.dropAndReplaceUserOperation({
    uoToDrop: request,
  });
 
  // 4. wait for the new user operation to be mined
  await client.waitForUserOperationTransaction({ hash: newHash });
}

In the above example, we only try to drop and replace once before failing completely, but you can build more complex retry logic using this combination of waitForUserOperationTransaction and dropAndReplace.