meow-cto.xyz

Why does my ERC-4337 UserOp keep failing with "AA21 didn't pay prefund"?

The "AA21 didn't pay prefund" error means your smart contract wallet's UserOperation failed because the entry point contract did not receive the required upfront payment for gas. This is a validation failure that happens before execution even begins, and it typically points to a problem with how your wallet or its paymaster is funding the operation.

What the prefund is and why it matters

In ERC-4337, every UserOperation must deposit enough ETH into the entry point contract to cover the maximum possible gas cost of the operation. This deposit is called the prefund. The entry point calculates it as:

prefund = (gas limit × max fee per gas) + call gas limit × max priority fee per gas

The prefund is not optional. It serves as collateral that the entry point can draw from during execution. Without it, the entry point cannot guarantee it will be paid for the work of processing your UserOp, so it rejects the operation at the validation stage with error code AA21.

Common causes and how to fix them

1. Your smart wallet hasn't deposited ETH to the entry point

If you are paying your own gas (not using a paymaster), your smart wallet contract must have deposited ETH into the entry point contract beforehand. A common mistake is assuming the wallet's native ETH balance is enough - it is not. The deposit is a separate balance tracked inside the entry point.

What to check: - Visit the entry point contract on a block explorer and look for the getDepositInfo function for your wallet address. - If the deposit is zero or below the required prefund, send ETH to the entry point's depositTo function, specifying your wallet as the account address.

2. The paymaster deposit is too low

If you are using a paymaster to sponsor gas, the paymaster contract must have deposited enough ETH into the entry point to cover the prefund. Paymasters can fail silently if their deposit runs low, especially during periods of high network congestion when gas prices spike.

What to check: - Verify the paymaster's deposit on the entry point contract. - If you run the paymaster yourself, ensure it is topped up. If you use a third-party paymaster, check their service status - they may have run out of funds.

3. Signature or validation logic fails before prefund check

The error can be misleading. In some implementations, the wallet's validateUserOp function runs first. If that function reverts (for example, due to an invalid signature or a guardian not approving a social recovery attempt), the entry point may still emit an AA21 error because the validation failure prevents the prefund from being accounted for correctly.

What to check: - Test your UserOp with a simple, known-good signature first. - Ensure your wallet's validation function does not modify storage in a way that triggers a revert during the validation phase.

4. Incorrect gas parameters in the UserOp

If you set maxFeePerGas or maxPriorityFeePerGas unrealistically low, the calculated prefund may be tiny, but the entry point may still reject the operation if the actual execution would require more gas than you specified. More often, setting these values too low causes a different error (AA13 or AA14), but in edge cases where the prefund calculation wraps or underflows, AA21 can appear.

What to check: - Use current network gas prices. Many bundlers provide a recommended gas quote endpoint. - Ensure your gas limit is realistic for the execution your UserOp triggers.

5. The bundler rejected the operation before submitting

Some bundlers perform their own prefund check and will return an AA21 error even before the UserOp reaches the entry point. This can happen if the bundler's estimate of the prefund exceeds what your wallet or paymaster has deposited.

What to check: - If you are submitting through a specific bundler (like Stackup, Biconomy, or a custom one), check their documentation for deposit requirements. Some bundlers require a separate deposit with them, not just the entry point.

How to diagnose the specific cause

  1. Simulate the UserOp using the eth_estimateUserOperationGas RPC method on the bundler. This will often give a more descriptive error than "AA21".

  2. Call the entry point's simulateValidation function directly. If the call reverts, the revert reason will usually pinpoint the exact validation failure.

  3. Check the bundler's logs if you have access. Bundlers often log the reason for rejection with more detail than the error code.

When the error persists after fixing deposits

If deposits are sufficient and gas parameters look correct, the problem may be in your wallet's implementation. Common implementation issues include:

Double-check that you are using the correct entry point address for the network. ERC-4337 entry points are not the same across all chains, and some chains have deployed multiple versions.

Not financial advice. meow-cto.xyz publishes market data and general information about Meow. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.

Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.

Back to smart wallets