[Ethereum] Does view functions cost gas? And how to send transaction in a proper way

contract-developmentcontract-invocationtransactionsview

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:...})

Best Answer

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.