Smart Contract Factories: How to create a contract, to create another contract

P.Misirov
Coinmonks
3 min readDec 28, 2021

--

TLDR; the new keyword in solidity allows to create and deploy a new contract instance using a template. ‘SomethingSomethingFactory’ is a naming convention and a pattern used to create ‘SomethingSomething’ contracts.

The intention of this article is to go through the process of making a simple contract factory while understanding how it works and imagine potential use cases. Lets dive right in.

A Contract Factory is a design pattern that creates contracts based on a template. Users can introduce values into a field which are used as input to initialize the state variables of the newly deployed contract. Why is this useful? Depending on your use case you can do cool things like:

For this example I'm going to use a Bank contract (template) and the BankFactory contract (factory) i wrote this morning. The intention is to deploy the Factory once, and create as many Bank contracts as we want without having to deploy each one ourselves.

Simple diagram

1 — First the Bank (template):

The constructor initializes state variables passed to by the BankFactory. Here we pass the amount of funds the Bank has, who is the owner of the Bank and who has created the Bank contract.

2 — Second the BankFactory (factory):

Here we instantiate the Bank contract Bank bank, create a list of type Banks Bank[] public list_of_banks to keep track of the newly created contracts and finally the magic function createBank() which takes the Bank’s constructor parameters as arguments.

This is the line that creates a new contract:

bank = new Bank(_owner, _funds)

The value at index 0 in the list_of_banks array becomes the address of the created contract!

Wonderful. This is all there is. Now when the Factory has been deployed, calling the createBank(..,..) function with the desired values as arguments will result in a new Bank contract holding those values!

Want to see how to do this with HardHat? Sure let’s do it, the pizza in the oven is not ready anyways.

Testing with HardHat

1 — Make a new directory and install hardhat:

2 — Create a new project and install dependencies:

3 — Open folder with your text editor of choice. I like Visual Studio Code:

4 — Create a new Solidity file and copy the code into it:

5 — Compile the contract:

6 — Test it!. Notice the testing script. Copy paste the code into it and lets run it!

Voila! Hope you had as much fun as i did! Gotta go now, the pizza is ready.

Links of interest:

https://docs.uniswap.org/protocol/V2/reference/smart-contracts/factory

https://research.csiro.au/blockchainpatterns/general-patterns/contract-structural-patterns/factory-contract/

https://arxiv.org/abs/1706.03700

https://www.researchgate.net/publication/303996559_Untrusted_Business_Process_Monitoring_and_Execution_Using_Blockchain

https://docs.openzeppelin.com/learn/developing-smart-contracts

--

--

P.Misirov
Coinmonks

UX Designer and Software Developer passionate about Blockchain, InfoSec, psychology and geopolitics