0

Please let me how do I actually instantiate a new contract from factory on geth console. I am not able to instantiate from the geth console. The Factory Contract's createFactory shown below worked perfectly fine when I try calling it from Remix browser online. It also increments the count by 1 each time it gets called. My Miner is up and running when I do all this stuff.

Below is the source code .I compile the source using Remix online browser solidity compiler

pragma solidity ^0.4.6;

contract Factory {

    bytes32[] Names;
    address[] newContracts;
    uint public count=0;

    function createContract (bytes32 name) {
        address newContract = new Contract(name);
        count=count+1;
        newContracts.push(newContract);
    } 

    function getName  (uint i) constant returns (address sblcAddress){
        Contract con = Contract(newContracts[i]);
      sblcAddress= address(con);
       return sblcAddress;
    }
}

contract Contract {

    bytes32 public Name;

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

After opening the geth console and unlocking my base account I deploy the below code.

var browser_contractfactory_sol_factoryContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"createContract","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"i","type":"uint256"}],"name":"getName","outputs":[{"name":"sblcAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"}]);
var browser_contractfactory_sol_factory = browser_contractfactory_sol_factoryContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x60606040526000600255341561001457600080fd5b5b610376806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd146100545780633f811b801461007d5780636b8ff574146100a4575b600080fd5b341561005f57600080fd5b610067610107565b6040518082815260200191505060405180910390f35b341561008857600080fd5b6100a260048080356000191690602001909190505061010d565b005b34156100af57600080fd5b6100c560048080359060200190919050506101b7565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60025481565b600081610118610205565b808260001916600019168152602001915050604051809103906000f080151561014057600080fd5b9050600160025401600281905550600180548060010182816101629190610214565b916000526020600020900160005b83909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505b5050565b6000806001838154811015156101c957fe5b906000526020600020900160005b9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508091508191505b50919050565b60405160e58061026683390190565b81548183558181151161023b5781836000526020600020918201910161023a9190610240565b5b505050565b61026291905b8082111561025e576000816000905550600101610246565b5090565b9056006060604052341561000f57600080fd5b6040516020806100e5833981016040528080519060200190919050505b80600081600019169055505b505b609d806100486000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638052474d14603d575b600080fd5b3415604757600080fd5b604d606b565b60405180826000191660001916815260200191505060405180910390f35b600054815600a165627a7a7230582072ea4eb8d6bf397a5e1d641def036a3a3776b2de002fb3d8a696958a36fdb86f0029a165627a7a72305820f96383d7dc97bbb8dcb737fe3cc485d872fa45409d75db689ccda27f6d1448f90029', 
     gas: '1087718887660'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 });

The above code creates a new factory contract at a new address .I get the address where my Factory contract is created. Using that address I try to access the createContract function of Factory contract

var factoryCInstance = browser_contractfactory_sol_factoryContract.at('0x...FactoryContractAddress..');


factoryCInstance.createContract(3,{from: web3.eth.accounts[0]});

After running the above line all I get is the transaction hash. When I call factoryCInstance.count(); The count displayed is 0

  • @Rob Hitchens, Your help would be appreciated. My miner is up. I wait for a couple of minutes before calling the count() function. Everything works fine when I do it via remix but the createContract function does not work when I do it on geth console. – T_A Aug 28 '17 at 7:20
  • Can you check the receipt of the transaction that calls createContract? If the transaction failed it will not update the state variables. – Ismael Aug 28 '17 at 7:32
  • This is the transaction receipt when I call createContract { blockHash: "0xe71764eb35304f6a35b58b7b7dee8d94610e929a05ab2bcdc4371c0b2509640a", blockNumber: 3236, contractAddress: null, cumulativeGasUsed: 90000, from: "0x.. Eth Base Account address", gasUsed: 90000, logs: [], logsBloom: "0x000...000", root: "0xf661019257bb8522ad073c82ea244c43f6845e86f2b253388d28f714884cc7c8", to: "0xAccountAddress of Factory Contract", transactionHash: "0x5f64ea44e7e3b7658d8c99a08d5e06b5deb31fa595930aa8f4c6864d45 ce7825", transactionIndex: 0 } – T_A Aug 28 '17 at 8:30
  • @Ismael The blockhash and numbers are shown in the transaction receipt. Also on doing eth.pendingTransactions I get [] nothing. – T_A Aug 28 '17 at 9:23
2

Calling the createContract() with the gas value specified solved my issue

factoryCInstance.createContract(2,{from:eth.accounts[0],gas: 470000});

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.