2

When a contract creates another contract on the Mix blockchain, I see the block with the function call but not the contract that was generated, i.e, it doesn't show in the 'Contract Accounts' section.

Is there any other way to see the created contract?

1 Answer 1

1

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;

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.