Best Practices/Recommendations:
  • We recommend users to rely on the exact values returned by the eth_getUserOperationGasPrice endpoint rather than applying buffers, as the values provided by our bundler are designed to handle gas price volatility effectively.
  • When sending multiple UserOps back-to-back, querying the nonce from different providers may cause inconsistencies and stale nonces. For high-throughput use cases, we recommend:
    • Using parallel nonces so ordering doesn’t matter.
    • Managing nonces internally (query once, then increment locally for subsequent UserOps).
Gelato does not provide its own onchain paymaster, but you can use any onchain paymaster that is compatible with ERC-4337. If you’re looking to sponsor gas off-chain in a more efficient way, consider using Gas Tank.

Using Viem

To start sponsoring transactions or paying gas fees with ERC-20 tokens with onchain paymasters using Viem, 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, createPaymasterClient, 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 Paymaster Client

Create a PaymasterClient with Paymaster URL. Learn more about Paymaster Client here.
const paymasterClient = createPaymasterClient({ 
    transport: http('Paymaster URL'), 
})
5

Create a Bundler Client

Create a BundlerClient with the account, paymasterClient and publicClient and pass the apiKey as query parameter to the transport option. Learn more about Bundler Client here.
const bundlerClient = createBundlerClient({
    account,
    paymaster: paymasterClient,
    client: publicClient,
    transport: http(`https://api.gelato.digital/bundlers/${chainID}/rpc?apiKey=${apiKey}`),
    userOperation: {
        estimateFeesPerGas: async ({ account, bundlerClient, userOperation }) => {
            const gasPrices =
                await bundlerClient.request<EthGetUserOperationGasPriceRpc>({
                    method: "eth_getUserOperationGasPrice",
                    params: [],
                });
            return {
                maxFeePerGas: BigInt(gasPrices.maxFeePerGas),
                maxPriorityFeePerGas: BigInt(gasPrices.maxPriorityFeePerGas),
            };
        },
    },
})
6

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 of sponsoring gas or paying gas fees with ERC-20 tokens using onchain paymasters. To use features like gas sponsorship, ERC-20 token payments, or other custom payment methods via onchain paymasters, follow these steps:
1

Create a API Key

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

Include Paymaster Fields

You must pass all required paymaster-related fields such as paymaster, paymasterData etc. along with the standard UserOperation parameters.
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 & paymasterVerificationGasLimit can be fetched from eth_estimateUserOperationGas API Endpoint.
Check out the required parameters for on-chain paymasters in the following scenarios: