What a paymaster kit does

A paymaster kit is a code module that lets your application pay gas fees on behalf of users. In the ERC-4337 ecosystem, it acts as a sponsor, not a bank. Users keep their assets; the paymaster simply covers the transaction costs to make the experience frictionless.

When a user interacts with a smart account, the paymaster kit intercepts the operation. It verifies the request, covers the gas cost, and submits the transaction to the bundler. This allows users to pay fees in ERC-20 tokens like USDC instead of holding native tokens like ETH.

Why is the Year of Embedded Finance
1
User initiates a transaction

The user signs a user operation (UserOp) through their smart account, requesting a specific action on the dApp.

2
Paymaster kit validates

The paymaster kit checks if the request meets your sponsorship rules, such as valid signatures or token balances.

Why is the Year of Embedded Finance
3
Kit pays the gas

The paymaster submits the UserOp to the mempool, covering the gas fees so the user pays nothing upfront.

This separation of duties is critical. The paymaster kit handles the logistics of gas payment, while the smart account handles the authorization. This architecture enables gasless onboarding and token-based fee payments without exposing private keys to third parties.

Why is the Year of Embedded Finance

Choose your sponsorship model

Deciding between full gas sponsorship and ERC-20 fee payment sets the foundation for your paymaster kit. This choice dictates how you configure your smart contracts, manage user onboarding, and handle backend costs. Alchemy defines ERC-4337 paymasters as tools that enable dapps to sponsor user operations or allow users to pay for gas in ERC-20 tokens and stablecoins.

Full Gas Sponsorship

With full sponsorship, your platform covers all gas costs for the user. This approach removes friction during onboarding, making it ideal for first-time users or high-value actions where you want to eliminate hesitation. You pay the network fees directly, which simplifies the user experience but requires your backend to manage fiat-to-crypto conversions and maintain a gas reserve.

ERC-20 Fee Payment

ERC-20 payment allows users to pay transaction fees using their own token balance, such as USDC or DAI. This model shifts the cost burden to the user, reducing your operational expenses while keeping them within the ecosystem. Users must hold the specific supported token, which adds a layer of complexity but ensures you are not subsidizing every transaction.

Comparison of Models

Use this comparison to evaluate which model fits your specific use case.

FeatureFull SponsorshipERC-20 Payment
Gas CostPaid by platformPaid by user
User FrictionLow (no token needed)Medium (needs token balance)
ImplementationSimple backend logicRequires token approval flow
Best ForOnboarding & marketingEstablished user bases

Implementation Considerations

Your choice impacts the complexity of your paymaster contract. Full sponsorship requires robust monitoring of gas prices and budget limits to prevent abuse. ERC-20 payment requires handling token approvals and ensuring the user has sufficient balance before signing. Both models can be combined in a single kit to offer flexibility, allowing users to choose their preferred payment method at runtime.

Integrate the paymaster kit

This section guides you through connecting your backend infrastructure to a smart account provider. We will use the Safe Protocol Kit as the reference implementation, as it provides a robust abstraction for ERC-4337 account factories and bundlers. You can adapt these steps to other providers like MetaMask Smart Accounts by swapping the specific SDK imports, but the configuration logic remains identical.

Why is the Year of Embedded Finance
1
Install the required SDKs

Begin by installing the core packages in your project directory. You need the Safe Provider SDK to manage the smart account instance and the Paymaster SDK to handle sponsorship logic. For a TypeScript environment, run the following command to add the necessary dependencies:

Shell
Shell
npm install @safe-global/safe-ethers-lib @safe-global/safe-core-sdk-types @safe-global/protocol-kit @safe-global/safe-modules-sdk @safe-global/safe-ethers-lib

Ensure your project is configured to target ES2020 or higher, as the SDK relies on modern JavaScript features like BigInt and optional chaining. Verify the installation by importing the SafeProvider in a test file to catch any version mismatches early.

Why is the Year of Embedded Finance
2
Configure the Safe Provider

