I'm writing a contract on Remix that is able to deploy and track child contracts.
Here are the 2 contracts:
pragma solidity ^0.5.0;
Contract Factory
contract ContractFactory{
ChildContract[] public createdContracts;
ChildContract public activeChild;
function createChild() public{
ChildContract newChild = new ChildContract();
createdContracts.push(newChild);
// if (address(activeChild) != address(0)){
activeChild.activate(false);
// }
activeChild = newChild;
}
}
Child Contract
contract ChildContract{
bool isActive;
constructor() public{
isActive = true;
}
function activate(bool status) public{
isActive = status;
}
}
I deployed the ContractFactory and it was successful. Then I tried calling the createChild
function. Then I got the following error:
transact to ContractFactory.createChild errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value. Debug the transaction to get more information.
As evident from the code, if I un-comment the commented out if condition
inside the createChild
function, the function will run without issues. My question is - Why is solidity throwing this misleading error, as the issue was with an unset variable?
if (address(activeChild) != address(0))
statement, hence the attempt to callactiveChild.activate
reverts the transaction. The note... well, it is just a note to keep you aware of the fact noted... – goodvibration Mar 19 at 10:16