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

# Managing Gas Limits

When interacting with the peaq network, managing gas limits allows you to maintain **predictable** transaction costs, and safeguard against unexpected gas spikes.
By explicitly setting a **gas limit**, you prevent overspending, if the network suddenly requires more gas than anticipated. Libraries like `ethers.js`, `web3.js`, etc.
make it straightforward to configure this behavior. For the purpose of demonstration we'll be using `ethers.js`.

## Prerequisites

* You understand basic concepts of EVM-like networks: transactions, gas, and gas pricing.
* You have Node.js and `ethers.js` installed.
* You have access to a **funded account** and the peaq RPC endpoint.
* You know how to manage sensitive information using environment variables (e.g., `.env` files).

## Instructions

### 1. Setting Up the Environment

* **Install Dependencies:**
  Ensure you install the necessary packages:

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
npm install ethers dotenv
```

* **Set ESM Module:**
  Add the following to your `package.json` to alllow for ESM modules.

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
"type": "module",
```

* **Configure Environment Variables:**
  Create a `.env` file with the following content:

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
PRIVATE_KEY=0xYOUR_PRIVATE_KEY
PEAQ_RPC_URL=https://quicknode.peaq.xyz
```

Make sure your `.env` file is listed in `.gitignore` so it's not checked into version control.

### 2. Setting up the Provider and Wallet

Create a `sendTransaction.js` file (for example) and load the environment variables using `dotenv`. Initialize the provider with the peaq RPC endpoint and load the wallet from the environment variable.

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
//sendTransaction.js

import dotenv from 'dotenv';
dotenv.config();
import { ethers } from 'ethers';

// Use environment variables from .env
const provider = new ethers.providers.JsonRpcProvider(process.env.PEAQ_RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
```

### 3. Deciding on a Gas Limit

Choose a suitable gas limit. Start with a safe upper bound, and adjust later as needed.

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
const gasLimit = 200000; // Example: Adjust as needed based on your transaction's complexity
```

### 4. Crafting the Transaction

Include the `gasLimit` in the transaction object. This ensures the transaction will revert if it exceeds the specified gas, protecting you from unexpected costs.

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
const tx = {
    to: '0xRECEIVER_ADDRESS', // Replace with the address you want to send to
    value: ethers.utils.parseEther('0.01'), // Example amount to send
    gasLimit: gasLimit
};
```

### 6. Sending the Transaction

Send the transaction and wait for it to be mined. If the transaction fails due to exceeding gas, you’ll catch the error in the `try/catch` block.

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
async function sendTransaction() {
    try {
        const response = await wallet.sendTransaction(tx);
        console.log('Transaction hash:', response.hash);

        const receipt = await response.wait();
        console.log('Transaction confirmed in block:', receipt.blockNumber);
    } catch (error) {
        // Failure could be due to insufficient gasLimit
        console.error('Transaction failed:', error);
    }
}

sendTransaction();
```

## 7. Adjusting Gas Limits Over Time

* If your transaction often runs out of gas, consider **raising** the limit.
* If you're consistently using less gas than your limit, you might **lower** it to be more cost-effective.
* For automated scripts or systems that regularly send transactions, predefining a gas limit helps protect against sudden gas spikes.
* Consider a dynamic approach like [Estimating Gas Fees](/peaqchain/build/basic-operations/gas-operations/estimate-gas-fees) in your calculation of a gas limit to prevent failed transactions.

## Summary

By incorporating environment variables, you keep sensitive data secure and easily manage configurations for different environments. Using the `ethers.js` library,
explicitly setting a **gas limit** gives you fine-grained control over your transaction costs on the peaq network. With a carefully chosen gas limit, you can execute
transactions confidently, knowing you're safeguarded against unexpected fee changes.
