August 2018
Intermediate to advanced
404 pages
11h 19m
English
Web3.js v1.x.x allows the user to estimate the required gas directly from the contract instance. This can be done with the help of the estimateGas method. Consider the fact that the estimation can differ from the actual utilization due to the varying state:
// Method syntaxmyContractInstance.methods .myMethod([param1[, param2[, ...]]]) .estimateGas(options [, callback])// Example
helloWorldInstance.methods .changeText("Greetings!") .estimateGas({ from: "0xce5C2D181f6DD99091351f6E6056c333A969AEC9" }, function(error, gasAmount){
console.log(gasAmount);
});
You can also use the promise returned from the function:
// Promise
helloWorldInstance.methods .changeText("Greetings!") .estimateGas({ from: "0xce5C2D181f6DD99091351f6E6056c333A969AEC9" ...