Prerequisites
- You have deployed a contract with Hardhat before.
- You can read JavaScript.
Instructions
Building an Upgradable Smart Contract
Deployment goes through OpenZeppelin’sdeployProxy.
1. Install upgrades plugin
In your Hardhat project, install the OpenZeppelin package that holds the upgrade logic:hardhat.config.js:
2. Create the contract
Place thisSimpleStorage.sol in the contracts/ directory of your project:
3. Write the deployment script
The deployment script goes inscripts/, not in ignition/modules/. Create scripts/upgrade_storage.js with the following code:
4. Deploy the contract
Upgradable contracts are deployed withnpx hardhat run, not with ignition deploy:
5. Interact with the contract
Open the Hardhat console:0xYourProxyAddress with the proxy address printed by your deployment script:
10, so the proxy was initialized correctly.
Upgrade previously deployed contract
Now add a feature to the deployed contract: incrementing the stored value.1. Create a new contract
Create a second Solidity contract incontracts/ called SimpleStorageV2:
increment function.
2. Upgrade previously deployed contract
OpenZeppelin’supgradeProxy points the existing proxy at the new logic. Create scripts/upgrade_storagev2.js:
3. Run the upgrade
Run the upgrade script against the same network:4. Interact with the contract
Back in the Hardhat console,increment() is callable. Use the same deployed proxy address in place of 0xYourProxyAddress:
increment() was called. For how proxies do this, see the OpenZeppelin documentation.
