Concept Flow - Factory pattern
Start
Call Factory
Factory creates new contract
Return new contract address
Use new contract
End
The factory pattern creates new contracts on demand and returns their addresses for use.
Jump into concepts and practice - no test required
contract SimpleContract {
uint public value;
constructor(uint _value) { value = _value; }
}
contract Factory {
SimpleContract[] public contracts;
function create(uint _value) public {
SimpleContract c = new SimpleContract(_value);
contracts.push(c);
}
}| Step | Action | Input | New Contract Created? | Contracts Array Length | Output |
|---|---|---|---|---|---|
| 1 | Call Factory.create | value=10 | Yes | 1 | Address of new SimpleContract #1 |
| 2 | Call Factory.create | value=20 | Yes | 2 | Address of new SimpleContract #2 |
| 3 | Call Factory.create | value=30 | Yes | 3 | Address of new SimpleContract #3 |
| 4 | Read contracts array length | - | - | 3 | 3 |
| 5 | Access SimpleContract #2 value | - | - | - | 20 |
| 6 | Stop | - | - | - | No more actions |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| contracts.length | 0 | 1 | 2 | 3 | 3 |
| SimpleContract #1.value | - | 10 | 10 | 10 | 10 |
| SimpleContract #2.value | - | - | 20 | 20 | 20 |
| SimpleContract #3.value | - | - | - | 30 | 30 |
Factory Pattern in Blockchain: - Factory contract creates new contracts using 'new' - Stores addresses of created contracts in an array - Allows tracking and interacting with multiple contracts - Useful for deploying many similar contracts easily - Access contracts by their stored addresses
What is the main purpose of the Factory pattern in blockchain development?
Which of the following is the correct Solidity syntax to deploy a new contract inside a factory contract?
function create() public returns (address) {
address newContract = new ?();
return newContract;
}new ContractName().new ?() expects the contract name without 'new' repeated.Consider this Solidity factory contract snippet:
contract Simple {
uint public value;
constructor(uint _value) {
value = _value;
}
}
contract Factory {
Simple[] public simples;
function createSimple(uint _val) public {
Simple s = new Simple(_val);
simples.push(s);
}
function getValue(uint index) public view returns (uint) {
return simples[index].value();
}
}What will getValue(0) return after calling createSimple(42) once?
createSimple(42) creates a new Simple contract with value = 42 and stores it in simples array.getValue(0) returnsvalue of the first Simple contract, which is 42.Identify the error in this factory contract code snippet:
contract Product {
uint public id;
constructor(uint _id) {
id = _id;
}
}
contract ProductFactory {
Product[] public products;
function createProduct(uint _id) public {
Product p = Product(_id);
products.push(p);
}
}new keyword.Product p = Product(_id); misses new, it should be Product p = new Product(_id);.new keyword when creating Product -> Option BYou want to build a factory contract that creates multiple token contracts with different initial supplies and keeps track of them. Which approach best applies the factory pattern to save gas and organize your project?