> ## 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"}
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 { 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](/javascript/reference/zerodev/estimate-user-operation-gas) - Estimate gas cost for a user operation (makes network calls)
* [simulateZerodevUserOperation](/javascript/reference/zerodev/simulate-zerodev-user-operation) - Full simulation with asset diffs and security validation
* [sendUserOperation](/javascript/reference/zerodev/send-user-operation) - Send a user operation
* [calculateEvmTransactionFee](/javascript/reference/evm/calculate-evm-transaction-fee) - Fee estimation for standard EVM transactions
* [getNetworksData](/javascript/reference/wallets/get-networks-data) - Get network configuration objects
