0

I want to call function, which returns some value from child contract inside parent contract. Child contract will serve me as configuration, aka values from config contract can be reused in parent contract.

I tried to do something like this:

pragma solidity ^0.4.23;

import "./config.sol";

contract ParentContract{

    ConfigContract two;

    constructor() public{
        two = ConfigContract(msg.sender);
    }

    function getNameFromOtherContract() public view returns(string) {
        return two.getName();
    }
}

Config:

pragma solidity ^0.4.23;

contract ConfigContract{

    constructor() public{
    }

    function getName() external pure returns (string) {
        return "Amel";
    }
}

I use remix, I deploy ConfigContract first: enter image description here

Then I deploy ParentContract: enter image description here

When I then execute getNameFromOtherContract, I get error:

call to ParentContract.getNameFromOtherContract errored: VM error: revert.

3

Here's the problem in ParentContract:

constructor() public{
    two = ConfigContract(msg.sender);
}

You are assigning a ConfigContract with address of msg.sender to two. But, msg.sender isn't actually a ConfigContract, unless you instantiate the ParentContract inside the ConfigContract.

Here's my solution: first deploy the ConfigContract, then pass it's address to the constructor of ParentContract, which then saves it in two.

I updated the constructor for you in the code below

pragma solidity ^0.4.23;

contract ParentContract{

    ConfigContract two;

    constructor(ConfigContract _conf) public{
        two = _conf;
    }

    function getNameFromOtherContract() public view returns(string) {
        return two.getName();
    }
}

contract ConfigContract{

    constructor() public{
    }

    function getName() external pure returns (string) {
        return "Amel";
    }
}
  • That's it thanks. Didn't understand that contract has to be connected with address. Have to go solidity from scratch on weekend ;) – Dzeki Novak May 17 '18 at 9:36
2

There is an issue in the first contract.

two = ConfigContract(msg.sender);

The address you need to put in ConfigContract('address') is the address at which ConfigContract was deployed. Like this:

pragma solidity ^0.4.23;

import "./config.sol";

contract ParentContract{

    address public configContractAddress;
    ConfigContract two;

    constructor() public{
        configContractAddress = 0x610...;
        two = ConfigContract(configContractAddress);
    }

    function getNameFromOtherContract() public view returns(string) {
        return two.getName();
    }
}

Hope this helps

  • That's it thanks. Didn't understand that contract has to be connected with address. Have to go solidity from scratch on weekend ;) Can't accept two answers though. – Dzeki Novak May 17 '18 at 9:07

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.