Inheritance & External Contracts - Discussion

I had exact same issue, spent like 2 hours trying to fix it :rofl: :joy:
It looks that wrong government address makes error that function should be payable. not sure if I would come up with the error being wrong government address if weren’t for you, but for future ref, if I would want it to solve it myself, Is there explanation why?

Hi @Kamil37

Errors are not always accurate.
In this case you should go step by step though your code and debug it.

Happy learning,
Dani

1 Like

hey I’ve had alot of trouble getting my contracts to compile. I don’t understand the error message.

Screen Shot 2021-04-06 at 10.31.51 AM

2 Likes

Hey @HanaYezi, hope you are well.

Your struct name is transaction, you just have to rename your array to transaction[] transactionLog; for example. (the array data type should have the same name as the struct)

Carlos Z

1 Like

Hello, can anyone help out what’s wrong here?

interface Financial {
    function addTransaction (address _from, address _to, uint _amount) external; 
    
}
contract bank is Ownable {
    
    
    FinancialInterface FinancialInstance = FinancialInterface(0xf8e81D47203A594245E36C48e151709F0C19fBe8);
    
error message is saying (identifier not found or unique) but as you can see below, it is declared in my 'Financial' contract and interface is integrated into the main contract.


pragma solidity 0.7.5;

contract Financial {
    
    struct Transaction {
        address from;
        address to;
        uint amount;
        uint txId;
        
    }
    
    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);
    }
}

Edit: code edited for clarity (dani)

Thanks

Hey @NetworkP

Please read this faq that will guide you to correctly post code in the forum: FAQ - How to post code in the forum

You called your interface Financial therefore your interface declaration has to use the same name.
Something like this should work:

interface FinancialInterface {
    function addTransaction (address _from, address _to, uint _amount) external; 
    
}
contract bank  {
    
    
    FinancialInterface  financial = FinancialInterface(0xf8e81D47203A594245E36C48e151709F0C19fBe8);
}


pragma solidity 0.7.6;

contract Financials {
    

Cheers,
Dani

1 Like

School boy error. Good old debugging for you!! always staring you in the face lol…Thanks alot. Much appreciated

1 Like
  1. What is the base contract?

It is a parent contract which is inherited by a child contract, and it itself inheriting from another parent contract.

  1. Which functions are available for derived contracts?
    public and internal scoped functions

  2. What is hierarchical inheritance?
    It is a (number >1) of contracts which inherits from the same derived class.

1 Like

What is the base contract?

  • The parent contract from which the child (derived contract) inherits.

Which functions are available for derived contracts?

  • Public and Internal functions and state variables

What is hierarchical inheritance?

  • A structure where multiple derived contracts inherit from a single base contract.

Polymorphism Question:

  • Based on my reading it seems like the purpose of polymorphism is to define as general a ‘template’ function as possible and then modify that function elsewhere as needed to minimize the need to rewrite code.

Is this accurate? Are there any benefits of polymorphism beyond this?

1 Like

Hello,
I wonder what happens when you send Eth from your contract to an external contract during the execution of one of your functions, but then one of the assert() or require()'s fail during the rest of your function execution. Does the Eth arrive at the external contract or will this transfer be undone? The same with when you call the function of an external contract, does this external functioncall go through when an assert() or require() within your function fails after making the function call?

Hey @Florian_P

If a contract does not pass a require statement, then the whole transaction is reverted therefore funds will not be sent to the receiver contract/user.

Cheers,
Dani

  1. What is the base contract?
    A base contract is a parent contract where other contracts inherit from.

  2. Which functions are available for derived contracts?
    All the functions within the derived contract as well as all public and external functions of the parent contract.

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

1 Like
  1. What is the base contract?
    Base contract is a parent contract in inheritance

  2. Which functions are available for derived contracts?
    All public and internal scoped functions and state variables are available to derived contracts.

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

1 Like
  1. What is the base contract?
    A. he 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?
    A Single inheritance, multi-level inheritance, hierarchical inheritance, multiple inheritance, function polymorphism, contract polymorphism.

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

1 Like
  1. What is the base contract?
    In contract composition, the base contract is the parent contract which is inherited by a child contract (derived contract).

  2. Which functions are available for derived contracts?
    All public and internal scoped functions in base contracts are available for derived contracts.

  3. What is hierarchical inheritance?
    Hierarchical inheritance describes the relationship where a single base contract is inherited by more than one derived contract.

1 Like

My bank contract was working great, and worked well with the government interface too. Then I got to the section about value calls, and when I updated my code it stopped working. So I went back and undid the changes but its still not working and I dont know what I’m missing, the error message isnt helping much but it says:
“transact to Bank.transfer errored: VM error: revert. 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.”
Also I get one if I try depolying contract with an amount of ether:
“creation of Bank errored: VM error: revert. 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.”

Here is my code:




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

contract Bank {
    
    GovernmentInterface GovernmentInstance = GovernmentInterface(0xd9145CCE52D386f254917e481eB44e9943F39138);
    
    mapping(address => uint) balance;
    address owner;
    event balanceAdded(uint amount, address indexed depositedTo);
    event transferred(uint amount, address indexed sentFrom, address indexed sentTo);
    
    modifier onlyOwner{
        require(owner==msg.sender);
        _;
    }
    
    constructor(){
        owner = msg.sender;
    }
    
    function deposit() public payable returns(uint){
        balance[msg.sender] += msg.value;
        emit balanceAdded(msg.value, msg.sender);
        return balance[msg.sender];
    }
    
    function withdraw(uint amount) public returns(uint){
        require(balance[msg.sender] >= amount, "not enough funds");
        msg.sender.transfer(amount);
        balance[msg.sender] -= amount;
        return balance[msg.sender];
    }
    
    function addBalance(uint _toAdd) public returns (uint){
        balance[msg.sender] += _toAdd;
        emit balanceAdded(_toAdd, msg.sender);
        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);
        require(msg.sender != _recipient);
        
        _transfer(msg.sender, _recipient, _amount);
        
        GovernmentInstance.addTransaction(msg.sender,_recipient,_amount);
        
        
        emit transferred(_amount, msg.sender, _recipient);
        
    }
    
    function _transfer(address _from, address _to, uint _amount) private{
        balance[_from] -= _amount;
        balance[_to] += _amount;
    }
    
    
    
    
    
}

1 Like
  1. What is the base contract?
    The base contract is the parent to the child or derived contract and is inherited.

  2. Which functions are available for derived contracts?
    All public and internal scoped functions.

  3. What is hierarchical inheritance?
    When a base contract is inherited by more than one derived contracts.

1 Like

Hey @Mitchell_Giddens

The errors are related to conditions not satisfied, more specifically:

So I went back and undid the changes but its still not working and I dont know what I’m missing, the error message isnt helping much but it says: […]

What function are you calling, what parameters are you sending?
Can you tell how to replicate it step by step?

Also I get one if I try depolying contract with an amount of ether:

If you are trying to send eth to your contract during the deployment you should make sure your constructor is payable otherwise the transaction will revert.

Cheers,
Dani

1 Like
  1. What is the base contract?

Parent contract that you are deriving from.

  1. Which functions are available for derived contracts?

External, public, private, internal

  1. What is hierarchical inheritance?

A single contract acts as a base for multiple derived contracts.

1 Like

Hey @Mitchell_Giddens, hope you are great.

As @dan-i mention you about the conditions not being satisfied, i would like to add:

you have 2 require statements in your transfer function, you should add an error msg on them and try again, maybe one of those are being trigger. thats why you have a revert error probably.

Let us know :face_with_monocle:

Carlos Z