Drop and Replace

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.

If sponsoring gas, like in the example below, each call to sendUserOperation and dropAndReplace will generate pending gas sponsorships in your dashboard. This can result in you hitting your gas manager limit. At the moment, pending sponsorships expire after 10 minutes of inactivity, but it is possible that retrying excessively can temporarily exhaust your sponsorship limits.

1import { client } from "./client";
2
3// 1. send a user operation
4const { hash, request } = await client.sendUserOperation({
5 uo: {
6 target: "0xTARGET_ADDRESS",
7 data: "0x",
8 value: 0n,
9 },
10});
11
12try {
13 // 2. wait for it to be mined
14 const txHash = await client.waitForUserOperationTransaction({ hash });
15} catch (e) {
16 // 3. if it fails, resubmit the user operation via drop and replace
17 const { hash: newHash } = await client.dropAndReplaceUserOperation({
18 uoToDrop: request,
19 });
20
21 // 4. wait for the new user operation to be mined
22 await client.waitForUserOperationTransaction({ hash: newHash });
23}

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.