Inheritance Assignment

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

contract Destroyable is Ownable {
    
    function close() public onlyOwner {
        selfdestruct(msg.sender);
    }
}
import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable {
// Original HelloWorld contract here
}
1 Like

Hi @mat.rhombus,

Nice solution :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
// Destroyable.sol
contract Destroyable is Owner {
    function close() public onlyOwner {
      selfdestruct(msg.sender);
    }
}

// HelloWorld.sol
contract HelloWorld is Owner, Destroyable {
  /* CODE */
}
1 Like
pragma solidity 0.5.12;

import "./Ownable.sol";

contract Destroyable is Ownable {
    
    function destroy() public onlyOwner {
        address payable payableOwner = msg.sender;
        selfdestruct(payableOwner);
    }
}
1 Like
import "./Ownable.sol";
pragma solidity 0.5.12;

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

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

function shutdownContract() public onlyOwner { 	// onlyOwner is custom modifier example shown below
   selfdestruct(address(uint160(owner)));  				// `owner` is the owners address
}

}

Hello program was changed only to include Destroyable
import “./Ownable.sol”;
import “./Destroyable.sol”;
pragma solidity 0.5.12;
/**

  • example of inheritance in solidity
    */
    contract HelloWorld12 is Ownable, Destroyable {

    // events

But how do I keep the createPerson() from eating ether after shutdown? I tryed to set a variable isDead = true but everything is reset after the shutdown. And I am not even sure if the createPerson is even executed (require() runs at least) after shutdown.

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

contract Destroyable is Ownable {

    function destroy() public onlyOwner { //onlyOwner is custom modifier
        selfdestruct(owner);
  // `owner` is the owners address and has been assigned to 'address payable'
  // in the Ownable contract.
    }
    
}
1 Like
import "./Ownable.sol";

pragma solidity 0.6.6;

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

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

contract Destroyable is Ownable {

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

}

1 Like

Hi @benr,

Nice Destroyable contract :ok_hand:
What changes did you also have to make to HelloWorld in terms of inheritance?

Nice solution @gmarcotte :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:

This is one of the disadvantages of using selfdestruct. After the contract has been destroyed and removed from the blockchain, there nothing to stop ETH still being sent to the contract by users who don’t yet realise that it’s been destroyed. This ETH will be lost forever as there is no way to retrieve it.

Good, well done @freddythebee for realising that we need to make owner (the address state variable in Ownable) a payable address, in order to be able to call selfdestruct with it in Destroyable :+1:

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

Nice Destroyable contract, @biokillos :ok_hand:
What changes did you also have to make to HelloWorld in terms of inheritance?

1 Like

Nice solution @Afronizer :ok_hand:

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

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

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

1 Like

Thanks @jon_m, obviously I had to add “Destroyable” to the HelloWorld inheritance declaration:

contract HelloWorld is Ownable, Destroyable {
1 Like

I ran into problems around “Payable” so I changed “address public owner” to “address payable public owner”

I was hoping to be able to do it without Destroyable having to import Ownable, but I couldn’t figure out if that could be done.

I’m assuming it works because the smart contract no longer gives me back data. I was expecting to get error messages at that stage, but I didn’t.

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
    }

}

pragma solidity 0.5.12;

contract Ownable {

address payable public owner;

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

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

}

1 Like

Forgot to post my HellowWorld here - it does indeed the obvious "contract HelloWorld is Ownable, Destroyable …

1 Like

Thanks! I imported the Destroyable contract to HelloWorld and made so it inherits the former e.g:

contract HelloWorld is Ownable, Destroyable {}

1 Like

Hi @freddythebee,

Correct … Now 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: