Factory Contract

Summary

Use an on-chain contract as a factory to spawn contract instances from a smart contract template

Context

Applications based on a blockchain need to use multiple smart contracts with similar functionality but different data. For example, in a business process management system, each business process instance might be represented by a smart contract implementing the business process model.

Problem

The smart contracts following the same functionality can be implemented as a template. However, when the off-chain template is used to deploy a new contract, one cannot guarantee that different smart contract instances were created from the same template as the source code of the template can be modified off-chain without being detected. How can we ensure all smart contract instances follow the same template?

Forces

  • Dependency management – Storing the source code of smart contract off-chain in a code repository introduces the issue of integrating more systems into the blockchain-based application.
  • Secure code sharing – Blockchain can be used as a secure platform to share smart contract code. As opposed to a traditional code repository, changes of code deployed on a smart contract can be strictly limited or prohibited.
  • Deployment – If a public code repository, like Github, is used to store the source code of a smart contract, a component is needed to implement the function of deploying a smart contract onto the blockchain. Otherwise, the end-users need to understand how to deploy smart contracts by sending transactions with the customised source code of the contract definition.

Solution

The factory pattern from object-oriented programming could be adopted to solve this problem. First, create the template smart contract. Second, deploy another smart contract that could spawn a new smart contract instance from the template contract embedded within its source code. Such a smart contract is called a factory contract. Finally, whenever a new contract is to be spawned, issue a transaction to the factory contract with relevant initialisation parameters to create and initialise a new smart contract instance from the template.

Factory contract pattern

The factory contract is deployed once from the off-chain source code. The factory may contain the definition of multiple smart contracts. Factory contract is analogous to a class in an object-oriented programming. Every transaction that generates a smart contract instance essentially instantiates an object of the factory contract class. This contract instance (the object) will maintain its own properties independently of the other instances but with a structure consistent with its original template.

Benefits

  • Security – Keeping the template contract on-chain guarantees the consistency of the contract definition.
  • Efficiency – If the contract definition is kept on-chain in a factory contract, smart contract instances are generated by calling a function defined in the factory contract. No off-chain handling is required to deploy a new contract instance.

Drawbacks

  • Cost – If a public blockchain is used, using a factory contract requires an extra cost to deploy the factory contract and invoke its contract spawning function.

Related Patterns

  • The contract registry pattern can be used to store the addresses of all the smart contract instances generated from a factory contract.
  • The factory and contract registry can be implemented in the same contract, although that limits upgradability.
  • The token template pattern uses the factory contract to spawn a new instance of the token contract.

Known uses