An example of hardhat contracts upgrades using OpenZeppelin
You need to have installed nodejs
See Hardhat setup enviroment tutorial
First you need to clone this repository
git clone https://github.com/rafius97/hardhat-oz-upgrades-example/
Make sure you are in the right folder
cd hardhat-oz-upgrades-example/
Next, you must install the dependencies
yarn
yarn compile
In order to test using a mainnet fork you must set ALCHEMY_MAIN_API_KEY in your .env file
yarn test
Imagine your using a testnet and constantly your making some changes to a deployed contract. In that case you can use:
yarn deploy-upgrade:rinkeby
In the example, rinkeby network it's used.
You need to make sure in the 3_deploy_upgrade.js file the contract factory has to be the same that your making the changes. For example:
const contract = await ethers.getContractFactory("Contract");
So, instead of create another contract file, just update the same contract your working with.
This is so useful when you are in the development phase, no need to redeploy the contract again.
Imagine you already deployed a contract in a mainnet and you need to make some changes to it:
yarn deploy-upgrade:rinkeby
In the example, rinkeby network it's used.
You need to make sure in the 3_deploy_upgrade.js file the contract factory has to be the contract file upgraded, usually version is added in the contract name. For example:
const contract = await ethers.getContractFactory("ContractV2");
This is so useful when you already have a contract deployed in the mainnet.
Remember always test before deploy
Verify an upgradeable contract now it's easy using Hardhat, I make a task to handle that, and use the @nomiclabs/hardhat-etherscan plugin for Hardhat:
yarn verify-contract:rinkeby --address "0x00..."
Note #1: Make sure you set ETHERSCAN_API_KEY in your .env file
Note #2: Make sure that the contract address not to be the proxy contract address
- Rafael Romero - rafius97
This project is under the MIT License - look up the file LICENSE.md for more details.