Inheritance & External Contracts - Discussion

transactionLog[_index] Is the position of the element on the array, but transactionLog is an array based on the struct Transaction so each element on the array will have the struct properties, like from or to, so in order to access this properties, you use the dot ..

Hope it helps, :nerd_face:

Carlos Z

Ownable contract:

pragma solidity 0.7.5;

contract Ownable{
    address public owner;

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

Destructible contract:

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

contract Destructible is Ownable{

    function Destructible_fun(address payable addr) public onlyOwner{
         selfdestruct(addr);
    }
}

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

contract Bank is Destructible{

1 Like

The base contract is the main parent contract that gets inherited into all of the child contracts.

All of the parent contract’s public and internal variables can be called within the child contract.

Hierarchical inheritance is where two or more more child contracts inherit from the base contract without inheriting from each other as they would with multi level inheritance.

2 Likes
  1. What is the base contract?
    The parent contract in a parent-child relationship.

  2. Which functions are available for derived contracts?
    all public and internal scoped functions and state variables from the base contract

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

1 Like

Blockquote
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.

My Transfer Function works intermittently. Still trying to work out why :pensive:

1 Like

Please share your code in the following way so i can help you to review it :nerd_face:

Carlos Z

1 Like
  1. What is the base contract?
    Is the parent contract.

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

  3. What is hierarchical inheritance?
    is one in which a single contract acts as the base contract for multiple derivative contracts.

3 Likes

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.
3 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