Inheritance Assignment

Nice solution, @Dragutin :ok_hand:

For this to work, what changes did you also have to make to HelloWorld in terms of inheritance?

Nice solution @Obi :ok_hand:

Also, take a look at this post — it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think you’ll find it interesting :slight_smile:

1 Like

I made owner address payable.

1 Like

Well corrected @Marlic :ok_hand:

What changes did you also have to make to HelloWorld in terms of inheritance?

In fact, your first version using  selfdestruct(owner)  also works as long as you make owner payable in contract Ownable:

contract Ownable {

    // change
    address public owner;
    // to
    address payable public owner;

    ....
}
1 Like

What changes did you also have to make to HelloWorld in terms of inheritance?
Hi Jon,
HelloWorld needs to inherit only Destroyable since Ownable is inherited by Destroyable.

1 Like

import “./Ownable.sol”;
pragma solidity 0.5.12;

contract Destroyable is Ownable {

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

1 Like
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
}
}
````import"./Ownable.sol";
import"./Destroyable.sol";
pragma solidity 0.5.12;

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

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

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable {
// ....
}
1 Like

Hi @scornkrypto,

A well-coded Destroyable contract :ok_hand:

What changes did you also have to make to HelloWorld in terms of inheritance?

1 Like

Hi @Alexiaa,

The following code, which you’ve included in your Destroyable function, would be better left in the separate Ownable function, because it is dealing with different functionality. You should also include the constructor, which you currently have commented out (although I’m not sure why?..)

Destroyable would then inherit Ownable as follows:

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

contract Destroyable is Ownable { ... }

Also, take a look at this post — it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think you’ll find it interesting :slight_smile:

2 Likes

Nice solution @franzmoro :ok_hand:

Also, take a look at this post — it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think you’ll find it interesting :slight_smile:

3 Likes

// created new contract named destroyable

contract Destroyable is Ownable {

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

//ADDED NEW CONTRACT TO PARENT
import './Ownable
import './destroyable

1 Like

Hi @NLUGNER,

Your Destroyable contract is correct — you just need to also import the file Ownable.sol:

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

contract Destroyable is Ownable {...}

I’m not really sure what you mean by this though. You should end up with multi-level inheritance where HelloWorld inherits Destroyable, and Destroyable inherits Ownable. So the other part of your solution should be:

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

contract HelloWorld is Destroyable {

In HelloWorld you don’t need to include code that explicity inherits Ownable. This is because Destroyable already inherits Ownable, and so by inheriting from Destroyable, HelloWorld also indirectly inherits Ownable via Destroyable.

2 Likes

@jon_m

correct, I realize that now.

Thx :+1: :clap:

1 Like

First I created a new contract in a new file - Destroyable.sol
It inherits from ownable contract to use onlyowner functionality. It contains only one function - selfdestruct.
Self destruct will transfer all the contracts balance to a specified address. I had to cast the owner address into a payable address before it could send the funds.

import “./Ownable.sol”;
pragma solidity 0.5.12;

contract Destroyable is Ownable {
function destruct() public onlyOwner{
selfdestruct(address(uint160(owner)));
}
}

Finally I imported Destroyable.sol into HelloWorld.sol and added it as a second base class for HelloWorld contract to inherit from.

import “./Ownable.sol”;
import “./Destroyable.sol”;
pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable {
…
}

1 Like
import"./Destroyable.sol";
pragma solidity 0.5.12:

contract HelloWorld is Destroyable{....
}

pragma solidity 0.5.12;
import './iot_ownable.sol';

contract Destroyable is Ownable {
    
    function close() public onlyOwner {
        selfdestruct(owner); 
    }
}

Uses the ownable contract which is

pragma solidity 0.5.12;

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

I used the following hierarchy for inheritance:

Ownable => Destroyable => HelloWorld

Since the HelloWorld contract inherits all features and functions from Ownable via inheriting from Destroyable there is no need to import “Ownable.sol” or inherit Ownable directly in “HelloWorld.sol”.

Destroying the contract will result in a zero address for the owner, deletion of data of a person created and the balance will be 0. Furthermore the functions of the HelloWorld contract for example createPerson will no longer work.

“Ownable.sol”

pragma solidity 0.5.12;

contract Ownable{
    address payable public owner;

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

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

“Destroyable.sol”

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

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

“HelloWorld.sol” (only the first few lines of code)

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

contract HelloWorld is Destroyable {
    struct Person {
        string name;
        uint age;
        uint height;
        bool senior;
    }
    
    event personCreated(string name, bool senior);
    event personDeleted(string name, bool senior, address DeletedBy);
    
    function getContractBalance() public view onlyOwner returns(uint256){
        return address(this).balance;
    }
    
    function withdrawAll () public onlyOwner {
        msg.sender.transfer(getContractBalance());
        assert(getContractBalance()==0);
    }
...
1 Like

image

image

i can’t really get it, how does it work when we do not use it in the main contract. How does it know which contract should be destructed? :thinking:

1 Like

Hi @acey,

Nice solution, and good analysis :ok_hand:

Also, take a look at this post — it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think you’ll find it interesting :slight_smile:
How would this affect your diagram of the inheritance relationships between the contracts? What type of inheritance would we have?