A Foundry-based implementation of upgradeable smart contracts using the UUPS (Universal Upgradeable Proxy Standard) proxy pattern. This project demonstrates how to deploy a V1 contract and then upgrade it to a V2 version while preserving the proxy's address and storage. This repository was completed as a part of the Advanced Foundry course (offered by Cyfrin Updraft) in the Upgradeable Smart Contracts section.
The protocol consists of two main implementation contracts (BoxV1, BoxV2) and two deployment scripts that work in concert with OpenZeppelin's ERC1967Proxy.
BoxV1.sol: The initial version of the contract, which inherits fromInitializable,UUPSUpgradeable, andOwnableUpgradeable. It contains aversion()function that returns1.BoxV2.sol: The upgraded version of the contract, which inherits fromUUPSUpgradeable. This contract adds asetNumberfunction and itsversion()function returns2.DeployBox.s.sol: A Foundry script that deploys the initialBoxV1contract implementation and anERC1967Proxythat points to it.UpgradeBox.s.sol: A Foundry script that handles the logic for upgrading theERC1967Proxyto point to the newBoxV2implementation.
-
Clone the repository (including submodules):
git clone --recurse-submodules https://github.com/tohidul3417/foundry-upgrades.git cd foundry-upgrades -
Build the project:
forge build
The project includes a comprehensive test suite to verify the deployment and upgrade logic.
- Run all tests:
forge test - Run the tests with verbose output:
forge test -vvv
The DeployAndUpgradeTest.t.sol test file includes a testUpgrades function that asserts the successful upgrade of the proxy to BoxV2 and checks that the new functions are available and the state is preserved.
The script/ directory contains Foundry scripts for deploying and upgrading the contracts.
DeployBox.s.sol: Deploys the initial proxy and implementation contracts.UpgradeBox.s.sol: Upgrades the deployed proxy to a new implementation.
This project was built for educational purposes and has not been audited. Do not use it in a production environment or with real funds. Always conduct a full, professional security audit before deploying any smart contracts.
This project is distributed under the MIT License. See LICENSE for more information.