Inheritance Assignment

contract Ownable {
address public owner;

modifier onlyOwner {
    require(msg.sender == owner);
    _; //run the function...
}

constructor() {
    owner = msg.sender;
}

}

contract Destroyable is Ownable {

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

}

contract Bank is Ownable, Destroyable {

}

2 Likes

pragma solidity 0.7.5;

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

constructor() {
   owner = msg.sender;
}

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

}

2 Likes

Solution:

pragma solidity 0.7.5;

abstract contract owned {
    address payable owner;
    
    constructor() { 
        owner = msg.sender; 
    }
    
    modifier onlyOwner {
        require(msg.sender == owner, "Only owner function");
        _;
    }
}


abstract contract Destroyable is owned {
    function destroy() public onlyOwner {
        selfdestruct(owner);
    }
}

contract somecontract is Destroyable {
}

1 Like
pragma solidity 0.7.5;
abstract contract Destroyable {
    
    address payable private owner;
    
    modifier onlyOwner() {
        require(msg.sender == owner, "Must be contract owner");
        _;
    }
    
    constructor() {
        owner = msg.sender;
    }
    
    function destroy() public onlyOwner {
        selfdestruct(owner);
    }
}

contract EthKitty is Destroyable {
// ... code goes here
}
1 Like
contract Ownable{
    address public owner;
    
    modifier onlyOwner{
        require(msg.sender == owner);
        _;
    }
    
    constructor() {
        owner= msg.sender;
    }
}

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

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

contract Bank is Ownable, Destroyable{
2 Likes

pragma solidity 0.7.5;

contract Destroyable {

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


modifier onlyOwner {
    require(owner == msg.sender,"You don't have access");
    _;
}

function suicide() public onlyOwner {

  selfdestruct(owner); 

  // here at the argument(owner) I get an error, 
  //that the type of argument is invalid.
  //I'm not sure why Can someone HELP? :)

}

}

contract toBeDestroyed is Destroyable {

// toBeDestroyed is inheriting the suicide function

// (and everything else) from Destroyable

}

1 Like

Hey @Cryptogirl

// here at the argument(owner) I get an error,
//that the type of argument is invalid.
//I’m not sure why Can someone HELP? :slight_smile:

The error might be related to the fact that owner is not payable, let me explain.
Whenever the function selfdestruct() is called, all the funds in your contract are automatically sent to the parameter you give for example: selfdestruct(owner) will send all the remaining funds to owner.

In your case, you have declared owner as follow address public owner; but, because owner will receive funds, the address has to be declared or casted as payable.

You can do these 3 things:

  • Declare owner as payable address payable public owner;
  • Cast owner to payable
function suicide() public onlyOwner {
  selfdestruct(payable(owner));
}
  • Use msg.sender instead of owner, because your function already uses a onlyowner modified (therefore you know that msg.sender == owner):
function suicide() public onlyOwner {
  selfdestruct(msg.sender);
}

Give it a try and keep me posted :slight_smile:

Happy learning!
Dani

2 Likes

Thanks @dan-i :slight_smile:

It all makes sense now. :smiley:

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

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

First create a contract with all of the Owner’s details as the solidity conventional way.

pragma solidity 0.7.5;

contract Ownable {
address owner;

constructor(){
    owner = msg.sender;

}

modifier onlyOwner(){
    requires(msg.sender == owner);

}
}

Now create the Destroyable contract that inherits the Ownable contract

pragma solidity 0.7.5;

import “./Ownable.sol”;

contract Destroyable is Ownable {

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

}

Finally create a contract that inherits from the Destroyable contract.
contract ChildDestroyable is Destroyable{
//
}

1 Like
Destroyable.sol

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

HelloWorld.sol

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

change Ownable contract so address owner has internal visibility

pragma solidity 0.7.5;

contract Ownable {
    address internal owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; // runs the function
    }
    
    constructor() {
        owner = msg.sender;
    }
}

create new contract Destroyable inheriting from Ownable

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

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

change Bank contract to inherit from Destroyable

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

contract Bank is Destroyable {
1 Like
pragma solidity 0.7.5;

import "./Ownable.sol";

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

1 Like

This is quite eye-opening comment from the instructor.

1 Like
import './Ownable.sol';

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

In HelloWorld Contract

pragma solidity 0.7.5;

import './Ownable.sol';
import './Destroyable.sol';

contract Bank is Ownable, Destroyable {
1 Like

pragma solidity 0.7.5;

import “./Ownable.sol”;

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{
   
   function close() public onlyOwner {
          selfdestruct(msg.sender);
   }
}   
1 Like
pragma solidity 0.7.5;

import "./Ownable.sol";

contract Destroyable is Ownable {
    function destroy() public onlyOwner {
        address payable receiver = msg.sender;
        //Added the above line and changed the below to receiver instead of msg.sender after checking Filip's solution :)
        selfdestruct(receiver);
    }
}
1 Like
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.5;

contract Destroyable {
    
    address payable public owner;
    
    constructor() {
        owner = msg.sender;
    }
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    
    function close() public onlyOwner {
        selfdestruct(owner);
    }
}

contract SimpleContract is Destroyable {
    function destroy() public {
        super.close();
    }
}

1 Like

This is what I did in the destroyable contract. It looks good and no errors.

pragma solidity 0.7.5;

import "./Ownable.sol";

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

This is what I have done to the Hellow world contract.

pragma solidity 0.7.5;

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


contract Bank is Ownable, Destroyable {
1 Like