Code to change owner

0 votes
Beginner = Tried the below code is not changing owner/account upon compiling. Any help will be appreciated

pragma solidity >=0.4.22 <0.7.0;
contract Owner {

    address private owner;
    
    event OwnerSet(address indexed oldOwner, address indexed newOwner);
    
    // modifier to check if caller is owner
    modifier isOwner() {
        _;
    }
    
    constructor() public {
        owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
        emit OwnerSet(address(0), owner);
    }
    function changeOwner(address newOwner) public isOwner {
        emit OwnerSet(owner, newOwner);
        owner = newOwner;
    }
    function getOwner() external view returns (address) {
        return owner;
    }
}
Sep 26, 2020 in Blockchain by Wilson
• 140 points

edited Sep 28, 2020 by Gitika 438 views

Hi, @Wilson,

Are you facing any error while executing your code? As you have mentioned that the owner is not getting changed, does it giving any error?

2 answers to this question.

0 votes

@Wilson,

I would suggest you to go through this: 

modifier isOwner() {

require(msg.sender == owner,'you are not owner');

_;

}

contract Example {

constructor() public {}

function checkOwner() external isOwner() {

//is owner modifier called before function body then function can excute.

}

}
answered Oct 5, 2020 by Roshni
• 10,480 points
0 votes

Hi, @Wilson,

U have to write the require error handling function in modifier and then u have to store the variable in msg.sender..like constructor public(){

Owner = msg.sender;

}

Modifier onlyOwner(){

require(msg.sender == owner);

}
answered Oct 5, 2020 by Rajiv
• 8,910 points

Related Questions In Blockchain

0 votes
1 answer

Solidity code to change value of name

Try this for setName: function setName(string _name) public ...READ MORE

answered Jul 30, 2018 in Blockchain by slayer
• 29,310 points
264 views
+1 vote
1 answer

I am unable to change port of composer-rest-server. Please help!

Use full format for running a business ...READ MORE

answered Mar 27, 2018 in Blockchain by Christine
• 15,830 points
616 views
0 votes
1 answer

Can I change the code of the node and runit usually?

Say, you run an unmodified ethereum node, ...READ MORE

answered Jun 19, 2018 in Blockchain by charlie_brown
• 7,780 points
128 views
0 votes
1 answer

How to change "gasLimit" on Azure private blockchain?

Before deploying your network on Azure you ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,170 points
310 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,830 points
725 views
0 votes
1 answer
0 votes
1 answer

How to set the ERC20 token price in respect to ether in solidity?

Hi, @Jas, I would suggest you go through ...READ MORE

answered Nov 9, 2020 in Blockchain by anonymous
• 10,480 points
2,661 views