There are two ways a contract can be created in Solidity.

They can be created “from outside” by a creator — a developer who is deploying the contract or a user that is interacting with a dApp which deploys the contract on their behalf. They can also be created by another smart contract.

Today I’m going to focus on #2.

There are a lot of great articles out there about using a factory to create smart contracts. These articles focus on creating multiple instances of the same contract. In my case, I needed a very simple factory. A parent contract that would deploy one instance of a child contract. Just once. We don’t want twins, triplets, quadruplets… that is more then we can handle.

Whether you want to create many instances of the same contract or you only want to create one, you can use the new keyword as described briefly here in the Solidity docs. In my case, I decided to have my parent contract create the child contract in it’s constructor. That way it would all happen at once on deployment.

A family example

Let’s take a look at an example.

In this example, we are going to have our parent contract that we will call MomContract deploy a child contract called DaughterContract.

:)

pragma solidity ^0.5.0; import "./DaughterContract.sol"; 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;

}

}

We are starting with the MomContract and creating the DaughterContract using the new keyword. We pass the parameters needed to create the DaughterContract to the MomContract so we can pass them on. We have a variable daughter of type DaughterContract , which will allow us to call that contract.

Now let’s take a look at the DaughterContract.

pragma solidity ^0.5.0; contract DaughterContract { string public name;

uint public age; constructor(

string memory _daughtersName,

uint _daughtersAge

)

public

{

name = _daughtersName;

age = _daughtersAge;

}

}

The name and age that is passed from the MomContract is used to create the DaughterContract , and we set those arguments to the name and age variable.

Now we know how a contract can create another contract. Great.

Let’s take it a step further.

Creating a contract from another contract is particularly useful when you want to restrict access of the child contract functions to it’s parent contract. To do this, I recommend using Open Zeppelin’s library. They have several ways you can control access, as written in this guide. I’m going to use the well known and popular Ownable.sol file, which is perfect when you only need to provide access to one address. To use the Ownable.sol file, you’ll need to import it and inherit it in your child contract.

pragma solidity ^0.5.0; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; contract DaughterContract is Ownable {

// contract code

}

Let’s add a function that is restricted to the MomContract .

pragma solidity ^0.5.0; import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

import "./MomContract.sol"; contract DaughterContract is Ownable {



string public name;

uint public age;

bool public canDate; constructor(

string memory _daughtersName,

uint _daughtersAge

)

public

{

name = _daughtersName;

age = _daughtersAge;

canDate = false;

}

} function permissionToDate() onlyOwner {

canDate = true;

}

We added a canDate variable and set it false (we don’t want the daughter to date without her mom’s permission). Next, we added a permissionToDate function and added the onlyOwner modifier available to us from the Ownable contract. Now, the only contract that can call this function is the MomContract . To test this, I added a function in the MomContract that calls the permissionToDate function in the DaughterContract .

function allowDaughterToDate() public {

daughter.permissionToDate();

}