Inheritance & External Contracts - Discussion

Hi Filip,

I have a question, it’s probably really straight forward.

Assume I launch my own smart contract, a token for example. I then inherit code from openzeppelin on github. What would happen to my smartcontract if openzeppelin deletes it’s account or changes all the files.

Or does the blockchain inherits once all the code?

thanks!

1 Like
  1. What is the base contract?
    the contract has a parent known as the derived class and the parent contract is known as a base contract .

  2. Which functions are available for derived contracts?
    There is a is a relationship between base and derived contracts and all public and internal scoped functions and state variables are available to derived contracts .

  3. What is hierarchical inheritance?
    Hierarchical inheritance is again similar to simple inheritance. Here, however, a single contract acts as a base contract for multiple derived contracts.

2 Likes

Ownable.sol

pragma solidity 0.8.7;

contract Ownable {
    address payable public owner;

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

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

}

Destroyable.sol

pragma solidity 0.8.7;

import "./Ownable.sol";

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

Bank.sol


import "./Destroyable.sol";

contract Bank is Destroyable {
...
2 Likes

Hello,

So I have a question. For some reason, when I add the code into the transfer function to send a copy of the transaction to the government contract, it makes the transfer function no longer work properly. No matter what I do it keeps reverting and showing an error message. I have uploaded a screenshot of the error message and pasted my full code below. Any help would be greatly appreciated, thanks!

Bank Contract

pragma solidity 0.7.5;

interface GovernmentInterface{
   function addTransaction(address _from, address _to, uint _amount) external;
}

contract Bank {

   GovernmentInterface governmentInstance = GovernmentInterface(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);

    mapping(address => uint) balance; 

    event depositDone(uint toAdd, address 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 returns(uint){
       require(amount <= balance[msg.sender], "You can't withdraw more than your posted balance");
       msg.sender.transfer(amount); 
       balance[msg.sender] -= 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, "Insuficient Balance"); 
       require(msg.sender != recipient, "Cannot Transfer to Yourself"); 

       uint previousSenderBalance = balance[msg.sender];    

       _transfer(msg.sender, recipient, amount);

       governmentInstance.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; 
    } 

}

Government Contract

pragma solidity 0.7.5;

contract Government {

    struct Transaction {
        address from;
        address to;
        uint amount;
        uint id;
    }

    Transaction[] transactionLog; 
    
    function addTransaction(address _from, address _to, uint _amount) external {
        transactionLog.push( Transaction(_from, _to, _amount, transactionLog.length) );
    }

    function getTransaction(uint _index) public view returns(address, address, uint){
        return (transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount);
    }
}

1 Like

Im not sure why its failing for you, i have just copy/paste your contracts, deploy the government contract first, copy the address, paste it on your bank contract and deployed it.

I was successfully able to transfer without any error, and I just copy/paste your contracts, maybe try to restart the process (delete all deployments, deploy again and test again).

Carlos Z

1 Like
  1. What is the base contract?
    The Parent contract that is inherited.

  2. Which functions are available for derived contracts?
    All from inherited contract

  3. What is hierarchical inheritance?
    A single contract is the base for multiple derived contracts.

2 Likes

I tried that many times and it just wont work for some reason. Any possible ideas as to why it would work perfectly fine on your computer but not on mine?

1 Like

What is the base contract?
=> It is the contract in the top of the hierarchy

Which functions are available for derived contracts?
=> all public and internal functions are available to derived contracts

What is hierarchical inheritance?
=> It is the tree of Contracts, with parents on top and their children below.

2 Likes

Q1. What is the base contract?
The contract that is inherited by the child contracts is called the parent contract or base contract.

Q2. Which functions are available for derived contracts?
All public and internal scoped functions and state variables of the parent contract/s are available to derived contracts.

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

contract B is A {

}

contract C is A {

}

2 Likes
pragma solidity 0.7.5;

contract Ownable{
     address public owner;

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


and

pragma solidity 0.7.5;

import "./Ownable.sol";

contract Destroyable is Ownable {

 

    function ELIMINATE() public onlyOwner {

      selfdestruct(msg.sender);

    }

:sunglasses:

2 Likes
  1. Its the contract form which other contracts inherit.
  2. all public and internal scoped functions and state variables are available to derived contracts.
  3. a single contract can act as base for multiple derived contracts.
2 Likes
  1. The base contract, also known as the “Parent” is the contract which the solidity compiler
    compiles the data into bytecode and copies it into the derived contract - which is the “Child”.

2.The functions which are availibale to the derived contract are all the public and internal functions,
and also all of the state variables that exists in the base contract.

3.Hierarchial Inheritance is a method of inheriting data from a base contract
into 2 different contracts that run independently and seperate from each other.

2 Likes

I always get confused when Filip uses the . I can understand that when it is an array if you write array.length means that you are choosing everything that is included in the array but other times he uses dots for other examples and use cases too. For instance in the external contract video he writes

returns (transactionLog[_index].from, transactionLog[_index].to, transactionLog[_index].amount.

can someone explain to me why the dots are used? it is a bit confusing. thank you

1 Like

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