This project demonstrates the implementation of upgradable smart contracts using the Foundry framework. The project focuses on deploying an initial version of a contract (BoxV1) and upgrading it to a new version (BoxV2) while maintaining state continuity. This approach ensures that contract functionalities can evolve without disrupting existing deployments or requiring redeployment.
- Upgradable Smart Contracts: Implements proxy-based upgradability to allow seamless upgrades of contract logic.
- Versioned Contracts: Two versions of the
Boxcontract, each introducing specific features and functionalities. - Deployment and Upgrade Automation: Scripts for deploying the initial version and upgrading to the new version of the contract.
- State Preservation: Ensures that data stored in the contract remains intact during upgrades.
The initial version of the Box contract. Key functionalities include:
store(uint256 newValue): Stores a new value in the contract.retrieve(): Retrieves the stored value, allowing users to access the current state.
The upgraded version of the Box contract, extending BoxV1 with additional functionality:
increment(): Increments the stored value by 1, adding more utility to the contract while retaining the existing state and methods.
A deployment script that automates the process of:
- Deploying the proxy contract.
- Deploying the initial implementation (
BoxV1). - Setting up the proxy to delegate calls to
BoxV1.
This script ensures the initial deployment is ready for use and upgradable.
A script that automates the upgrade process. It performs the following:
- Deploys the upgraded implementation (
BoxV2). - Updates the proxy to point to the new implementation.
- Ensures the state from
BoxV1is preserved and accessible inBoxV2.
$ forge build$ forge test$ forge fmt$ forge script script/DeployBox.s.sol --rpc-url <RPC_URL> --private-key <PRIVATE_KEY> --broadcast
$ forge script script/UpgradeBox.s.sol --rpc-url <RPC_URL> --private-key <PRIVATE_KEY> --broadcastThis project showcases the benefits of contract upgradability, enabling flexibility and scalability while preserving data integrity. For additional insights into implementing and managing upgradable contracts, consider exploring proxy patterns like Transparent Proxy and UUPS Proxy.