Upgradeable smart contracts with ZeppelinOS

OpenZeppelin
OpenZeppelin

The biggest difference between the smart contract and other types of programs is that the smart contract cannot be changed once it is on the blockchain. This is the main advantage of blockchain technology. I'm going to introduce you to ZeppelinOS, a platform that allows you to create upgradeable smart contracts.

OpenZeppelin is well known for its expertise in developing smart contracts and provides many example programs for this purpose. This article mainly introduces how to deploy an upgradeable contract using zos.

Deploy a contract

First, install NodeJS and ganache-cli.

Install ZeppelinOS

npm install --global zos

Creating a new project

mkdir MyProject
cd MyProject

Initializing the project

npm init
zos init MyProject
npm install zos-lib

At this point, the contents of the MyProject folder are the same as when truffle init was run. There will be contracts, migrations folders, and truffle-config.js. The environment has been set up, and then the contract is written.

You will find the official example in contracts/MyContract.sol.

// MyContract.sol
pragma solidity ^0.4.24;
import "zos-lib/contracts/Initializable.sol";

contract MyContract is Initializable {
   uint256 public x;
   string public s;

   function initialize(uint256 _x, string _s) initializer public {
      x = _x;
      s = _s;
   }
}

Here is a brief explanation:

  1. Inherit Initializable

  2. There cannot be a constructor because it is not supported in the upgradeable smart contract. The function initialize is used as a constructor.

Add contract to the project:

zos add MyContract

Deploy contract on ganache-cli:

zos session --network local --from <address> --expires 3600

Set the network used by truffle, the user's address, and the validity period of the session (in seconds). Then run:

zos push

Zos Push is equivalent to migrating the contract to the chain with truffle. If you need to cast initial parameters (that is, initialize the call function) when deploying a contract, the deploy method looks like this.

zos create MyContract --init initialize --args 11,hello

--init: After deploying the contract, call a function to initialize it.

--args: The parameters to be brought into the function next, separated by "," and no spaces are allowed

Now, Zos has already deployed the contract, so it's just a matter of using truffle to interact with it. The next step explains how to upgrade the contract!

Upgrade a contract

We now add a new function increment() in MyContract.sol as:

pragma solidity ^0.4.24;
import "zos-lib/contracts/Initializable.sol";

contract MyContract is Initializable {
   uint256 public x;
   string public s;

   function initialize(uint256 _x, string _s) initializer public {
      x = _x;
      s = _s;
   }

   function increment() public {
      x += 1;
   }
}

Deploying the new contract:

zos push

Finally, update the contract as:

zos update MyContract

Woohoo!! You have just deployed the upgraded version of the smart contract.

It should be noted that the contract address will be the same, as the one created at the beginning (because it is the address of the proxy contract). To check the status of the contract, you can use the truffle console and then call the function directly to get its value.