Inheritance & External Contracts - Discussion

I’m not able to deploy this contract. It doesn’t give me the “Deploy” button. Error is shown in this photo below. What did i do wrong?

You have to select the same solidity version from the contract to the compiler, in the same solidity compiler options, at the start you can select the version, it must match the same of your contract.

Carlos Z

pragma solidity ^0.7.5;

contract Ownable {

 address payable owner;

 constructor () {

owner = msg.sender;

    }

 modifier onlyOwner {

     require (msg.sender ==owner);

     _;

 }

}

pragma solidity ^0.7.5;

import “./Ownable.sol”;

contract Selfdestruct is Ownable {

function close () public onlyOwner {

    selfdestruct(owner);

}

}

pragma solidity 0.7.5;

import “./Ownable.sol”;

import “./Selfdestruct.sol”;

contract Bank is Selfdestruct {

mapping (address => uint) balance;

event depositDone(uint amount, address to);

event _transferredinfo(uint amount, address from, address to);

function deposit () public payable returns (uint){

balance[msg.sender] += msg.value;

emit depositDone (msg.value, msg.sender);

return balance[msg.sender];

}

function getBalance (address _address) public view returns(uint){

return balance[_address];

}

function transfer (address recipient, uint amount) public {

_transfer(msg.sender, recipient, amount);

emit _transferredinfo(amount, recipient, msg.sender);

}

function withdraw (uint amount) public returns (uint){

require (amount<=balance[msg.sender] , "you try to withdraw more than your deposit");

    msg.sender.transfer(amount);

    balance[msg.sender]-=amount;

    return balance[msg.sender];

}

function _transfer (address from, address to, uint amount) private {

balance[from] -= amount;

balance[to] += amount;

close () ;

}

}

1 Like

So I’ve been working on codes and I’ve been given an error despite following what you have been doing.
I have been getting an error for the line msg.sender.transfer(amount); .
The error message I’ve received is:
from solidity:
TypeError: “send” and “transfer” are only available for objects of type “address payable”, not “address”.
–> contracts/helloworld2.sol:19:9:
|
19 | msg.sender.transfer(amount);
| ^^^^^^^^^^^^^^^^^^^

Is there a solution? I’ve tried numerous things. Thank you

Please share your code in the following way so we can review it and help you solve the issue:

Carlos Z

I had managed to solve it, there was a problem with the compiler. Thank you.

  1. The base contract is the same as the parent contract, while the derived contract is the same as a child contract.

  2. All functions on the base contract are available in the derived contract.

  3. Hierarchical inheritance is a phenomenon where a single contract act as a base contract for multiple contracts.

2 Likes
  1. What is the base contract? It is the parent contract.
  2. Which functions are available for derived contracts? All the functions of the parent contract.
  3. What is hierarchical inheritance? Multiple child contracts from the same parent contract.
2 Likes

Why does my contract not show the owner tab after I deploy the contract with the key word ‘public’?

This is the parent contract (right?) saved in another file:
contract Ownable {

address public owner;

modifier onlyOwner {

    require(msg.sender == owner);

    _;

}

constructor() {

    owner = msg.sender;

}

}

and i’ve used:
import “./ownable.sol”;
in my Hellowworld.sol Bank contract but I still only get: depost, transfer, withdraw and getBalance as button options in my result.

for reference here is my contract:

pragma solidity 0.7.5;

import “./ownable.sol”;

contract Bank is ownable{

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);

    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);

           

    assert(balance[msg.sender] == previousSenderBalance - amount);

}



function _transfer(address from, address to, uint amount) private {

    balance[from] -= amount;

    balance[to] += amount;

}

Any thoughts?

Many thanks

Could you please provide the code properly in the following way? The code you provided is not entirely in a good syntax.

Carlos Z

  1. the base contract is the parent contract
  2. functions of derived contracts are; single inheritance ,multi-level inheritance, hierarchical inheritance, multiple inheritance, encapsulation,polymorphism, and function polymorphism.
    3.a single contract acts as a base contract for multiple derived contracts
1 Like

Hi Ben17,

I ran into the same issue.

What you need to check is to make sure you are using the correct GovernmentInterface(address).

Make sure you copy the address in the deployed contract:
govtaddress

And put it in this line of your Bank contract:

Make sure you are not copying and using the address from the account drop down list with the 10 addresses to pick from.

That’s what corrected the same error message for me.

Hope that helps.

Hi justkaz,

I ran into the same issue.

What you need to check is to make sure you are using the correct GovernmentInterface(address).

Make sure you copy the address in the deployed contract:
govtaddress

And put it in this line of your Bank contract:

Make sure you are not copying and using the address from the account drop down list with the 10 addresses to pick from.

That’s what corrected the same error message for me.

Hope that helps.

1 Like

Destroyable contract


// SPDX-License-Identifier: MIT
import "./Ownable.sol";
pragma solidity 0.7.5;

//Allow any contractcontract inheriting from it to self destruct.
//This action should only be available for contract owner.
//keep contract owner functionality seperate in Ownable


contract Destroyable is Ownable {

    function destroyable() external onlyOwner  {

        //self destruct takes in 1 input, an addy to send all ether in contract.
        //Address(msg.sender) must be payable to work because it has to be capable of accepting eth.
        selfdestruct(payable(msg.sender)); 

    }

}

Ownable Contract

// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

contract Ownable{

    address public owner ;

modifier onlyOwner{

        require(msg.sender == owner);

        _; // run function

}

   constructor(){

        owner= msg.sender;

    }

// a tiny function placed to run before the main function code.
// If you have a piece of code that is going to be used alot for different fxns.
// require(msg.sender == owner), is called with modifier onlyOwner
//add onlyOwner to fxn after visibility.
   
}

Main Bank Contract

// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

//To iherit from a diff contract(ownable)

//1.import contract, 2. contractA is contractB{},3. add modifier from contract B to contract A function(withdraw)

import "./Ownable.sol";
import "./Destroyable.sol";
        
    
//Multiple inheritance start with most baselike(ownable), destroyable has prop of ownable.

contract Bank is Ownable, Destroyable{
1 Like

After reading the article https://betterprogramming.pub/solidity-what-happens-with-selfdestruct-f337fcaa58a7,
I still don’t understand the test methods store() and retrieve().
Why do we need to store and retrieve values, and these tests? What’s the purpose of the tests? What if we don’t do them?

  1. The parent contract is the base contract.
  2. All public and internal scoped functions are available inside derived contracts.
  3. Hierarchical inheritance is similar to simple inheritance. A single contract acts as a base contract for multiple derived contracts.
1 Like
  1. What is the base contract? The Base Contract is the parent contract that all other related contracts inherit from.
  2. Which functions are available for derived contracts? All functions are availible to derieved contracts
  3. What is hierarchical inheritance? hierarchical inheritance in a form of inheritance that has the base contract with multiple contracts inheriting from the base.
1 Like

1 The Base contract is the contract from which another contract inherits functions and variables. is the first in the chain and he does not inherit anything from other contracts.

2 all types of function and variables may be derived from child contracts

3 multiple child contracts inheriting from the same base contract. The contracts should follow a specific order while inheriting, starting from the base contract through to the most derived contract.

1 Like

i don’t get the difference between let owner variable be public or owner variable be internal.
we made a function in the “helloworld” contract that makes owner public from internal, what is the difference from leaving it directly public into the parent contract?

1 Like
  1. What is the base contract?
    the parent contract is known as a base contract

  2. Which functions are available for derived contracts?
    state variables and internal methods .

  3. What is hierarchical inheritance?
    a single contract acts as a base contract for multiple derived contracts.

1 Like