Overview
TheStorageFactory 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.State Variables
public keyword automatically creates a getter function to access contracts by index.
Core Functions
createSimpleStorageContract()
- Uses the
newkeyword to deploy a fresh SimpleStorage contract - Stores the contract reference in the array
- 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()
_simpleStorageIndex- Index of the target contract in the array_newSimpleStorageNumber- Value to store in that contract
sfStore(0, 42) stores 42 in the first SimpleStorage contract
sfGet()
_simpleStorageIndex- Index of the target contract
Inheritance: AddFiveStorage
TheAddFiveStorage contract demonstrates inheritance and function overriding.
- Uses
is SimpleStorageto inherit all functionality - Overrides the
store()function to add custom behavior - Automatically adds 5 to any number before storing it
- Must use the
overridekeyword to override parent functions
Usage Examples
- Deploy Contracts
- Store Values
- Retrieve Values
- Direct Access
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
newkeyword to deploy contracts programmatically - Storing and managing contract references in arrays
- Calling functions on external contract instances
- Inheritance with
iskeyword - Function overriding with
overridekeyword - 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