Hey I have file 'contractFactory.sol' inside this file I have

contract contractFactory{

    event contractCreated(string name, address institution, address parent);

    function createContract(
        string _name,
        address head,
        address creator,
        address[] _Members)
    public returns(address){

        Contracts  ContractAddress = new Contracts(_name , head , creator , _Members);
        emit contractCreated(_name,address(ContractAddress), creator);
        return address(ContractAddress);

contract Contracts{

    function  dosomthing(){}

Now in front-end web3js, I had create 'Contracts' contract using 'contractsFactory'.Does any one know how I could access dosomting() methods in web3js?


Here is a working example which relies on web3.js v1.0.0-beta.34.

Please note that I had to fix various compilation errors in your contracts, so it is not entirely compatible with your (non-working) code, yet should suffice for you to get started.

On top of that, please note that I have also declared address institution as indexed, in order to make the extraction of this data from the receipt of executing function createContract easier.

Your altered code:

pragma solidity 0.4.25;

contract ContractInstance {
    string public _name;
    address public _head;
    address public _creator;

    constructor(string name, address head, address creator) public {
        _name = name;
        _head = head;
        _creator = creator;

    function doSomething(string name) public {
        _name = name;

contract ContractFactory {
    event ContractCreated(string name, address indexed institution, address parent);

    function createContract(string name, address head, address creator) public returns (address) {
        ContractInstance contractInstance = new ContractInstance(name, head, creator);
        emit ContractCreated(name, address(contractInstance), creator);
        return address(contractInstance);

Web3.js script:

const fs = require("fs");
const Web3 = require("web3");
const web3 = new Web3("http://localhost:8545");

const YOUR_PRIVATE_KEY = "0x0000000000000000000000000000000000000000000000000000000000000001";
const YOUR_ARTIFACTS_PATH = "C:/Your/Artifacts/Path/";

const ARG2 = "0x1234567812345678123456781234567812345678";
const ARG3 = "0x8765432187654321876543218765432187654321";

async function deploy(contractName, contractArgs) {
    const abi = fs.readFileSync(YOUR_ARTIFACTS_PATH + contractName + ".abi");
    const bin = fs.readFileSync(YOUR_ARTIFACTS_PATH + contractName + ".bin");
    const contract = new web3.eth.Contract(JSON.parse(abi));
    const options = {data: "0x" + bin, arguments: contractArgs};
    const transaction = contract.deploy(options);
    const handle = await send(transaction);
    const args = transaction.encodeABI().slice(options.data.length);
    return new web3.eth.Contract(JSON.parse(abi), handle.contractAddress);

function deployed(contractName, contractAddr) {
    const abi = fs.readFileSync(YOUR_ARTIFACTS_PATH + contractName + ".abi");
    return new web3.eth.Contract(JSON.parse(abi), contractAddr);

async function send(transaction) {
    const options = {
        to   : transaction._parent._address,
        data : transaction.encodeABI(),
        gas  : (await web3.eth.getBlock("latest")).gasLimit
    const signedTransaction  = await web3.eth.accounts.signTransaction(options, YOUR_PRIVATE_KEY);
    const transactionReceipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
    return transactionReceipt;

async function run() {
    const contractFactory  = await deploy("ContractFactory", []);
    const receipt1         = await send(contractFactory.methods.createContract("MyName", ARG2, ARG3));
    const contractAddress  = "0x" + receipt1.logs[0].topics[1].slice(-40);
    const contractInstance = deployed("ContractInstance", contractAddress);
    const oldName          = await contractInstance.methods._name().call();
    const receipt2         = await send(contractInstance.methods.doSomething("YourName"));
    const newName          = await contractInstance.methods._name().call();
    console.log("oldName:", oldName);
    console.log("newName:", newName);

|improve this answer|||||
  • @yehiatarek: You're welcome. – goodvibration May 2 '19 at 14:54

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.