/hardhat-oz-for-upgrades-example

An example of hardhat contracts upgrades using OpenZeppelin

Primary LanguageJavaScriptMIT LicenseMIT

hardhat-oz-upgrades-example

An example of hardhat contracts upgrades using OpenZeppelin

Getting Started 🚀

Pre-requisites 📋

You need to have installed nodejs

See Hardhat setup enviroment tutorial

Installation 🔧

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

Compiling 🗃️

yarn compile

Running tests ⚙️

In order to test using a mainnet fork you must set ALCHEMY_MAIN_API_KEY in your .env file

yarn test

Usage 🕹️

Case #1: Update the deployed contract

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.

Case #2: Upgrade the contract

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

Extra: Verify the contract on Etherscan

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

Built with 🛠️

Author ✒️

License 📄

This project is under the MIT License - look up the file LICENSE.md for more details.