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
.env
file by renamingenv.example
to.env
and 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
.env
file to.gitignore
to avoid putting private information to github. -
Install
npm i dotenv
package to use the environment variables from.env
file. -
We can add more plugins like
hardhat-gas-reporter
,solidity-coverage
andhardhat-solhint
using 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/contracts
B.
npm install --save-dev @openzeppelin/hardhat-upgrades
C.
hardhat-config.js
is already configured to use thehardhat-upgrade
plugin.
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
deployProxy
script 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.js
script. Now let's deploy the script using commandnpx hardhat run scripts/execute_boxv1_func.js --network localhost
. It will return13
as a value. -
Now it's time to deploy
BoxV2
smart-contract which contains additional functionality when compared toBox
smart-contract. -
We will be using
upgradeProxy
script 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
Box
toBoxv2
while 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
BoxV2
smart-contract by deploying theexecute_boxv2_func.js
script. 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 return35
as a value.
References:
Hardhat docs
OpenZeppelin docs v4.x
Youtube Video: OpenZeppelin Upgrade contracts
Credits
OpenZeppelin Team