Factory Contracts

Spawn smart contract instances from a smart contract template.

A factory contract is a smart contract that can produce and deploy other smart contracts from a code template. This can be useful for applications with similar needs that we want to keep separate. For instance, a lending protocol can spawn a separate smart contract for each loan to keep risks contained.

The factory pattern

The factory contract pattern is similar to how classes are used in object-oriented programming. We define a code template (a class), and the factory contract can import it and instantiate it as smart contract instances (objects), each implementing that code. These smart contract instances are spawned on-chain by calling a function in the factory contract, which ensures users that their code doesn't deviate from the template.

Get hands-on with 1400+ tech skills courses.