11

I have a method defined as: getNames() view public , if i call on it contractInstance.getNames.estimatedGas() it returns me a non-zero value, why? I mean, view methods should not consume any gas! On the other way, since this is a call it does not return to me a tx hash so i can't invoke the eth.gettransactionReceipt, how can i check if it really consumed gas ?

Furthermore, supposing method() is a set function of my deployed contract, hence a transaction mining is needed, which is the difference between calling:

contractInstance.method(args,{from:web3.eth.coinbase, gas:...})

and calling:

contractInstance.method.sendTransaction(args,{from:web3.eth.coinbase, gas:...})

2 Answers 2

13

I mean, view methods should not consume any gas! On the other way, since this is a call it does not return to me a tx hash so i can't invoke the eth.gettransactionReceipt, how can i check if it really consumed gas ?

Pure and view functions actually do cost gas. But by calling them via RPC only your local node has to do the calculation and therefore the gas cost is simulated.

If a deployed contract calls these functions everyone in the network has to do the calculation and therefore they do have to cost gas.

Furthermore, supposing method() is a set function of my deployed contract, hence a transaction mining is needed, which is the difference between calling:

contractInstance.method(args,{from:web3.eth.coinbase, gas:...})

and calling:

contractInstance.method.sendTransaction(args,{from:web3.eth.coinbase, gas:...})

The difference here is that by using sendTransaction you can send ether to the function. If the function is not payable it will fail.

4
  • Now is more clearer! One more thing, the .sendTransaction hence is used to call a function that can receive ether right? On the other way, if a function is not payabale it will just get the fucntion parameters with no ether. ? May 24, 2018 at 9:44
  • Your first point is correct. If you want to use a method that isn't payable you don't have to use sendTransaction. A non-payable function will always revert if you try to send eth to it.
    – Chris
    May 25, 2018 at 17:05
  • By the way: You actually don't have to use sendTransaction at all. You can specify the value you want to send as parameter as shown here: ethereum.stackexchange.com/a/27522/40051
    – Chris
    May 25, 2018 at 17:06
  • Couldn't a deployed contract call a view or pure function without costing gas? Why does everyone need to perform the calculation if no change in state has been made? Aug 28, 2019 at 6:48
12

In solidity, if a function reads state but does not modify any state, this needs to be declared as view function.

If a function does not read any state and also does not modify any state, this can be declared as pure function.

If a function modifies state in the blockchain, to invoke the method you need to make a transaction in the blockchain and sendTransaction is the api to do this.

estimateGas api provides the amount of gas to be incurred to access/modify in the blockchain.

In solidity, only making transaction will cost you gas. If you just directly call view/pure function, it will not cost you any gas.

Point to be noted that, if a view function is internally called inside a non view function, so this must be transaction as it is modifying state(non view function).

Any operation in the blockchain state will cost you gas in case of making transaction.

sendTransaction is only for sending ether, is not true. Sending ether/ updating any variable of a contract means changing in the state of blockchain. So this will be transaction and this will cost ether.

estimateGas is an api which gives the estimation of gas to be incurred if that method is called as a part of a transaction.

transactionReceipt is returned only when a transaction has been added in the blockchain after being mined. So, in case of invoking pure/view function, you will not get either transaction hash or receipt.

Obviously, if a function needs to receive ether at the time of transaction, only then you will add payable modifier to the function.

Hope it will help you to understand the concept of transaction in ethereum blockchain.

For further information, go through the documentation of solidity. Solidity Documentation

Your Answer

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

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