I built out a fairly complex contract factory and I noticed the factory was only creating instances of itself, not the "child" contract. After observing this I figured I was doing something wrong so I scaled back and tried to implement a super basic version first. However, I'm still observing the same behavior. I wrote this in the Remix IDE and deployed it on the Javascript VM
environment.
Here is my factory:
pragma solidity ^0.4.21;
contract ExampleContract {
string public Name;
function ExampleContract (string name) public {
Name = name;
}
}
contract Factory {
address[] public contracts;
function createContract(string name) public returns(address) {
address newContract = new ExampleContract(name);
contracts.push(newContract);
return newContract;
}
}
Everything compiles and deploys fine. When I use the createContract
function it deploys a new instance of the Factory
contract. Am I missing something?
Factory
, then I calledcreateContract("test")
. Then I calledcontracts(0)
to get the address of the new contract. I choseExampleContract
from the dropdown, pasted the address next to "At Address" and then clicked "At Address." I then had an instance ofExampleContract
, and callingName()
on it correctly returned the string "test" (as I originally supplied tocreateContract
).At Address
feature. Noob mistake. Seems obvious now. If you want to move your response to an actual answer I will mark it as correct. Thanks again!