Book a free call

ERC-4337 Best Practices: Ship Smart Wallets Safely

ERC-4337 gives wallets recovery, spending limits and gas sponsorship, and each power adds a way to go wrong. These practices keep a smart wallet launch safe.

Hertiv engineering teamReviewed by a senior engineer
7 min read
ERC-4337 Best Practices: Ship Smart Wallets Safely
On this page

Short answer

ERC-4337 best practices mean using reviewed account code, simulating every UserOperation before it runs, capping what a paymaster will spend per user and per day, choosing bundlers with a fallback ready, and testing recovery on a testnet before launch. Each rule closes a specific way a smart wallet launch can fail.

What are ERC-4337 best practices?

ERC-4337 best practices are the rules teams follow when they ship smart contract wallets: use reviewed account code, validate every UserOperation before it runs, cap what a paymaster will spend, choose bundlers with care, and test on a testnet before real money moves.

The standard gives wallets new powers: recovery, spending limits, gas sponsorship and batched actions. Each power adds a way to go wrong. A paymaster without a cap can be drained, a custom account contract can hide a bug, and a UserOperation that nobody simulated can fail on chain and still cost gas. The practices below close those gaps one by one.

This article is for teams building or integrating ERC-4337 wallets. It assumes you know the basic flow: a user signs a UserOperation, a bundler submits it, and the EntryPoint contract checks and runs it. The ERC-4337 guide covers that flow in full.

How do you choose a safe account contract?

The account contract is the wallet, so it is the first place to get right. Use a reviewed account implementation from a known source rather than writing one from scratch, and keep any custom logic small enough to audit in one sitting.

Account choiceWhen it fits
Reviewed implementationMost teams: tested code, known audit history
Small custom logic on topOne extra rule, such as a spending limit
Full custom accountRare: only with a full audit and a testnet pilot

Pin the EntryPoint version your accounts were built against. The EntryPoint is the single contract that checks and runs every UserOperation, and your account code is written to match one version of it. Mixing versions is a common source of failures that only show up in production.

Deploy accounts through a factory with deterministic addresses, so the address is known before the account exists. That lets your app show a user their wallet address and accept deposits before the first UserOperation, which makes onboarding smoother and avoids a second round of address changes.

How do you validate UserOperations before they run?

A UserOperation is a message that asks the chain to do work, and every one should be checked before it is submitted. Simulate it first: run it against the current state and confirm it would succeed, with gas limits that cover the real cost.

Check the fields, not just the signature. The sender, nonce, gas limits and paymaster data all need to match what your app expects, and a UserOperation with an odd nonce or a gas limit far above the norm should be rejected before it reaches a bundler.

Log every rejected operation with a reason. A sudden rise in rejections usually means a bug in your app or an attack on your paymaster, and the log is what tells you which one. The implementation guide shows where validation sits in the build.

How do you set paymaster rules that cannot be drained?

A paymaster agrees to pay gas for users, which makes it a target. Set a cap per user and per day, and a cap on the total the paymaster will spend, so one bad actor cannot empty it in an afternoon.

Paymaster ruleWhat it stops
Per-user daily capOne address burning the budget
Total monthly capRunaway spend across all users
Action allowlistSponsoring calls you never meant to fund
Daily spend alertA slow drain nobody notices

Allowlist what the paymaster will sponsor. A paymaster that pays for any call will eventually pay for calls you never meant to fund. Restrict it to the actions your app needs, and reject everything else before it reaches the chain.

Watch spend daily and pause fast. Set an alert when spend passes a threshold, and give one person the job of pausing sponsorship when the alert fires. A paymaster that nobody watches is an open tap, and the bill is the first sign that something went wrong.

How do you pick a bundler?

A bundler collects UserOperations and submits them to the EntryPoint, and its behaviour shapes what your users experience. Check its reputation, its uptime record and how long its bundles usually take to land.

Compare fees and latency before you commit. A bundler that charges more but lands bundles quickly can be cheaper for your users than a slow one that wastes their time, and a bundler with a public status page is easier to debug when something stalls.

Keep a second bundler ready. If your main bundler goes down, your users' operations stop, so have a fallback configured and tested. Running your own bundler is an option for teams with the traffic to justify it, but it is a service to operate, not a setting to switch on. The account abstraction guide explains the bundler role in the wider flow.

How do you budget gas and sponsorship?

Gas is the running cost of every UserOperation, and sponsorship turns it into your bill. Estimate it honestly: simulate each operation type, add the pre-verification gas the EntryPoint charges, and keep a margin for price spikes.

