Creating a new Contract from another Contract in Solidity

Rahul Ravindran

Introduction 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 Ether if deployed on the Ethereum 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.

Prerequisite To follow along with this article, you should have prior knowledge of smart contract development with Solidity.

The following will be covered in this article:

The factory pattern Benefits of using the factory pattern in Solidity When to use the factory pattern Types of factory patterns in Solidity Our first Solidity smart contract Writing our first factory contract A drawback of the normal factory pattern The cloned factory pattern Why the clone factory pattern? Using the clone factory pattern Comparison between the normal factory and clone factory patterns Conclusion The factory pattern 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 does it for you.

This is the same with Solidity because smart contracts are objects. In Solidity, a factory is a contract that will deploy multiple instances of other contracts.

We sometimes 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.

Benefits of using the factory pattern in Solidity The following are benefits of using the factory pattern in Solidity:

Deployments of multiple contracts with high gas-efficiency Keep track of all deployed contracts Save gas on multiple contract deployments When to use the factory pattern The factory pattern is effective in the following situations:

When we need to quickly produce multiple instances of a smart contract at runtime When we’re dealing with a large number of contracts that all have the same functionalities Types of factory patterns in Solidity We’ll discuss two types of factory patterns commonly implemented in Solidity smart contracts. These patterns include:

The normal factory pattern — this factory pattern deploys multiple instances of other contracts without any optimization to save gas on each deployment The cloned factory pattern — this factory pattern deploys multiple instances of other contracts with emphasis on optimization to save gas on each deployment Our first Solidity smart contract 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.4.23 <0.9.0;

contract Foundation { string public name; address public _owner;

constructor(
    string memory _name,
    address _owner
) public {
    name = _name;
    _owner = msg.sender;
}

} Because Ethereum is an open source project, 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.4.23 <0.9.0;
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;
    }
}

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.

0 Comments

Leave a Reply

More great articles

Your First Smart Contract In Solana

Overview In this article you’ll learn about some of the very high level topics related to Solana development such as:…

Read Story

Web 2 Vs Web 3

There’s a lot of talk about web 3.0 and the big changes it’ll bring to the industry, but few people…

Read Story

Loopring Explained

Blockchain technology may be decentralizing the world, but there is one element of the distributed ledger technology ecosystem that is…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up