Inheritance Assignment

Destroyable.sol:

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

contract Destroyable is Ownable {

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

HelloWorld.sol:

import “./Ownable.sol”;
import “./Destroyable.sol”;

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable{

struct Person {
  uint id;
  string name;
  uint age;
  uint height;
  bool senior;
}

event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address deletedBy);

uint public balance;

modifier costs(uint cost){
    require(msg.value >= cost);
    _;
}

mapping (address => Person) private people;
address[] private creators;

function createPerson(string memory name, uint age, uint height) public payable costs(1 ether){
  require(age < 150, "Age needs to be below 150");
  require(msg.value >= 1 ether);
  balance += msg.value;

    //This creates a person
    Person memory newPerson;
    newPerson.name = name;
    newPerson.age = age;
    newPerson.height = height;

    if(age >= 65){
       newPerson.senior = true;
   }
   else{
       newPerson.senior = false;
   }

    insertPerson(newPerson);
    creators.push(msg.sender);

    assert(
        keccak256(
            abi.encodePacked(
                people[msg.sender].name,
                people[msg.sender].age,
                people[msg.sender].height,
                people[msg.sender].senior
            )
        )
        ==
        keccak256(
            abi.encodePacked(
                newPerson.name,
                newPerson.age,
                newPerson.height,
                newPerson.senior
            )
        )
    );
    emit personCreated(newPerson.name, newPerson.senior);
}
function insertPerson(Person memory newPerson) private {
    address creator = msg.sender;
    people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior){
    address creator = msg.sender;
    return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
}
function deletePerson(address creator) public onlyOwner {
  string memory name = people[creator].name;
  bool senior = people[creator].senior;

   delete people[creator];
   assert(people[creator].age == 0);
   emit personDeleted(name, senior, owner);

}
function getCreator(uint index) public view onlyOwner returns(address){
return creators[index];
}

function withdrawAll() public onlyOwner returns(uint) {
uint toTransfer = balance;
balance = 0;
msg.sender.transfer(balance);
return toTransfer;
}

}

1 Like

Glenn_CostaRica

pragma solidity 0.5.12;

contract Ownable{ // PARENT
address payable public owner;

modifier onlyOwner(){
require(msg.sender==owner); // if not the Owner, abort execution!
_;
}

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

function getOwner()public view returns(address payable) { // this func gives you the Owner Add
return owner;
}

}

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

contract Destroyable is Ownable{ // CHILD

function destroyContract() public onlyOwner { //onlyOwner-> modifier in the Parent contract
address payable toDestroy = getOwner();
selfdestruct(toDestroy); // `“owner”-> owners address
}

}

Hi @Glenn_CostaRica

Did you try your code in remix ?
Your function getOwner won’t work for 2 reasons:

  • first you should use return instead of returnS
    returnS is only used your function prototype declaration, inside the function if you want to return a value you should use return.
  • The second issue is the type you are using, in solidity 0.5.12 the selfdestruct function is taking a variable of type “address payable” as parameter.

@SimonLondon @Spartak @Crypto_Jeff @Glenn_CostaRica

Don’t forget to use this balise to display your code.

It’ll make your answers more readable :grin:

1 Like

@gabba Thank you sooo much <3 Yes, I had a problem with returns --> return :smile: :pray: :bowing_man: It took me a few minutes to find the problem !!! I should’ve checked here first !! About not having used PAYABLE for my address-type variable, I discovered the problem when I re-watched Filip’s video !!!.. In the end, my code worked: but, I found Filip’s solution was so much nicer and simpler !!!

1 Like

@gabba I just fixed my publication, up here, with the code that actually ran well in remix !! Thank you again !!! <3

1 Like

BTW…, I am so excited !! This course has been amazing !! I don’t sleep or think about anything else these days :smile: :laughing: :heart_decoration: LOVE FROM COSTA RICA AND ALSO JAPAN ( :costa_rica: :jp:): I have two hearts !!!

:mechanical_arm: great @Glenn_CostaRica :computer:
You will see during eth 210 you will be able to develop a complete Dapp it’s really cool

1 Like

@gabba Man!! It sounds awesome!! I’m crazy about taking the 201 course <3<3<3 I’ve been an Ethereum super fan for very long time. I’ve studied so much about Ethereum theoretically. Now, that I am actually learning how to code in Solidity, I truly feel part of a big thing! Part of such an amazing thing! I live and work in Costa Rica now. But, my kids (son and daughter) are in Japan. I speak with them almost everyday, and recently all I talk about is Programming_in_Ethereum!

1 Like

Created new sol file Destroyable.sol

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

contract Destroyable is Ownable{

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

}

and added inheritance in HelloWorld.sol

contract HelloWorld is Ownable, Destroyable{…}

1 Like

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

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

1 Like

I did do as per the video the when creating Destroyable.sol i wrote this in it:

import “./Ownable.sol”;

pragma solidity 0.5.12;

contract Destroyable is Ownable{

address payable test=address(uint160(owner));

function destruction() internal onlyOwner {

selfdestruct(test); 

}

}

I did import this contract to HelloWorld as well and i changed the address owner to internal too.
:grinning:

1 Like

New file called Destroyable.sol:

import "./Ownable.sol";

pragma solidity 0.5.12;



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

Import Destroyable.sol to child contract HelloWorld.sol:

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

pragma solidity 0.5.12;

contract HelloWorld is Ownable, Destroyable  {

pragma solidity 0.5.12;

contract Ownable {
address payable internal owner;

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

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

}

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

contract Destructable is Ownable {

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

}

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

contract HelloWorld is Destructable {

}

  1. Create destroyable contract
import "./Ownable.sol";

pragma solidity 0.5.12;

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

}
  1. Import Destroyable.sol into HelloWorld contract
  2. Add Destroyable to HelloWorld contract inheritance
import "./Ownable.sol";
import "./Destroyable.sol";

pragma solidity 0.5.12;

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

contract Destroyable is Ownable {
    function destroyContract() public {
        selfdestruct(address(uint160(owner)));
    }
}
1 Like

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

contract CloseContract is Ownable {

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

}
HelloWorld has to extend this CloseContract

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

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

I changed ‘owner’ to a payable address in the Ownable contract:

contract Ownable {
    address payable public owner;
1 Like

Hi @timtim
Your contract HelloWorld doesn’t have to inherit from the Ownable contract as the Destroyable contract is inheriting from it.

1 Like

@filip @gabba

I can’t seem to figure out how to get the onlyOwner modifier to work in the Destroyable contract file. Please Help.

Here is the modifier in case thier is somthing wrong with it.

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

Here is the contract Destroyable.

pragma solidity 0.5.12;

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

Let me know If you need more information or display of code. This is my first time asking for help.