Solidity – Remix Error: Constructor Should Be Payable If Value Is Sent

remixsolidity

Below is the code of the solidity contract :-

pragma solidity ^0.4.24;

contract Auction {
    address public owner;
    uint public startBlock;
    uint public endBlock;
    string public ipfsHash;

    enum State { Started, Running, Ended, Canceled }
    State public auctionState;

    uint public highestBindingBid;
    address public highestBidder;
    uint bidIncrement;

    mapping(address => uint) public bids;

    constructor() payable public {
        owner = msg.sender;
        auctionState = State.Running;
        startBlock = block.number;
        endBlock = block.number + 40320;
        ipfsHash = "";
        bidIncrement = 10;
    }

    modifier notOwner() {
        require(msg.sender != owner);
        _;
    }

    modifier afterStart() {
        require(block.number >= startBlock);
        _;
    }

    modifier beforeEnd() {
        require(block.number <= endBlock);
        _;
    }

    function min(uint a, uint b) pure internal returns(uint) {
        if(a <= b) {
            return a;
        } else {
            return b;
        }
    }

    function placeBid() payable public notOwner afterStart beforeEnd returns(bool) {
        require(auctionState == State.Running);
        require(msg.value > 0.001 ether);

        uint currentBid = bids[msg.sender] + msg.value;

        require(currentBid > highestBindingBid);

        bids[msg.sender] = currentBid;

        if(currentBid < bids[highestBidder]) {
            highestBindingBid = min(currentBid + bidIncrement, bids[highestBidder]);
        } else {
            highestBindingBid = min(currentBid, bids[highestBidder] + bidIncrement);
            highestBidder = msg.sender;
        }
       return true; 
    }
}

I deployed this contract by using Javascript VM but on click of PlaceBid in Remix , I get an error message stating :-.

Note: The constructor should be payable if you send value. Debug the
transaction to get more information.

As you can see that both place placeBid and constructor are payable . What is causing this and how can it be resolved?

Best Answer

The error message is a bit misleading (which is unfortunately typical with Solidity/Ethreum). The main point in the error message is transact to Auction.placeBid errored: VM error: revert. The point about payable constructor is just a guess and it's typically a bad guess.

So the problem is that your transaction reverts. It probably reverts due to some require statement. You can either try to debug the execution or just remove require statements until you find out which one causes the error.

Related Topic