1

The web3.js documentation states that with a contract method, call vs. transaction is chosen automatically:

Contract Methods

// Automatically uses call or sendTransaction based on the method type
myContractInstance.myMethod(param1 [, param2, ...] [, ...]);

*edited for brevity

I have tested two ways to call a function that does not mutate anything

1.testInstance.testfunc(param1, {from:eth.accounts[0]}) 2.testInstance.testfunc.call(param1)

Events are being issued with option 1 only, so I know that web3.js is choosing sendTransaction(). I expected it to automatically choose a call(), because the function doesn't change any state.

How does web3.js determine which to use?

  • 1. It is a transaction, it can modify the contract state and generate events. The 'automagically determine' doesn't work sometimes. 2. It is a call it cannot modify contract state and will not generate events. – Ismael Sep 1 '17 at 16:31
  • Thanks for clarifying your question; I think it is covered by Solidity constant. – eth Sep 2 '17 at 5:39
0

It depends on which version of Web3.js you're using.

Web3.js Stable

At version 0.19.0, web3 uses only the ABI constant keyword to decide whether to issue a call() or a sendTransaction():

SolidityFunction.prototype.execute = function () {
    var transaction = !this._constant;

    // send transaction
    if (transaction) {
        return this.sendTransaction.apply(this, Array.prototype.slice.call(arguments));
    }

    // call
    return this.call.apply(this, Array.prototype.slice.call(arguments));
};

~source

In Solidity, you need to mark your function with the constant keyword in order to generate an ABI where the function is marked constant.

Web3.js v1 Beta

Based on the documentation, it appears that contract methods will no longer be callable without specifying call() or send().

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.