0

I have three contracts Ignition.sol , Factory.sol, Cookie.sol , Ignition should call the function 'factoryStart()' in the Factory contract. newCookie() creates new Cookie.sol contracts depending on the passed arguments.

Parts of code has been omitted for readability.

Ignition.sol:

pragma solidity ^0.7.0;

contract Ignition {
 
 uint256 attr1;
 uint256 attr2;

 constructor(uint256 _attr1, uint256 _attr2){
   attr1 = _attr1;
   attr2 = _attr2;
 }

 // do stuff...

    function factoryStart() public {

     Factory.newCookie( attr1, attr2 );
    }

}

Factory.sol :

pragma solidity ^0.7.0;

contract Factory {

 uint256 attr1;
 uint256 attr2;

 constructor(uint256 _attr1, uint256 _attr2){
 attr1 = _attr1;
 attr2 = _attr2;
 }

 // do stuff...

 function newCookie() public {

 Cookie newCookie = new Cookie(attr1, attr2);
 }

}

Right now, I get a compilation error;
TypeError: Cannot call function via contract type name.

I know my solution must be wrong somehow because I need the ABI/Address of Factory in Ignition so that interaction can happen but I couldn't figure out how.

Any help is appreciated.

1
  • Let's start with a basic question - what do you expect (or want) Factory.newCookie( attr1, attr2 ) to do? – goodvibration Nov 21 '20 at 17:40
0

I know my solution must be wrong somehow because I need the ABI/Address of Factory in Ignition so that interaction can happen but I couldn't figure out how.

No, your solution is wrong because you need to call a contract function via an instance of the contract, not via the name of the contract.

A given contract can have several instances of it deployed at several different addresses, so calling a contract function via the name of the contract makes no sense.

Technically speaking, you need to do something like this:

function factoryStart(Factory factory) public {
    factory.newCookie(attr1, attr2);
}

Alternatively, Factory factory can be a global variable in your contract, which you will need to initialize to point to a Factory instance before calling function factoryStart, for example:

Factory factory;
uint256 attr1;
uint256 attr2;

constructor(Factory _factory, uint256 _attr1, uint256 _attr2) public {
    factory = _factory;
    attr1 = _attr1;
    attr2 = _attr2;
 }
1
  • thank you @goodvibrations , I solved by declaring the contract instance a global variable. – newbnoob Nov 21 '20 at 22:32

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.