1

I'm trying to resolve a problem and google is not useful this time.

On my localhost truffle + testrpc I have a contract which returns a new instance of a new contract.

import 'contracts/MyBasicContract.sol';

contract Factory {

 function createContract() returns (address created){

  return new MyBasicContract();

 }

}

What I'm trying to do is to generate a new contract with a new address on which I'll be able to call functions from the front-end. After deploying Factory I called function createContract which returns me address but still the same address again and again.

What am I doing wrong? Or how looks the best practise for generating new contracts with new address from frontend?

Thank you all for some hints.

1 Answers1

0
import 'contracts/MyBasicContract.sol';

contract Factory {

 event LogNewContract(address sender, address newContract);

 function createContract() public returns (address created){  
   MyBasicContract c = new MyBasicContract();
   LogNewContract(msg.sender, c);
   return c;    
 }
}

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • Seems to be working but not as I expected. When I'm testing it in truffle console, to receive a new address I must as first execute this:

    Factory.deployed().then((e)=>{return e.createContract()})

    and then this give me a new address: Factory.deployed().then((e)=>{return e.createContract.call()})

    and I'm comfused here why is it doing it and how I am supposed to do it at front end. My front-end look like this: FactoryContract.methods.createContract().call().then((result) => { console.log('result ', result) //show address });

    – Ondrej Sarnecký Oct 18 '17 at 19:13
  • 1
    When you use .call() you are insisting that it should be a "dry-run" on not actually do anything, so that's incorrect if you want it to do something. IMO, this is too simplistic to support a front-end, although you could get somewhere with an event listener. For a more complete pattern with possibility of enumerating the deployed contracts, have a look at this: https://ethereum.stackexchange.com/questions/13415/is-there-a-simple-contract-factory-pattern – Rob Hitchens Oct 18 '17 at 19:26
  • This link was like superuseful for me. I was looking for something exactly like this. Thank you! – Ondrej Sarnecký Oct 19 '17 at 14:31