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 1,854 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,870 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,370 points
1,214 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,790 points
1,124 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,220 points
850 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,720 points
764 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,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,426 views
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
5,159 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP