0

I want to test three contracts in total: Main.sol, Factory.sol, PCE.sol

Here's the flow I want:

  1. accounts[0] calls factoryStart in Main.sol
  2. Main.sol should call a function in Factory.sol, passing the address of accounts[0]
  3. Factory.sol should create/deploy a new PCE.sol, passing the args

Below is the half-working code. Parts of code are omitted for readability:

Main.sol

import '../factory/Factory.sol';

pragma solidity ^0.7.0;

contract Main:
{

    Factory factory;
    uint256 factoryStartCount;

    constructor(address _factory)
    {
        factory = Factory(_factory);
    }


    function factoryStart(uint256 attr1, uint256 attr2) public payable 
    {  

        AM_ADDRESS = msg.sender;

        factory.newPCE( attr1, attr2, AM_ADDRESS);
        
        factoryStartCount++;

    }

    function getFactoryStartCount() public view returns(uint256)
    {
        return factoryStartCount;
    }

}

Factory.sol:

import '../PCE/PCE.sol';

pragma solidity ^0.7.0;

contract Factory
{
 
  uint256 numPCE;

  function newPCE(uint256 attr1, uint256 attr2, address payable AM_ADDRESS) public payable 
  returns(PCE)
   {
       
    address payable VAULT  = 0x1B2F262A2320939001a0276FB335EAf7b6185e38;
        
    PCE cookie =  new PCE( attr1, attr2, AM_ADDRESS, VAULT) ;
            
    numPCE++;
    return cookie;
   }

    function getNumPCE() public view returns(uint256)
     {
      return numPCE;
     }
}

PCE.sol:

pragma solidity ^0.7.0;

contract PCE{
 
  constructor (uint256 attr1, uint256 attr2, address payable AM_ADDRESS, address payable VAULT)
  {
   // do stuff
  }
  // do other stuff
}

2_deploy_contracts.js :

const PCE_Factory     = artifacts.require("./Factory")
const Main            = artifacts.require("./Main");

module.exports = async function(deployer)
{
  
  await deployer.deploy(PCE_Factory);

  const Factory = await PCE_Factory.deployed();

  await deployer.deploy(Main, Factory.address);

};

test.js:

const Main          = artifacts.require("Main");
const Factory       = artifacts.require("Factory");

contract('Main', async (accounts) =>
{
    before( async() => {

        instance_factory = await Factory.new()
        instance_main    = await Main.new(Factory.address);

    });

   it('PCE can be created upon request: TEST.A', async() => {

        try{

        await instance_main.factoryStart.call( 2 , 5 
            {from: accounts[0] } );
        }

        catch(error){

        console.log(error);
        };

    });


    it('should increment factoryStartCount: TEST.B ', async() => {

        let factoryCount        = await instance_main.getFactoryStartCount.call();
        let int_factoryCount    = new BigNumber(factoryCount)

        console.log('       The factory has been initiated ' +int_factoryCount.toString() + ' times');
        assert.equal(1,int_factoryCount.toString());

    });


    it('should increment numPCE: TEST.C', async() => {

        let currentPCE    = await instance_factory.getNumPCE.call();
        let int_CurrentPCE = new BigNumber(currentPCE);

        console.log('       There are ' + int_CurrentPCE.toString() + 'PCE.' );
        assert.equal(1,int_CurrentPCE.toString());
        
    });
})

The interesting part is TEST.A passes, but TEST.B and TEST.C fail. I don't understand how TEST.A can pass without incrementing factoryStartCount and/or numPCE. Where am I mistaken ?

TEST.B and TEST.C outputs are both zero.

Any and all help is appreciated.

node: v12.18.4
ganache-cli: v6.12.1
Truffle v5.1.49

1

I solved the problem. The mistake was that I was getting the wrong address in testing.js before function.

Wrong version:

before( async() => {

        instance_factory = await Factory.new()
        instance_main    = await Main.new(Factory.address);

    });

Correct version:

before( async() => {

        instance_factory = await Factory.new()
        instance_main    = await Main.new(instance_factory.address);

    });

Also as Ismael pointed out :

Since the code uses .call() when invoking the transaction any change will be discarded.

If you want to persist the modifications call like this:

await instance_main.factoryStart(2 , 5 
    { from: accounts[0] }
);

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.