> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# calculateFeeForUserOperation

> Calculate the fee for a ZeroDev user operation from known gas values

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

```bash theme={"system"}
npm install @dynamic-labs-sdk/zerodev
```

## Usage

```javascript theme={"system"}
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

| Parameter | Type | Description |
| - | - | - |
| `userOperationData` | `object` | Gas values for the user operation |
| `userOperationData.callGasLimit` | `bigint` | Gas limit for the call phase |
| `userOperationData.verificationGasLimit` | `bigint` | Gas limit for the verification phase |
| `userOperationData.preVerificationGas` | `bigint` | Fixed pre-verification gas overhead |
| `userOperationData.maxFeePerGas` | `bigint` | Maximum fee per gas unit in wei |
| `networkData` | `NetworkData` | Network configuration. Get this from `getNetworksData()` |
| `nativeTokenPriceUsd` | `number` (optional) | USD price of the native token, used to calculate `usdAmount` |

## Returns

`EvmTransactionFeeData` (synchronous — not a Promise):

| Field | Type | Description |
| - | - | - |
| `nativeAmount` | `bigint` | Total fee in wei: `(callGasLimit + verificationGasLimit + preVerificationGas) × maxFeePerGas` |
| `humanReadableAmount` | `string` | Fee in ETH, formatted for display |
| `usdAmount` | `string` (optional) | Fee in USD. Present only when `nativeTokenPriceUsd` is provided |
| `gasEstimate` | `bigint` | Total gas units: `callGasLimit + verificationGasLimit + preVerificationGas` |
| `maxFeePerGas` | `bigint` | The `maxFeePerGas` value passed in |

## Examples

### Display fee before sending

```javascript theme={"system"}
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

```javascript theme={"system"}
import { calculateFeeForUserOperation } from '@dynamic-labs-sdk/zerodev';
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

```javascript theme={"system"}
import { calculateFeeForUserOperation } from '@dynamic-labs-sdk/zerodev';
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);
  }
}
```

## Related functions

* [estimateUserOperationGas](https://docs.dynamic.xyz/docs/javascript/reference/zerodev/estimate-user-operation-gas.md) - Estimate gas cost for a user operation (makes network calls)
* [simulateZerodevUserOperation](https://docs.dynamic.xyz/docs/javascript/reference/zerodev/simulate-zerodev-user-operation.md) - Full simulation with asset diffs and security validation
* [sendUserOperation](https://docs.dynamic.xyz/docs/javascript/reference/zerodev/send-user-operation.md) - Send a user operation
* [calculateEvmTransactionFee](https://docs.dynamic.xyz/docs/javascript/reference/evm/calculate-evm-transaction-fee.md) - Fee estimation for standard EVM transactions
* [getNetworksData](https://docs.dynamic.xyz/docs/javascript/reference/wallets/get-networks-data.md) - Get network configuration objects


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.