DEV Community

Cover image for Deploy Upgradeable Smart Contracts with Foundry and OpenZeppelin
Thomas Cosialls
Thomas Cosialls

Posted on

Deploy Upgradeable Smart Contracts with Foundry and OpenZeppelin

Today, I am gonna teach you how to deploy an upgradeable ERC20 token to Polygon Mumbai with Foundry and OpenZeppelin 🚀

I will assume you already have your Foundry environment set up and have already written an upgradeable smart contract for your ERC20 token, that we will call MyUpgradeableToken.sol. If you need help with this, you can read my full article about deploying an upgradeable ERC20 token with Foundry.

First, set up your environment to using OpenZeppelin's upgradeable contracts and Foundry extensions.

forge install OpenZeppelin/openzeppelin-foundry-upgrades forge install OpenZeppelin/openzeppelin-contracts-upgradeable 
Enter fullscreen mode Exit fullscreen mode

Then modify the foundry.toml file:

[profile.default] ffi = true ast = true build_info = true extra_output = ["storageLayout"] [rpc_endpoints] mumbai = "https://rpc.ankr.com/polygon_mumbai" 
Enter fullscreen mode Exit fullscreen mode

Now, you can create a new file called 01_Deploy.s.sol in the script directory. Don't forget to add your own PRIVATE_KEY variable in the .env file.

// SPDX-License-Identifier: MIT pragma solidity ^0.8.25; import "forge-std/Script.sol"; import "../src/MyUpgradeableToken.sol"; import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol"; contract DeployScript is Script { function run() external returns (address, address) { //we need to declare the sender's private key here to sign the deploy transaction uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); vm.startBroadcast(deployerPrivateKey); // Deploy the upgradeable contract address _proxyAddress = Upgrades.deployTransparentProxy( "MyUpgradeableToken.sol", msg.sender, abi.encodeCall(MyUpgradeableToken.initialize, (msg.sender)) ); // Get the implementation address address implementationAddress = Upgrades.getImplementationAddress( _proxyAddress ); vm.stopBroadcast(); return (implementationAddress, _proxyAddress); } } 
Enter fullscreen mode Exit fullscreen mode

Finally, you can deploy your smart contract to Polygon Mumbai!

forge script script/01_Deploy.s.sol:DeployScript --sender ${YOUR_PUBLIC_KEY} --rpc-url mumbai --broadcast -vvvv 
Enter fullscreen mode Exit fullscreen mode

Happy deployment and stay tuned for more tutorials to build and navigate in web3 ! 🏄‍♂️

Top comments (0)