0

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 = [
      "0x62CeeD6A2e86f1a4bA54cc7E9C1C1d61E229109a",
      "0xaFdB5c9612c0961Bb7f577202F5888d009ca84B0",
      "0x62CeeD6A2e86f1a4bA54cc7E9C1C1d61E229109a",
    ];
    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
    instance
      .fillUser(users, names, ids)
      .estimateGas({ from: accounts[0], gasPrice: 20000000000 }, function (
        error,
        gasAmount
      ) {
        console.log(Number(gasAmount));
      }); // This fails because estimateGas is not recognized as a function
  });
});

12
  • Try .fillUser.estimateGas(users, names, ids, { from: ...}). 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). 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
    Jan 7, 2021 at 9:46
  • That was a 'BTW' comment. Have you tried my initial suggestion? 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
    Jan 7, 2021 at 9:48

1 Answer 1

0

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.