0

I have contract A and contract B with the following structures:

contract A {
  int x;
  function setX(){
       x = _x;
     }
}

contract B {
   A[] contractInstances;
   
   function createContractInstance() {
       contractInstances.push(new A());
      }

}

How can I access the method setX() of the created instance, when I am doing the testing with truffle? P.S. when I try to access them directly from the array, it is not working, cause the js platform sees the contract as string addresses.

1

You have to inspect contract B to discover the instances.

By extension, you need something to inspect. Consider:

contract B {
   address[] public contractInstances;
   
   function createContractInstance() public returns(A newA) {
       A newA = new A();
       contractInstances.push(address(newA));
   }
}

Once you have that, then

const A = artifacts.require(A);
const B = artifacts.require(B); // separate .sol file

let aInstance = await b.contractInstances(0); // first one
let a = await A.at(aInstance);

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.