Inheritance Assignment

@jaykch

Oh yeah, I have understood your question now . it wouldnt destroy the usid. it just destroy itself but it transfer the token to the msg.sender before if it has some.

Happy learning :grinning:
Abel Sebhatu

Destroyable.sol

pragma solidity 0.7.5;

import "./Ownable.sol";

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

Modifications to Bank.sol

pragma solidity 0.7.5;

import "./Destroyable.sol";

contract Bank is Destroyable {
   
   mapping(address => uint) balance;
...
1 Like

Okay im donesky so this is destroyable.sol

contract destroyable {

address owner;

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

}

 function destroy () public OnlyOwner{
   selfdestruct (msg.sender);
   }

}

and here is the bank contract inheriting the good stuff xD

pragma solidity 0.7.5;

import"./destroyable.sol";

contract bank is destroyable {

mapping(address => uint) balance;

event depositDone(uint amount, address depositedTo);

event transferListener(address depositedFrom, address depositedTo, uint amount);

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 returns(uint){
require(balance[msg.sender] >= amount, “you cant withdraw more than you have”);
msg.sender.transfer(amount);
balance[msg.sender]-= amount;
}

function getBalance() public view returns (uint){
return balance[msg.sender];

}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount);
require(msg.sender != recipient, “Don’t send funds 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;
emit transferListener(from, to, amount);
}
}

Pls let me know of my multiple failures as i know theres plenty of them xD, but yes let me know where i have to work seniores, your help is really appreciated :slight_smile:

1 Like

Hey @Carlosvh96, hope you are great.

I have tested your code, it works very good, congratulations.
Next time when you need to share coude, You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){
X	
let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

Carlos Z.

For child destroyable.sol

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

contract Destroyable is Ownable {

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

import “./Owner.sol”
pragma solidity >=0.4.22 <0.7.0;

/**

  • @title Destroyer

  • @dev Set & change owner
    */
    contract Destroyer is Owner{
    //onlyOwner is custom modifier
    function destory() public onlyOwner {
    selfdestruct(msg.sender); //‘owner’ s the owners address

    }
    }

1 Like

contract Ownable {
address owner;

constructor() {
    owner = msg.sender;
}

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

}

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

contract Bank is Destroyable {
// Bank contract code here
}

1 Like

I made the Ownable file first:

pragma solidity 0.7.5;

contract Ownable {

 address owner;

modifier onlyOwner {
    require(msg.sender == owner, "You are not the owner of contract");
    _;
}

constructor() {
    owner = msg.sender;
}

}

Then used in the destruct file.

pragma solidity 0.7.5;

import “./Ownable.sol”;

contract Destruct is Ownable{

function destruct() public onlyOwner {

selfdestruct(owner);
}
}

1 Like
pragma solidity 0.8.1;

contract Ownable {
    address owner;
    
    //modifier can take arguments e.g. modifier onlyOwner (uint cost){ .......}
    modifier onlyOwner {
        require(msg.sender == owner, "No premission for this operation!"); // if reqire contition is not met, the function will be revert
        _; // _ means run the function. This line will be replaced with the function code
    }
        
    constructor(){
        owner = msg.sender;
    }
    
}

contract Destroyable is Ownable{

    function close() internal onlyOwner { //onlyOwner is a custom modifier
        selfdestruct(payable(owner));
    }
}
1 Like

pragma solidity 0.7.5;

contract Ownable{

address owner;

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

}

pragma solidity 0.7.5;

import “./Ownable.sol”;

contract Destroyable is Ownable{

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

}

1 Like
pragma solidity 0.7.5;

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



pragma solidity 0.7.5;

import "./newOwnable"; 

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

HelloWorld contract

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

contract HelloWorld is Ownable, Destroyable{

Ownable Contract

pragma solidity 0.5.12;

contract Ownable{
address public owner;

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

}

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

}

Destroyable Contract

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

contract Destroyable is Ownable{

   function close() public onlyOwner { //onlyOwner is custom modifier
    selfdestruct(msg.sender);  // 'owner' is the owners address
}

}

1 Like
pragma solidity 0.8.0;

import "./Ownable.sol";

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

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

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

}

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

Is selfdestruct meant to take the deployer’s address or the contract address as the parameter?
Or the payable address of the function?

pragma solidity ^0.7.5;


contract Ownable {
    
    address payable public owner;
    
    constructor() {
        owner = msg.sender;
    }
    
    modifier onlyOwner {
        require(msg.sender == owner, "only owner allowed");
        _;
    }
}


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

If you check carefully, the owner variable value is set in the constructor, which is msg.sender, so when the contract is deployed by an address, it will be bind to the owner variable (deployer address), then in the Destroyable contract the destroy() function just call the internal function selfdestruct with the owner variable as the argument, which will be the address to send all the funds before the contract get destroyed. You could also specify another address in the selfdestruct function if you want to for example.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Ownable.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.5;

contract Ownable {
    address payable owner;

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

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

Destroyable.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.5;

import "./Ownable.sol";

contract Destroyable is Ownable {

    event contractShutdowned(address contractShutdownedBy, uint fundsReclaimed);

    /// destroy the contract and reclaim the funds.
    function shutdown() public onlyOwner {
        emit contractShutdowned(owner, address(this).balance);
        selfdestruct(owner);
    }
}
2 Likes
pragma solidity 0.7.5;

import './Ownable.sol';

contract Destructible is Ownable {
    
    function destroy() public onlyOwner {
       selfdestruct(owner); 
    }
}
1 Like

Ah OK, makes sense. Thanks for the reply :slight_smile:

1 Like