0

Lots of answers here on this topic, but I'm still not quite sure of this case.

If I have a non-state changing function in my contract, (a simple getter, for example) and I call it directly from say, ethers.js (i.e. its not called as part of a transaction) will gas will actually get charged to the caller? Or does the dispatcher somehow determine even without the view modifier that no transaction is required, so it will only get executed on a single node, and no gas will be charged to the caller, even without the view modifier?

3 Answers 3

0
function retVal1() public pure returns (uint256) // no gas - least when called from another contract 

function retVal2() public view returns (uint256) // no gas - more when called from another contract

function retVal3() public returns (uint256) // yes gas

retVal1 and retVal2 don't cost you any gas but retVal3 does.

However retVal1 and retVal2 will incur a gas cost if they are read from another smart contract.

0

when you call a function with the "view" or "pure" modifier, the Ethereum Virtual Machine (EVM) is able to determine that the function does not modify the contract's state, and therefore the cost of executing the function is less.

When you call a non-state changing function directly from a library like ethers.js, without a transaction, the EVM will still execute the function, but the gas costs will be significantly lower. This is because the function is executed locally on a single node, rather than being broadcast to the entire network. The gas costs will be charged to the caller, but they will be significantly lower than if the function were called as part of a transaction.

In summary, even if the function don't have view or pure, the EVM can understand that the function doesn't modify the state, the gas costs will still be significantly lower because is not a transaction and the function is executed locally on a single node, rather than being broadcast to the entire network.

0

If a function is marked as view or pure, it will not modify the state of the contract and no gas will be charged to the caller when it is called.

If the function is not marked as view or pure, but it doesn't modify the state of the contract, it will still execute on a single node, but gas will be charged to the caller since the EVM doesn't know that the function does not change the state.

Your Answer

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

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