0

I want to create a contract that can create contract itself

I find something like this


pragma solidity ^0.5.0;

contract DaughterContract {
 string public name;
 uint public age;
 constructor(
  string memory _daughtersName,
  uint _daughtersAge
 )
  public
 {
  name = _daughtersName;
  age = _daughtersAge;
 }
}


contract MomContract {
 string public name;
 uint public age;
 DaughterContract public daughter;
 constructor(
  string memory _momsName,
  uint _momsAge,
  string memory _daughtersName,
  uint _daughtersAge
 )
  public
 {
  daughter = new DaughterContract(_daughtersName, _daughtersAge);
  name = _momsName;
  age = _momsAge;
 }
}

but it's only working when I hardcode Daughter contract And it only works once

I want to create a mother contract that is able to create multiple daughters by calling function

Something like this

enter image description here

whats should be change

1 Answer 1

1

MomContract deploys a daughter contract only on its constructor, which only runs onces, you should have a function you can call several times for you to deploy the daughter at will.

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.