0

This question came up reading the specifics of view functions in solidity (https://ethereum.org/en/developers/tutorials/erc20-annotated-code/):

This function is also a view, which means that it does not change the state, so it can be executed on a single node instead of having every node in the blockchain run it. This kind of function does not generate a transaction and does not cost gas.

Does this imply that when I call a view function from different node I could get different results if they are not in sync? Or is it that it only returns the state of finalized blocks?

1 Answer 1

2

developer advocate from Chainstack here.

Does this imply that when I call a view function from different node I could get different results if they are not in sync?

Yup, your understanding is correct. A view function is a read contract call. It doesn't change the state so it can be called without gas.

It is usually called by eth_call method. Below is a sample eth_call request object:

{"method":"eth_call","params":[{"from":null,"to":"0x514910771AF9Ca656af840dff83E8264EcF986CA","data":"0x70a08231000000000000000000000000271682deb8c4e0901d1a1550ad2e64d568e69909"}, "latest"],"id":1,"jsonrpc":"2.0"}

It basically retrieves information from the smart contract account; the result depends on the state of the node.

So if two nodes are out of sync, and you are calling against the "latest" block, you may get different result.

Or is it that it only returns the state of finalized blocks?

Not really, you can use eth_call to call this function against an old block. In such cases, the function returns the result from the past.

We have a blog post explaining how eth_call works, hope it may help you.

Happy coding!

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.