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

# Estimating Gas Fees

On peaq and other EVM-compatible networks, transaction execution costs are measured in **gas**. Before sending a transaction on the network, it's often helpful to **estimate** the gas required so that you don't overpay,
or risk your transaction running out of gas. Most EVM networks provide a built-in RPC method, `eth_estimateGas`, which allows you to programmatically estimate the gas usage for a given transaction.
This estimation is done by **simulating** the transaction on a node, without actually broadcasting it.

## Prerequisites

1. **You have access to one of our EVM-Compatible RPC Endpoints:**
   * [Connecting to peaq](/peaqchain/build/getting-started/connecting-to-peaq)

2. **You know the Transaction Data you’re sending:** The parameters of the transaction you want to send are well-defined, including `from`, `to`, `data` (if interacting with a contract), and optionally `value` if sending PEAQ or the respective native token. You don't need the exact gas amount upfront; that's what you're estimating.

3. **JSON-RPC Client:** You can use a JSON-RPC client (e.g., `curl`, `Postman`, `web3.js`, `ethers.js`, or any library that supports JSON-RPC) to make requests to the RPC endpoint. In our example below we'll be sending a JSON payload using a `curl` request to an RPC Endpoint.

## Instructions

### 1. Prepare the JSON-RPC Request

Use the `eth_estimateGas` method to request a gas estimate. The parameters should describe the transaction you intend to send. For example:

```json theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
{
    "jsonrpc": "2.0",
    "method": "eth_estimateGas",
    "params": [{
    "from": "0xYourFromAddress",
    "to": "0xRecipientOrContractAddress",
    "value": "0x0",             // hex for 0
    "data": "0xYourEncodedData" // if calling a contract function
    }],
    "id": 1
}
```

<Note>Addresses and values must be in hex format. If you don't need to send value (PEAQ), you can omit it or set it to `"0x0"`</Note>

### 2. Send the Request to the RPC Endpoint

Using `curl` from your terminal, for example:

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
curl -X POST \
    -H "Content-Type: application/json" \
    --data '{
    "jsonrpc":"2.0",
    "method":"eth_estimateGas",
    "params":[{"from":"0xYourFromAddress","to":"0xRecipientOrContractAddress","data":"0xYourEncodedData"}],
    "id":1
    }' \
    https://quicknode.peaq.xyz //Your chosen RPC provider's URL
```

### 3. Read the Response

The node will return a JSON response. A successful response might look like:

```json theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
{
    "jsonrpc":"2.0",
    "id":1,
    "result":"0x5208"
}
```

The `"result"` field is a hexadecimal string representing the estimated gas limit. For example, `0x5208` (in hex) is `21000` in decimal—typical for a simple ETH transfer.

### 4. Convert and Use the Returned Value

* **Conversion:**
  If the returned gas estimate is in hex, you can convert it to a decimal integer. In many programming languages, you can parse the hex string and turn it into an integer type. For example, in JavaScript:

```javascript theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
const hexGasEstimate = "0x5208";
const gasEstimate = parseInt(hexGasEstimate, 16); // returns 21000
```

* **Adjustment for Safety:** While `eth_estimateGas` gives a good baseline, you may want to add a buffer (e.g., add 10-20% more) to ensure your transaction doesn't fail if conditions change by the time it's mined.

### 5. Use the Estimated Gas in Your Transaction

When constructing the final transaction, set the `gas` field to the estimated amount (plus any safety buffer). Consider the current gas price or fee structures (`base fee + priority fee`) when calculating the total transaction cost.

## Summary

Making an `eth_estimateGas` call over a JSON-RPC endpoint and parsing the response, you can **accurately estimate** the **gas limit** needed for your transaction on an **EVM network**. This helps ensure you neither **waste gas** nor have insufficient gas for a successful execution.
