0
function selectWinner() public view returns(address)
     {
         require(msg.sender==manager);
         require(participants.length>=3);
         uint r=random();
         address payable winner;
         uint index = r % participants.length;
         winner=participants[index];
         winner.transfer(getBalance());
         participants=new address payable[](0);
     }

2 Answers 2

1

you are changing a state here.

function selectWinner() public view returns(address)
     {
         require(msg.sender==manager);
         require(participants.length>=3);
         uint r=random();
         address payable winner;
         uint index = r % participants.length;
         winner=participants[index];
         winner.transfer(getBalance());
         participants=new address payable[](0);  // <---- HERE
     }

when view functions are called, which enforces the state to stay unmodified as part of the EVM execution. (solidity docs reference)

When you have view or pure modifiers you can return the value but if you remove the view modifier best solution to get a value from the execution is using event logs, if you need the value to return do remove the state change

0

The fix is simple: removing the word view from your function declaration. Because your function either modifying the state or having fund transfer:

function selectWinner() public returns(address) { 
 require(msg.sender==manager);
 ...
}

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.