Skip to content

Commit

Permalink
fixed typos and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
prayagsingh committed Sep 17, 2021
1 parent 71287be commit a074b55
Show file tree
Hide file tree
Showing 7 changed files with 5,526 additions and 298 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ cache
artifacts
.env
.openzeppelin
package-lock.json
package-lock.json
coverage.json
coverage
54 changes: 22 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
# Openzeppelin upgradable smart-contract example

Deploy upgradable smart contract using Openzeppelin on Polygon test network
Deploy upgradable 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**

1. `mkdir openzeppelin_upgradable_sc`

2. `cd hardhat_polygon_nft`
2. `cd openzeppelin_upgradable_sc`

3. `npm init --yes`

4. `npm install --save-dev hardhat`

5. `npx hardhat`

6. Create an `.env` file and add the private-key and rpc api-key in this file. we require these keys for configuring hardhat.config.js file.
6. Create an `.env` file by renaming `env.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.

7. Please add this `.env` file to `.gitignore` to avoid putting private information to github.

8. Install `npm i dotenv` package to use the environment variables from `.env` file
8. Install `npm i dotenv` package to use the environment variables from `.env` file.

9. Since we are using openzeppelin then we have to add some plugins.
9. We can add more plugins like `hardhat-gas-reporter`, `solidity-coverage` and `hardhat-solhint` using below commands.

A. `npm install hardhat-gas-reporter --save-dev`. For more info please refer [documentation](https://hardhat.org/plugins/hardhat-gas-reporter.html)

B. `npm install solidity-coverage --save-dev`. For more info please refer [documentation](https://hardhat.org/plugins/solidity-coverage.html)

C. `npm install @nomiclabs/hardhat-solhint --save-dev`. For more info please refer [documentation](https://hardhat.org/plugins/nomiclabs-hardhat-solhint.html).

10. Since we are using openzeppelin then we have to add some plugins.

A. `npm install @openzeppelin/contracts`

Expand All @@ -38,41 +46,23 @@ Deploy upgradable smart contract using Openzeppelin on Polygon test network

**Deploy Project**

***WARNING:*** This section requires some changes after the recent changes in the code.
***WARNING:*** There is some issue with Polygon rpc testnet endpoints. They are not responding well when deploying the upgradeable smart-contract.

1. Deploying `deployProxy` [script](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/main/scripts/deployProxy_box.js) using command `npx hardhat run --network polygon_test scripts/deployProxy_box.js`. please note down the address. we will use it in Step `2.C`.
1. Open a new terminal and start a hardhat local node using command `npx hardhat node`.

2. Now interact with `Box` contract by using `Hardhat-console`. Please follow below steps.
2. Deploying `deployProxy` [script](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/main/scripts/deployProxy_box.js) using command `npx hardhat run --network localhost scripts/deployProxy_box.js`. please note down the address.

A. `npx hardhat console --network polygon_test`

B. `const Box = await ethers.getContractFactory('Box');`

C. `const box = await Box.attach('<the address of our proxy contract from when we deployed our Box contract>');`

D. `(await box.retrieve()).toString();` Result shoud be `42`.
3. Lets test the above smart-contract but first lets update the contract address [here](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/71287be9032d9f57b1ac9e6663e76adee90137c4/scripts/execute_boxv1_func.js#L15) in `execute_boxv1_func.js` script. Now let's deploy the script using command `npx hardhat run scripts/execute_boxv1_func.js --network localhost`. It will return `13` as a value.

3. Now it's time to deploy `BoxV2` smart-contract which contains additional functionality when compared to `Box` smart-contract.
4. Now it's time to deploy `BoxV2` smart-contract which contains additional functionality when compared to `Box` smart-contract.

4. We will be using `upgradeProxy` [script](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/main/scripts/upgradeProxy_box.js) for this operation. But before deploying this script please make sure to replace the address [here](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/7473f927600b716cb9265d3e5dc95d939521781b/scripts/upgradeProxy_box.js#L8) with the address created in Step 1.
5. We will be using `upgradeProxy` [script](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/main/scripts/upgradeProxy_box.js) for this operation. But before deploying this script, please make sure to replace the address [here](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/71287be9032d9f57b1ac9e6663e76adee90137c4/scripts/upgradeProxy_box.js#L8) with the contract address generated in Step 2.

5. Deploy the script using command `npx hardhat run --network polygon_test scripts/upgradeProxy_box.js`
6. Deploy the script using command `npx hardhat run --network localhost scripts/upgradeProxy_box.js`

6. Now we have successfully upgraded the smart-contract `Box` to `Boxv2` while ***keeping its state and the same address as before***.
7. Now we have successfully upgraded the smart-contract `Box` to `Boxv2` 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.

7. Verify the upgradation using below commands.

A. `npx hardhat console --network polygon_test`

B. `const BoxV2 = await ethers.getContractFactory('BoxV2');`

C. `const box = await BoxV2.attach('< same address as in Step 2.C >');`

D. `(await box.retrieve()).toString();` Result shoud be `42`.

E. execute increment function using command `await box.increment();`

F. `(await box.retrieve()).toString();` Result should be `43`.
8. Now lets test the `BoxV2` smart-contract by deploying the `execute_boxv2_func.js` script. But before deploying it, update the contract address [here](https://github.com/prayagsingh/openzeppelin_upgrdabale_sc/blob/71287be9032d9f57b1ac9e6663e76adee90137c4/scripts/execute_boxv2_func.js#L15). Now lets deploy the script using command `npx hardhat run scripts/execute_boxv2_func.js --network localhost`. It will return `35` as a value.

**References:**

Expand Down
3 changes: 2 additions & 1 deletion contracts/Box.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ contract BoxV2 is Box {
bool v2Initialized;

// @note not possible to override the initialize function since it executes only once and not virtual in nature
// @note In upgradable smart contract we can't change anything in the Box contract. we can only add new functions and variables
// @note In upgradable smart contract we can't change anything in the Box contract.
// we can only add new functions and variables
// @note https://forum.openzeppelin.com/t/understanding-upgradeable-smart-contract/15485

function initializeV2() public returns (bool) {
Expand Down
4 changes: 4 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POLYGON_TEST_API_KEY=
POLYGON_TEST_PRIVATE_KEY=
METADATA_CID=
REPORT_GAS=true
11 changes: 11 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require("@nomiclabs/hardhat-waffle");
require('@nomiclabs/hardhat-ethers');
require('@openzeppelin/hardhat-upgrades');
require("hardhat-gas-reporter");
require("solidity-coverage");
require("@nomiclabs/hardhat-solhint");

// for using .env file which contains private info.
require('dotenv').config();
Expand Down Expand Up @@ -29,6 +32,7 @@ module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
initialBaseFeePerGas: 0, // workaround from https://github.com/sc-forks/solidity-coverage/issues/652#issuecomment-896330136 . Remove when that issue is closed.
},
// rinkeby: {
// url: "https://eth-mainnet.alchemyapi.io/v2/123abc123abc123abc123abc123abcde",
Expand Down Expand Up @@ -58,6 +62,12 @@ module.exports = {
timeout: 60000,
}
},

gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
},

solidity: {
version: "0.8.4",
settings: {
Expand All @@ -67,6 +77,7 @@ module.exports = {
}
}
},

paths: {
sources: "./contracts",
tests: "./test",
Expand Down
Loading

0 comments on commit a074b55

Please sign in to comment.