estimateGas method in web3js — things to check

Suyash Sumaroo
1 min readMar 9, 2018

Lately, I have been having a problem concerning the estimateGas method from the web3js library. Basically, I was getting the following error: “gas required exceeds allowance or always failing transaction”.

The first thing to check is whether your transaction is valid or not. For instance, if you are estimating the gasAmount for sending a certain number of tokens to another address, then the main 2 obvious things to check are:

  1. If you have enough ether in your sending address.
  2. If you have enough tokens in your sending address.

These seem to be obvious things to check but some people could make the mistake of thinking that the method estimateGas is just to calculate estimate. This is not the case. It will run through the method without really executing any code and throw any error if the actual conditions that you set in terms of parameters are not correct.

A snippet of the code necessary to estimate the gas for sending a token:

tokenContract.methods.transfer(recipientAddress, numtokens)
.estimateGas({from: tokenHolderAddress}, function(gasAmount) {
console.log(gasAmount);
});

Hope this helps!

--

--

Suyash Sumaroo
Suyash Sumaroo

Written by Suyash Sumaroo

Experienced in the Cryptocurrency and Blockchain technology and Founder of Codevigor and Horizon Africa, companies focused on the use of Blockchain technology.

No responses yet