Inheritance Assignment

Created new Destroyable.sol file

pragma solidity 0.7.5;

import "./Ownable.sol";

contract Destroyable is Ownable {

    function destroy() public onlyOwner {

        selfdestruct(msg.sender);

    }

}

Then added in the main contract


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 { //onlyOwner is custom modifier
  selfdestruct(msg.sender);  // `owner` is the owners address
}
}

Should this have a compiler version or should it be left off? @filip

pragma solidity 0.7.5;

contract Ownable {
    address owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; //run the function
    }
    
    constructor(){
        owner = msg.sender;
    }
}
pragma solidity 0.7.5;

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

contract Bank is Ownable, destroyable {
    
//no changes to below code so not included.

1 Like

Hey @Baidis

You should specify a compiler version in all your contracts.

Cheers,
Dani

2 Likes

Hello there!

I have created a file (ā€œDestroyable.solā€) with this code in it:

pragma solidity 0.7.5;

import ā€œownable.solā€;

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

}

Regards :slight_smile:

1 Like

pragma solidity 0.7.5;

import ā€œ./Ownable.solā€;

contract destroy 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 onlyAdmin { 
  selfdestruct(owner);
}
    
}
1 Like

pragma solidity 0.7.5;

contract Ownable {

address payable owner;

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

constructor() {
    owner = msg.sender;
}

}

contract Destroyable is Ownable {

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

}

1 Like
  1. create an Ownable.sol with a modifier that checks id the sender is the actual owner.
pragma solidity 0.7.5;

contract Ownable {
    address owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; //run the function
    }
    
    constructor(){
        owner = msg.sender;
    }
}
  1. create a Destroyable.sol which inherits from Ownable.sol
pragma solidity 0.7.5;

import "./Ownable.sol";

contract Destroyable is Ownable {
    
    function destroy() public onlyOwner {
        selfdestruct(msg.sender);
    }
}
  1. create the main contract which finally inherits from both, the Ownable and Destroyable
pragma solidity 0.7.5;

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


contract Bank is Ownable, Destroyable {
}
1 Like

Ownable contract:

pragma solidity 0.7.5;

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

Destroyable contract (inherits ownable):

pragma solidity 0.7.5;

import "./ownable.sol";

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

Any contract (inherits from ownable and destroyable):

pragma solidity 0.7.5;

import "./ownable.sol";
import "./destroyable.sol";

contract ctrct is Ownable, Destroyable {
    //code
}
1 Like

I just reviewed the solution on github and I have a few questions.

In the Ownable contract the address is set as:
address internal owner;
Is it needed to declare the variable as internal? What is the default visibility classification when declaring a variable like address owner;? And if it needs to be internal, why is that?

I was also wondering if it is necessary to create the address payable receiver = msg.sender; variable in the destroy function and if it makes any difference if we directly write selfdestruct(msg.sender);

Hey @Santiago

Is it needed to declare the variable as internal? What is the default visibility classification when declaring a variable like address owner; ? And if it needs to be internal, why is that?

You should state the visibility of your variables and functions carefully and you should always think about how you want them to be used.
Always use ā€˜public’ is not the correct way to code in Solidity.
I suggest you these two videos where Filip explains visibility:
Introduction to visibility
Implementing Visibility

I was also wondering if it is necessary to create the address payable receiver = msg.sender; variable in the destroy function and if it makes any difference if we directly write selfdestruct(msg.sender);

This is not necessary, you can pass msg.sender as parameter to selfdestruct.

Let me know if you have questions :slight_smile:

happy learning!
Dani

1 Like

I used a state variable and a constructor and set self destruct to the constructor instead of using a modifier… This should give any child contract the ability to use the function same as a modifier would. I think…

pragma solidity 0.7.5;
contract destroyable {

address payable this1;

constructor(){
this1 = msg.sender;
}
function knockOff () public {
selfdestruct(this1);
}
}

contract madeTodestroy is destroyable {

function Hello(string memory _saySomething) public returns (string memory){
    return _saySomething;
}

}

1 Like

Adding a selfdestruct contact to project. I had to cast the owner as payable to remove compile error or replace owner with msg.sender?

contract Destroyable is Ownable{

function destroyable() public onlyOwner { // check that it is the owner

 selfdestruct( payable(owner)); // Had to cast owner as "payable" to remove compile error

}

1 Like

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

1 Like

thats a completely valid way, what you can also do is:

define owner address has address payable owner.

Or use msg.sender in the selfdestruct() function.

Either ways are complete valid, just different paths to achieve the same result. (in future you have to take in mind gas costs, some are cheaper than others, but for now dont focus too much on that :nerd_face:)

Carlos Z

import ā€œ./ownable.solā€;

contract destroyable is ownable{

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

–> in addition to changing the owner state variable to
address payable public owner;

1 Like

Bank

pragma solidity 0.7.5;

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

contract Bank is Ownable, Destructable {
    
    mapping(address => uint) balance;
    
    
    event depositDone(uint amount, address indexed depositedTo);
    
    
    
    
    function deposit() public payable returns (uint)  {
        balance[msg.sender] += msg.value;
        emit depositDone(msg.value, msg.sender);
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public onlyOwner returns (uint){
        require(balance[msg.sender] >=  amount, "You dont have enough money");
        balance[msg.sender] -= amount;
        msg.sender.transfer(amount);
        return balance[msg.sender];
    }
    
    function getBalance() public view  returns (uint){
        return balance[msg.sender];
    }
    
    function transfer(address recipient, uint amount) public  {
        require(balance[msg.sender] >= amount, "Balance not sufficent");
        require(msg.sender != recipient, "You cant transfer money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender]== previousSenderBalance - amount);
        
        
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}

Ownable

pragma solidity 0.7.5;
contract Ownable {
    address  internal owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    
    constructor()   {
        owner = msg.sender;
    }
}

Destroyable

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

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

Ok what am i doing wrong…??

I’ve created a separate Destroyable file:
pragma solidity 0.7.5;

import ā€œ/Ownable.solā€;

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

}

and here is the error message I get when I compile the BankContract
Screen Shot 2021-03-16 at 1.02.22 PM

and the error message…

Screen Shot 2021-03-16 at 1.02.33 PM

pragma solidity 0.7.5;

import ā€œ./Ownable.solā€;

contract Destroyable is Ownable {

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

1 Like

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

1 Like