> ## 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.

# calculateEvmTransactionFee

> Estimate the gas fee for an EVM transaction

Estimates the gas fee for an EVM transaction without running a full simulation. Returns fee data in
native units (wei), human-readable format (ETH), and optionally USD — but no asset diffs or
security validation.

Use this when you need a fee estimate only. Use
[simulateEvmTransaction](https://docs.dynamic.xyz/docs/javascript/reference/evm/simulate-evm-transaction.md) when you also want to
preview asset changes and security validation.

## Installation

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

## Usage

> [tab] JavaScript
>
> ```javascript theme={"system"}
> import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
> import { getNetworksData } from '@dynamic-labs-sdk/client';
> import { parseEther } from 'viem';
>
> const networks = getNetworksData();
> const networkData = networks.find((n) => n.networkId === '1'); // Ethereum mainnet
>
> if (networkData) {
>   const feeData = await calculateEvmTransactionFee({
>     transaction: {
>       from: walletAccount.address,
>       to: recipientAddress,
>       networkId: networkData.networkId,
>       value: parseEther('0.1'),
>     },
>     networkData,
>   });
>
>   console.log(`Estimated fee: ${feeData.humanReadableAmount} ETH`);
> }
> ```

> [tab] React
>
> Estimate fees inside a button handler or `useEffect`. Re-estimate whenever the transaction or network changes:
>
> ```tsx theme={"system"}
> import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
> import { getNetworksData } from '@dynamic-labs-sdk/client';
> import { useGetWalletAccounts } from '@dynamic-labs-sdk/react-hooks';
> import { useState } from 'react';
> import { parseEther } from 'viem';
>
> function FeeEstimator({ recipientAddress }) {
>   const { data: walletAccounts = [] } = useGetWalletAccounts();
>   const walletAccount = walletAccounts[0];
>   const [fee, setFee] = useState(null);
>
>   const handleEstimate = async () => {
>     if (!walletAccount) return;
>
>     const networks = getNetworksData();
>     const networkData = networks.find((n) => n.networkId === '1');
>     if (!networkData) return;
>
>     const feeData = await calculateEvmTransactionFee({
>       transaction: {
>         from: walletAccount.address as `0x${string}`,
>         to: recipientAddress,
>         networkId: networkData.networkId,
>         value: parseEther('0.1'),
>       },
>       networkData,
>     });
>
>     setFee(feeData.humanReadableAmount);
>   };
>
>   return (
>     <div>
>       <button onClick={handleEstimate}>Estimate Fee</button>
>       {fee && <p>Estimated fee: {fee} ETH</p>}
>     </div>
>   );
> }
> ```

## Parameters

| Parameter | Type | Description |
| - | - | - |
| `transaction` | `object` | The transaction to estimate fees for |
| `transaction.from` | `string` | The sender address |
| `transaction.to` | `string` | The recipient or contract address |
| `transaction.networkId` | `string` | The network the transaction runs on, matching `networkData.networkId` |
| `transaction.value` | `bigint` (optional) | Native token amount in wei |
| `transaction.data` | `string` (optional) | Encoded contract call data |
| `networkData` | `NetworkData` | Network configuration. Get this from `getNetworksData()` |
| `nativeTokenPriceUsd` | `number` (optional) | USD price of the native token, used to calculate `usdAmount` |

## Returns

`Promise<EvmTransactionFeeData>`:

| Field | Type | Description |
| - | - | - |
| `nativeAmount` | `bigint` | Total fee in wei |
| `humanReadableAmount` | `string` | Fee in ETH, formatted for display |
| `usdAmount` | `string` (optional) | Fee in USD. Present only when `nativeTokenPriceUsd` is provided |
| `gasEstimate` | `bigint` | Estimated gas units |
| `maxFeePerGas` | `bigint` (optional) | EIP-1559 max fee per gas |
| `maxPriorityFeePerGas` | `bigint` (optional) | EIP-1559 priority fee per gas |
| `gasPrice` | `bigint` (optional) | Legacy gas price. Present when EIP-1559 is not available |

## Examples

### Simple ETH transfer

```javascript theme={"system"}
import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
const feeData = await calculateEvmTransactionFee({
  transaction: {
    from: walletAccount.address,
    to: recipientAddress,
    networkId: '1',
    value: parseEther('1'),
  },
  networkData,
});

console.log(`Fee: ${feeData.humanReadableAmount} ETH`);
console.log(`Gas units: ${feeData.gasEstimate}`);
```

### With USD conversion

```javascript theme={"system"}
import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
const ETH_PRICE_USD = 3200;

const feeData = await calculateEvmTransactionFee({
  transaction: {
    from: walletAccount.address,
    to: recipientAddress,
    networkId: '1',
    value: parseEther('0.5'),
  },
  networkData,
  nativeTokenPriceUsd: ETH_PRICE_USD,
});

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

### Contract interaction

```javascript theme={"system"}
import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
import { encodeFunctionData } from 'viem';

const feeData = await calculateEvmTransactionFee({
  transaction: {
    from: walletAccount.address,
    to: tokenContractAddress,
    networkId: '1',
    data: encodeFunctionData({
      abi: erc20Abi,
      functionName: 'transfer',
      args: [recipientAddress, parseUnits('100', 18)],
    }),
  },
  networkData,
});
```

### Error handling

```javascript theme={"system"}
import { calculateEvmTransactionFee } from '@dynamic-labs-sdk/evm';
import { FeeEstimationFailedError } from '@dynamic-labs-sdk/client';

try {
  const feeData = await calculateEvmTransactionFee({ transaction, networkData });
} catch (error) {
  if (error instanceof FeeEstimationFailedError) {
    console.error('Fee estimation failed:', error.message);
  }
}
```

## Related functions

* [simulateEvmTransaction](https://docs.dynamic.xyz/docs/javascript/reference/evm/simulate-evm-transaction.md) - Full simulation with asset diffs and security validation
* [calculateSolanaTransactionFee](https://docs.dynamic.xyz/docs/javascript/reference/solana/calculate-solana-transaction-fee.md) - Fee estimation for Solana
* [calculateFeeForUserOperation](https://docs.dynamic.xyz/docs/javascript/reference/zerodev/calculate-fee-for-user-operation.md) - Fee calculation for ZeroDev user operations
* [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.