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

Estimate without gas limit

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

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.

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 to learn more about gas estimation in general.