This project demonstrates how we can create a simple upgradeable faucet with any custom ERC20 token. Upgradeable contracts using hardhat upgrades allows to upgrade the functionality of the deployed contract using Transparent Proxy pattern.
In this we have two contracts:-
- Faucet_V1
- Faucet_V2
In this user uses the requestToken function to request the token to their address. They pass their address and amount of token needed as argument if all the condition like number of times allowed and amount allowed are satisfied then it mints the amount to the users account otherwise it reverts with an error.
-
We first create a Faucet_V1 contract and define the initialize function in it. Which initializes the ERC20, Ownable, mint and variables in proxy contract and also generates proxy admin.
-
Then we create a deploy_faucetV1.js script for deploying the Faucet_V1. In this we use Hardhat Upgrades deployProxy method. For more info refer to OpenZeppelin Hardhat Upgrades API.
-
Once deployed we get three contracts:-
- Transparent Upgradeable Proxy
- Proxy Admin
- Faucet_V1
We need to verify the Faucet_V1 contact using hardhat:
npx hardhat verify --network <network name> <contract address> -
Then we need to go to polygon scan and open Transparent Upgradeable Proxy and go to contract tab -> more options menu -> and click Is this a proxy? and click verify and save for the proxy contract prefilled, one can also change it if needed.
Now we can access the contract from Faucet_V1 and read all the variables that are initialized from the Transparent Upgradeable Proxy contract.
-
We first create a Faucet_V2 contract but this time we don't need to write initialize function. Add any functionality wanted.
-
Then we create a upgrade_faucet.js script for deploying the Faucet_V2. In this we use Hardhat Upgrades upgradeProxy method with proxy contract address as the argument. For more info refer to OpenZeppelin Hardhat Upgrades API.
-
Once Upgraded we get two contracts:-
- Proxy Admin
- Faucet_V2
We need to verify the Faucet_V2 contact using hardhat:
npx hardhat verify --network <network name> <contract address> -
Then we need to go to polygon scan and open Transparent Upgradeable Proxy and go to contract tab -> more options menu -> and click Is this a proxy? and click verify and save for the proxy contract prefilled for the new contract deployed.
Now we can call the functions in Faucet_V2 and see the update's in Transparent Upgradeable Proxy contract.
This is how we upgrade the contracts.
To run this project, you will need to add the following environment variables to your .env file
MATIC_API_URL = "https://polygon-mumbai.g.alchemy.com/v2/YOUR_API_KEY"
SCAN_API_KEY = "YOUR-POLYGON-SCAN-API_KEY"
PRIVATE_KEY = "YOUR-METAMASK-PRIVATE_KEY"
- Openzeppelin
- OpenZeppelin Hardhat Upgrades API
- Hardhat Upgrades
- Hardhat Ethers
- Chai
- Ethers
- ethereum-waffle
- dotenv
Clone the github repo:
https://github.com/itsshantanu/ERC20_Faucet_Upgradeable.git
Install Node Modules
npm install
Compile
npx hardhat compile
Test
npx hardhat test
Deploy on Localhost
npx hardhat node
npx hardhat run scripts/deploy_faucetV1.js --network localhost
Upgrade on Localhost
npx hardhat node
npx hardhat run scripts/upgrade_faucet.js --network localhost
Deploy on Mumbai Testnet
npx hardhat run scripts/deploy_faucetV1.js --network mumbai
Help
npx hardhat help