Inheritance Assignment

Created a new file called Destroyable.sol:

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

contract Detroyable is Ownable{
    
    function DestroyContract() public onlyOwner {
  selfdestruct(msg.sender); 
}
}

Then had hello world inherit both Ownable and Destroyable.

1 Like

Here is my code:

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

contract Destroyable is Ownable{
    function close() public onlyOwner{
        address payable theOwner= address(uint160(owner));
        selfdestruct(theOwner);
    }
}
1 Like

Thanks so much, so just to clarify…
Ownable is the Parent of Destroyable and FirstContractExample, but at the same time FirstContractExample is deriving from Destroyable as well. So its a weird kind of relationship where there is Ownable who has 2 children, but one of those children is also the parent to the other child…

1 Like

Your are right!
To make your like easy

You can stop here

THEN

Destroyable is PARENT of FirstContractExample

So we don’t have to create new relationship :wink:

2 Likes
  1. self destruct contract
import "./ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable{
    
    function close() public onlyOwner{
        selfdestruct(msg.sender);
    }
}
  1. main contract
import "./ownable.sol";
import "./destroyable.sol";

pragma solidity 0.5.12;

contract structArray is Ownable,Destroyable{
    
    struct Person{
        
        uint id;
        uint age;
        string name;
        uint height;
        bool senior;
        
    }
    
    event personCreated(string name, bool senior);

    event personDeleted(string name, bool senior, address deletedBy);
    
    
    uint public balance;
    

    modifier costs(uint cost){
    
    require(msg.value >= cost);  
    _;
    
    }
    

    mapping (address => Person) private people;
    address[] private creators;
    
    
    
    function createPerson(string memory name, uint age, uint height) public payable costs(1 ether){
        require(age < 150);
        
        balance += msg.value;
        
        Person memory newPerson;
        newPerson.name = name;
        newPerson.age = age;
        newPerson.height = height;
       
        
            if (age >= 65){
                newPerson.senior = true;
            }
            
            else{
                newPerson.senior = false;
            }
            
            insertPerson(newPerson);
            creators.push(msg.sender);
            
            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);
            
        //people.push(Person(name, people.length, age, height));
    }
    
    function insertPerson(Person memory newPerson) private{ //inserts newPerson
        address creator = msg.sender;
        people[creator] = newPerson;
    }
    
    function getPerson() public view returns(string memory name, uint height,  uint age, bool senior){
        address creator = msg.sender;
        return (people[creator].name, people[creator].height, people[creator].age, people[creator].senior);
    }
    
   function deletePerson(address creator) public onlyOwner{
       string memory  name = people[creator].name;
       bool senior = people[creator].senior;
       delete people[creator];
       assert (people[creator].age == 0);
       emit personDeleted(name, senior, msg.sender);
   }
   
   function getCreator(uint index) public view onlyOwner returns(address){ //returns creator
   
       return creators[index];
   }
   
   function withdrawAll() public onlyOwner returns(uint){ //function transfers balance of contract
       uint toTransfer = balance;
       balance = 0;
       if(msg.sender.send(toTransfer)){
           return toTransfer;
       }
       
       else{
           balance = toTransfer;
           return 0;
       }
       
   }
}
1 Like

By creating another contract with name Destroyable and Ownable functionality. Create a function closeContract() that destroy the actual contract. It is only for destro the actual contract. For a real usage, it is important to add also the withdraw functionality before destroying the actual contract.

function closeContract() public ownerOnly { 
  selfdestruct(msg.sender);  
}
3 Likes

Inheritance assignment.

pragma solidity 0.5.12;

import "./Ownable.sol";

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

HelloWorld contract which inherits from Destructible contract.

pragma solidity 0.5.12;

import "./Destructible.sol";

contract HelloWorld is Destructible{...
}

}
1 Like
import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable {
    
    
    function killme() public onlyOwner {
        selfdestruct(msg.sender);
    }
}
1 Like

I believe selfdestruct function already withdraws the contract balance and sends it to msg.sender before the contract is destroyed. It worked for me in remix where i saw that the ether balance went back to my owner account.

Great solution @CasNWK :+1:

Apologies for the late review.

Absolutely right :ok_hand:

1 Like

Hi @CodyM,

Great question! I also wondered the same when I first did this exercise. The short answer is that it’s not necessary for the reason that you give. Have a look at this post, where this is discussed in more detail and also gives the reason for doing it this way in the video.

I hope this clarifies things for you.

@Taha

3 Likes

You get an error, because:

  1. You’ve declared owner within the modifier. It should be outside, because you want it to be a state variable.
  2. There is no such thing as msg.owner. It should be msg.sender
  3. You’ve missed off the semicolon at the end of the require function statement.

@Taha

2 Likes

@jon_m Thank you so much for the answers!

@dbeelow0323 for your part its best to understand the error which the remix or truffle shows :slight_smile:
if you are getting undefined it is because of unknown variable
syntax error is mostly because of semi-colon or not getting the code structure correct.

This is also called as debugging and will help you in the long run. :slight_smile:

2 Likes

I wrote it as a child to ownable, in a single inheritance line

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

contract Destroyable is Ownable {

    function Kaboom() public onlyOwner{
       selfdestruct (msg.sender);
    }
}
1 Like

Ah, thank you so much.

1 Like

Here it is:

pragma solidity 0.5.12;

import './Ownable.sol';

contract Extinguish is Ownable{
    function kamikaze() public onlyOwner {
        selfdestruct(address(uint16(owner)));
    }
}
1 Like
import"./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable{
    function Destroy() public onlyOwner {
        selfdestruct(msg.sender);
    }
}

// add in helloworld.sol:
//import"./Ownable.sol";
//import"./Destroyable.sol";

//pragma solidity 0.5.12;

//contract HelloWorld is Ownable, Destroyable {
//    ...
//    }
1 Like
import "./Ownable.sol";
pragma solidity 0.5.12;


contract Destroyable is Ownable{
    // only destroyable by the owner.
    // owner field has been changed to payable in Ownable.sol
    function destroyContract() public onlyOwner {
        selfdestruct(owner); // sends eth to @param address
    }
}
1 Like
import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable {
    
    function destroyContract() public onlyOwner{
        selfdestruct(msg.sender);
    }
    
}

and

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

contract Hello is Ownable, Destroyable{
1 Like

pragma solidity 0.5.12;

contract Destroyable{


    address public owner;


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

}

…meanwhile inside HelloWorld…


import "./destroyable.sol"
contract HelloWorld is ownable, Destroyable {
1 Like