# Gas Estimation

Vanar has a different gas fees structure to charge gas fees for transactions based on their transaction size. It is important for  developers and end users (such as dApp users with Metamask wallet) to pass the right amount of gas when sending the transactions to the Vanar network.

The Vanar protocol by default estimates gas for the first tier (gas limit up to 12,000,000 gas) if no gas limit is provided when doing the gas estimation for a transaction. If a transaction is going to consume more than 12,000,000 gas then the gas estimation will fail if no gas limit is provided, as the default limit is set to 12,000,000.

You always get a working gas estimate if you pass the full block limit as gas limit (that is 30,000,000) when estimating the gas for a transaction. You can safely pass this estimated gas when sending the transaction to the network. Below are different scenarios of how this works:

Below is the sample code for gas estimation with [web3.js](https://web3js.readthedocs.io/) library.

### Estimate without gas limit

Below is sample code where gas is estimated without passing the gas limit.

```javascript
const estimatedGas = await myContract.methods.methodToCall(parameters).estimateGas();
                                
console.log('Estimated gas is:', estimatedGas);
```

#### Possible results:

1. It will estimate the gas correctly if the maximum gas required for the transaction will be less than 12,000,000 gas which is the max limit for tier 1 in Vanar protocol.
2. It will return an error showing the gas required is exceeded the limit like:

   `Error sending transactions: Error: Returned error: gas required exceeds allowance (12000000)`

### Estimate with gas limit

Below is sample code where gas is estimated by passing the gas limit for estimation.

```javascript
const estimatedGas = await myContract.methods.methodToCall(parameters)
                                .estimateGas({gas: 30000000});
                                
console.log('Estimated gas is:', estimatedGas);
```

#### Possible results:

1. It will estimate the gas correctly if the maximum gas required for the transaction will be less than the maximum block size which is 30,000,000 gas in Vanar protocol.
2. It will throw an error showing the gas required is exceeded the limit like:

   `Error sending transactions: Error: Returned error: gas required exceeds allowance (30000000)`

Refer to the official [web3.js docs](https://web3js.readthedocs.io/en/v1.10.0/web3-eth-contract.html?highlight=estimateGas#methods-mymethod-estimategas) to learn more about gas estimation in general.


---

# Agent Instructions: 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:

```
GET https://docs.vanarchain.com/builders/for-developers/gas-estimation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
