Upgradeable smart contracts are a desirable design practice given today’s extremely important and ever-changing landscape for smart contract security. In this article, we deploy a plain ERC20 contract, and later upgrade it to a mintable token contract to allow for the functionality of minting new tokens.
However, it is important to keep in mind that there are some key design principles to be followed while coding such contracts. On the surface, smart contract upgradeability may seem like a pretty cool feature to have. However, it violates a fundamental property of Blockchains — immutability. I will be creating a follow-up article which addresses this topic. But for now, let’s focus on the implementation alone.
We use ZeppelinOS to achieve our task.
Refer to the diagram below for a simple reference on how ZeppelinOS achieves upgradeability. I’m not going to delve deeper into the technical aspects as they already have pretty good documentation in place.
Let’s get into writing code. For this deployment, I’m going to use Ganache as my local Ethereum node. I’m also going to assume you already have node.js installed on your system.
- Install ZeppelinOS using the command below
npm install --global zos
If you are on Windows, and you’re running into issues with node-gyp, check out this guide.
2. Let’s say we’re creating the project demo-token. Create a folder and initialize the project.
mkdir demo-token && cd demo-tokennpm init zos init demo-token
This will create a truffle-config.js file along with a zos.json.
Install the ZeppelinOS libraries and link the standard libraries with the following commands
npm install zos-libzos link openzeppelin-zos
3. Now let’s write that upgradeable smart contract. In the contracts, folder create a new file DemoToken.sol with the contents provided below