Deploy upgradeable smart contract using Openzeppelin on local test network OR polygon test network. This example uses Transparent Proxy approach for deploying upgradeable smart contract.
Commands to create a new project using Hardhat
-
mkdir openzeppelin_upgradeable_sc -
cd openzeppelin_upgradeable_sc -
npm init --yes -
npm install --save-dev hardhat -
npx hardhat -
Create an
.envfile by renamingenv.exampleto.envand add the private-key and rpc api-key in this file. we require these keys for configuring hardhat.config.js file. please note that private-key and rpc api-key is only needed when using polygon_test network. -
Please add this
.envfile to.gitignoreto avoid putting private information to github. -
Install
npm i dotenvpackage to use the environment variables from.envfile. -
We can add more plugins like
hardhat-gas-reporter,solidity-coverageandhardhat-solhintusing below commands.A.
npm install hardhat-gas-reporter --save-dev. For more info please refer documentationB.
npm install solidity-coverage --save-dev. For more info please refer documentationC.
npm install @nomiclabs/hardhat-solhint --save-dev. For more info please refer documentation. -
Since we are using openzeppelin then we have to add some plugins.
A.
npm install @openzeppelin/contractsB.
npm install --save-dev @openzeppelin/hardhat-upgradesC.
hardhat-config.jsis already configured to use thehardhat-upgradeplugin.
Compile Project
npx hardhat compile
Test Project
npx hardhat test
Deploy Project
WARNING: There is some issue with Polygon rpc testnet endpoints. They are not responding well when deploying the upgradeable smart-contract.
-
Open a new terminal and start a hardhat local node using command
npx hardhat node. -
Deploying
deployProxyscript using commandnpx hardhat run --network localhost scripts/deployProxy_box.js. please note down the address. -
Lets test the above smart-contract but first lets update the contract address here in
execute_boxv1_func.jsscript. Now let's deploy the script using commandnpx hardhat run scripts/execute_boxv1_func.js --network localhost. It will return13as a value. -
Now it's time to deploy
BoxV2smart-contract which contains additional functionality when compared toBoxsmart-contract. -
We will be using
upgradeProxyscript for this operation. But before deploying this script, please make sure to replace the address here with the contract address generated in Step 2. -
Deploy the script using command
npx hardhat run --network localhost scripts/upgradeProxy_box.js -
Now we have successfully upgraded the smart-contract
BoxtoBoxv2while keeping its state and the same contract address as before. Compare the output of Step 2 and Step 5. Both are using same contract address. -
Now lets test the
BoxV2smart-contract by deploying theexecute_boxv2_func.jsscript. But before deploying it, update the contract address here. Now lets deploy the script using commandnpx hardhat run scripts/execute_boxv2_func.js --network localhost. It will return35as a value.
References:
Hardhat docs
OpenZeppelin docs v4.x
Youtube Video: OpenZeppelin Upgrade contracts
Credits
OpenZeppelin Team