
What are upgradeable smart contracts?
Usually, when we deploy a smart contract, it’s impossible to update or change the code since it’s on-chain, and that’s how it should be. This increases the safety and trust of users who interact with that contact.
But there might be cases where you want to upgrade your smart contract, like fixing severe bugs and adding some critical features for the users. Traditionally doing this is not possible. The best one can deploy is a new smart contract with bug fixes with all the information migrated. This does not end here; one must update the references where the old contract address was used and inform existing users to use the new contract.
This can be overwhelming, but there is a way to handle these cases by using Upgradeable contracts. An upgradeable smart contract can be updated and edited after the deployment. This can be achieved by a plugin/tool created byOpenZeppelin.
In a nutshell
The plugin is used to deploy the contracts onHardhat ortruffle. Whenever in the future you wish to upgrade the smart contract, use the same address that you used to deploy the first contract via the plugin, and the plugin will handle the transferring of any state and data from the old contract while keeping the same contract address to interact with.
OpenZeppelin uses a proxy pattern where they deploy three smart contracts to manage the storage layer and implement smart contracts. Whenever a contract call is invoked, the user indirectly calls the proxy contract, and the proxy contract passes the parameters to the implemented smart contract before sending the output back to the user. Now since we have a proxy contract as a middle man, imagine how you want to change something in your implemented contract. All you have to do is deploy the new contract and tell your proxy contract to refer to the latest smart contract, and voila. All users are using the updated contract.
How do upgradeable contracts work?
When we use OpenZeppelin’s upgradeable plugin to deploy the contract, three contracts are deployed —
- Implemented Contract — The contract the developers create which contains all the logic and functionalities.
- Proxy Contract — The contract that the end-user interacts with. All the data and state of the contract are stored in the context of the Proxy contract. This Proxy Contract is an implementation of theEIP1967 standard.
- ProxyAdmin Contract — This contract links the Proxy and Implementation contract.
What is ProxyAdmin? (According to OpenZeppelin docs)
A ProxyAdmin is a contract that acts as the owner of all your proxies. Only one per network gets deployed. When you start your project, the ProxyAdmin is owned by the deployer address, but you can transfer ownership of it by calling transferOwnership.
When a user calls the proxy contract, the call isdelegated to the implementation contract. Now to upgrade the contract, what we have to do is:
- Deploy the updated Implementation Contract.
- Updating the ProxyAdmin so that all the calls are redirected to the newly implemented contract.
OpenZeppelin has created a plugin for Hardhat and Truffle to handle these jobs for us. Let’s see step by step how to create and test the upgradeable contracts.
Writing upgradeable smart contracts
We will be using Hardhat’s localhost to test the contract locally and Celo Alfajores test network.
Initialize project and install dependencies
mkdirupgradeable-contract&&cdupgradeable-contractyarn init-yyarn add hardhatyarn hardhat // choose typescript
Add plugin and updatehardhat.config.ts
yarn add @openzeppelin/hardhat-upgrades// hardhat.config.tsimport'@openzeppelin/hardhat-upgrades';
To understand the flow, we will be usingGreeter.sol
a contract. We will create and deploy several versions of this contract.
- Greeter.sol
- GreeterV2.sol
- GreeterV3.sol
NOTE — The big difference between a normal contract and an upgradeable smart contract is upgradeable smart contracts do not have a constructor.
//contracts/Greeter.sol//SPDX-License-Identifier: Unlicensepragma solidity ^0.8.0;contract Greeter { string public greeting; // Emitted when the stored value changes event ValueChanged(string newValue); function greet() public view returns (string memory) { return greeting; } function setGreeting(string memory _greeting) public { greeting = _greeting; emit ValueChanged(_greeting); }}
This is a very simple Greeter contract that returns the value ofgreeting
whenever we callgreet()
method.
Unit testing forGreeter.sol
Create a file called1.Greeter.test.ts
and add the following content.
// test/1.Greeter.test.tsimport { expect } from "chai";import { Contract } from "ethers";import { ethers } from "hardhat";describe("Greeter", function () { let greeter: Contract;beforeEach(async function () { const Greeter = await ethers.getContractFactory("Greeter"); greeter = await Greeter.deploy(); await greeter.deployed(); });it("should greet correctly before and after changing value", async function () { await greeter.setGreeting("Celo to the Moon"); expect(await greeter.greet()).to.equal("Celo to the Moon"); });});
Run the test:
yarn hardhattest test/1.Greeter.test.ts
Results:
Greeter ✔ should greet correctly before and after changing value1 passing (334ms)✨ Done in 1.73s.
Let’s write a deploy script and deployGreeter.sol
it to Hardhat’s local node. Create a file namedGreeter.deploy.ts
inscripts
directory and paste the following code.
// scripts/Greeter.deploy.tsimport { ethers, upgrades } from "hardhat";async function main() { const Greeter = await ethers.getContractFactory("Greeter"); console.log("Deploying Greeter..."); const greeter = await upgrades.deployProxy(Greeter);console.log(greeter.address, " greeter(proxy) address"); console.log( await upgrades.erc1967.getImplementationAddress(greeter.address), " getImplementationAddress" ); console.log( await upgrades.erc1967.getAdminAddress(greeter.address), " getAdminAddress" );}main().catch((error) => { console.error(error); process.exitCode = 1;});
Run the local node and deploy theGreeter.sol
contract:
yarn hardhat nodeyarn hardhat run scripts/Greeter.deploy.ts--network localhost
Results:
Deploying Greeter...0x9A676e781A523b5d0C0e43731313A708CB607508 greeter(proxy) address0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0 getImplementationAddress0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82 getAdminAddress✨ Done in 2.74s.
Note: If you run the deployment command several times you can notice that adminAddress does not change.
Now we need to implement theincrement
feature in the existing contract. Instead of replacing the current contract we will create a new contract and change the proxy to refer to the new contract.
CreatingGreeterV2.sol
// contracts/GreeterV2.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;import "./Greeter.sol";contract GreeterV2 is Greeter { uint256 public counter; // Increments the counter value by 1 function increment() public { counter++; }}
Writing unit tests for GreeterV2 before deploying.
import{expect}from"chai";import{BigNumber,Contract}from"ethers";import{ethers}from"hardhat";describe("Greeter V2",function(){letgreeterV2:Contract;beforeEach(asyncfunction(){constGreeterV2=awaitethers.getContractFactory("GreeterV2");greeterV2=awaitGreeterV2.deploy();awaitgreeterV2.deployed();});it("should retrieve value previously stored",asyncfunction(){awaitgreeterV2.setGreeting("Celo to the Moon");expect(awaitgreeterV2.greet()).to.equal("Celo to the Moon");});it("should increment value correctly",asyncfunction(){expect(awaitgreeterV2.counter()).to.equal(BigNumber.from("0"));awaitgreeterV2.increment();expect(awaitgreeterV2.counter()).to.equal(BigNumber.from("1"));});});
Run the test —
yarn hardhattest test/2.GreeterV2.test.ts
Results —
Greeter V2 ✔ should retrieve value previously stored ✔ should increment value correctly2 passing (442ms)✨ Done in 6.43s.
The above test was designed to only testGreeterV2.sol
not the upgraded version ofGreeter.sol
. Let’s write and deploy theGreeterV2
in proxy patter and test ifGreeterV2
works correctly.
// test/3.GreeterV2Proxy.test.tsimport{expect}from"chai";import{Contract}from"ethers";import{ethers,upgrades}from"hardhat";describe("Greeter (proxy) V2",function(){letgreeter:Contract;letgreeterV2:Contract;beforeEach(asyncfunction(){constGreeter=awaitethers.getContractFactory("Greeter");constGreeterV2=awaitethers.getContractFactory("GreeterV2");greeter=awaitupgrades.deployProxy(Greeter);// setting the greet value so that it can be checked after upgradeawaitgreeter.setGreeting("WAGMI");greeterV2=awaitupgrades.upgradeProxy(greeter.address,GreeterV2);});it("should retrieve value previously stored correctly",asyncfunction(){expect(awaitgreeterV2.greet()).to.equal("WAGMI");awaitgreeter.setGreeting("Celo to the Moon");expect(awaitgreeterV2.greet()).to.equal("Celo to the Moon");});});
Here we are setting thegreeting
value toWAGMI inGreeter
(V1) and after upgrading, checking thegreeting
value inGreeterV2
.
Run the test —
yarn hardhattest test/3.GreeterV2Proxy.test.ts
Results —
Greeter (proxy) V2 ✔ should retrieve value previously stored correctly1 passing (521ms)✨ Done in 6.45s.
Writing a script to updateGreeter
toGreeterV2
.
Note that the greeter proxy address is0x9A676e781A523b5d0C0e43731313A708CB607508
that we got when we deployedGreeter.sol
. We need a proxy address to deployGreeterV2.sol
.
Create a file calledGreeterV2.deploy.ts
and add the following code.
// scripts/2.upgradeV2.tsimport{ethers,upgrades}from"hardhat";constproxyAddress="0x9A676e781A523b5d0C0e43731313A708CB607508";asyncfunctionmain(){console.log(proxyAddress," original Greeter(proxy) address");constGreeterV2=awaitethers.getContractFactory("GreeterV2");console.log("upgrade to GreeterV2...");constgreeterV2=awaitupgrades.upgradeProxy(proxyAddress,GreeterV2);console.log(greeterV2.address," GreeterV2 address(should be the same)");console.log(awaitupgrades.erc1967.getImplementationAddress(greeterV2.address)," getImplementationAddress");console.log(awaitupgrades.erc1967.getAdminAddress(greeterV2.address)," getAdminAddress");}main().catch((error)=>{console.error(error);process.exitCode=1;});
Running deployment script forGreeterV2.sol
We need to start from the beginning, i.e., deploy Greeter.sol first, get the proxy address, and use that to deployGreeterV2.sol
.
yarn hardhat nodeyarn hardhat run scripts/Greeter.deploy.ts--network localhost
Here we got0x9A676e781A523b5d0C0e43731313A708CB607508
as a proxy address. (Update the proxy address inscripts/GreeterV2.deploy.ts
).
yarn hardhat run scripts/GreeterV2.deploy.ts--network localhost
Results —
0x9A676e781A523b5d0C0e43731313A708CB607508 original Greeter(proxy) addressupgrade to GreeterV2...0x9A676e781A523b5d0C0e43731313A708CB607508 GreeterV2 address(should be the same)0x0B306BF915C4d645ff596e518fAf3F9669b97016 getImplementationAddress0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82 getAdminAddress✨ Done in 2.67s.
Overriding the existing methods
Now let’s say you want to add a custom name field in with the greeting such that whengreet()
is called, the name is added to the returned string.
Create a new file calledGreeterV3.sol
incontracts
directory and add the following code —
// contracts/GreeterV3.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;import "./GreeterV2.sol";contract GreeterV3 is GreeterV2 { string public name; function setName(string memory _name) public { name = _name; } function greet() public view override returns (string memory) { return string(abi.encodePacked(greeting, " ", name)); }}
Let’s write test cases to deploy and testGreeterV3
. Create a file called4.GreeterV3Proxy.test.ts
and add the following test cases.
// test/.GreeterV3Proxy.test.tsimport{expect}from"chai";import{BigNumber,Contract}from"ethers";import{ethers,upgrades}from"hardhat";describe("Greeter (proxy) V3 with name",function(){letgreeter:Contract;letgreeterV2:Contract;letgreeterV3:Contract;beforeEach(asyncfunction(){constGreeter=awaitethers.getContractFactory("Greeter");constGreeterV2=awaitethers.getContractFactory("GreeterV2");constGreeterV3=awaitethers.getContractFactory("GreeterV3");greeter=awaitupgrades.deployProxy(Greeter);// setting the greet value so that it can be checked after upgradeawaitgreeter.setGreeting("WAGMI");greeterV2=awaitupgrades.upgradeProxy(greeter.address,GreeterV2);greeterV3=awaitupgrades.upgradeProxy(greeter.address,GreeterV3);});it("should retrieve value previously stored and increment correctly",asyncfunction(){expect(awaitgreeterV2.greet()).to.equal("WAGMI");expect(awaitgreeterV3.counter()).to.equal(BigNumber.from("0"));awaitgreeterV2.increment();expect(awaitgreeterV3.counter()).to.equal(BigNumber.from("1"));});it("should set name correctly in V3",asyncfunction(){expect(awaitgreeterV3.name()).to.equal("");constname="Viral";awaitgreeterV3.setName(name);expect(awaitgreeterV3.name()).to.equal(name);expect(awaitgreeterV3.greet()).to.equal(`WAGMI${name}`);});});
Note 1 — Since all the data and state is stored in Proxy contract you can see in the test cases that we are calling
increment()
method inGreeterV2
and checking if the value is incremented or not inGreeterV3
.Note 2 — Since in our
GreeterV3.sol
the contract, we have added space and name to thegreeting
If the name is null, then thegreet
the call will return thegreeting
with an extra space appended to it.
Run the tests —
yarn hardhattest test/4.GreeterV3Proxy.test.ts
Results —
Greeter (proxy) V3 with name ✔ should retrieve value previously stored and increment correctly ✔ should set name correctly in V32 passing (675ms)✨ Done in 2.38s.
Deployment script for GreeterV3.sol
Create a file calledGreeterV3.deploy.ts
inscripts
and paste the following code —
// scripts/3.upgradeV3.tsimport{ethers,upgrades}from"hardhat";constproxyAddress="0x4ed7c70F96B99c776995fB64377f0d4aB3B0e1C1";asyncfunctionmain(){console.log(proxyAddress," original Greeter(proxy) address");constGreeterV3=awaitethers.getContractFactory("GreeterV3");console.log("upgrade to GreeterV3...");constgreeterV3=awaitupgrades.upgradeProxy(proxyAddress,GreeterV3);console.log(greeterV3.address," GreeterV3 address(should be the same)");console.log(awaitupgrades.erc1967.getImplementationAddress(greeterV3.address)," getImplementationAddress");console.log(awaitupgrades.erc1967.getAdminAddress(greeterV3.address)," getAdminAddress");}main().catch((error)=>{console.error(error);process.exitCode=1;});
Deploy theGreeterV3
yarn hardhat run scripts/GreeterV3.deploy.ts--network localhost
Note — If you get “Error: Proxy admin is not the one registered in the network manifest” when you try to deploy
GreeterV3
, you need to runGreeter.deploy.ts
andGreeterV2.deploy.ts
again and copy the new proxy address to be used to deploy the upgraded contracts.
Results —
0x4ed7c70F96B99c776995fB64377f0d4aB3B0e1C1 original Greeter(proxy) addressupgrade to GreeterV3...0x4ed7c70F96B99c776995fB64377f0d4aB3B0e1C1 GreeterV3 address(should be the same)0x3Aa5ebB10DC797CAC828524e59A333d0A371443c getImplementationAddress0x59b670e9fA9D0A427751Af201D676719a970857b getAdminAddress✨ Done in 2.63s.
Deploying the upgraded contract manually
Let’s write a new contract —GreeterV4
where we need to —
- Change the state variable
name
from public to private. - Add
getName
method to fetchname
value.
Create a file calledGreeterV4.sol
incontracts
directory and add the following code —
// contracts/GreeterV4.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;import "./GreeterV2.sol";contract GreeterV4 is GreeterV2 { string private name;event NameChanged(string name);function setName(string memory _name) public { name = _name; }function getName() public view returns (string memory) { return name; }}
Here we need to inherit fromGreeterV2
instead ofGreeterV3
becauseGreeterV3
already has a state variable called name and we cannot change the visibility but what we can do is inherit fromGreeterV3
which does not havename
variable and set the visibility as we want.
Let’s write test cases forGreeterV4.sol
.
// test/5.GreeterV4Proxy.test.ts/* eslint-disable no-unused-vars */import{expect}from"chai";import{Contract}from"ethers";import{ethers,upgrades}from"hardhat";describe("Greeter (proxy) V4 with getName",function(){letgreeter:Contract;letgreeterV2:Contract;letgreeterV3:Contract;letgreeterV4:Contract;beforeEach(asyncfunction(){constGreeter=awaitethers.getContractFactory("Greeter");constGreeterV2=awaitethers.getContractFactory("GreeterV2");constGreeterV3=awaitethers.getContractFactory("GreeterV3");constGreeterV4=awaitethers.getContractFactory("GreeterV4");greeter=awaitupgrades.deployProxy(Greeter);greeterV2=awaitupgrades.upgradeProxy(greeter.address,GreeterV2);greeterV3=awaitupgrades.upgradeProxy(greeter.address,GreeterV3);greeterV4=awaitupgrades.upgradeProxy(greeter.address,GreeterV4);});it("should setName and getName correctly in V4",asyncfunction(){expect(awaitgreeterV4.getName()).to.equal("");constgreetername="Celo";awaitgreeterV4.setName(greetername);expect(awaitgreeterV4.getName()).to.equal(greetername);});});
Run the test —
yarn hardhattest test/5.GreeterV4Proxy.test.ts
Results —
Greeter (proxy) V4 with getName ✔ should setName and getName correctly in V41 passing (608ms)✨ Done in 6.18s.
Preparing for the upgrade, but not really upgrading
When we callupgrades.upgradeProxy()
there are a couple of things happening —
- Your contract is deployed first,
- ProxyAdmin called the
upgrade()
method and link the proxy to the newly implemented contract’s address.
To do these steps manually, we can callupgrades.prepareUpgrade()
which only deploys your contract but does not link it with the proxy YET. This is left for the developers to link manually. This is useful when you want to test in production before wanting all the users to use the new contract.
Create a file calledGreeterV4Prepare.deploy.ts
inscripts
directory and add the following code —
import{ethers,upgrades}from"hardhat";constproxyAddress="0xc5a5C42992dECbae36851359345FE25997F5C42d";asyncfunctionmain(){console.log(proxyAddress," original Greeter(proxy) address");constGreeterV4=awaitethers.getContractFactory("GreeterV4");console.log("Preparing upgrade to GreeterV4...");constgreeterV4Address=awaitupgrades.prepareUpgrade(proxyAddress,GreeterV4);console.log(greeterV4Address," GreeterV4 implementation contract address");}main().catch((error)=>{console.error(error);process.exitCode=1;});
Note — You might want to run all the deployment scripts again if you face any issues while running this script.
Run the script —
yarn hardhat run scripts/GreeterV4Prepare.deploy.ts--network localhost
Results —
0xc5a5C42992dECbae36851359345FE25997F5C42d original Greeter(proxy) addressPreparing upgrade to GreeterV4...0x9E545E3C0baAB3E08CdfD552C960A1050f373042 GreeterV4 implementation contract address✨ Done in 2.56s.
Deploying all the contracts to Celo’s Alfajores Network
First, we need to add Alfajores RPC details inhardhat.config.ts
. Refer to this site for the latest RPC details —https://docs.celo.org/getting-started/wallets/using-metamask-with-celo/manual-setup
alfajores:{url:"https://alfajores-forno.celo-testnet.org",accounts:process.env.PRIVATE_KEY!==undefined?[process.env.PRIVATE_KEY]:[],},
Run the script and deployGreeter.sol
.
For your reference, here are the three deployed contracts —
0xeE5Dc1234Cdb585F54cc51ec08d67f6f43d0183B
— Implementation Contract0xc2E6c44e9eA569C6D1EfF7Ce0229Fb86a4A1d2d1
— ProxyAdmin Contract0xd15100A570158b7EEbE196405D8a456d56807F2d
— Proxy Contract
UpgradeGreeter
toGreeterV2
Run the following command to deploy theGreeterV2
. Make sure before running update theproxyAddress
inGreeterV2.deploy.ts
with your deployed proxy contract address.
yarn hardhat run scripts/GreeterV2.deploy.ts--network alfajores
Since we calledupgrades.upgradeProxy
in the deploy script, it did two things, deployed the new implementation contact and calledproxyAdmin.upgrade()
the method to link proxy and new implementation contract.
UpgradingGreeterV2
toGreeterV3
.
Run the following command —
yarn hardhat run scripts/GreeterV3.deploy.ts--network alfajores
UpgradingGreeterV3
toGreeterV4
.
InGreeterV4Prepare.deploy.ts
we had only calledprepareUpgrade
which only deploys the implementation contract. We will use a hardhat console to manually callupdate()
the method to link the proxy with the implementation contract. Run the following command to deploy the implementation contract.
yarn hardhat run scripts/GreeterV4Prepare.deploy.ts--network alfajores
Results —
0xd15100A570158b7EEbE196405D8a456d56807F2d original Greeter(proxy) addressPreparing upgrade to GreeterV4...0x8843F73D7c761D29649d4AC15Ee9501de12981c3 GreeterV4 implementation contract address✨ Done in 5.81s.
We will use a hardhat console to link the proxy with the implementation contract. Run the following command to start the console.
yarn hardhat console--network alfajores
For linking, we need two things —
- ProxyAdmin address
GreeterV4
contract factory instance
Run the following commands in the console —
> const GreeterV4= await ethers.getContractFactory("GreeterV4");> await upgrades.upgradeProxy("0xd15100A570158b7EEbE196405D8a456d56807F2d", GreeterV4);
This will point the proxy contract to the latestGreeterV4
.
Conclusion
Congratulations 💪 on making it to the end. I know this article is a bit long, but now you know how upgradeable smart contracts work under the hood. You have built and deployed proxy contracts in the local testnet and Alfajores testnet. If you have doubts or just fancy saying Hi 👋🏻, you can reach out to me onTwitter or Discord[0xViral (Celo)#6692].
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse