how do I know if the msg.sender is the owner (the person that deployed) of the contract or a random address that interacted with the contract? in this case for example:

    address payable owner  =  msg.sender;   

    modifier onlyOwner(){
        require(msg.sender == owner);
    function close() public onlyOwner { 

why isn't the msg.sender reset at each interaction?


Solidity enables constructors which have code executed at contract creation. So you need to instantiate owner in the constructor to catch the contract deployer address :

address payable owner;
constructor() public {
owner = msg.sender;

There are also standardized contracts to manage ownership. Exemple of implementation can be found here : https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol.

  • yes, I know that but my question was a bit different. for example how does the contract know that I am not the owner? msg.sender is the person that interacts with the contract – Newdude Jan 9 at 10:55
  • 1
    owner is set at the deployment. So let's say for exemple the msg.sender who deploys the contract is the address "0xabc...". Your smart contract then registers owner=0xabc.... Next, if someone with the address "0x123..." interacts with the contract and calls the close function, the modifier will see that "0x123..." (the msg.sender) is different from the owner address ("0xabc...") and the transaction will be rejected. – clement Jan 9 at 11:09
  • 1
    oh ok thnkx :)) – Newdude Jan 10 at 0:03

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.