Inheritance Assignment

Post your solution to the inheritance assignment here. Or ask questions :slight_smile:

Hi Filip
I don’t see any link as mentioned.
your instructions/ Can yousend the “Destroyer” link.
Thanks

Guy

Sorry about that. I have updated the lecture now :slight_smile:

pragma solidity 0.5.12;

contract Destroyable{

address public owner;


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

}

1 Like
  1. I opened a new file called Destruction:
import "./ownable.sol";
pragma solidity 0.5.12;

contract SelfDestruct is Ownable {
  
     function toSuicide() public onlyOwner{
       selfdestruct (msg.sender);
       }
    
}

in the HelloWorld contract :

import "./ownable.sol";
import "./Destruction.sol";
pragma solidity 0.5.12;
contract HelloWorld is Ownable, SelfDestruct{
1 Like

After looking at the solution - isn’t msg.sender is already “address payable”?
why do we need to convert it again to a “payable address receiver”?
Thanks.

Good question. I assume you are talking about this line of code.

address payable creator = msg.sender;

You are correct that msg.sender is already a payable address. But what we do in the code is not a conversion. We are saving msg.sender to a variable. And if we want the address to remain payable, we need to save it to a payable address. If we were to save msg.sender to a non-payable address variable, then it wouldn’t be payable anymore.

This would be a “conversion” (from payable msg.sender to non payable creator).

address creator = msg.sender;

I hope it became clearer now :slight_smile:

Yes.Thank you. understand that It is not a conversion…but can we avoid the saving of msg.sender as a variable and use:

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

It works…and only the owner can destroy the contract so it looks quite safe.

Are there any issues that I am not aware of involved?

Yes, that’s absolutely safe. That’s a better solution than to use a variable as a go-between. But sometimes people find it clearer and easier to read when we break the code up into multiple steps.

created new contract Destroyable:

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

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

added to main contract:

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

contract HelloWorld is Ownable, Destroyable{
1 Like

Destructible contract which inherits from Ownable.

pragma solidity 0.5.12;

import "./Ownable.sol";

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

HelloWorld contract which inherits from Destructible contract.

pragma solidity 0.5.12;

import "./Destructible.sol";

contract HelloWorld is Destructible{...
}