Inheritance Programming Assignment

Post your solution or questions here.

pragma solidity 0.5.1;

contract Ownable{

    address public owner;

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

    constructor() public{
        owner = msg.sender;
    }
    
    function transferOwner(address newAddress) public onlyOwner() {
         owner=newAddress ;
    }
}
5 Likes

@Capaburro your answer made me realize I totally misunderstood the question… and made it much more difficult than intended. I thought he meant to change the owner of the animal to a different address. Nonetheless, here is my solution:

DOG.SOL

pragma solidity 0.5.1;

import "./animal.sol";

contract DogContract is AnimalContract {

    // made public to the outside world to add new dogs. points to the inherited addAnimal function
    function addDog(string memory _name, uint _age) public returns (uint){
        // call the addAnimal function from the inherited animal contract
        return _addAnimal(_name, _age, AnimalType.DOG);
    }
}

ANIMAL.SOL

pragma solidity 0.5.1;

import "./ownable.sol";

contract AnimalContract is Ownable {
    
    enum AnimalType {DOG, CAT}
    
    struct Animal {
        string name;
        uint age;
        AnimalType animalType;
        
    }
    
    mapping(address => Animal[]) ownerToAnimals;

    // function name has _ due to being internal. Programming culture for internal and private functions
    // Using internal since the addDog function in dog.sol references this function to create a new AnimalType
    // and is not public as only internal functions will call on it
    function _addAnimal(string memory _name, uint _age, AnimalType _animalType) internal returns (uint){
        // return index position value of the newly added animal
        return ownerToAnimals[msg.sender].push(Animal(_name, _age, _animalType)) -1;
    }
        
    function getAnimal(uint _id) public view returns (string memory){
        return ownerToAnimals[msg.sender][_id].name;
    }
    
        // allows changing of owner
    function changeOwner(uint _id, address _newOwner) public onlyOwner {
        
        // duplicate animal to new owner's mapping
        uint newOwnerKey = ownerToAnimals[_newOwner].push(ownerToAnimals[msg.sender][_id]) -1;
        newOwnerKey;
        // delete from original owner array
        delete ownerToAnimals[msg.sender][_id];
        assert(ownerToAnimals[msg.sender][_id].age != ownerToAnimals[_newOwner][newOwnerKey].age);
    }
}

OWNABLE.SOL

pragma solidity 0.5.1;

contract Ownable {
    
    address public owner;
    
    modifier onlyOwner(){
        require(msg.sender == owner, "You are not the owner!");
        _;
    }
    
    constructor() public {
        owner = msg.sender;
    }

    // just added this last minute after seeing Capaburro's code and realizing that I misunderstood the quiz...
    function transferOwner(address newAddress) public onlyOwner() {
         // added this require after seeing the solution video for good measure and future reference
         require(newAddress != address(0));
         owner=newAddress ;
    }
}
1 Like

Function to be added to the Ownable contract is

function transferOwnership(address _newOwner) public onlyOwner {
require (_newOwner != address(0));
owner = _newOwner;
}

1 Like

pragma solidity 0.5.2;

contract Ownablefilip {

address public owner;

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

modifier onlyOwner() { //this checks conditions before running a function
    require(msg.sender == owner);
    _;
}

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

}

1 Like

To allow a new owner we have to create the inherited function in the Ownable contract. The following is the added function:

function newOwner (address _newOwner) public onlyOwner {
    //Checks to ensure only the msg.sender calls this function to set a new owner.
    //Set new owner
    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 setNewOwner(address newAddress) public onlyOwner() {
     owner=newAddress ;
}

}

1 Like

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 {
  owner = __newOwner;
}

}

1 Like

My solution:

pragma solidity ^0.5.1;

contract OwnableRelOP{
    address public owner;
    
    constructor() public{
        owner = msg.sender;
    }
    
    
    modifier onlyOwner(){
        require(msg.sender==owner, 'You are not the owner');
        _;
    }
    
/* this  function changes  ownership of contract*/
    function _RelinquishOwnership(address _NewOwnerAddress ) public onlyOwner{
        owner=_NewOwnerAddress;
    } 
    
    
}    

I wonder if there is a command so that after executing a function remix displays a prescribed message, like “Ownership has changed” .

You could return a string from the _RelinquishOwnership. That is the best solution to what you are asking. You could (and probably should) emit an event as well. Then other people could listen for that event and get notified when ownership is changed.

2 Likes

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 {
  owner = __newOwner;
}

}

1 Like

My attempt:

pragma solidity 0.5.1;

contract Ownable{
    
    address public owner;
    
    modifier onlyOwner(){
       require(msg.sender == owner);
       _;
   }
    
    constructor() public{
       owner = msg.sender;
   }
    
    function handOwnership(address _address) public {
       owner = _address;
    }
    
}
1 Like

Okay, so I missed the onlyOwner after public, noted :neutral_face:

1 Like

pragma solidity 0.5.1;

contract Ownable{

address public owner;

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

}

constructor() public{ 
   owner = msg.sender;

}

function transferOwnership(address _newOwner) public onlyOwner {
require (_newOwner != address(0));
owner = _newOwner;
}

}

1 Like

I would include these few lines in the code:

function changeOwnership(address _newOwner) public onlyOwner{
    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 changeOwner(address newOwner) public onlyOwner {
        owner = newOwner;
    }
}
1 Like

careful, I see only 1 underscore in the params _newOwner and 2 underscores in the body … = __newOwner. That might cause issues :slight_smile:

1 Like

Lauriane

Thank you very much. I will double check. That sounds like it could be
the problem.

Sincerely,

7unrider

2 Likes

mine:
pragma solidity 0.5.1;

contract Ownable{

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

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

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

}

1 Like
pragma solidity 0.5.1;

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