1

I'm trying to change the an address data variable to a one, which is selected via a function.

Keep getting the below error.

Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.

address public winneraddress;

function getHighestBidID() public onlyOwner view returns (uint highestID, address _highestidaddress) {
    require(bids.length > 0,"Bid length");
    uint highestAmount = 0;
    for (uint i=0; i<bids.length; i++) {
        if (bids[i].amount > highestAmount) {
            highestAmount = bids[i].amount;
            highestID = i;
            _highestidaddress = bids[highestID].bidderAddress;
        }
    }
    winneraddress = _highestidaddress;
}
        mapping(address => uint256) public payments; //List of payments done to the Contract

    function makeBid(string memory _extraData) public inState(State.Active) payable returns(uint bidIterator) {
    require(msg.value > 0, "No value inserted");
    require(bidders <= 3, "Contract reached max registrations");
    require(payments[msg.sender] == 0, 'Sender previously paid into escrow.');
    bids.push(Bid(msg.sender, msg.value, _extraData, true));
    payments[msg.sender] = msg.value;
    bidders ++; 
    return bids.length - 1;
} 
    /*function withdraw() external inState(State.WinnerSelected) payable{
    require(msg.sender != winneraddress && winneraddress != address(0));
    payments[msg.sender].transfer(bids[msg.value].amount);
}*/

Any ideas how to fix it and what is causing the error?

  • Either get rid of the view, or get rid of the assignment winneraddress = _highestidaddress;. – goodvibration Jul 2 at 13:11
1

Let's go over the compilation error that you're getting:


Function declared as view

That one is clearly true, since this is how you've declared the function:

function getHighestBidID() public onlyOwner view ...

But this expression modifies the state

That one is also true, since you are indeed modifying one of the state-variables:

winneraddress = _highestidaddress;

A view function can read state-variables, but it cannot write (modify) them.

So either get rid of the view, or get rid of the winneraddress = _highestidaddress.

  • Thank you, much appreciated. – user51821 Jul 2 at 14:03
  • I'd like to set a require statement that winneraddress is not required. require (!= winneraddress), any ideas what is the exact syntax? Thanks again. – user51821 Jul 2 at 14:08
  • @user51821: If you explain to what what you mean by "winneraddress is not required", then I might be able to answer that. – goodvibration Jul 2 at 14:33
  • I'm trying to write a function where users can withdraw the funds they deposited in the contract. However, the winner whose address we've set above using highest address wont be able to withdraw since this amount will only be withdrawn by the contract owner. Thus a function for all the bidders except the one selected winner by the contract owner. – user51821 Jul 2 at 16:19
  • So function withdraw should start with require(msg.sender != winneraddress) but you should probably also ensure that function getHighestBidID has already been invoked in order to determine the winner, so perhaps your statement in function withdraw should actually be require(msg.sender != winneraddress && winneraddress != address(0)). – goodvibration Jul 2 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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