Skip to main content

Overview

The StorageFactory contract demonstrates advanced Solidity concepts including contract composition, the factory pattern, and inheritance. It creates and manages multiple SimpleStorage contract instances, allowing you to interact with each one. Key Features:
  • Deploy new SimpleStorage contracts programmatically
  • Manage multiple contract instances in an array
  • Interact with child contracts from the factory
  • Demonstrates contract-to-contract interactions

Contract Composition

StorageFactory

The main factory contract that creates and manages SimpleStorage instances.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import {SimpleStorage} from "./SimpleStorage.sol";

contract StorageFactory {
    SimpleStorage[] public listOfSimpleStorageContracts;

    function createSimpleStorageContract() public {
        SimpleStorage newSimpleStorageContract = new SimpleStorage();
        listOfSimpleStorageContracts.push(newSimpleStorageContract);
    }

    function sfStore(uint256 _simpleStorageIndex, uint256 _newSimpleStorageNumber) public {
        listOfSimpleStorageContracts[_simpleStorageIndex].store(_newSimpleStorageNumber);
    }

    function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) {
        return listOfSimpleStorageContracts[_simpleStorageIndex].retrieve();
    }
}

State Variables

SimpleStorage[] public listOfSimpleStorageContracts;
An array that stores addresses of all deployed SimpleStorage contracts. The public keyword automatically creates a getter function to access contracts by index.

Core Functions

createSimpleStorageContract()

function createSimpleStorageContract() public {
    SimpleStorage newSimpleStorageContract = new SimpleStorage();
    listOfSimpleStorageContracts.push(newSimpleStorageContract);
}
Deploys a new SimpleStorage contract and adds it to the managed list. What it does:
  1. Uses the new keyword to deploy a fresh SimpleStorage contract
  2. Stores the contract reference in the array
  3. Each contract gets the next available index (0, 1, 2, …)
Deploying contracts costs significant gas. Each call to this function deploys a completely new contract instance on the blockchain.

sfStore()

function sfStore(uint256 _simpleStorageIndex, uint256 _newSimpleStorageNumber) public {
    listOfSimpleStorageContracts[_simpleStorageIndex].store(_newSimpleStorageNumber);
}
Interacts with a specific SimpleStorage contract to store a value. Parameters:
  • _simpleStorageIndex - Index of the target contract in the array
  • _newSimpleStorageNumber - Value to store in that contract
Example: sfStore(0, 42) stores 42 in the first SimpleStorage contract

sfGet()

function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) {
    return listOfSimpleStorageContracts[_simpleStorageIndex].retrieve();
}
Retrieves the stored value from a specific SimpleStorage contract. Parameters:
  • _simpleStorageIndex - Index of the target contract
Returns: The favorite number stored in that contract

Inheritance: AddFiveStorage

The AddFiveStorage contract demonstrates inheritance and function overriding.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import {SimpleStorage} from "./SimpleStorage.sol";

contract AddFiveStorage is SimpleStorage {
    function store(uint256 _favoriteNumber) public override {
        myFavoriteNumber = _favoriteNumber + 5;
    }
}
Key Points:
  • Uses is SimpleStorage to inherit all functionality
  • Overrides the store() function to add custom behavior
  • Automatically adds 5 to any number before storing it
  • Must use the override keyword to override parent functions
The parent contract’s store() function must be marked as virtual for this to work. In production code, ensure parent functions are properly marked.

Usage Examples

StorageFactory factory = new StorageFactory();

// Create three SimpleStorage contracts
factory.createSimpleStorageContract(); // Index 0
factory.createSimpleStorageContract(); // Index 1
factory.createSimpleStorageContract(); // Index 2

Architecture Diagram

Concepts Demonstrated

Factory Pattern

Deploy and manage multiple contract instances from a single factory contract.

Contract Composition

Import and use other contracts within your contract code.

Inheritance

Extend existing contracts and override functions with custom behavior.

Contract Interactions

Call functions on other deployed contracts using their addresses.

Key Takeaways

StorageFactory teaches advanced Solidity patterns:
  • Using the new keyword to deploy contracts programmatically
  • Storing and managing contract references in arrays
  • Calling functions on external contract instances
  • Inheritance with is keyword
  • Function overriding with override keyword
  • The factory pattern for managing multiple similar contracts
Factory Pattern Use Cases:
  • Creating multiple instances of the same contract type
  • Managing upgradeable contract systems
  • Building decentralized exchanges with multiple token pairs
  • Deploying user-specific contract instances

Gas Considerations

Deploying contracts is expensive! Each call to createSimpleStorageContract() deploys a full contract to the blockchain.Gas costs:
  • Creating a new contract: ~150,000+ gas
  • Storing values: ~20,000-50,000 gas
  • Reading values: Free (if called externally)

Build docs developers (and LLMs) love