0

I am trying to create a factory contract and storing the smart contract address in an array but when I trying to retrieve the value, it always gives null value. Please find the contracts that I have used below.

pragma solidity ^0.4.7;

contract ContractTest {
    bytes32 public Name;

    function ContractTest (bytes32 name) {
        Name = name;
    }
}


pragma solidity ^0.4.7;
import './ContractTest.sol';

contract FactoryTest{
    address[2] newContracts;

    function createContract (bytes32 name) {
        address newContract = new ContractTest(name);
        newContracts[0]=newContract;
    }
        function getContract() returns(address) {
          return newContracts[0];
        }
}
1

With a bit of refactoring and reformatting, I ended up with this code:

pragma solidity ^0.4.7;

contract ContractTest { 
    string public Name;

    function ContractTest (string name) {
        Name = name;
    }
}

contract FactoryTest { 
    address[2] newContracts;

    function createContract (string name) {
        address newContract = new ContractTest(name);
        newContracts[0]=newContract;
    }

    function getContract() returns(address) {
      return newContracts[0];
    }
}

The only real change is the use of string instead of bytes. I was testing it in Remix and I couldn't get it to accept input as bytes so I changed the input type.

The code above works just fine. So either the problem is somehow with the bytes or then you are testing it wrong.

  • Hi Lauri, It is working in remix but not working in geth .Could you please try this in ethereum private network. I was trying this in local ethereum network with geth. – Basilji Aug 14 '18 at 12:52
  • I don't have private network (Ganache) available right here now and unfortunately I will probably forget to test this the next time I have it available. – Lauri Peltonen Aug 14 '18 at 12:58
  • Anyway, it shouldn't make a difference. The result should be the same. – Lauri Peltonen Aug 14 '18 at 12:58
  • Unfortunately for me the result is coming as null value – Basilji Aug 14 '18 at 16:01
0

How are you calling getContract? From solidity or javascript.

The function getContract is not declared as view, for example if you call it from js it will execute it as a transaction and will not return the value you are expecting.

contract FactoryTest { 
    address[2] newContracts;

    function createContract(string name) public {
        address newContract = new ContractTest(name);
        newContracts[0] = newContract;
    }

    function getContract() public view returns (address) {
      return newContracts[0];
    }
}
  • Hi Ismael I have tried the same and I was getting address via remix (connected via web3 provider to local geth. But when I tried the same via geth its not returning the address. contract = eth.contract(JSON.parse(abiData)) contractObj = contract.new({from: eth.accounts[0], data: binData, gas: 2000000 }) ContractInst = contract.at(“Contract Address”) ContractInst.createContract.sendTransaction("test",{from: eth.accounts[0], gas: 100000}) ContractInst.getContract.call() returns 0x000000000000000 – Basilji Aug 20 '18 at 9:29
  • 1
    issue got resolved . the amount of gas was given was not sufficient – Basilji Aug 20 '18 at 11:11

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.