Inheritance Assignment

Hi @HardlyCodeMan,

Your solution works, but there is no need to have the additional destroy function in HelloWorld. This is because you are only actually deploying a single contract which includes all the inherited functionality (from Ownable and Destroyable) and so what you’ve currently got is code duplication. Calling withdrawAll() as well as lightTheFuse() is also code duplication, because, before destroying the contract, selfdestruct() automatically transfers the contract balance to whichever address is passed to it as an argument (in our case msg.sender i.e. the owner), and so already includes the same functionality as the withdrawAll function, anyway.

To avoid the code duplication you can remove the destroy function from HelloWorld and change lightTheFuse() in Destroyable from internal to public visibility.

Also, have a look at this post — it explains how you can streamline the inheritance by having HelloWorld just inherit Destroyable. I think you’ll find it interesting :slight_smile:

Thanks for the feedback @jon_m,

I only did it this way to play with internal view, and later realised over a beer that I had doubled up on the withdraw which occurs automatically with the selfdestruct()

2 Likes
HelloWorld.sol (changes)
import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;

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

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

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

contract Destroyable is Ownable{

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

}

1 Like

Tjena Filip!

Here’s my code and when I deploy it and use the function it will only be executed from one address but when I saw your solution it’s not the same?

import “./Ownable.sol”;

pragma solidity 0.5.12;

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

1 Like

I suppose this should be enough:

pragma solidity 0.5.12;

contract Ownable{
address public owner;

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

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

}

contract Destroyable is Ownable{

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

}

//Just the minimal stub to keep it brief
contract HelloWorld is Destroyable {
address public justSomeAddress;
}

1 Like

Hello everybody,

i would like to mention that on the new version of Eth Smart Contract 101, on section Value Calls there is no assignment attached.
Additionally, there is no solution or code on filip’s Github profile.
I tried to find the assignment on the old Eth Smart Contract 101 course, but since today is not active.
Furthermore, on the entire course the forum’s link below in each video its not visible as it was before.
Thank you
@filip

1 Like

Hi @ChrisPap, hope you are ok.

I think you are right, at the end of the video filips mention an assignment about value calls but there is no assignment lesson after this video, thank you for notifying it, we will be fixing it this next days, stay tuned!

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like
import "./Ownable.sol";

pragma solidity 0.5.12;

contract Destroyable is Ownable{

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

}
1 Like

pragma solidity 0.7.5;

import “./Ownable.sol”

contract Destroyable is Ownable {

event contractDestroyed( uint timestamp)

function closeContract() public onlyOwner {
emit contractDestoyed(now);
selfdestruct(owner);
}
}

1 Like

Hi, here is my contract for selfdestruct.

pragma solidity 0.7.5;

contract Ownable {
address payable owner;
constructor() {
owner = msg.sender;
}
}

contract destroyable is Ownable {
function detroyme() public {
require(msg.sender == owner, “Only contract Owner can destroy this smartcontract!”);
selfdestruct(owner);
}
}

1 Like

pragma solidity 0.7.5;

import “./Ownable.sol”;

contract Destroyable is Ownable {

event contractDestroyed(uint timestamp);

function close() onlyOwner public {
    emit contractDestroyed(block.timestamp);
    selfdestruct(msg.sender);
}

}

1 Like
contract Destroyable {

	address owner;
	
	constructor() {
		owner = msg.sender;
	}
	
	function close() public { 
		require(msg.sender == owner);
		selfdestruct(owner); 
	}
	
}
1 Like

Filip, in your solution you code both ownable and destroyable to the bank contract.
Isn’t enough to only code Destroyable because it already inherits from the ownable contract?

1 Like
pragma solidity 0.7.5;

import "./ownable.sol";

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

Destroyable:

pragma solidity 0.5.12; 

import "./Ownable.sol";

contract Destroyable is Ownable {

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

} 
add to helloworld.sol:

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

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

Hi @Lamar,

Exactly right - well thought out!

Once you’re satisfied with your own version, check out this post — it explains how you can streamline the inheritance by having HelloWorld just inherit Destroyable, which is exactly what you’re asking I think… I think you’ll find it helpful to check how you’ve structured and coded your inheritance :slight_smile:

Nice Destroyable contract, @Paul_Mun :ok_hand:

What changes did you also have to make to the Bank contract in terms of inheritance?

1 Like

Nice solution @AaronRene :ok_hand:

You are absolutely right to spot that we can streamline the inheritance by having HelloWorld just inherit Destroyable, because it will then indirectly inherit Ownable via Destroyable. However, doing this means that there is also no longer any need to import the file Ownable.sol either, and so this line can be removed.

In the main contract:
pragma solidity 0.7.5;
import “./Ownable.sol”;
import “./Destroyable”;

contract Bank is Ownable, Destroyable {…
and created a new contract Destroyable.sol
pragma solidity 0.7.5;
import “./Ownable.sol”;
contract Destroyable is Ownable {
function destroy () public onlyOwner {
selfdestruct (msg.sender);
}
}

And now I have a problem when I try to deploy Transfer.sol it sad that “not found browser/Destroyable”, what could it be?

1 Like