Search code examples
smartcontractsnearprotocol

View vs Change methods of Smart Contracts


I have been reading the docs and evaluating examples when I discovered two types of functions in smart contracts: view and change methods.

// View methods are read only. They don't modify the state, but usually return some value.

// Change methods can modify the state. But you don't receive the returned value when called.

from https://docs.nearprotocol.com/hackathon/hackathon-startup-guide-10-min

How strict are those requirements? What is going to happen if those assumptions are not held (e.g. state gets modified inside a view method or no modifications are done inside a change method)?


Solution

  • As berryguy said, near view will fail if the function tries to change the state. However, I as suggested here https://github.com/nearprotocol/NEPs/pull/3#discussion_r306526647 the restriction is artificial and should be dropped. We should allow users to run any function as near view and treat it as a dry run, because it has multiple nice usages:

    • A user can dry run a function to estimate how much gas it consumes;
    • A user can dry run to see how function will behave with the current
      state of the contract.