This method allows gas fees to be paid using native tokens (e.g., ETH, MATIC, etc.) under the ERC-4337 standard. It does not require a paymaster to be configured.

Using Viem

To start paying gas fees with native tokens using Viem in ERC-4337 Standard, follow these steps:
1

Create a API Key

Check out our How-To Guide for detailed instructions on generating a API key.
2

Import Dependencies

import { createPublicClient, http } from 'viem'
import { createBundlerClient, toSoladySmartAccount } from "viem/account-abstraction";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";
3

Setup Smart Account

Any smart account that implements viem’s Account type can be used here. Check out other available smart accounts here.
const publicClient = createPublicClient({ chain: mainnet, transport: http() });
const signer = privateKeyToAccount(PRIVATE_KEY as any);

const account = await toSoladySmartAccount({ 
    client: publicClient, 
    owner: signer, 
}) 
4

Create a Bundler Client

Create a BundlerClient with the account and publicClient and pass the apiKey as query parameter to the transport option. Learn more about Bundler Client here.
const bundlerClient = createBundlerClient({
    account,
    client: publicClient,
    transport: http(`https://api.gelato.digital/bundlers/${chainID}/rpc?apiKey=${apiKey}`)
})
5

Send a UserOperation

Send a UserOperation with the bundlerClient and the account.
const userOperationHash = await bundlerClient.sendUserOperation({
    account,
    calls: [{ to: account.address, value: 0n, data: "0x" }],
})

console.log("UserOperation Hash: ", userOperationHash);

Using Bundler API Endpoints

This is the standard method where users pay gas fees directly using their native tokens (like ETH, MATIC, etc.). To use this method, follow these steps:
1

Create a API Key

Check out our How-To Guide for detailed instructions on generating a API key.
2

Keep Paymaster Fields Empty

Do not include any paymaster-related fields in the UserOperation parameters. This ensures the transaction is treated as a self-sponsored native token payment.
3

Set `sponsored` query parameter to `false` or don't include it

When calling Gelato API endpoints, make sure to include the apiKey and sponsored set to false as a query parameter.Your Bundler URL will look like this:
https://api.gelato.digital/bundlers/${chainID}/rpc?apiKey=${apiKey}
4

Fetch Gas Parameters from Gelato APIs

Use the following endpoints to retrieve gas-related values:
  • maxFeePerGas and maxPriorityFeePerGas can be fetched from eth_getUserOperationGasPrice API Endpoint.
  • callGasLimit, verificationGasLimit, preVerificationGas can be fetched from eth_estimateUserOperationGas API Endpoint.
Check out the required parameters for paying gas with native tokens in the following scenarios: