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 25 in Blockchain by Wilson
• 140 points

edited Sep 28 by Gitika 46 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 by Roshni
• 4,630 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 by Rajiv
• 3,160 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 29, 2018 in Blockchain by slayer
• 29,300 points
113 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,810 points
463 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 18, 2018 in Blockchain by charlie_brown
• 7,780 points
71 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,030 points
189 views
0 votes
1 answer
0 votes
1 answer

How to change Endorsement policies

Well the good news is that it ...READ MORE

answered Jul 18, 2018 in Blockchain by charlie_brown
• 7,780 points
112 views
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,810 points
409 views
0 votes
1 answer