> For the complete documentation index, see [llms.txt](/llms.txt).

# Use an ERC-20 paymaster with a smart account

`delegation toolkit` `smart accounts kit` `ERC-20 paymaster` `smart accounts`MetaMask Developer Relations | Sep 2, 2025

Open in Claude

This tutorial walks you through using an ERC-20 paymaster with [MetaMask Smart Accounts](/smart-accounts-kit/concepts/smart-accounts/), enabling users to pay gas fees in USDC. This tutorial uses Pimlico's paymaster, but you can use any paymaster of your choice.

## About paymasters

A paymaster is an important component of the [account abstraction (ERC-4337)](/smart-accounts-kit/concepts/smart-accounts/) standard, responsible for abstracting gas fees for end users. There are different types of paymasters, such as gasless paymasters and ERC-20 paymasters. While a gasless paymaster covers the transaction on behalf of the user, an ERC-20 paymaster allows users to pay gas fees using a supported ERC-20 token. This removes the need for users to hold native tokens, allowing them to perform onchain actions using only stablecoins.

## Prerequisites

- Install [Node.js](https://nodejs.org/en/blog/release/v18.18.0) v18 or later.
- Install [Yarn](https://yarnpkg.com/), [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), or another package manager.
- [Create a Pimlico API key](https://docs.pimlico.io/guides/create-api-key#create-api-key).

## Steps

### 1. Install the Smart Accounts Kit

Install the [Smart Accounts Kit](https://www.npmjs.com/package/@metamask/smart-accounts-kit) in your project:

**npm**

```bash
npm install @metamask/smart-accounts-kit

```

**Yarn**

```bash
yarn add @metamask/smart-accounts-kit

```

**pnpm**

```bash
pnpm add @metamask/smart-accounts-kit

```

**Bun**

```bash
bun add @metamask/smart-accounts-kit

```

### 2. Set up a Public Client

Set up a Public Client using Viem's [createPublicClient](https://viem.sh/docs/clients/public) function. You will configure a [smart account](/smart-accounts-kit/development/reference/glossary#metamask-smart-account)**MetaMask smart account** A smart contract account created using the Smart Accounts Kit that supports programmable behavior, flexible signing options, and ERC-7710 delegations. and Bundler Client with the Public Client, which you can use to query the [signer](/smart-accounts-kit/development/reference/glossary#signer)**Signer** An account that can sign transactions for a smart account.'s account state and interact with the blockchain network.

```typescript
import { createPublicClient, http } from 'viem'
import { sepolia as chain } from 'viem/chains'

const publicClient = createPublicClient({
  chain,
  transport: http(),
})

```

### 3. Set up a Paymaster Client

Set up a [Paymaster Client](/smart-accounts-kit/development/reference/glossary#paymaster)**Paymaster** A service that pays for user operations on behalf of a smart account.using Viem's [createPaymasterClient](https://viem.sh/account-abstraction/clients/paymaster) function. This client interacts with the paymaster service, enabling users to pay gas fees in USDC.

Replace `<YOUR-API-KEY>` with your Pimlico API key:

```typescript
import { createPaymasterClient } from 'viem/account-abstraction'

const paymasterClient = createPaymasterClient({
  transport: http('https://api.pimlico.io/v2/11155111/rpc?apikey=<YOUR-API-KEY>'),
})

```

### 4. Set up a Bundler Client

Set up a Bundler Client using Viem's [createBundlerClient](https://viem.sh/account-abstraction/clients/bundler) function. You can use the [bundler](/smart-accounts-kit/development/reference/glossary#bundler)**Bundler** An ERC-4337 component that manages the alternate mempool: it collects user operations from smart accounts, packages them, and submits them to the network. service to estimate gas for user operations and submit transactions to the network.

To use the ERC-20 paymaster, configure the `paymasterContext` with the ERC-20 token you wish to use to pay for gas fees. For this tutorial, specify the Sepolia USDC token address.

```typescript
import { createBundlerClient } from 'viem/account-abstraction'

// USDC address on Ethereum Sepolia.
const token = '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238'

const bundlerClient = createBundlerClient({
  client: publicClient,
  transport: http('https://your-bundler-rpc.com'),
  paymasterContext: {
    token,
  },
})

```

### 5. Create and fund a smart account

Create a [Hybrid smart account](/smart-accounts-kit/guides/smart-accounts/create-smart-account/#hybrid-smart-account). A Hybrid smart account is a flexible smart account implementation that supports both an [EOA](/smart-accounts-kit/development/reference/glossary#externally-owned-account-eoa)**Externally owned account (EOA)** A private-key-controlled account with no built-in programmable execution logic. owner and any number of [passkey](/smart-accounts-kit/development/reference/glossary#passkey)**Passkey** A cryptographic key that can be used to sign transactions instead of a private key. (WebAuthn) signers.

```typescript
import { Implementation, toMetaMaskSmartAccount } from '@metamask/smart-accounts-kit'
import { privateKeyToAccount } from 'viem/accounts'

const account = privateKeyToAccount('0x...')

const smartAccount = await toMetaMaskSmartAccount({
  client: publicClient,
  implementation: Implementation.Hybrid,
  deployParams: [account.address, [], [], []],
  deploySalt: '0x',
  signer: { account },
})

```

Fund the smart account with some Sepolia USDC to pay gas fees.

note

You can use [Circle's faucet](https://faucet.circle.com/) to get Sepolia USDC.

### 6. Send a user operation

The ERC-20 paymaster works by transferring the token from the smart account, and reimbursing itself for paying the gas fees on the user's behalf.

To send a [user operation](/smart-accounts-kit/development/reference/glossary#user-operation)**User operation** A pseudo-transaction object defined by ERC-4337 that describes what a smart account should execute. User operations are submitted to the alternate mempool managed by bundlers. with the ERC-20 paymaster, use the [sendUserOperation](https://viem.sh/account-abstraction/actions/bundler/sendUserOperation) method from the Bundler Client. You must include a call approving the ERC-20 token to be used by the paymaster. To modify the token allowance for the paymaster, perform a write operation on the USDC contract. For this tutorial, set an allowance of 10 USDC tokens.

note

In a production dapp, you should first check the existing token allowance and only approve the amount required by the paymaster.

Batch the approve call with other onchain actions you want to perform using the ERC-20 paymaster. Pass the `paymasterClient` from [Step 3](#3-set-up-a-paymaster-client) to the `paymaster` property.

```typescript
import { parseAbi, parseEther } from 'viem'

// Appropriate fee per gas must be determined for the bundler being used.
const maxFeePerGas = 1n
const maxPriorityFeePerGas = 1n

const pimlicoPaymasterAddress = '0x777777777777AeC03fd955926DbF81597e66834C'

// 10 USDC in wei format. Since USDC has 6 decimals, the wei value is 10 * 10^6.
const approvalAmount = 10000000n

const userOperationHash = await bundlerClient.sendUserOperation({
  account: smartAccount,
  calls: [
    // USDC approve call
    {
      // USDC token address
      to: token,
      abi: parseAbi(['function approve(address,uint)']),
      functionName: 'approve',
      args: [pimlicoPaymasterAddress, approvalAmount],
    },
    // Batch the approve call with other onchain actions.
    {
      to: '0x1234567890123456789012345678901234567890',
      value: parseEther('1'),
    },
  ],
  maxFeePerGas,
  maxPriorityFeePerGas,
  paymaster: paymasterClient,
})

```

## Next steps

- Learn more about [smart account implementations](/smart-accounts-kit/guides/smart-accounts/create-smart-account/).
- To sponsor gas for end users, see how to [send a gasless transaction](/smart-accounts-kit/guides/smart-accounts/send-gasless-transaction/).

[Share](https://www.facebook.com/sharer/sharer.php?https://metamask.io/tutorials/use-erc20-paymaster)[Tweet](http://twitter.com/share?text=Checkout Use an ERC-20 paymaster with a smart account published by @MetaMask&url=https://metamask.io/tutorials/use-erc20-paymaster)Copy

On this page
- [About paymasters](#about-paymasters)
- [Prerequisites](#prerequisites)
- [Steps](#steps)
  - [1. Install the Smart Accounts Kit](#1-install-the-smart-accounts-kit)
  - [2. Set up a Public Client](#2-set-up-a-public-client)
  - [3. Set up a Paymaster Client](#3-set-up-a-paymaster-client)
  - [4. Set up a Bundler Client](#4-set-up-a-bundler-client)
  - [5. Create and fund a smart account](#5-create-and-fund-a-smart-account)
  - [6. Send a user operation](#6-send-a-user-operation)
- [Next steps](#next-steps)
