- Use Transparent Proxy Pattern and Gnosis Multisignature wallet for the implementation.
- You can refer the document under the resource section of 8th February live class to perform this task
- The pattern for this upgradable contracts is divided into three phases of upgradable smart contract- Phase 1- contract version 1, Phase 2- contract version 2 and Phase 3 - contract version 3.
- Phase 1- Create a simple ERC20 contract with a pre-minting of 1 million tokens
- Phase 2- Add the functionality to mint and burn tokens, add allowance mechanism functions. All payable and transaction functions should be implemented using the call method.
- Phase 3- Add Reentrancy Guard for vulnerable functions(use a custom modifier to implement reentrancy guard), add onlyOwner modifier wherever necessary(use custom modifier for this implementation).
- Perform testing for all the phases.
Note- All features from Phases 1,2 and 3 should be able to be executed directly using the Proxy Address from goerli etherscan.
- Creating a simple directory
mkdir UpgradableSmartContract
cd UpgradableSmartContract
- Initializing Node and Installing Hardhat
npm init
npm install hardhat
- Creating Hardhat project and installing Hardhat toolbox
npx hardhat
npm install --save-dev @nomicfoundation/hardhat-toolbox
OR
npx hardhat
npm install --save-dev "hardhat@^2.12.7" "@nomicfoundation/hardhat-toolbox@^2.0.0"
- Install Hardhat Upgrades from Openszppelin, Hardhat Etherscan and Openzeppelin contracts (To use Timelock behavior).
npm install @openzeppelin/hardhat-upgrades @nomiclabs/hardhat-etherscan @openzeppelin/contracts
- Editing hardhat.config.js file: Specifying Solidity version (The same for Smart Contract): Forr that, supplementary infos should be required:
- Infura API key.
- Etherscan API key.
- Wallet private key (From MetaMask Goerli account).
These three parameters will be specified inside the
secrets.json(This file should be created to the root of the project) file like that:
{
"alchemyAPIKeyGoerli":"achemyKey",
"deployerWalletPrivateKey":"accountPrivateKey",
"etherscanAPIKey":"etherscanKey"
}
- Create a simple ERC20 contract with a pre-minting of 1 million tokens.
- Instead of using constructor, we will follow the openzeppelin pattern without inporting openzeppelin file (Read here).
- Compile the contract file:
npx hardhat compile
- Write the test cases (For the contract V1 and the Proxy version also), and run them by typing this command:
npx hardhat test
- Write the
scripts/deploy.jsfile. - Deploy the scripts to
goerli testnetand save the output address as Proxy Address inside addresses.json file (.txt could be also used, just a preference):
npx hardhat run scripts/deploy.js --network goerli
This ProxyAddress will keep track of state variables and help to separate state (Proxy Contract) from the logic (Implementation Contract): Link the Proxy with Implementation.
- We can proceed with the verification contract using the deployed address directly from goerli ethercan. For this project, the full link with address was this.
- Navigate to contract tab.
- Chooce More Options.
- Click on Is this is a proxy?.
- Click on Verify.
- Copy the Implementation address and save it inside the
addresses.json file(proxyAddress).
If the verification failed, we can use this command (This was the case):
npx hardhat verify --network goerli 0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7
The result will look like:
Verifying implementation: 0xf6486485Fc8CEB4a5914e1516FcbE5E162C4eEe1
Nothing to compile
Successfully submitted source code for contract
contracts/JosamTokenV1.sol:JosamToken at 0xf6486485Fc8CEB4a5914e1516FcbE5E162C4eEe1
for verification on the block explorer. Waiting for verification result...
Successfully verified contract JosamToken on Etherscan.
https://goerli.etherscan.io/address/0xf6486485Fc8CEB4a5914e1516FcbE5E162C4eEe1#code
Verifying proxy: 0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7
Contract at 0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7 already verified.
Linking proxy 0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7 with implementation
Successfully linked proxy to implementation.
Verifying proxy admin: 0x609BB601d0130697DfbFB43e03484473EF04b151
Contract at 0x609BB601d0130697DfbFB43e03484473EF04b151 already verified.
Proxy fully verified.
- Copy the
Tmelock.solcontract fromnode_modules(/node_modules/@opezeppelin/contracts/governance/TimelockController.sol) and past it on the contract folder. Update import with openzeppelin full import (This contract will behave as the Administrator of Smart Contract during upgrade process and delaying Smart Contract function execution.). - Create a multisignature wallet with Gnosis safe, and play the role of
proposerandexecutor. This will allow multisign transaction and call timelock to wait for the delay to upgradeJosamTokenV1toJosamTokenV2.
- Connect MetaMask account used in earlier steps.
- Provide a project name:
ProxyUpgradeImplementationand selectgoerli. - Add at least one owner and give a name of each one (eg.: first, second, etc.).
- Create
arguments fileto specify time duration to execute transaction (By the contract itself), executors array and proposers array to be passed as arguments to Timelock.sol contract during deployment. - Create Admin address for the Timelock.sol contract (By following the documentation) to be deployed.
- Deploy the
Timelock.solcontract after writing the proper script filescripts/deploy-timelock.js.
npx hardhat run scripts/deploy-timelock.js --network goerli
The result will look like this:
Compiled 11 Solidity files successfully
Deploying contract with the account: 0x383ed93db019c2B371AFD70fA49b0B55f836ECdE
Account balance: 1276545331521920582
Timelock contract deployed on address: 0x66b251eaC8E7E567165C676e612F334af65E8819
- Verify that the Timelock contract has deployed successfully by typing this command:
npx hardhat verify --network goerli --constructor-args arguments.js 0x66b251eaC8E7E567165C676e612F334af65E8819
Result will look like this:
Successfully submitted source code for contract
contracts/Timelock.sol:TimelockController at 0x66b251eaC8E7E567165C676e612F334af65E8819
for verification on the block explorer. Waiting for verification result...
Successfully verified contract TimelockController on Etherscan.
https://goerli.etherscan.io/address/0x66b251eaC8E7E567165C676e612F334af65E8819#code
- inside
.openzeppelin/goerli.jsonfile, copy theAdmin addressinside theaddresses.json fileasProxyAdmin. - Write the file
transfer_ownership.jsto makeTimelock Contractthe ownership of our proxy contract to change the Administrator. Write the script file and execute it (The current ProxyAdmin address should became theTimelockController):
npx hardhat run scripts/transfer_ownership.js --network goerli
The result will look like this:
Transferring ownership of ProxyAdmin
✔ 0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7 (transparent) proxy ownership transfered through admin proxy
Transferred ownership of ProxiAdmin to: 0x66b251eaC8E7E567165C676e612F334af65E8819
Now, the
Timelock Contractis the new owner.
- Prepare
JosamTokenV2by writting the new Smart Contract. - Compile the contract file with the command:
npx hardhat compile
- Write test cases for
JosamTokenV2andJosamTokenV2.proxy(Where we deploy V1 Proxy contract, and V2 Proxy using V1 upgrade Proxy address), then run it with this command:
npx hardhat test
All tests should passed to go forward.
- Prepare the upgrade by writting a new script file:
prepare_upgradeV1.V2.js(The propose part of the upgration, and The execution part will be done at the next step.):
npx hardhat run scripts/prepare_upgradeV1.V2.js --network goerli
The result will look like this (Then copy this deployed address on addresses.json file.):
Preparing upgrade...
JosamTokenV2 upgrade at: 0xA26c47Dd9D6a6C0D6Ac59F7d7003E32C0974cCF5
This address will be the
Implementation Addressof theJosamTokenV2 Contract.
- Getting Hexadecimal Data.
- Use the
ProxyAdminAddressfrom earlierTimelockcreation on Goerli Ethercan. - Go to Contract tab, and connect MetaMask wallet.
- Choose option 4
upgradeand copyproxy address(Our first deployed contract Address:0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7) andImplementation address(For JosamTokenV2 from prepare_upgradeV1.V2:0xA26c47Dd9D6a6C0D6Ac59F7d7003E32C0974cCF5). - Click on write, and on MetaMask (Don't validate transaction), copy the
HEX DatafromHEX tabsomewere. - Reject MetaMask Transaction.
- Schedule and Execute transaction from Gnosis Safe.
- Connect MetaMask wallet (It will be the case by default, because it was done at earliers stage).
- Create transaction by cliking on
New transaction. - Chose
Contract interaction. - Specify the
Timelock Addresson address field. - Specify the
abi(Including brackets also) code of the Timelock Contract fromartifactsdirectory of the Hardhat Project. - On
Transaction informationleaves at it is. - Choose
scheduleonContract Method Selectorto schedule the Upgrade execution (For the delau=y specified ealier). - Specify
ProxyAdmin Addresson theTarget field. valuewill be0.datawill be theHEX datacopied on earlier stage.- For
predecessorandsalt, we can give zero address (0x0000000000000000000000000000000000000000000000000000000000000000). - For the
delay, we specify150as we did on in the deploy scripts at earlier stage. - Click on
Add transactionto proceed. - Click first on
Simulateto be sure that all field are properly filled. - Click on
Create Batch. - Click on
Submitand validate transaction on MetaMask. - Click on
Send Batch.
- Verify the execution of the
Scheduleevent on etherscan by copying theTimelock Addresson Etherscan an verify on theevent tab(CallScheduled) the execution of the task after150 secondes. - Execute the transaction.
- Create transaction by cliking on
New transaction. - Chose
Contract interaction. - Specify the
Timelock Addresson address field. - Specify the
abi(Including brackets also) code of the Timelock Contract fromartifactsdirectory of the Hardhat Project. - On
Transaction informationleaves at it is. GOR valuewill be0.- Oon
Contract Method Selectorinstead ofschedule, we are going to chooseexecuteto execute the final transaction (To process the upgrade from V1 to V3). - Specify
ProxyAdmin Addresson theTarget field. valuewill be0.payloadwill be theHEX datacopied on earlier stage.- For
predecessorandsalt, we can give zero address (0x0000000000000000000000000000000000000000000000000000000000000000). - Click on
Add transaction. - Click on
Create Batch. - Click first on
Simulateto be sure that all field are properly filled. - Click on
Send Batch.
- Gnosis success Transactions (
ScheduleandExecute) of V1 to V2.
- Use the
Proxy Contract Address(0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7) on Etherscan (OnContract Tab) to be able toRead as ProxyandWrite as Proxyto verify that the contract was upgraded successfully. - Then to be able to use the feature of the current contract version (
JosamTokenV2), a verification should be require, do that by using the last Contract implementation Address (0xA26c47Dd9D6a6C0D6Ac59F7d7003E32C0974cCF5) with this command:
npx hardhat verify --network goerli 0xa26c47dd9d6a6c0d6ac59f7d7003e32c0974ccf5
The result will look like this:
Nothing to compile
Successfully submitted source code for contract
contracts/JosamTokenV2.sol:JosamTokenV2 at 0xa26c47dd9d6a6c0d6ac59f7d7003e32c0974ccf5
for verification on the block explorer. Waiting for verification result...
Successfully verified contract JosamTokenV2 on Etherscan.
https://goerli.etherscan.io/address/0xa26c47dd9d6a6c0d6ac59f7d7003e32c0974ccf5#code
- JosamTokenV2 upgraded implementation on Etherscan
- Write
version 3of the contractJosamTokenV3withonlyOwnerbehavior andcustom ReentrancyGuardbehavior also. - Write test cases for
JosamTokenV3(IncludingJosamTokenV3.proxy) and run them with this command.
npx hardhat test
- Write te
prepare_upgradeV2.V3.jsscript file and run it to upgrade fromV2toV3, and run it also:
npx hardhat run scripts/prepare_upgradeV2.V3.js --network goerli
The result will look like this:
Preparing upgrade...
JosamTokenV3 upgrade at: 0xd057a43029B1f4097450D10edaCFAe6A04e4E8Fb
The new address will be the new Implementation Address of the Contract (JosamTokenV3). We are going to coy it inside the addresses.json.
-
Then we can proceed with the previous steps (Step3 : Phase 2: From 5 to 9) by using address of
latest contract implementationand the newHEX Data. -
Gnosis success Transactions (
ScheduleandExecute) of upgrade from V2 to V3.
- Use the
Proxy Contract Address(0x62Ecc819e15c7d5ef471E3356FdaF9Da52C591C7) on Etherscan (OnContract Tab) to be able toRead as ProxyandWrite as Proxyto verify that the final (JosamTokenV3) has upgraded successfully. - Then to be able to use the feature of this final contract version (
JosamTokenV3), a verification should be require, do that by using the last Contract implementation Address (0xd057a43029B1f4097450D10edaCFAe6A04e4E8Fb) with this command:
npx hardhat verify --network goerli 0xd057a43029B1f4097450D10edaCFAe6A04e4E8Fb
The result will look like this:
Nothing to compile
Successfully submitted source code for contract
contracts/JosamTokenV3.sol:JosamTokenV3 at 0xd057a43029B1f4097450D10edaCFAe6A04e4E8Fb
for verification on the block explorer. Waiting for verification result...
Successfully verified contract JosamTokenV3 on Etherscan.
https://goerli.etherscan.io/address/0xd057a43029B1f4097450D10edaCFAe6A04e4E8Fb#code
- JosamTokenV3 upgraded implementation on Etherscan
- With that, the upgrade of
JosamTokenV1toJosamTokenV2then toJosamTokenV3is done successfully with all features inside final Contract version (Custom ERC20 Token with custom Reentrancy guard behavior).



