0

I have a contract that creates instances of a contract and now I have to get the data of the newly created contract. Is there a way to do that?

This is my contract that creates new contracts:

pragma solidity ^0.4.8;

contract myFirstContract
{
    address[] public childContracts;
    uint public idd;
    string public name1;

    function newContracts(uint _id,string _name) public returns(address _newContract)
    {
        uint id=_id;
        string memory name=_name;
        anotherContract a = new anotherContract(id,name);
        childContracts.push(a);
        return a;
    }

    function getContractData(uint _address) public
    {

    }
}

contract anotherContract
{
    string public name;
    uint  public id;
    constructor(uint _id,string _name) public
    {
        name=_name;
        id=_id;
    }
}

Now, let's say that there is a new anotherContract getting created and I have the address of this contract then how do I get the data in this new contract.

1 Answer 1

1

Please try this. You need to define get methods inside your anotherContract. This way I was able to create new anotherContract with method newContracts and later later it data with method getContractData.

pragma solidity ^0.4.8;

contract myFirstContract
{
    address[] public childContracts;
    uint public idd;
    string public name1;

    function newContracts(uint _id,string _name) public returns(address _newContract)
    {
        uint id=_id;
        string memory name=_name;
        anotherContract a = new anotherContract(id,name);
        childContracts.push(a);
        return a;
    }

    function getContractData(uint _address) public view returns(uint, string)
    {
        anotherContract anotherContractInstance = anotherContract(_address);
        return (anotherContractInstance.getId(), anotherContractInstance.getName());
    }
}

contract anotherContract
{
    string public name;
    uint public id;

    constructor(uint _id,string _name) public
    {
        name=_name;
        id=_id;
    }

    function getId() view public returns(uint) {
        return id;
    }

    function getName() view public returns(string) {
        return name;
    }
}
1
  • So in getContractData I was trying to have something like uint idd = _address.id; and it wasn't working and this does. Thanks
    – vteja711
    Dec 23, 2019 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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