Smart Contract Utilities with ZeppelinOS: Installation and Use

Prerequisites

An understanding of Solidity the programming language for smart contracts.

Truffle, a development framework for Ethereum to test and deploy smart contracts.

Ganache, a personal blockchain installed to test and run our smart contracts.

Installing

After installing Node.js we are now ready to install ZeppelinOS. Using terminal do the following:

Note: For Windows users, I recommend Powershell over Command Prompt.

npm install -g zos

That's it! We installed ZeppelinOSs.

Note: zos --help gives you a full list of all ZeppelinOS commands should you require them.

Creating our project

In the directory of your choice, create your project and then change to that directory:

mkdir first-project
cd first-project

Now we're going to create our project.json file to store the relevant data for the project. You will see a dialogue of properties to fill in. Fill them in if you wish or press enter to leave as the default.

npm init

To initialize as a ZeppelinOS project run the following:

zos init first-project

This command initializes a Truffle config file, two empty folders called contracts and migrations, and a zos.json file which has more information about the project in relation to ZeppelinOS.

The last step is to download the ZeppelinOS project library.

Note: You have to install this library with every project, you can't use it from project to project.

npm install zos-lib --save

Creating a Contract

After installing and initializing our project we are now ready to create our smart contract.

Open the project folder in an editor of your choice (I use atom) and notice that the file structure should look as follows:

This is a basic contract that initializes 3 variables: year, age and name. We will update it later to make it more useful.

In ZeppelinOS we use an initialize function instead of a standard constructor because this allows for the contract to be upgradeable. Initializable.sol is a contract we've imported from the zos library. It makes it possible to have the initialize function.

We see a mnemonic associated with our test account as well as details about our development blockchain. Now we're going to start a new session and test our contract. Open up another terminal and run the command below replacing {ADDRESS} with an address from Ganache.

zos session --network local --from {ADDRESS} --expires 3600

We've began a session with the local network. We're using a default sender address and we specify an expiry time for how long the session runs.

Our contract is ready to deploy.

zos push

Note: If you get a message at any point saying "A network name must be provided to execute the requested action" it means that our session expired. Run the zos session command from above and try again from where you left off.

That's it! Our contract is successfully deployed to the local network from the default address. If you look at your ganache terminal you will see details about the deployment. In our project folder there is a new file called zos.dev-"network id".json It contains all the information about your project on this network.

Upgrading

The contract we created is now deployed to our local network and we want to upgrade it. Normally this would be impossible but with ZeppelinOS we have the ability to do this.

Before we upgrade, to make sure there are no errors later on, we're going to compile our contract.

truffle compile

To begin we create an instance of our contract:

zos create FirstContract --init initialize --args 2019,19,Juliette

We re-initialize our contract through the initialize function and pass arguments to it. Thus we are initializing our contract to have the year2019, age19, and nameJuliette.

After creating our instance we to test it using our Truffle console.

npx truffle console --network local

Once the Truffle console is up do the following:

firstContract = await FirstContract.at('your-address')

The address you use is directly underneath the 'Instance created at sentence from the zos create command earlier. Run the following four separate commands: