Inheritance Assignment

import “./Ownable.sol”;

pragma solidity 0.5.12;
contract Destroyable is Ownable{ /* Helloworld.sol will import Destroyable.sol and therefore Ownable.sol as well */

/* moved withdraw money function from helloworld to destroyable. Originally said address wasn’t payable so changed it.*/
function getFunds() public onlyOwner{
selfdestruct(address(uint160(owner)));
}
}

deploying the contract on helloworld and self-destructing it returned all the funds to the owner but trying to add people afterwards didn’t change the balance, which is what is supposed to happen after self-destructing

1 Like

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

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

and

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

contract BasicSolidty is Ownable, Destroable
{

1 Like

my answer for the destroyable task:

first my child “helloworld” looks like this:

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

contract HelloWorld is Ownable, Destroy{…

and my parent for destroyable looks like this:

pragma solidity 0.5.12;

contract Destroy{
function destroyContract() public onlyOwner{
if(owner == msg.sender){
selfdestruct(owner);
}
}
}

1 Like

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

contract HelloWorld is Ownable, Destructable {

Then:

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

contract Destructable is Ownable {

function destroyThis() public onlyOwner { 
    address payable theOther = msg.sender;
    selfdestruct(theOther);  
}

}

1 Like

HelloWorld.sol

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”;
import “./Destroyable.sol”;

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable{
}

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


contract Destroyable is Ownable{

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

}

contract Destroyable {
function Destroy() public ;
}

Created an abstract class and implemented it in Ownable

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

then to the HelloWorld class i inherit Ownable

contract HelloWorld is Ownable {

}

Hi @CryptoDev

If you want your destroy function to use the onlyOwner modifier don’t forget to make your Destroyable contract inherit from the Ownable contract.
You can also declare this function directly inside the Destroyable contract.

Then make your main contract inherit of the Destroyable contract.
There is no inheritance in your answer.

You can have a look at other answer in this topic there is good exemple.
Do not hesitate to try your solution on remix to be sure it works. :slight_smile:

Hi thanks i edited my answer forgot to say i inherited the Ownable contract into HelloWorld.
I wasn’t sure how to use the Ownable modifiers with duplicating code now i realize i can just have Destroyable inherit Ownable and have HelloWorld inherit Destroyable.

contract Ownable {

address payable public owner;

modifier onlyOwner(){
    require(msg.sender == owner , "Caller needs to be owner");
    _; //continues exec in function
}

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

}

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

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

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

contract HelloWorld is Destroyable{

}

1 Like

My solution:

// 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 (address payable in Ownable.sol)
    }
}
// HelloWorld.sol
import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;

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

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

contract Destroyable is Ownable {

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

}

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

contract HelloWorld is Ownable, Destroyable {

}

1 Like

Changes in the hello world file:

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


contract HelloWorld is Ownable, Destroyable

Changes in the destroyable file:

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

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

Also, on a side note for feedback: Can you please change the links in the course that direct to the reading assignment or the forum, so that they will open up in a new tab and not in the same window? :joy:

1 Like

Hi @illyushin

“Can you please change the links in the course that direct to the reading assignment or the forum, so that they will open up in a new tab and not in the same window?”
Hey indeed it will be great :slight_smile: , I will ask them if it s possible good suggestion

1 Like

My solution is kind of one liner in the function
Destroyable.sol:
import “./Ownable.sol”;
pragma solidity 0.5.12;

contract Destroyable is Ownable{
function destroy() public onlyOwner {
selfdestruct(msg.sender);
}
}
main contract have been added with:
import “./Destroyable.sol”;

contract HelloWorld is Ownable, Destroyable{

1 Like

Destroyable:

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

contract Destroyable is Ownalble{

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

Helloworld:

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

contract HelloWorld is Ownalble, Destroyable{

1 Like

Destroyer.sol

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

contract Destroyer is Ownable{

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

helloworld.sol

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

contract HelloWorld is Ownable, Destroyer{
1 Like

Solution:

//Destroyable.sol

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

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

}


//HelloWorld.sol 

pragma solidity 0.5.12;

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

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

Hi @Dora
It’s not a big deal but their is a small typo in your HelloWolrd.sol import :wink:

gabba,

I am glad you pointed out, appreciate it.

Dora

1 Like

This is what i was able to come up with:

import “./HelloWorld.sol”;

pragma solidity 0.5.12;

contract Destroyable{
address public owner;

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

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

}