In This Section We Will Learn How to Upgrade Smart Contracts.
-
Upgrading Smart Contract Does Not Compromise The Immutability of Smart Contract in Blockchain
-
In This Chapter We Will Be Using
@openzeppelin/hardhat-upgrades
Package For Upgrading our Smart COntract -
When We Initially Deploy Contarct, It Deploy Three Contracts in Real.
- Implementation Contract (Our Original)
- ProxyAdmin Contract
- Proxy Contarct
We Deal With Proxy Contarct For Calling Our Specified Methods
-
When We Deploy a Contarct Using
deployProxy
Method, That Contract Instance Can Be Upgraded Later -
Only The Address That Originally Deployed Has the Rights to Upgrade
-
On Upgrading, The State Values (Vairables) and Contarct Adress etc Remains Same as The Previous One
npm install @nomiclabs/hardhat-ethers
npm install @nomiclabs/hardhat-waffle
npm install @openzeppelin/hardhat-upgrades
npm install ethereum-waffle
npm install ethers
npm install hardhat
And Dont Forget to Add Below Lines in hardhat.config.js
require("@nomiclabs/hardhat-waffle");
require("@openzeppelin/hardhat-upgrades");
npm init -y
npm i hardhat
npx hardhat
Open Terminal and Run
npx hardhat node
to Run Hardhat Local Blockchain
Now Deploy Our Contarc to Localhost
npx hardhat run Scripts/DeployOld.js --network localhost`
Now Copy The Address of This Deployed Smart Contract
Now Open Terminal and Type
npx hardhat console --network localhost
const A1=await ethers.getContarctFactory("Old")
const A2=await A1.attach("0x9AB3SHDJD .... ") //Contract Address
(await A2.Get()).toString() // Will Print Value of Num (1)
// Now Deploy Our New Contract
npx hardhat run Scripts/DeployNew.js --network localhost
//Now Run Again
const B1=await ethers.getContarctFactory("New")
const B2=await B1.attach("0x9AB3SHDJD .... ") //Contract Address
(await B2.Increment())
(await A2.Get()).toString() // Will Print Value of Num (2)