CLONING SMART CONTRACT: UNDERSTANDING FACTORY PATTERN.

Paul Elisha
4 min readFeb 25, 2024

--

In blockchain development, mining sets of data into a blockchain is quite an expensive process because fees are charged for each new data mined into the next block. Deploying smart contracts on blockchain results in mining the contract’s data into the next block, which will cost some gas fees that are charged in klay if deployed on the Klaytn blockchain.

This article will demonstrate practically how to deploy multiple instances of your smart contract the right way using the factory pattern. Also, we’ll discuss the factory pattern, its benefits, and when to use it.

PREREQUISITES:

. A basic understanding of Solidity.

WHAT ARE FACTORY 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.

Much like a shoe factory produces shoes to a certain standard, factory contracts will ensure that all of the smart contracts that it produces adhere to certain arbitrary qualities. This is a common pattern that you see many, if not all, large dApps using. For example if you are familiar with Uniswap, they implement this pattern. Every time you are interacting with a Uniswap pool, that pool is actually a smart contract that was generated by the Uniswap factory contract.

The factory design pattern is a well-known programming pattern. The concept is simple: instead of directly creating instances of objects, you have a single object (the factory) that creates objects for you. In the case of Solidity, an object is a smart contract and so a factory will deploy new contracts for you.

So sometimes we need to create different types of objects, but we don’t know what kind of object we’ll instantiate until the code is executed at runtime. In such cases, the factory technique comes in handy.

Generally, a basic factory contract should be able to deploy multiple instances of the same contract, store these created instances on the blockchain, and retrieve them when needed. You may want to add more functionality for managing deployed contracts like retrieving a specific instance of the contract, disabling an instance of the contract, and so on.

A factory contract is created for the following reasons:

. You want to keep track of all deployed contracts.

. You want to save gas on deployments.

. You want a simple way for users or yourself to deploy contracts.

We’ll create a simple smart contract that will be used by the factory contract to deploy multiple instances of it:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;

contract Foundation {
string public name;
address public owner;

constructor(
string memory _name,
address _owner
) public {
name = _name;
owner = _owner;
}

}

Because Klaytn is an open source blockchain, the first line shows the contract’s open source license. The second line specifies the Solidity version necessary to execute this contract.

Following that, we establish the Foundation contract, which is analogous to a class in other object-oriented programming languages. The constructor function here initializes the contract’s state variables with the values supplied in as arguments. When we create an instance of the contract, the constructor function is called.

. Writing our first factory contract

The Foundation contract currently has no means of being created. So, we are going to make a factory contract that will create the individual instances of the Foundation contract using the normal factory pattern.

Below is what a normal factory contract should look like:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
import "./Foundation.sol";
contract FoundationFactory {
Foundation[] private _foundations;
function createFoundation(
string memory name
) public {
Foundation foundation = new Foundation(
name,
msg.sender
);
_foundations.push(foundation);
}
function allFoundations(uint256 limit, uint256 offset)
public
view
returns (Foundation[] memory coll)
{
return coll;
}
}

Here, this code imports the Foundation contract, which we want to create multiple instances of. The “_foundations” variable holds the instances of the Foundation contract created.

The “createFoundation” function deploys an instance of the Foundation contract and stores it in the blockchain while the function allFoundations retrieves all instances of the Foundation contract stored in the blockchain.

A drawback of the factory pattern

The gas cost for the CREATE opcode is presently 32,000 Gwei. Each time an instance of the Foundation contract is deployed, a gas fee of 32,000 Gwei is charged.

The major drawback of the factory pattern is high gas costs. And that’s where the cloned factory pattern comes in handy.

To understand the ERC-1167 pattern, click here.

Conclusion:

Congrats on making it to the end! You have definitely leveled up your Solidity skills, and are well on your way to slinging Solidity with the best of them. In this guide you learned about the factory contract method, and how to implement your own.

--

--