1/5
_Follow along the course with this video._ --- ### UUPS Upgrade Let's keep our momentum from the last lesson and jump right into writing our UpgradeBox.s.sol script. The boilerplate for this will be as expected. ```js // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; import {Script} from "forge-std/Script.sol"; contract UpgradeBox is Script { function run() external returns (address){} } ``` Alright, in order to reference our deployment of ERC1967Proxy, we're going to use our DevOps Tools library again to assist. Begin by installing it. ```bash forge install Cyfrin/foundry-devops --no-commit ``` We can then import this and leverage the get_most_recent_deployment functionality to acquire our ERC1967Proxy address. ```js // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; import {Script} from "forge-std/Script.sol"; import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol"; contract UpgradeBox is Script { function run() external returns (address){ address mostRecentDeployment = DevOpsTools.get_most_recent_deployment("ERC1967Proxy", block.chainid); } } ``` Next, we'll need to import and deploy BoxV2! ```js ... import {BoxV2} from "../src/BoxV2.sol"; contract UpgradeBox is Script{ function run() external returns (address){ address mostRecentDeployment = DevOpsTools.get_most_recent_deployment("ERC1967Proxy", block.chainid); vm.startBroadcast(); BoxV2 newBox = new BoxV2(); vm.stopBroadcast(); } } ``` In order to modularize things a bit for the tests we'll write in the next lesson, we'll write an `upgradeBox` function in which our proxy is called. ```js function upgradeBox(address proxyAddress, address newBox) public returns (address) { vm.startBroadcast(); BoxV1 proxy = BoxV1(proxyAddress); proxy.upgradeTo(address(newBox)); vm.stopBroadcast(); return address(proxy); } ``` > ❗ **NOTE** > We're unable to call a function on an address provided as a parameter here, but by wrapping the address in BoxV1 (which needs to be imported), we provide our function the ABI necessary to reference the upgradeTo function within the proxy address. Now, we can add upgradeBox to our scripts run function. ```js // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; import {Script} from "forge-std/Script.sol"; import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol"; import {BoxV1} from "../src/BoxV1.sol"; import {BoxV2} from "../src/BoxV2.sol"; contract UpgradeBox is Script { function run() external returns (address) { address mostRecentDeployment = DevOpsTools.get_most_recent_deployment("ERC1967Proxy", block.chainid); vm.startBroadcast(); BoxV2 newBox = new BoxV2(); vm.stopBroadcast(); address proxy = upgradeBox(mostRecentDeployment, address(newBox)); } function upgradeBox(address proxyAddress, address newBox) public returns (address) { vm.startBroadcast(); BoxV1 proxy = BoxV1(proxyAddress); proxy.upgradeTo(address(newBox)); vm.stopBroadcast(); return address(proxy); } } ``` ### Wrap Up We now have the ability to programmatically upgrade our BoxV1 protocol to BoxV2! In this UpgradeBox script, we are acquiring our most recent ERC1967 deployment by leveraging the DevOpsTools library. We're then deploying BoxV2 and then calling our upgradeBox function. This function passes our new BoxV2 address to our proxy, upgrading the protocol's implementation address! In the next lesson we'll set up some tests to see this in action. Let's gooooo!
Tutorial on upgrading UUPS proxy smart contracts, including script writing and execution.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on November 29, 2024
Solidity Developer
Advanced FoundryDuration: 36min
Duration: 3h 06min
Duration: 5h 02min
Duration: 2h 47min
Duration: 1h 23min
Duration: 4h 28min
Duration: 1h 19min
Duration: 58min
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on November 29, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar