In Remix IDE, it's straightforward to deploy a smart contract and then execute a function call to see the debug log for gas usage. How may we see the gas consumption for function calls in hardhat? Assume that npx hardhat node is run on a different tab while contract is also deployed npx hardhat run --network localhost scripts/deploy.js

enter image description here


After a lot of trial and error, the following worked for me. First, we have to add web3 to hardhat. To do this run the following command in the terminal.

$ npm install --save-dev @nomiclabs/hardhat-web3 web3

Then open hardhat.config.js file and add dependency.


Finally we can estimate gas with web3 in our tests.

    var methodSignature = web3.eth.abi.encodeFunctionSignature(func);
    var encodedParameter = web3.eth.abi.encodeParameter("string", "ABCDEFGH");

    var data = methodSignature //method signature
        + encodedParameter.substring(2); //hex of input string without '0x' prefix

    let gas = await provider.estimateGas({
                                           from: owner.address,
                                           to: addr2.address,
                                           data: data,
                                           value: 1000000000000000,
                                           function(estimatedGas, err) {
                                               console.log("estimatedGas: " + estimatedGas);
                                               console.log("Err:" + err);
    console.log("Gas: " + gas);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.