This project demonstrates how to create, deploy, and upgrade smart contracts using the UUPS (Universal Upgradeable Proxy Standard) pattern with OpenZeppelin's upgradeable contracts library and Foundry.
The project consists of:
- BoxV1: Initial implementation with ability to store and retrieve a number
- BoxV2: Upgraded implementation that adds functionality to modify the stored number
- Deployment Scripts: Foundry scripts for deployment and upgrade operations
- Tests: To verify the functionality of both versions and the upgrade process
- UUPS proxy pattern for upgradeability
- OpenZeppelin upgradeable contracts
- Foundry for development, testing, and deployment
- Comprehensive test coverage
- Access control with Ownable pattern
- Foundry
- Basic understanding of Solidity and proxy patterns
-
Clone the repository:
git clone https://github.com/yourusername/upgradeable-smart-contract-boxv1.git cd upgradeable-smart-contract-boxv1
-
Install dependencies:
forge install
forge build
forge test
Deploy the initial implementation and proxy:
forge script script/DeployBox.s.sol:DeployBox --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast
Upgrade the implementation to BoxV2:
forge script script/UpgradeBox.s.sol:UpgradeBox --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast
- Adds setter functionality
- Maintains compatibility with BoxV1 storage layout
- Improves upgrade authorization with onlyOwner
├── src/ # Smart contracts
│ ├── BoxV1.sol # Initial implementation
│ └── BoxV2.sol # Upgraded implementation
├── script/ # Deployment scripts
│ ├── DeployBox.s.sol # Initial deployment script
│ └── UpgradeBox.s.sol # Upgrade script
├── test/ # Test files
│ └── DeployAndUpgradeTest.t.sol
└── foundry.toml # Foundry configuration
- The upgrade mechanism is protected by the
onlyOwner
modifier in BoxV2 - Storage layout is carefully preserved between versions
- BoxV1 uses
_disableInitializers()
in the constructor to prevent initialization attacks
This project is licensed under the MIT License - see the LICENSE file for details.