I have a hub contract (1) that uses a contract factory (2) to create a contract (3). I am writing some tests in JavaScript to ensure that contract 3 is created correctly. I can tell events are being emitted correctly, but when I want to check the functions of contract 3 (#tryingtobethorough), it looks like Truffle does not recognize the created contract 3 address as a contract (i.e. TypeError: contract3Address.functionName is not a function
). Does Truffle Javascript testing only recognize addresses as contracts if deployed through Truffle migrations? Is there a work around for this in Javascript? Perhaps it is a better test case for Solidity testing?
Below is some simplified versions of what I am working with:
Contract 1: Hub
import "./Contract2Interface.sol";
contract Contract1 {
Contract2Interface I;
function Contract1(address Contract2Address){
I = Contract2Interface(Contract2Address);
}
function creation() returns (address newcontract3) {
address theCreatedContract3 = I.createNewContract3();
return newcontract3;
}
}
Contract 2: Contract Factory
import "./Contract3.sol";
import "./Contract2Interface.sol";
contract Contract2 is Contract2Interface {
event ContractCreation(address _newContract);
function createNewContract3() public returns(address) {
Contract3 createdContract3 = new Contract3();
ContractCreation(createdContract3);
return createdContract3;
}
}
Contract 3: Contract to be Created
contract Contract3{
uint256 public num;
function storeNum(uint256 newNum) public returns(bool) {
num = newNum;
}
}
JavaScript Test This is simplified, but I think it gives enough of an idea.
beforeEach(async function(){
contractFactory = await Contract2.new();
contractHub = await Contract1.new(contractFactory.address);
});
describe("Creating contract 3", async function(){
it("Should allow us to create contract 3", async function(){
let result = await contract1.createNewContract3({from: owner});
//Event data
let log = resultTwo.logs[0];
let contract3Address = log.args._newContract;
console.log(contract3Address) //I can see this is a valid contract address & event has been emitted properly.
let newnum = await contract3Address.storeNum(7);
// At this point I get an error that "storeNum" is not a function of contract3Address from Truffle testing
})
})