I'm trying to test my contracts using Truffle and Ganache, but I'm facing some problems with some functions. When I call a non view function, I'm getting the error "out of gas". Moreover, if I try to use the "estimateGas" function to check how much gas is spent on that transaction, the function isn't recognized and the console says that "estimateGas is not a function" even where, if I log my contract function, it clearly says that estimateGas exists.

I'm leaving here the code I'm using in the Javascript test file.

const MyContract= artifacts.require("MyContract");

contract("MyContract", (accounts) => {
  it("should construct correctly", async () => {
    let users = [
    let names = ["Jack", "Mark", "Jack"];
    let ids = [1, 2, 3];

    const instance = await MyContract.deployed();
    console.log(instance.fillUser.estimateGas); // This log says that estimateGas is a function
      .fillUser(users, names, ids)
      .estimateGas({ from: accounts[0], gasPrice: 20000000000 }, function (
      ) {
      }); // This fails because estimateGas is not recognized as a function

  • Try .fillUser.estimateGas(users, names, ids, { from: ...}). Commented Jan 7, 2021 at 9:36
  • BTW, you don't need to pass gasPrice to it (and you do need to pass value, if the function is payable). Commented Jan 7, 2021 at 9:37
  • @goodvibration it's not payable, it's just not a view-only function, it changes some logic in smart contract (changes mapping values)
    – Fsanna
    Commented Jan 7, 2021 at 9:46
  • That was a 'BTW' comment. Have you tried my initial suggestion? Commented Jan 7, 2021 at 9:46
  • @goodvibration I tried it right now, it doesn't give the error anymore but it doesn't log anything either, the gasAmount is not shown in the console
    – Fsanna
    Commented Jan 7, 2021 at 9:48

1 Answer 1


It is called wrongfully. It should be:

const instance = await MyContract.deployed();
const gasCost = await instance.fillUser.estimateGas(users, names, ids, { 
    from: accounts[0], 
    gasPrice: 20000000000 

Note the parameters are passed to estimateGas not to the contract function fillUser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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