8

I try something like this:

Child.sol

pragma solidity ^0.4.6;
contract Child {
  address owner;

  function Child() {
    owner = msg.sender;
  }
}

Parent.sol

pragma solidity ^0.4.6;

import "./Child.sol"

contract Parent {

  address owner;


  function Parent(){
    owner = msg.sender;
  }

  function createChild() {
    Child child = new Child()
  }
}

Then I deploy Parent.sol. I can find this transaction on etherscan. Then I call createChild() function, but the contract is not deploying, because I can't find any new transactions.

So the question is, how should I do it right. Thank you.

Or should I just archive all adresses of the Child contracts in Parent contract? Then Parent contract can be really heavy.

2 Answers 2

2

It's possible you aren't seeing the Child contract because it's in an internal transaction. Look under the "Internal Transaction" tab on etherscan (on the Parent contract's page) to check.

1
  • 1
    Yeah, you are right, thanks! I found my Child contract adress in internal transactions section. But anyway, it is will be really hard to interact with that contract. Only if as @rob-hitchens said we will have an array of these contracts in Parent and then interact with them through this contract. Commented Jun 28, 2017 at 15:37
3

It's a good start in my opinion, but Parent does say anything about child so it's not useful.

Some ideas.

    pragma solidity ^0.4.6;

    contract Child {

      address public owner; // public, so you can see it when you find the child

      function Child() {
        owner = msg.sender;
      }
    }

and ...

pragma solidity ^0.4.6;

import "./Child.sol"

contract Parent {

  address owner;
  address[] public children; // public, list, get a child address at row #
  event LogCreatedChild(address child); // maybe listen for events

  function Parent(){
    owner = msg.sender;
  }

  function createChild() {
    Child child = new Child();
    LogChildCreated(child); // emit an event - another way to monitor this
    children.push(child); // you can use the getter to fetch child addresses
  }
}

I just did that right here in SE so I hope I didn't flub the syntax on you.

Hope it helps.

5
  • Thanks, I'll try it. But is it good to store children in Parent? I mean if we have a lot of children units the Parent contract will be realy heavy. Commented Jun 27, 2017 at 21:43
  • You're right to think about scale and cost. That's an important consideration. So far, it's a very simple example and the gas cost and performance should be consistent. It would be a little on the wordy side to explain the details. Suffice it to say that the .push() does not increase in cost as the size of the table grows. Commented Jun 28, 2017 at 1:05
  • Could you please explain me. We create Child instances and store it in Parent contract. When we send transactions, for example, to some of the children (we change some value in child contract), will it cost the same as for Parent with an array storing 100000 Child instances and for Parent with an array storing 100 instances? So as I got it now, we don't actually pay for storing heavy contracts, we pay for transactions, so if the transaction is only to change some value or transfer ether to some address, this transaction should cost the same as other simple transactions? Commented Jun 28, 2017 at 13:54
  • I just think how can I develop it right. Is it sufficient just deoloy one Parent contract and store there other Child contracts, or should I somehow use a node on a server to separately deploy new Child contracts to the blockchain. Commented Jun 28, 2017 at 13:56
  • The precise design depends on use-case. As far as this example goes, so far so good on all your questions, although I sense some hidden assumptions and would clarify some things for you. This thread is getting too long for SE, so let's take it offline. Please find my contact info in profile and feel free to reach out. Commented Jun 28, 2017 at 14:26

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.