0

Contract code:

pragma solidity ^0.4.11;

contract Base {
    uint stored;
    function set (uint _stored) {
        stored = _stored;
    }
    function get () constant returns (uint) {
        return stored;
    }
}

contract Child {
    Base b;
    function set (uint _stored) {
        b.set(_stored);
    }
    function get () constant returns (uint) {
        return b.get();
    }
}

Deployed as:

var i_sol_baseContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_stored","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]);
var i_sol_base = i_sol_baseContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b5b60c68061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b11460445780636d4ce63c146061575bfe5b3415604b57fe5b605f60048080359060200190919050506084565b005b3415606857fe5b606e608f565b6040518082815260200191505060405180910390f35b806000819055505b50565b600060005490505b905600a165627a7a72305820bcd4b3fa50cc6fc6b02c37155d3b2a414e76e8e152dc32cb9d7b75e06e5dfca60029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

var i_sol_childContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_stored","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]);
var i_sol_child = i_sol_childContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b5b6102078061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b1146100465780636d4ce63c14610066575bfe5b341561004e57fe5b610064600480803590602001909190505061008c565b005b341561006e57fe5b61007661012e565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166360fe47b1826040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561011957fe5b6102c65a03f1151561012757fe5b5050505b50565b6000600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d4ce63c6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050602060405180830381600087803b15156101be57fe5b6102c65a03f115156101cc57fe5b50505060405180519050505b905600a165627a7a72305820a394fde97515faefe918adeb9034603b30ce48d9f132e9ea9b29823befd3d12b0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

Setting and getting the value works when I call set and get on the base contract. However, get from the Child always returns 0. What is wrong in this situation?

Output from geth:

> i_sol_base.set(100, {from: eth.coinbase})
"0x4b35370e66082ec3eb88af8aa4c8bce64a9149daa479fdd17256b2208bd9a90a"
> i_sol_base.get()
100
> i_sol_child.set(200, {from: eth.coinbase})
"0xb9e8464468dfed83fd6d3f369bae229cf4e06de892e30f39080340c3247da777"
> i_sol_child.get()
0
> i_sol_child.get({from: eth.coinbase})
0
1

I haven't looked at the deployment side of this but I found issues with the contract. Corrected and tested with Remix.

pragma solidity ^0.4.11;

contract Base {
    uint stored;
    function set (uint _stored) {
        stored = _stored;
    }
    function get () constant returns (uint) {
        return stored;
    }
}

contract Child {

    // this describes the ABI for Base above ^
    // but it is silent on deployed address.

    Base b;

    // This constructor will run once. Child needs a hint
    // about where it can find a deployed Base to talk to. 

    function Child(address bAddress) {
        b = Base(bAddress);
    }

    function set (uint _stored) {
        b.set(_stored);
    }

    // need to return what was received (or else default 0)

    function get () constant returns (uint) {
        return b.get();
    }
}

In Remix to show it working.

enter image description here

  1. Deploy Base.
  2. Copy Base Address.
  3. Deploy Child and pass in Base Address.
  4. Set
  5. Get
  6. Ta Da :-)
  7. You have a known-good contract to work with as you work out the JS side.

Hope it helps.

  • In this case the child contract is modifying data stored in the base contract itself right? So if there are two child contracts c1 and c2 both of them would be modifying the same stored. I am trying to use inheritance the way it is typically used in OOP. So c1.stored would be different from c2.stored. Isn't this possible in solidity? – rohithpr Jun 18 '17 at 20:50
  • Your interpretation is correct. Currently structured so Base is a storage contract. For inheritance and separate storage you might be looking for contract Child is Base {} Some refactoring I think as it's a non-trivial change. Child would inherit Base's storage definition and functions; a completely different arrangement. Each Child would have separate storage. – Rob Hitchens - B9lab Jun 18 '17 at 20:54
  • Gotcha! I mixed two different concepts and ended up with that. Thanks a lot :) – rohithpr Jun 18 '17 at 21:05

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.