How can we pre-compute Gas needed for successful execution of a function inside a smart contract. I got to know about web3.eth.estimateGas but cannot follow it completely from the docs.

My function receives some input parameters, calls Oraclize and then updates data in mappings inside the contract. My understanding is that in order to calculate gas estimate, value of input parameters must be used every time. What is the correct way to calculate gas estimates in this scenario, please help..

   function createProof (string _title, string _ipfshash, string 
    _proofremarks, string _prooftags)
    payable public {        
    /*** Some Logic ***/
   /// Call Oraclize to get current timestamp
  • estimateGas is indeed the right way. Perhaps you could show us the code where you're trying to get the gas estimate and call the function. – user19510 Aug 17 '18 at 7:34
  • @Smarx below is my function to estimate Gas: function howmuchgas(params, fileId) { const gasEstimate = Blockproof.createProof.estimateGas((params["title"], fileId, params["remarks"], params["tags"]),{ from: web3.eth.accounts[0] } ); console.log(gasEstimate);}. Here Blockproof is the name of my contract and createProof is the name of my function whose gas I wish to estimate. On running this code I am getting ' Cannot read property 'estimateGas' of undefined' in my Chrome Browser Console. – tanmay Aug 17 '18 at 10:48
  • Which version of web3.js are you using? According to the error, Blockproof.createProof is undefined. – user19510 Aug 17 '18 at 13:29
  • I am using "web3": "^0.20.7", and no web3.js. Also have used below commands in my app.js to access the contract abi json import blockproof_artifacts from "../../build/contracts/Blockproof.json"; const Blockproof = contract(blockproof_artifacts); import { default as Web3 } from "web3"; – tanmay Aug 17 '18 at 13:39
  • You need to u se an instance of the contract, like you would to actually call the function. E.g. const blockproofInstance = BlockProof.at('0x1234...') – user19510 Aug 17 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.