Creating a ZeppelinOS project

Let's create a new project from scratch and use the ZeppelinOS development platform to write an upgradable contract:

  1. Create the project folder:
$ mkdir project
$ cd project
  1. Under the project folder, initialize the project with the npm package. Follow the instructions and provide the required parameter when asked by the command. This will create a package.json file:
$ npm init
  1. Install the ZeppelinOS development platform command and the related files in the current project. This will install a zos command that we can use further:
$ npm install [email protected] [email protected]
  1. Initialize the project with the zos command, as follows:
$ npx zos init project v1

This command will create two new files, called zos.json and truffle-config.js. The zos.json file is the configuration file for zos in which all the contract-related details are kept.

The v1 parameter is the version name of the contract we are defining. The preceding command will create the zos.json file, which contains the following file contents:

{
"zosversion": "2.2",
"name": "project",
"version": "v1",
"contracts": {}
}

The content of the truffle-config.js file will be as follows:

module.exports = {
networks: {
local: {
host: 'localhost',
port: 9545,
gas: 5000000,
gasPrice: 5e9,
network_id: '*',
}
}
}

As you can see, the network name is local, and Truffle would look to connect to Ganache on localhost and port 9545.

The zos-lib library contains the library contracts related to contract upgradability.

Let's now create an upgradable contract, StableToken.sol:

import "zos-lib/contracts/Initializable.sol";
contract
StableToken is Initializable {

function initialize(
address _owner,
string memory _name,
string memory _symbol,
uint8 _decimals
)
public initializer
{
owner = _owner;
name = _name;
symbol = _symbol;
decimals = _decimals;

uint tokensToMint = 1000000 * (10 ** uint(decimals));
totalSupply += tokensToMint;
balances[owner] += tokensToMint;
}

//DO NOT USE THIS IN PRODUCTION
//Integer overflow possible

function transfer(address _to, uint _amount) public returns (bool) {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
return true;
}
}

The actual code file is relatively big; hence, we have only shown part of the code. For the full code, you can head to this GitHub link: https://github.com/PacktPublishing/Mastering-Blockchain-Programming-with-Solidity/blob/master/Chapter11/contracts/StableToken_v1.sol. The code is kept in the StableToken_v1.sol file. You can use the code and put it in your newly created project/contracts folder.

The StableToken contract inherits from the Initializable contract. The Initializable contract is defined in the zos-lib package, which helps in initializing upgradable contracts. This contract has a initializer modifier, which we are using in the StableToken contract.

As you can see in the preceding code, no constructor has been defined. It is a requirement of an upgradable contract with ZeppelinOS that the contract must not have a constructor defined. Instead, it should be replaced with an initialize() function. As you can see, the initialize() function is using the initializer modifier. This ensures that the initialize() function can be called only once.

The preceding code is prone to integer overflow attacks; hence this code should not be used in production. We are only using this code to showcase upgradable contract creation and upgrade it with a fix. We intentionally created the contract with a bug so that we could upgrade it with a fix, using the zos framework.

The code mints 1 million tokens when the contract is initialized, and a token holder is allowed to transfer the tokens to another person.

Now, let's add the contract to zos so that it can be made upgradable with the zos framework. To do this, run the following command:

$ npx zos add StableToken

The preceding command will register the StableToken contract to the list of contracts that will be upgraded via zos and will also update the zos.json file. As we have added the contract in the zos configuration file, let's deploy it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.53.32