Inheritance Assignment

pragma solidity 0.5.12;

contract Destroyable {
    address payable private owner;
    
    constructor() public{
        owner = msg.sender;
    }
    
    modifier onlyOwner(){
        require(msg.sender == owner, "Caller is not owner");
        _;
    }
    
    function destroy() public onlyOwner { 
        selfdestruct(owner);  
    }
}
1 Like

Great! :smiley:
As contract Destroyable inherits contract Ownable , you can also streamline things even more by removing the code from contract HelloWorld that explicity inherits contract Ownable. This is because HelloWorld already inherits from Destroyable , and so now indirectly inherits Ownable via Destroyable .

What I’m trying to say is that in contract HelloWorld you can just have:

import "./Destroyable.sol";
pragma solidity 0.5.12;
 
contract HelloWorld is Destroyable {...}
5 Likes

Great!

In case you didn’t know, before you type in your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. You will see 2 sets of 3 back ticks.
```
```
If you now input your code between these, you will end up with nicely formatted code, which is then also easier for you to organise with the correct spacing and indentation etc. You should end up with something like:

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

contract Destroyable is Ownable {

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

}

Hope that helps :slightly_smiling_face:

1 Like

Good start!..but don’t forget the contract declaration and to state the name of the actual contract it is inheriting (not just the file).

Also, would you make any modifications to HelloWorld ?

If you post the finished product I’ll check it for you :slightly_smiling_face:

1 Like

pragma solidity ^0.6.0;

import “./Ownable.sol”;

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

And also imported Destroyable.sol to helloworld contract and inherited from it along with ownable.

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

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

Hi @cryptoGerry!

Great code!

By the way, what amendments did you make to  contract HelloWorld  in terms of its inheritance of  contract Destroyable ?

Also, if you amend  contract Destroyable  so that it inherits from  contract Ownable , you won’t need to repeat the owner variable, the modifier or the constructor, as these will be inherited from Ownable. However, you would then need to make an amendment to EITHER your destroy function, OR the owner variable in contract Ownable. See if you can work out what it would be and why… :thinking:

2 Likes

Great @EdsonRamirez!

By the way, check this post out — it explains how you can make an additional adjustment.

Ownable.sol

pragma solidity 0.5.12;

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

Destroyable.sol

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

contract Destroyable is Ownable{
    
    function destroy() public onlyOwner{
        selfdestruct(owner);
    }
}
1 Like

Thank you.
I did not work with the original HelloWorld contract that was used in the videos.
I just created a generic contract that I could use with other contracts that I had in remix and that didn’t have the Ownable function.
But you’re right, if I wanted to do a real separation of concerns here, the right thing to do would be to have the constructor and modifier within a contract Ownable.sol, and then just the call to selfDestruct () in the contract Destructible.sol.

2 Likes

//Destroyable.sol file

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

contract Destroyable is Ownable{

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

}

//HelloWorld.sol file

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

contract HelloWorld is Ownable, Destroyable{

1 Like

Hi @filip ,
Is it better that it be written like you did
import “./Ownable.sol”;
pragma solidity 0.5.12;

contract Destroyable is Ownable{

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

}

rather than how i did originally?

Destroyable.sol

import "./Ownable.sol";

contract Destroyable is Ownable {

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

}

HelloWorld.sol

import "./Destroyable.sol";

contract HelloWorld is Destroyable {
[...]

I made Destroyable inherit from Ownable, because to be Destroyable, a contract needs some safety so that only the owner can destroy it. So in the end if you want a contract that is Destroyable, you also get the “Ownable” properties and functions.

I also had to fix some error because “owner” had to be a payable address so:

Ownable.sol

contract Ownable {

    address payable public owner;

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

    constructor() public {
        owner = msg.sender;
    }
    
    function setNewOwner(address payable _address) public onlyOwner returns (address) {
        require(_address != address(0));
        owner = _address;
        return owner;
    }
}
1 Like

Hi @filip (hello to anybody else too, who wants to answer :wink: )

I’ve seen in your solution:

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

contract HelloWorld is Ownable, Destroyable {

in my solution I did simply:

import "./Destroyable.sol";

contract HelloWorld is Destroyable {

In HelloWorld I didn’t import Ownable, because that is imported already in Destroyable. Also I didn’t specify is Ownable, because Destroyable is already ownable.

Why did you do that redundancy? Do you agree that it’s not necessary? I tested my solution and of course it works, so I assume it’s ok :wink:

1 Like

Destroyable contract which inherets fromOwnable

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

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

And the HelloWorld contract inherets from the Destroyable contract

contract HelloWorld is Ownable, Destroyable{
}

1 Like

Hi @Yves_T!

Nice solution :ok_hand:

By the way, what amendments did you make to contract HelloWorld in terms of its inheritance?

1 Like

Hi @mjwatson10!
You may find this answers your question:

2 Likes

Hi @mjwatson10

Check this post out — it explains how you can make an additional adjustment in terms of the inheritance.

2 Likes

Nice detailed analysis and summary, @firepol!

And I like the idea of the additional setNewOwner Function :+1:

You should remove returns(address) and return owner because this function is a setter not a getter, and these additional pieces of code don’t actually do anything anyway. Contract Ownable automatically generates a getter for owner which is a state variable and has public visibility, and so is inherited by Hello World, and so this enables us to get the owner’s address from there.

Have a go at making these changes and you’ll see what I mean.

1 Like

Hi @firepol!

You are exactly right in your observation. There is actually some discussion about this a bit further up in this thread…

I think maybe in the solution code, contract HelloWorld was left inheriting Ownable (as well as Destroyable) from the previous lecture. It isn’t an error, but your inheritance composition is the more efficient, and everyone agrees with that. :smiley:

1 Like