Inheritance Assignment

you are missing the file format, which is .sol at the end. :nerd_face:

Try it and let us know.

Carlos Z

2 Likes

hey Filip, I have a question… Dont know what this function means
govermentInstance.addTransaction(msg.sender, recipient, amount);
it shows me error that its undefined…
browser/Bank.sol:35:9: DeclarationError: Undeclared identifier. govermentInstance.addTransaction(msg.sender, recipient, amount); ^---------------^

1 Like

@thecil thank you!!! :wink:
now I checked and it works! :slight_smile:

1 Like
pragma solidity 0.7.5;

contract Destroyable{
    
    address public owner;
    modifier onlyOwner{
        require(msg.sender==owner, "not owner");
        _;
    }
    
   constructor(){
        owner=msg.sender; 
    }
    
    event lastGoodbye(string lastMessage);
    
    function kaboom() public onlyOwner { 
      emit  lastGoodbye("Bye cruel world!");  
      selfdestruct(address(uint160(owner))); 
    }
}

In the task it is mentioned that we should create a new contract. That is why I put all in this one contract. Is this ok? Or should have I reused existing files like I saw in the solution.
(I know that is good pratcice to use imports, but just asking for this task)

1 Like

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

contract Destroyable is Ownable{

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

}

1 Like

Thank you :+1:

For the bank contract I did:

import "./Destroyable.sol"

contract Bank is Destoryable {
}
1 Like

Hey @Gorana, hope you are ok.

there could be an issue in your smart contract, code line # 35 or 9. You might want to share your code so we can help you find the solution :nerd_face:

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

Carlos Z.

1 Like

Hi @inahan, hope you are ok.

Your solution can work with no big issue, but has you realize, it will be better to inherit functionalities from other contracts (like ownable), so i could suggest to keep the good practice of importing other contracts into a new one in order to re-use that code properly.

I mean, if your Ownable contract already have all the functionalities for the onlyOwner modifier and conditions, it is better practice to just import it to the Destroyable contract instead of re-writing the same functions.

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

Carlos Z.

@thecil Thank you Carlose again, it is Filip’s solution for assignment that shows me that:
pragma solidity 0.7.5;
import “./Ownable.sol”;
import “./Destroyable.sol”;

contract Bank is Ownable, Destroyable {

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);
    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 sufficient");
    require(msg.sender != recipient, "Don't transfer money to yourself");
    
    uint previousSenderBalance = balance[msg.sender];
    
    _transfer(msg.sender, recipient, amount);
    
    **govermentInstance.addTransaction(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;
}

}

I wached next video about external contracts, and its there, it is a function from external contract that I havent made yet, I tried to finish assignment first, and than checked for solution that wasn’t working for me.

1 Like

thank you, I’ll do that!

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

contract Destroyable is Ownable {

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

Indeed, the bank contract works good if you delete a code line that should not be there for this assignment, at least not for now.

**govermentInstance.addTransaction(msg.sender, recipient, amount);**

If you delete or comment that line, the entire contract works as expected.

What does code lines do?: (if you are wondering…)
govermentInstance should be a variable that bind the interface of a contract (government contract i assume) so you can use that variable as instance for a contract interface.

govermentInstance.addTransaction(msg.sender, recipient, amount); means, from the govermentInstance variable, invoke the function addTransaction.

Problem is:

  • In the bank contract we do not import nor define the contract neither its interface.
    Easy Fix:
  • Remove that code line, contract works perfect.

This could be just a mistype that we made, and thank you for notifying it :nerd_face:.
We will be fixing it these next days.

@jon_m just tagging you to made you aware of this partner :face_with_monocle:

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

Carlos Z.

1 Like

This is my parent contract Ownable ( “ownable.sol”) that controls access. It’s located within the same directory as the child.

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

This is my “Destroyable” contract, as you can see it inherits “Ownable” and contains the function “removeContract()” which permits the self destruction, callable only by the owner address.

import "./ownable.sol";

contract Destroyable is Ownable{
    
    function removeContract() public onlyOwner {
        selfdestruct(owner);
}
    
    
} 
1 Like
pragma solidity 0.7.5;

import './ownable.sol';

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

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

modifier onlyOwner {
require(msg.sender == owner);
_;//Proceed to function code.
}
}

contract Bank is Ownable {

mapping(address => uint) balance;

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

}

1 Like
contract Ownable {
    
    address owner;
    
    modifier onlyOwner { //here we restrict access
        require(msg.sender == owner); // this function is available only to the contract owner
        _;
    }   
        constructor (){
        owner = msg.sender; 
    }
}

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(owner);
    }
    
}
1 Like

Here it is, with a child contract as a test :

pragma solidity 0.7.5;

contract Destroyable {
    
    
    address owner;
    
    modifier onlyOwner{
        require(msg.sender == owner);
        _;
    }
    
    constructor(){
        owner = payable(msg.sender);
    }
    
    function DestroyContract() public onlyOwner returns (bool isDestroyed){
        selfdestruct(payable(owner));
        return true;
    }
    
}

contract MyContract is Destroyable{
    
    // used to test the disposition of balance after the destruction of contract
    function Deposit() public payable returns (uint balance){
        return address(this).balance;
    }
    
}
1 Like

pragma solidity 0.7.5;
contract Destroyable {

address payable owner;

constructor(){
    owner = msg.sender;
}

function destroy() public { 
    require(msg.sender == owner);
    selfdestruct(owner); 
}

}

1 Like