Can there be a require statement in the constructor in the solidity programming language please

Viewed 13

I actually think something is wrong with my constructor


//SPDX-License-Identifier: MIT

pragma solidity 0.8.7;

contract Application{

    uint public count;
    address public owner;
    address public constant ADDR = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

    constructor(){
        require(owner == msg.sender);
        owner = msg.sender;
    }

    function getCount() public view returns(uint){
        return count;
    }

    function incCount() external {
        count++;
    }

    function deccount() public {
        count--;
    }

}

I was able to successfully compile but I'm cuurently unable to deploy. This is what the error that keeps coming on:

creation of Application errored: VM error: revert.

revert
    The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

Thank you in advance.

1 Answers

When the code executes the constructor, the value of owner starts at addr(0), so the require fails.

    constructor(){
        require(owner == msg.sender);  // here owner=0x00
        owner = msg.sender;
    }

You can remove the require to let anyone deploy.

Related