Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set owner address when deploying a smart contract

Here is simple smart contract with owner:

pragma ^0.7.2

contract simple {
  address owner;

  constructor() public {
    //do something 
  }

  modifier() {
    require(
      owner == msg.sender,
      'No sufficient right'
    )
  }

  function setOwner() ownerOnly external {
     owner = msg.sender;
  }
}

My question is how to securely set owner address to the address of the smart contract owner?

like image 536
user938363 Avatar asked Sep 01 '25 20:09

user938363


1 Answers

You should set owner address directly in the constructor. msg.sender field will represent the contract creator.

constructor ()  {
       owner = msg.sender;
   }

And your function setOwner doesn't have a lot of sense, because even in case owner was set during creation, it can not be changed. Modifier will reject all transactions, that are sent not from owner. Otherwise it will just reassign owner to msg.sender who is also owner. It should look like this:

function setOwner(address newOwner) ownerOnly external {
     owner = newOwner;
  }
like image 198
Vladyslav Munin Avatar answered Sep 03 '25 18:09

Vladyslav Munin