Inheritance Assignment

pragma solidity 0.5.12;

contract Destroyable{

address public owner;


 modifier onlyOwner(){
    require(msg.sender == owner);
    _; //Continue execution
    
    function close() public onlyOwner{
        selfdestruct(owner);
    }

}

pragma solidity 0.5.12;
import "./Ownable.sol";


contract Detroyable is Ownable {
    
   function detroy() public onlyOwner {
        selfdestruct(owner);  
    }
}
1 Like

Hi @JRB
Do not hesitate to re watch the videos if you are not sure about something.
Here your Destroyable contract is almost good, you just have to inherit it from the ‘Ownable’ contract.
To do so you need to use the keyword ‘is’ when you are declaring your contract.
contract Destroyable is Ownable {…}

But in the code you are sharing i m not seeing the Ownable contract, you wrote a function onlyOwner but you need to include it in an other contract called Ownable,
You will also have to declare the owner variable in the Ownable contract.

Are you using remix to test your code ?

Hi @simplyprado

Your Destroyable contract is not inheriting from an other contract, the modifier and the owner should be declared in the contract your are inheriting from

1 Like

Created a new file named Destroyable.sol

pragma solidity 0.5.12;

contract Destroyable 
{
    address public owner;

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

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

function close() public onlyOwner 
{ //onlyOwner is custom modifier
  selfdestruct(msg.sender);  // `owner` is the owners address
}

}

the file is then imported to HelloWorld.sol

import "Destroyable.sol";

This is the Hello World contract so far.


import "./Ownable.sol";
import "./Destroyable.sol";

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable{
    
    // This is the person struct
    struct Person {
        uint id;
        string name;
        uint age;
        uint height;
        bool senior;
    }
    
    event personCreated(string name, bool senior);
    
    // This is the owner's address
    
    uint public balance;
    
    
    modifier costs(uint cost){
        require(msg.value >= cost);
        _;
    }
    
    
    
    // This is the mapping that attaches an adress to the person that gets created
    mapping(address => Person) private people;
    address[] private creaters;
    
    // This function creates the person
    function createPerson(string memory Name, uint Age, uint Height) public payable costs(1 ether) {
        // This require function makes it so that the person has to be below 150
        require(Age < 150, "Age needs to be below 150");
        
        // This checks if the user payed enof munney to creat a person.
        balance += msg.value;
        
        Person memory newPerson;
        newPerson.name = Name;
        newPerson.age = Age;
        newPerson.height = Height;
        
        // This if,else statement desides wether the person is a senior or not
        if(Age >= 65){
            newPerson.senior = true;
        }
        else{
            newPerson.senior = false;
        }
        
        
        insertPerson(newPerson);
        creaters.push(msg.sender);

        
        // Here we are useing asert to check if [msg.sender] is equal to newPerson
        assert(
            keccak256(
                abi.encodePacked(
                    people[msg.sender].name,
                    people[msg.sender].age,
                    people[msg.sender].height,
                    people[msg.sender].senior
                    )
                ) 
                == 
                keccak256(
                    abi.encodePacked(
                        newPerson.name, 
                        newPerson.age,
                        newPerson.height,
                        newPerson.senior
                    )
                )
            );
        emit personCreated(newPerson.name, newPerson.senior);
        
    }
    
    // This function inserts the person into the mapping
    function insertPerson(Person memory newPerson) private{
         address creater = msg.sender;
         
         people[creater] = newPerson;
    }
    
    // This function gets our person when we select the corect address
    function getPerson() public view returns(string memory Name, uint Age, uint Height, bool senior){
        address creater = msg.sender;
        return (people[creater].name, people[creater].age, people[creater].height, people[creater].senior);
    }
    
    // This function delets our person if the corect address is selected
    function deletePerson(address creater) public onlyOwner {
        delete people[creater];
        assert(people[creater].age == 0);
    }
    
    // This function gets the owner of the person
    function getCreator(uint index) public view onlyOwner returns(address){
        return creaters[index];
    }
    
    // This funtion withraws all of the balance
    function withdrawAll() public onlyOwner returns(uint) {
        balance = 0;
        msg.sender.transfer(balance);

    
    }
    

This is the Ownable contract so far.

pragma solidity 0.5.12;

contract Ownable{
    
    address public owner;
    
    // modifier onlyOwner
    modifier onlyOwner(){
        require(msg.sender == owner);
        _;
    }
    
    // This is the constructor that creats the owner when the contract first gets deployed
    constructor() public{
        owner = msg.sender;
    }
    
   
}

This is the Destroyable contract so far.

import "./Ownable.sol";

pragma solidity 0.5.12;

contract Destroyable is Ownable {
    
    function destroy() public onlyOwner { 
        address payable receiver = msg.sender;
      selfdestruct(receiver);  
  }
    
    
}

Hi @JRB
It is working , you can make your helloWorld contract inherit from Destroyable only, because Destroyable is inheriting from Ownable. Also in the code you provide there is a ‘}’ missing at the end of the contract but it guess this is a typo when you had copy past the code in the forum.

What is not working for you ?

As you can see the destroy function is failing when i m calling it with an other account than the owner account, and it succeed when i m using the owner account

You also need to make your HelloWorld contract inherit from Destroyable :slight_smile:

1 Like

yup yup, forgot to mention it my bad

contract HelloWorld is Destroyable {......}
1 Like

Thank you! everything is working as it needs to be. At this time I have finished the course. Thank you so much for all of your help!!!

1 Like

Thanks for the feedback

New contract “Destroyable1”

import "./Ownable1.sol"; <-- We need this new contract to import the functions/variables from the "Ownable1" contract, because we are going to use them in our code.

pragma solidity 0.5.12;

contract Destroyable1 is Ownable1{

   function DestroyContract() public onlyOwner { 
       selfdestruct(msg.sender);  
   }

}

Then, at the beginning of the “HelloWorld” contract, we have to call for it.

import "./Ownable1.sol";
import "./Destroyable1.sol";  <-- Added to link the two contracts through inheritance

pragma solidity 0.5.12;

contract HelloWorld is Ownable1, Destroyable1 {
__ rest of the code __
1 Like

Here is my solution. I created another class called Destroyable.

import "./Owner.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable {

    function destroy() public onlyOwner {
        selfdestruct(address(uint160(owner)));
    }

}

I used Filips code from Github, therefore I had to convert a non-payable address to a payable, otherwise I could not execute the code.

In the class helloworld I added the following in the header:

import "./Owner.sol"; // Only Owner has access to it
import "./Destroyable.sol"; // contract helloWorld can be destroyed

@filip in your HelloWorld.sol gibthub code the variable toTransfer (in the withdrawAll function) is without a declaration of a data type (maybe you should add an uint, otherwise beginners could not be able to run the code without problems!!!)

1 Like

Thx for noticing @KryptoDr
I opened a pull request to filip, if you see this kind of error do not hesitate to open a pull request yourself :slight_smile:

1 Like
pragma solidity 0.5.12;

import "./Ownable.sol";

contract destroyable is Ownable{
    
    function destroy() public onlyOwner{
        selfdestruct(msg.sender);
    }
    
}

and our helloworld.sol has to be modified as,

import "./destroyable.sol";

contract HelloWorld is Ownable, destroyable{
2 Likes

The “close” function in the destroyer contract had a converting nonpayable to payable address issue, I think to do with “address public owner” in the “Ownable” inheritance.
Once the onlyOwner was established in the function header I figured I could fiddle with how the owner is described in the body. I went with msg.sender, which compiled and passed several trials interacting, including the balance not changing no matter how much Ether I sent to the closed contract.

pragma solidity 0.5.12;
import "./Ownable.sol";

contract Destroyable is Ownable {
    
    function close() public onlyOwner { //onlyOwner is custom modifier
        selfdestruct(msg.sender);  // `owner` is the owners address
    }
    
    
}

Then of course

pragma solidity 0.5.12;
import "./Ownable.sol";
import "./Destroyable.sol";

contract HelloWorld is Ownable, Destroyable {
1 Like

I made a file called Destroyable.sol and made wrote this code

import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable{

    

 function destroyable() public onlyOwner { 
  selfdestruct(msg.sender);  
    }
    
}

in our HelloWorld file i added

import "./Destroyable.sol";

contract HelloWorld is Ownable, Destroyable

1 Like

Selfdestruct contract: import “./Inheritance.sol”;
pragma solidity 0.5.12;

contract destroyable is Ownable {

function close() public onlyOwner {
selfdestruct(msg.sender);
}

}

1 Like

Imported the destroyable contract into the Helloworld contract.

1 Like
pragma solidity 0.5.12;
import './Ownable.sol';


function Destroy() public onlyOwner {
  selfdestruct(owner);  
}