0

I'm new to Solidity and the whole blockchain environment. Learning it from a course.

I have been assigned a task which is to:

Make a parent contract that has the ability to deploy child contracts.

Can someone please guide or refer me to some resource that can help me? Thanks!

2 Answers 2

0

For create a new instance of contract you must to use the word 'new' and the name of the contract like:

[NameContract] contract = new [NameContract]();

With this statement you could use it to create a new child contract in a function or in parent contract's constructor. See this example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract ParentContract {
    // State variable ParentContract
    ChildContract private child;

    constructor() {
        // Create a new Child contract 
        child = new ChildContract("Test", 30);
    }
}

contract ChildContract {
    // State variables Child contract
    string name;
    uint age;

    constructor(string memory _name, uint _age) {
        name = _name;
        age = _age;
    }
}

If you want more details, see here.

0

Deploy contract from contract in Solidity

Should be lots of answers in here, but just know the basic format, where contract B is a contract:

  function newB()
    public
    returns(address newContract)
  {
    B b = new B();
    return b;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.