Skip to main content
Calculates the total fee for a ZeroDev user operation given its gas limits and max fee per gas. Returns fee data in native units (wei), human-readable format (ETH), and optionally USD. Unlike estimateUserOperationGas, this function is synchronous and does not make any network calls — it computes the fee from gas values you already have (e.g., after calling estimateUserOperationGas or building a user operation manually).

Installation

npm install @dynamic-labs-sdk/zerodev

Usage

import { calculateFeeForUserOperation, estimateUserOperationGas } from '@dynamic-labs-sdk/zerodev';
import { getNetworksData } from '@dynamic-labs-sdk/client';
import { parseGwei } from 'viem';

const networks = getNetworksData();
const networkData = networks.find((n) => n.networkId === '1');

const feeData = calculateFeeForUserOperation({
  userOperationData: {
    callGasLimit: 100_000n,
    verificationGasLimit: 50_000n,
    preVerificationGas: 21_000n,
    maxFeePerGas: parseGwei('50'),
  },
  networkData,
});

console.log(`Estimated fee: ${feeData.humanReadableAmount} ETH`);

Parameters

ParameterTypeDescription
userOperationDataobjectGas values for the user operation
userOperationData.callGasLimitbigintGas limit for the call phase
userOperationData.verificationGasLimitbigintGas limit for the verification phase
userOperationData.preVerificationGasbigintFixed pre-verification gas overhead
userOperationData.maxFeePerGasbigintMaximum fee per gas unit in wei
networkDataNetworkDataNetwork configuration. Get this from getNetworksData()
nativeTokenPriceUsdnumber (optional)USD price of the native token, used to calculate usdAmount

Returns

EvmTransactionFeeData (synchronous — not a Promise):
FieldTypeDescription
nativeAmountbigintTotal fee in wei: (callGasLimit + verificationGasLimit + preVerificationGas) × maxFeePerGas
humanReadableAmountstringFee in ETH, formatted for display
usdAmountstring (optional)Fee in USD. Present only when nativeTokenPriceUsd is provided
gasEstimatebigintTotal gas units: callGasLimit + verificationGasLimit + preVerificationGas
maxFeePerGasbigintThe maxFeePerGas value passed in

Examples

Display fee before sending

import {
  calculateFeeForUserOperation,
  sendUserOperation,
} from '@dynamic-labs-sdk/zerodev';

const feeData = calculateFeeForUserOperation({
  userOperationData: {
    callGasLimit: 100_000n,
    verificationGasLimit: 50_000n,
    preVerificationGas: 21_000n,
    maxFeePerGas: parseGwei('30'),
  },
  networkData,
});

console.log(`This operation will cost ~${feeData.humanReadableAmount} ETH`);

const receipt = await sendUserOperation({ walletAccount, calls });

With USD conversion

const ETH_PRICE_USD = 3200;

const feeData = calculateFeeForUserOperation({
  userOperationData: {
    callGasLimit: 100_000n,
    verificationGasLimit: 50_000n,
    preVerificationGas: 21_000n,
    maxFeePerGas: parseGwei('50'),
  },
  networkData,
  nativeTokenPriceUsd: ETH_PRICE_USD,
});

console.log(`Fee: ${feeData.humanReadableAmount} ETH ≈ $${feeData.usdAmount}`);

Error handling

import { FeeEstimationFailedError } from '@dynamic-labs-sdk/client';

try {
  const feeData = calculateFeeForUserOperation({ userOperationData, networkData });
} catch (error) {
  if (error instanceof FeeEstimationFailedError) {
    console.error('Fee calculation failed:', error.message);
  }
}