44

Based on documentation it should be possible to create new contract from contract

contractAddress = new OwnedToken(name);

When I invoke this method from contract, the address I receive seems to be normal address and not contract.

Is this even possible (I remember I read somewhere that only normal accounts can send contract creating transactions)?

2 Answers 2

48

Is this even possible (I remember I read somewhere that only normal accounts can send contract creating transactions)?

This is definitely possible.

When I invoke this method from contract, the address I receive seems to be normal address and not contract.

I'm not sure which address you are talking about. This kind of design works well:

contract Factory {
    address[] newContracts;

    function createContract (bytes32 name) {
        address newContract = new Contract(name);
        newContracts.push(newContract);
    } 
}

contract Contract {
    bytes32 public Name;

    function Contract (bytes32 name) {
        Name = name;
    }
}

In the array newContracts, you will have the addresses of the Contracts created by the factory.

Then if you want to call the Name from the first Contract created in the Factory contract, you should use :

con.Name();

after having defined

Contract con = Contract(newContracts[0]);
6
  • Thanks for answer. I tried and it works flawlessly. I had bug somewhere else in my contract. It seems etherscan.io does detect this kind of contract address correctly (if this is really a think). Is there any difference between contract created by contract or if it is created manually?
    – Ales
    Commented Feb 16, 2016 at 18:53
  • 1
    @Ales No, difference between a contract that's created manually or by another contract. Some explorers don't show as much internals and you might see more at live.ether.camp
    – eth
    Commented Feb 17, 2016 at 1:30
  • 2
    @Hcharlanes, very useful example! Thanks! What's going to be the value of msg.sender in the Contract constructor? The address of the account that sent the transaction or the address where contract Factory is deployed?
    – manidos
    Commented Jul 28, 2016 at 3:52
  • Won't this just return a Contract named after the address?
    – Rob
    Commented Sep 13, 2017 at 4:18
  • Do you need to do new Contract() instaed of Contract()?
    – user22075
    Commented Mar 4, 2018 at 15:54
7

I don't have enough rep to comment on Hcharlanes's answer but since I wasted an hour on it and lots of other people are using his sample, the line

Names[i] = con.Name();

is a bug. You can't assign off the end of a dynamic array. This has to be

Names.push(con.Name());

to not crash, but of course that code doesn't make any sense for any real purpose. You'd want to push the name onto that array immediately when creating the contract. Otherwise the sample is great! Hope this helps!

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.