Skip to main content
Added a third way to do it.
Source Link
Rob Hitchens
  • 55.6k
  • 11
  • 91
  • 146

The easiest way to accomplish this is to create a way for the creating contract to report what it's doing. I'll refer to the contracts as "factory" and "generated", where there is one "factory" with a function that can create unlimited "generated" contracts.

Factory needs a getter function or event emitter; some way of exposing the addresses of the contracts it created.

With event emitter:

pragma solidity ^0.4.2;

contract factory {

  event LogContractCreated(address generated);

  function createContract() public returns(bool success)
  {
    Generated g = new Generated();
    LogContractCreated(g); // Generated g is directly convertable to address
    return true;
  }
}

contract Generated {
    // whatever it does
}

With getter function:

pragma solidity ^0.4.2;

contract factory {

  // keep track of generated addresses
  address[] private generated;

  function createContract() public returns(bool success)
  {
    Generated g = new Generated(); // make a new Generated and not its address
    generated.push(g); // record the address for later
    return true;
  }

  // optionally, report how many rows are in the list
  function getGeneratedCount() public constant returns(uint count) {
    return generated.length;
  }

  // achieve similar result setting generated to "public" near line 6
  function getAddressAtIndex(uint row) public constant returns(address contractAddress) {
    return generated[row];
  }
}

contract Generated {
    // whatever it does
}

You might also decide that you want to return the address created from createContract. You would be able to find the address in the mined transaction.

Change a few things:

function createContract() public returns(address contractCreated) {
  ...
  return g;

The easiest way to accomplish this is to create a way for the creating contract to report what it's doing. I'll refer to the contracts as "factory" and "generated", where there is one "factory" with a function that can create unlimited "generated" contracts.

Factory needs a getter function or event emitter; some way of exposing the addresses of the contracts it created.

With event emitter:

pragma solidity ^0.4.2;

contract factory {

  event LogContractCreated(address generated);

  function createContract() public returns(bool success)
  {
    Generated g = new Generated();
    LogContractCreated(g); // Generated g is directly convertable to address
    return true;
  }
}

contract Generated {
    // whatever it does
}

With getter function:

pragma solidity ^0.4.2;

contract factory {

  // keep track of generated addresses
  address[] private generated;

  function createContract() public returns(bool success)
  {
    Generated g = new Generated(); // make a new Generated and not its address
    generated.push(g); // record the address for later
    return true;
  }

  // optionally, report how many rows are in the list
  function getGeneratedCount() public constant returns(uint count) {
    return generated.length;
  }

  // achieve similar result setting generated to "public" near line 6
  function getAddressAtIndex(uint row) public constant returns(address contractAddress) {
    return generated[row];
  }
}

contract Generated {
    // whatever it does
}

The easiest way to accomplish this is to create a way for the creating contract to report what it's doing. I'll refer to the contracts as "factory" and "generated", where there is one "factory" with a function that can create unlimited "generated" contracts.

Factory needs a getter function or event emitter; some way of exposing the addresses of the contracts it created.

With event emitter:

pragma solidity ^0.4.2;

contract factory {

  event LogContractCreated(address generated);

  function createContract() public returns(bool success)
  {
    Generated g = new Generated();
    LogContractCreated(g); // Generated g is directly convertable to address
    return true;
  }
}

contract Generated {
    // whatever it does
}

With getter function:

pragma solidity ^0.4.2;

contract factory {

  // keep track of generated addresses
  address[] private generated;

  function createContract() public returns(bool success)
  {
    Generated g = new Generated(); // make a new Generated and not its address
    generated.push(g); // record the address for later
    return true;
  }

  // optionally, report how many rows are in the list
  function getGeneratedCount() public constant returns(uint count) {
    return generated.length;
  }

  // achieve similar result setting generated to "public" near line 6
  function getAddressAtIndex(uint row) public constant returns(address contractAddress) {
    return generated[row];
  }
}

contract Generated {
    // whatever it does
}

You might also decide that you want to return the address created from createContract. You would be able to find the address in the mined transaction.

Change a few things:

function createContract() public returns(address contractCreated) {
  ...
  return g;
Source Link
Rob Hitchens
  • 55.6k
  • 11
  • 91
  • 146

The easiest way to accomplish this is to create a way for the creating contract to report what it's doing. I'll refer to the contracts as "factory" and "generated", where there is one "factory" with a function that can create unlimited "generated" contracts.

Factory needs a getter function or event emitter; some way of exposing the addresses of the contracts it created.

With event emitter:

pragma solidity ^0.4.2;

contract factory {

  event LogContractCreated(address generated);

  function createContract() public returns(bool success)
  {
    Generated g = new Generated();
    LogContractCreated(g); // Generated g is directly convertable to address
    return true;
  }
}

contract Generated {
    // whatever it does
}

With getter function:

pragma solidity ^0.4.2;

contract factory {

  // keep track of generated addresses
  address[] private generated;

  function createContract() public returns(bool success)
  {
    Generated g = new Generated(); // make a new Generated and not its address
    generated.push(g); // record the address for later
    return true;
  }

  // optionally, report how many rows are in the list
  function getGeneratedCount() public constant returns(uint count) {
    return generated.length;
  }

  // achieve similar result setting generated to "public" near line 6
  function getAddressAtIndex(uint row) public constant returns(address contractAddress) {
    return generated[row];
  }
}

contract Generated {
    // whatever it does
}