0
pragma solidity ^0.4.24;

contract Bank{
    uint private value;

    constructor(uint amount) public{
        value = amount;
    }
    function deposit(uint amount) public{
        value += amount;
    }

    function withdraw(uint amount) public{
        value -= amount;
    }

    function balance()view public returns (uint){
        return value;
    }
}

contract MyFirstContract is Bank(10){
    string private name;
    uint8 private age;

    function setName(string _name) public {
        name = _name;
    }

    function getName()view public returns(string){
        return name;
    }

    function setAge(uint8 _age) public{
        age = _age;
    }

    function getAge() view public returns (uint8){
        return age;
    }

}
  • 1
    Can you describe the steps you take to deploy, the expected result and how the actual result differs from expected? – Rob Hitchens - B9lab Nov 11 '18 at 16:21
  • @Rob Hitchens B9lab Before I created the "contract Bank{" I created "MyFirstContract" and everything showed perfectly. The goal is to show the balance of the bank and have "contract MyFirstContract" within "contract Bank. Right now I have my Bank balance shown as 10 "contract MyFirstContract is Bank(10)" When I hit contract deploy, deposit, withdraw and balance all show up. Deposit and withdraw can be changed as expected but my bank balance is not showing up as 10 rather it is saying zero. I am pretty new at solidity so if I apologize ahead of time if my response is confusing. – Xavi Nov 11 '18 at 16:57
0

Based on your comment, I think you're confusing inheritance and interaction.

It sounds like you expect MyFirstContract to interact with a Bank which be an interaction between two independent contracts. This is a common error because the syntax looks similar. For interaction to work, MyFirstContract will need knowledge of the ABI for the Bank contract, as well as its address in order to interact with it.

Your code is using inheritance to assign the properties of a Bank to MyFirstContract. MyFirstContract has all the functions of Bank and ignores the other Bank contract you deployed.

The code below changes it to an interaction. I've removed unrelated concerns for clarity. You can see it working by:

  1. Deploy a Bank (once)
  2. Deploy a MyFirstContract and pass in the address from step 1.
  3. Invoke depositInBank() in MyFirstContract
  4. Check Bank.value() and you will see it updating.

-

pragma solidity ^0.4.24;

contract Bank{
    uint private value;

    constructor(uint amount) public{
        value = amount;
    }
    function deposit(uint amount) public{
        value += amount;
    }

    function withdraw(uint amount) public{
        value -= amount;
    }

    function balance()view public returns (uint){
        return value;
    }
}

contract MyFirstContract { // No inheritance. This client is not a Bank.

    Bank b; // But it needs knowledge of the Bank, so cast b as a contract, Bank. This is how this contract learns the ABI for the Bank

    constructor(address bankAddress) public {
        b = Bank(bankAddress); // instantiate b at the deployed address. Now we can interact with it. 
    }

    // invoke a function in the other contract

    function depositInBank(uint amount) public {
        b.deposit(amount);
    }

}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.