Solidity – Fixing Remix Error: Function Declared as View but Potentially Modifies State

eventsremixsolidityview

This code is inside a view function and gives the error : Function declared as view but this potentially modifies state.

if ( auction.iS == false){      //check if such auction exists
emit NoSuchAuction(msg.sender, _auctionName); 
return; }

NoSuchAuction() is an event.
_auctionName is a string.
auction is a structure and iS is a boolean.

Best Answer

Events take a place in the blockchain which can be filtered. But functionality of a view function cannot make any changes in the blockchain state. That's why you are getting the error. Use return, to get the parameters back, if event is required, remove the view from it. View & event cannot live together.

Know more about state change : http://solidity.readthedocs.io/en/latest/contracts.html?highlight=emit#view-functions

Related Topic