0
0
Blockchain / Solidityprogramming~30 mins

Factory pattern in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Factory Pattern in Blockchain Smart Contracts
📖 Scenario: You are working on a blockchain project where you need to create multiple simple contracts that represent digital assets. Instead of deploying each contract manually, you will build a factory contract that can create new asset contracts on demand.
🎯 Goal: Build a factory contract in Solidity that can create new asset contracts. Each asset contract will store a name and an owner address. The factory will keep track of all created asset contracts.
📋 What You'll Learn
Create a simple asset contract with a name and owner state variables.
Create a factory contract that can deploy new asset contracts.
Store the addresses of all created asset contracts in the factory.
Provide a function to get the total number of created asset contracts.
💡 Why This Matters
🌍 Real World
Factory patterns are used in blockchain to efficiently deploy many similar contracts, such as tokens, NFTs, or digital assets, without manual deployment each time.
💼 Career
Understanding factory patterns is important for blockchain developers to write scalable and maintainable smart contracts that manage multiple contract instances.
Progress0 / 4 steps
1
Create the Asset Contract
Write a Solidity contract called Asset with two public state variables: string public name and address public owner. Add a constructor that takes string memory _name and address _owner as parameters and sets the state variables accordingly.
Blockchain / Solidity
Need a hint?

Think of the asset contract as a simple container that stores a name and an owner address. The constructor sets these values when the contract is created.

2
Create the Factory Contract with Storage
Write a Solidity contract called AssetFactory. Inside it, create a public dynamic array of addresses called assets to store the addresses of created asset contracts.
Blockchain / Solidity
Need a hint?

The factory contract will keep track of all asset contracts it creates by storing their addresses in an array.

3
Add Function to Create New Asset Contracts
Inside the AssetFactory contract, add a public function called createAsset that takes a string memory _name parameter. This function should deploy a new Asset contract with _name and msg.sender as the owner. Then, add the new asset's address to the assets array.
Blockchain / Solidity
Need a hint?

Use the new keyword to deploy a new contract inside another contract. Store the new contract's address in the array.

4
Add Function to Get Total Number of Assets and Test
Add a public view function called getAssetsCount in the AssetFactory contract that returns the number of created asset contracts. Then, write a simple test by calling createAsset twice with names "Gold" and "Silver", and print the result of getAssetsCount().
Blockchain / Solidity
Need a hint?

The length of the assets array tells how many asset contracts have been created. Solidity contracts do not print, so you return values to check.