Skip to main content
Best Practices/Recommendations:
  • We recommend users to rely on the exact values returned by the gas estimation endpoints rather than applying buffers, as the values provided by our bundler are designed to handle gas price volatility effectively.
  • In case you want to estimate gas cost with some state overrides such as balance, code, etc., you can check our state override section.

Implementations

npm install @gelatocloud/gasless viem
1

Create an API Key

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

Create Bundler Client

import {
  createGelatoBundlerClient,
  sponsored,
  token,
} from "@gelatocloud/gasless";
import { createPublicClient, http, type Hex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";
import { toSoladySmartAccount } from "viem/account-abstraction";

const owner = privateKeyToAccount(process.env.PRIVATE_KEY as Hex);

const client = createPublicClient({
  chain: baseSepolia,
  transport: http(),
});

const account = await toSoladySmartAccount({
  client,
  owner,
});

const bundler = await createGelatoBundlerClient({
  account,
  client,
  apiKey: process.env.GELATO_API_KEY,
  payment: sponsored(),
});
3

Estimate Gas (Basic)

Use estimateUserOperationGas() to get gas limits for a UserOperation:
const gasEstimate = await bundler.estimateUserOperationGas({
  calls: [
    {
      to: "0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De",
      data: "0xd09de08a",
    },
  ],
});

console.log("callGasLimit:", gasEstimate.callGasLimit);
console.log("verificationGasLimit:", gasEstimate.verificationGasLimit);
console.log("preVerificationGas:", gasEstimate.preVerificationGas);
4

Get Fee Quote (with Token Payment)

Use getUserOperationQuote() to get gas estimation plus fee in your payment token:
const USDC_ADDRESS = "0x036CbD53842c5426634e7929541eC2318f3dCF7e"; // Base Sepolia

const bundlerWithToken = await createGelatoBundlerClient({
  account,
  client,
  apiKey: process.env.GELATO_API_KEY,
  payment: token(USDC_ADDRESS),
});

const quote = await bundlerWithToken.getUserOperationQuote({
  calls: [
    {
      to: "0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De",
      data: "0xd09de08a",
    },
  ],
});

console.log("Fee:", quote.fee);           // Fee in USDC
console.log("Total gas:", quote.gas);     // Total gas
console.log("L1 fee:", quote.l1Fee);      // L1 data fee (L2 chains)
console.log("callGasLimit:", quote.callGasLimit);
5

Get Gas Prices

Use getUserOperationGasPrice() to get current gas prices:
const gasPrice = await bundler.getUserOperationGasPrice();

console.log("maxFeePerGas:", gasPrice.maxFeePerGas);
console.log("maxPriorityFeePerGas:", gasPrice.maxPriorityFeePerGas);

State Overrides

While estimating gas, you can include state overrides such as balance, code, etc. to test the gas cost with different state values.
const results = await bundler.estimateUserOperationGas({
  stateOverride: [
    {
      address: account.address,
      balance: 100000000000000000000n,
    },
  ],
  calls: [{ to: account.address, value: 0n, data: "0x" }],
});

console.log("Estimated gas values:", results);
Checkout parameters for state overrides here and full example code here.

SDK Methods Reference

MethodDescriptionReturns
estimateUserOperationGas()Standard ERC-4337 gas estimationGas limits only
getUserOperationQuote()Gas estimation + fee in payment tokenGas limits + fee
getUserOperationGasPrice()Current gas pricesmaxFeePerGas, maxPriorityFeePerGas