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:

2 Answers
2

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:

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. ?
– rollotommasiMay 24 '18 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.
– Jambo DevMay 25 '18 at 17:05

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.