Sponsorship needs a budget line, not a guess. Work out the cost per user per month from real usage, set the paymaster caps from that number, and review the spend every week. If the cost per user climbs, find out why before you raise the cap.

Let users pay in tokens where it helps. A paymaster can accept payment in a token other than ether, which suits apps whose users hold that token. The exchange rate still needs a rule: fix it per operation or cap the token amount, so a price swing does not leave the paymaster short. The Ethereum best practices guide covers keeping gas costs under control.

How do you design recovery and session keys?

Recovery is the reason many teams choose smart wallets, and it needs a design, not just a feature flag. Give users more than one recovery path: a second device, a trusted contact or a time-locked recovery, so losing one path does not lock them out.

Session keys should be narrow by design. A key that lets an app act for a session should cover only that app, only small amounts and only a short time. A session key without limits is a risk if the device is lost, and the limits are what make it safe to use.

Test recovery before launch. Walk through a lost-device scenario with real users and time it: if recovery takes days of back-and-forth, users will call support instead. The gaming adoption guide shows how recovery and simple sign-in change how many new players finish joining.

Example (hypothetical): a wallet caps sponsorship before launch

Example (hypothetical): a wallet app plans to sponsor gas for 5,000 new users in its first month. The team simulates the main actions and finds each costs about $0.02 in gas, and each user runs about 10 sponsored actions, so the expected bill is 5,000 times 10 times $0.02, which is $1,000.

The team sets the paymaster cap at $0.20 per user per day, which covers 10 actions, and a total cap of $1,500 for the month. It also allowlists the three actions the app needs and rejects everything else. In the first week, 800 users sign up and run 7,400 actions, which costs $148, inside the budget.

In week two, spend jumps. The logs show one address running the same action hundreds of times, so the team pauses that address and tightens the per-user cap. The total bill for the month lands at $940, under the $1,500 cap, because the caps and the logs caught the abuse early.

What are the common mistakes?

The most common ERC-4337 mistakes are shipping custom account code, leaving the paymaster open, and trusting one bundler for everything.

  • Custom account code. Writing your own account contract instead of using a reviewed one is the most expensive shortcut. Keep custom logic small and audited.
  • Paymaster without caps. Sponsorship without per-user and total caps can be drained in hours. Set both before launch.
  • No simulation. Submitting UserOperations without simulating them first wastes gas on failures that a dry run would have caught.
  • One bundler. A single bundler is a single point of failure. Keep a fallback configured and tested.
  • Session keys without limits. A session key with no amount or time limit is a risk if the device is lost. Scope every key.
  • Skipping testnet. Testnets exist so failures are cheap. Run the full flow there, including recovery, before mainnet.

Key takeaways

  • Use reviewed account code and keep custom logic small enough to audit
  • Simulate every UserOperation and check its fields before it reaches a bundler
  • Cap paymaster spend per user, per day and in total, and allowlist the actions it funds
  • Keep a second bundler ready so one outage does not stop your users
  • Scope session keys tightly and test recovery on a testnet before launch

Frequently asked questions

What is ERC-4337?

ERC-4337 is a standard for account abstraction on Ethereum and compatible chains. It replaces the usual signed transaction with a UserOperation, which a bundler submits to the EntryPoint contract. The wallet itself is a smart contract, so it can offer recovery, spending limits and gas sponsorship.

What is a UserOperation?

A UserOperation is a message that describes what a smart wallet should do, such as sending tokens or calling a contract. The user signs it, a bundler collects it with others, and the EntryPoint contract checks the signature and runs it. It replaces the single signed transaction of a normal wallet.

What is a paymaster?

A paymaster is a contract that agrees to pay the gas fee for a UserOperation when certain conditions are met. It can sponsor gas in full or accept payment in another token. Teams set per-user and total caps so sponsorship cannot be drained by one address.

What is a bundler?

A bundler collects UserOperations from many users and submits them together to the EntryPoint contract. Bundlers check that each operation is well formed, simulate it to make sure it will not fail, and are paid from the gas fee attached to each operation.

How do session keys work in ERC-4337?

A session key lets an app act for a user during one session without asking for approval on every action. The key is limited to one app, to small amounts and to a short time, so a stolen key cannot drain the account. The limits are set in the wallet contract.

Should I run my own bundler?

Most teams start with a bundler service and keep a second one as a fallback. Running your own bundler makes sense when traffic is high enough to justify operating it, since it is a service with uptime, fees and monitoring to manage, not a setting to switch on.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Adding ERC-4337 wallets to your product? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Smart Contract Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call