Initialize the provider with your RPC endpoint and the Safe singleton address. The provider acts as the bridge between your application and the blockchain, handling signature aggregation and transaction execution. Create a configuration object that includes the chain ID and the specific Safe version you intend to support.

TypeScript
TypeScript
import { SafeProvider } from '@safe-global/safe-ethers-lib';

const provider = new SafeProvider({
  provider: infuraProvider,
  safeSingletonAddress: SafeSingletonAddress.SAFE_V1_4_1,
});

This step establishes the read-only connection to the network. You will later attach a signer to this provider to execute transactions on behalf of the user.

3
Deploy the Paymaster contract

You must deploy a custom Paymaster contract to your target chain. This contract holds the funds (ETH or ERC-20 tokens) used to sponsor user transactions. Use Hardhat or Foundry to compile and deploy the contract, ensuring you have sufficient native token balance in the deployed address.

For ERC-20 sponsorship, the contract must approve the bundler to spend your tokens. After deployment, record the contract address and the ABI for use in the next step. Do not use testnet tokens for production integration; verify the contract interaction on a block explorer first.

4
Link the Paymaster to the Safe Account

Connect the deployed Paymaster contract to your Safe Protocol Kit instance. This configuration tells the smart account which Paymaster to call during transaction execution. You must pass the Paymaster address and the specific mode (e.g., StakeAware or ERC20) to the kit configuration.

TypeScript
TypeScript
import { SafeKit } from '@safe-global/protocol-kit';

const safeKit = new SafeKit({
  provider,
  signer,
  paymasterAddress: '0xYourPaymasterAddress',
  paymasterMode: PaymasterModes.ERC20,
});

Once linked, the Safe Kit will automatically include the Paymaster data in the user’s transaction request. Test this connection by attempting a small transaction; if successful, the gas fees will be deducted from your Paymaster contract, not the user’s wallet.

Fund and test the paymaster

Before deploying to mainnet, you must ensure the paymaster contract holds sufficient funds to cover user operations and that it correctly processes them on a testnet. This section walks you through funding the contract and verifying sponsorship logic using the eth_sendUserOperation flow.

1
Fund the paymaster contract

Transfer native testnet tokens (ETH on Sepolia or Holesky) directly to your paymaster contract address. Most ERC-4337 paymasters require a minimum balance to initiate sponsorship. If your paymaster supports ERC-20 gas payments, you must also approve and transfer the specific token (e.g., USDC) to the contract. Use a faucet for testnet ETH or bridge funds from your development wallet. Verify the balance using a block explorer or a simple balanceOf call if your contract implements it.

2
Verify contract balance

Confirm that the contract holds enough funds to cover the estimated gas cost of a typical user operation. A single user operation on Sepolia typically costs less than $0.01 in gas, but you should fund the contract with at least 0.01–0.05 ETH to allow for multiple test transactions. If your paymaster uses a strict balance check, an insufficient balance will cause the user operation to fail with a PaymasterAndData error.

3
Test user operations on testnet

Use a bundler (such as the Alchemy Bundler or a local node) to submit a test user operation via eth_sendUserOperation. Ensure the paymasterAndData field is correctly encoded with your contract address and any necessary sponsorship parameters. Monitor the transaction in the bundler’s response and on a block explorer. A successful sponsorship will show the gas fees paid by the paymaster contract rather than the user’s wallet.

4
Confirm gas refund logic

If your paymaster charges users in ERC-20 tokens or implements a post-operation refund mechanism, verify that the balance changes are correct. Check the user’s token balance and the paymaster’s token balance before and after the transaction. Ensure that the gas refund logic does not leave the contract in a state where it cannot cover future operations.

5
Review bundler and node logs

Check the bundler and node logs for any errors or warnings related to the paymaster. Look for UserOperation reverted messages that indicate the paymaster rejected the operation due to invalid signatures, insufficient funds, or failed validation. Resolve any issues before proceeding to mainnet deployment.

Frequently asked: what to check next