Inheritance Programming Assignment

Here it is…

function changeOwner(address _newOwner) public onlyOwner {
owner = __newOwner;

1 Like

My solution guess:

pragma solidity 0.5.1;

contract ownable{

  address public owner;

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

  constructor() public{
    owner = msg.sender;
  }

  function ownerHandOver(address newOwner) public onlyOwner returns (address) {
    require(newOwner != address(0));
    owner = newOwner;
  }

}

1 Like
function changeOwnership(address _newOwnerAddress) public onlyOwner {
        owner = _newOwnerAddress;
    }
1 Like
pragma solidity 0.5.11;

contract Ownable {

    address public owner;
    
    constructor() public {
        owner = msg.sender;
    }
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    
    function passOwnership(address newOwner) public onlyOwner{
        require(newOwner != owner, "You already owner of this contract!");
        owner = newOwner;
    }
}
1 Like

pragma solidity 0.5.1;

contract Ownable{

address public owner;

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

constructor() public {
    owner = msg.sender;
}

function NewOwnerChange (address _address) public onlyOwner {
    owner = _address;
    
}

}

1 Like

function setNewOwner(address _newOwner) public onlyOwner {
owner = _newOwner;
}

1 Like
pragma solidity ^0.5.1;

contract Ownable{
    
    address public owner;

    event Confirmed(string);
    
    modifier onlyOwner(){
    require(owner == msg.sender);
    _;
    }
    
    constructor() public {
        owner = msg.sender;
    }
    
    function transferOwnership(address _newOwner) public onlyOwner returns (string memory) {
        owner = _newOwner;
        emit Confirmed("Transfer of Ownership Completed");
        return string("Transfer of Ownership Completed");
    }
}

After I finished the assignment (I swear I didn’t peek :innocent:), I read a comment about returning a confirmation string, so I tried to add the equivalent above. I also included an event and emit function as mentioned in the response to that comment. I’ve seen a few examples of the event and emit keywords in my research, but I would love to know if there is a more appropriate way of using them for the above example.

1 Like

Good idea! I would have made a few small changes. Instead of sending the string “Transfer of Ownership Completed” to the event. We can get the same info but naming the event NewOwner and instead sending the new owner as the event parameter. In that way we can find the history of the owners.

But there was nothing wrong with your previous code. Good job!

pragma solidity ^0.5.1;

contract Ownable{
    
    address public owner;

    event NewOwner(address newOwner);
    
    modifier onlyOwner(){
    require(owner == msg.sender);
    _;
    }
    
    constructor() public {
        owner = msg.sender;
    }
    
    function transferOwnership(address _newOwner) public onlyOwner returns (string memory) {
        owner = _newOwner;
        emit NewOwner(owner);
        return string("Transfer of Ownership Completed");
    }
}
1 Like
function updateOwner(address _newOwner) onlyOwner public{
    owner = _newOwner;
}

fix to updating owner by owner only in Ownable contract

1 Like

This is my solution!!

pragma solidity 0.5.1;

contract Ownable {
address public owner;

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

constructor() public{
    owner = msg.sender;
}

function newOwner(address _newOwner) public onlyOwner{
    owner = _newOwner;
}

}

1 Like

Add:

function newOwner(address _newOwner) public onlyOwner {
owner = _newOwner;
}

1 Like

pragma solidity 0.5.1;

contract Owner {

struct newOwner {
    address newOwner;
}

mapping(address => newOwner) new_owner;

address public owner;

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

constructor() public {
    owner = msg.sender;
}

function _newOwner(address _owner) public {
    new_owner[msg.sender].newOwner = _owner;
}

function display_newOwner() view public returns (address) {
    return new_owner[msg.sender].newOwner;
}

}

1 Like
 function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0)) 
        owner = newOwner;
    }

i added a require statement to check if the new address is a more valid address than 0, obviusly there is ungodly more possibility, but i will leave this for later.

1 Like

just cheched the solution video :tropical_drink::sunglasses::smoking:

2 Likes

Hello, here’s my solution for the exercise.

pragma solidity 0.5.1;

contract Ownable{
    
    address public owner;
    
    modifier onlyOwner(){
        require(msg.sender == owner);
        _;
    }
    
    constructor() public {
        owner = msg.sender;
    }
    
    function changeOwner(address newOwner) public onlyOwner {
        require (newOwner != msg.sender);
        owner = newOwner;
        
        
    }
    
}

I’m a bit confused with some of the answers I have seen. I’m probably not as tech gifted as most folks on here. I have just googled how to change owner on and on solidity I have found this:

My question is what does “require (newOwner != msg.sender);” add to the simple version solidity offers?

Hello MartinH,

this additional code is not absolutely necessary, but it has one advantage:

It checks if the new owner’s address is different from the old owner’s address. So in this solution these two addresses have to be different.

In the version without this check an owner could transfer the animal to himself by providing his own adddress to the transferOwnership function. While this doesn’t make much sense, it is possible in the version without the additional code.

So you can argue that this scenario is not what the function transferOwnership is supposed to do. And therefore you can add this code in order to prevent this particular scenario.

Hope this helps.

Best regards

Ouzo69

1 Like

To use the modifier is important, because only the owner himself should be allowed to call this function and give the ownership to someone else. One weakness of this solution is that the current owner could transfer the animal to himself by providing his current address to this function. But since this makes no sense and also requires to provide transactions fees, the owner will refrain from using the transferOwner function in this way.

pragma solidity 0.5.1;

contract Ownable{

    address public owner;
    


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

    constructor() public{
        owner = msg.sender;
    }
    
    transferOwner(address newOwner) public onlyOwner() {
        owner = newOwner;
    }
}

pragma solidity 0.5.1;

contract Ownable{

address public owner;



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

constructor() public{
    owner = msg.sender;
}

transferOwner(address newOwner) public onlyOwner() {
    owner = newOwner;
}

}

1 Like

I was thinking of making a centralized registry of different possible addresses the contract would change ownership too. but i assume to only changing ownership at random so here is my solution.

          contract Ownable {

address public owner;

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

constructor() public {
    owner = msg.sender;
}

function changeOwner(address _newAddress) public onlyOwner {
    owner = _newAddress;
}

}

1 Like