0

I've just learned that one can easily obtain the return-value of a non-constant function, by using myContract.func.call() (instead of myContract.func(), which retrieves a response object).

Up until now, I've always added inside func an event containing the return-value, and then parsed the response object returned from myContract.func() in order to extract that value.

This has also been the guidance in dozens of answers across his website!

Questions:

  1. How is it possible that so many people here have missed this?
  2. Is it possibly just a tweak added in Truffle, or can one rely on it when writing an operational Web3.js script?

For the record, I am using Truffle v4.1.14, which relies on Web3 v0.20.6.

Here is the contract that I've used for testing this:

pragma solidity 0.4.24;

contract MyContract {
    uint public kkk;
    function func() public returns (uint) {
        kkk += 100;
        return kkk;
    }
}

And here is the actual test:

contract("Test", function(accounts) {
    it("MyContract", async function() {
        const myContract = await artifacts.require("MyContract").new();
        const x = await myContract.func();
        const y = await myContract.func.call();
        console.log(x);
        console.log(y.toFixed());
    });
});

Thank you!

  • When called as .call() it shouldn't create a transaction, any change should not be persistent. Are you testing with ganache or geth/parity? – Ismael Aug 26 at 21:01
  • @Ismael: Ganache. Are you saying that I should have called kkk() after everything and check its value? – goodvibration Aug 27 at 4:20
  • Yes, ´myContract.func.call()´ does not modify contracts storage. I asked a related question previously that is not answered yet ethereum.stackexchange.com/questions/28040/…. – Ismael Aug 27 at 6:01
  • @Ismael: So it is in fact not the correct way of executing non-constant functions in your tests (unless, perhaps, you're interested in a unit-test for that specific function). And it is most certainly not the correct way of executing non-constant functions in your operational scripts (my 2nd question). Am I right? That would also mean that at least part of the answer below is wrong. – goodvibration Aug 27 at 7:11
  • Yes, he is mixing web3 v0.20 syntax with v1.2 documentation. Also Truffle v4 has a different syntax. – Ismael Aug 27 at 12:43
2

I always use myContract.func.call(). Actuall I have never heard that myContract.func().

After all this is the recommended way of doing it!! Furthermore, the performance is better (I think).

  1. Maybe that's your perception? Read the official web3 documentation. I read every page of it. Here is the link to the correct article.
  2. I don't use truffle and it works nonetheless. This function is implemented in plain web3.

I hope this information helps.

  • You are mixing syntax of web3 v0.20 with v1.2 documentation. Also the question was about Truffle v4 that has different syntax than web3 v1.2 and closer to web3 v0.20. – Ismael Aug 27 at 12:41

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.