How do I give access to only owner to call the function? As when I try this code owner is not able to access or else both owner as well as others are able to access the function.

 modifier _ownerOnly(){
   require(msg.sender == owner);


  function getAllUsers()public view _ownerOnly returns (address[], string[], string[], string[], string[]){

    address[] memory wallet_addresses = new address[](registeredArray.length);
    string[]  memory _ids = new string[](registeredArray.length);
    string[] memory firstNames = new string[](registeredArray.length);
    string[] memory lastNames = new string[](registeredArray.length);
    string[] memory emailIds = new string[](registeredArray.length);
    for (uint i = 0; i < registeredArray.length; i++) {
        wallet_addresses[i] = registeredArray[i].wallet_address;
        _ids[i] = registeredArray[i].userId;
        firstNames[i] = registeredArray[i].first_name;
        lastNames[i] = registeredArray[i].last_name;
        emailIds[i] = registeredArray[i].email_id;
    return (wallet_addresses, _ids, firstNames, lastNames, emailIds);

2 Answers 2


You have to use the modifier on your function like so.

address owner;

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

constructor(address _owner){
owner = _owner;

function MyOwnedFunction(address arg1, uint arg2) public onlyOwner {
  // do things only Owner can do here

The owner variable needs to be set to whatever address you would like before you can use it.

You could potentially just hardcode in whatever address you wanted, for example:

address public owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;

But a common convention is to set your owner variable inside your contract's constructor so that it is equal to the address that deployed the contract.

Which would look something like this:

address public owner;

constructor () {
  owner = msg.sender;

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.