> For the complete documentation index, see [llms.txt](https://docs.botanixlabs.com/botanix/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.botanixlabs.com/botanix/build-applications/build-on-the-botanix-evm/gas-fees.md).

# Gas fees

{% hint style="warning" %}
**TL;DR**

Some versions of the Ethers.js library hardcode the `maxPriorityFee` to 1.5 gwei which may be unnecessarily high depending on the current base fee. To avoid overpaying for a transaction, calculate the `maxPriorityFee` manually.
{% endhint %}

Gas fees are a critical component of the Ethereum Virtual Machine (EVM) ecosystem, ensuring that the network remains secure and efficient. They incentivize validators to process transactions and execute smart contracts, playing a pivotal role in maintaining decentralized operations.

### The Role of Gas Fees in the EVM

Every transaction on the EVM requires computational resources, and gas fees are the mechanism by which users pay for those resources. Gas fees consist of two main components:

1. **Base Fee**: Introduced with Ethereum's London Hard Fork (EIP-1559), the base fee is a mandatory, network-determined amount that adjusts dynamically based on network demand. It is burned, reducing the total ETH supply, which helps stabilize gas prices over time.
2. **Priority Fee (Tip)**: The priority fee is an optional amount that users can include to incentivize miners or validators to prioritize their transactions. This tip is paid directly to the miner or validator and is in addition to the base fee.

### The Problem with Hardcoded maxPriorityFee in Ethers.js

The popular Ethers.js library, widely used by developers for building decentralized applications (dApps), hardcodes the `maxPriorityFee` to 1.5 gwei in some versions. While this value might suffice in high-demand situations, it often leads to users overpaying for transactions, particularly when network or application demand is low.

#### Example Scenario

* **Base Fee**: 0.000000008 gwei
* **Max Priority Fee**: 1.5 gwei (hardcoded by Ethers.js)

In this scenario, the priority fee is disproportionately high compared to the base fee. Instead of paying a small additional amount to incentivize miners, the user ends up paying almost the entire transaction cost as a tip. This inefficiency can significantly increase transaction expenses, especially for frequent users or in low-cost networks like Botanix.

### The Fix: Calculating the maxPriorityFee Dynamically

To prevent overpayment, developers should avoid relying on the hardcoded `maxPriorityFee` value provided by Ethers.js. Instead, they can calculate an appropriate priority fee dynamically based on the current base fee and network conditions.

#### Steps to Calculate the maxPriorityFee Dynamically

1. **Fetch the Current Base Fee** Use the network’s RPC endpoint to retrieve the current base fee from the latest block.
2. **Calculate an Appropriate Priority Fee** Determine a reasonable multiplier or fixed amount relative to the base fee. For example:

   ```javascript
   const priorityFee = Math.max(baseFee * 2, 0.0001); // Example logic
   ```
3. **Override the Default Ethers.js Behavior** Explicitly set the `maxPriorityFee` when sending transactions:

   ```javascript
   const transaction = {
       to: recipient,
       value: amount,
       maxPriorityFeePerGas: priorityFee,
       maxFeePerGas: baseFee + priorityFee
   };
   await signer.sendTransaction(transaction);
   ```

By calculating the `maxPriorityFee` dynamically, developers can significantly reduce transaction costs for their users while ensuring fair compensation for validators.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.botanixlabs.com/botanix/build-applications/build-on-the-botanix-evm/gas-fees.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
