Open Zeppelin

OpenZeppelin is a library for writing secure Smart Contracts on Ethereum. https://github.com/OpenZeppelin/openzeppelin-solidity. OpenZeppelin is meant to provide secure, tested and community-audited code, however, nothing is 100% secure so make sure to thoroughly test and audit your code. Take a look in their contracts folder and become familiar with the code as they have become a standard for writing Smart Contracts. We will be using their implementation of an ERC20 Token.

To install the OpenZeppelin library, run the following in your Solidity project root directory:

npm init -y

npm install -E openzeppelin-solidity

Coding a Smart Contract:

Create a new file under /contracts and name it ERC20Token.sol , the .sol means it’s a Solidity file. Write the Solidity version we will be using at the top:

pragma solidity ^0.4.23;

Import the Open Zeppelin dependencies for our ERC20 Token:

import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

You will see an error with the import. Currently, the only way to make the Open Zeppelin import work with our Solidity extension is to update our preferences. This will likely be fixed in the future. In VS Code, type ctrl + comma to open User Settings and add this and save:

"solidity.packageDefaultDependenciesContractsDirectory": "",

"solidity.packageDefaultDependenciesDirectory": "",

Write the ERC20 Token contract:

/**

* @title ERC20Token

* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.

* Note they can later distribute these tokens as they wish using `transfer` and other

* `StandardToken` functions.

*/

contract ERC20Token is StandardToken { string public constant name = "ERC20Token"; // solium-disable-line uppercase

string public constant symbol = "ERC"; // solium-disable-line uppercase

uint8 public constant decimals = 18; // solium-disable-line uppercase uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals)); /**

* @dev Constructor that gives msg.sender all of existing tokens.

*/

constructor() public {

totalSupply_ = INITIAL_SUPPLY;

balances[msg.sender] = INITIAL_SUPPLY;

emit Transfer(0x0, msg.sender, INITIAL_SUPPLY);

}

}

We’re going to make it a simple ERC20 Token which includes a name, symbol, decimal, and initial supply. https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/examples/SimpleToken.sol.

Writing Tests

Next we’re going to write tests for our ERC20 Token. Create a new file under /test and name it ERC20Token.test.js :

const ERC20Token = artifacts.require('ERC20Token'); contract('ERC20Token', accounts => {

let token;

const creator = accounts[0]; beforeEach(async function () {

token = await ERC20Token.new({ from: creator });

}); it('has a name', async function () {

const name = await token.name();

assert.equal(name, 'ERC20Token');

}); it('has a symbol', async function () {

const symbol = await token.symbol();

assert.equal(symbol, 'ERC');

}); it('has 18 decimals', async function () {

const decimals = await token.decimals();

assert(decimals.eq(18));

}); it('assigns the initial total supply to the creator', async function () {

const totalSupply = await token.totalSupply();

const creatorBalance = await token.balanceOf(creator); assert(creatorBalance.eq(totalSupply));

});

});

Run the test:

truffle test

You will see the following output:

Contract: ERC20Token

✓ has a name

✓ has a symbol

✓ has 18 decimals

✓ assigns the initial total supply to the creator 4 passing (356ms)

Additional files

Create a new file in the root directory and name it .gitignore . We will use it to ignore our /build and /node_modules folders when committing to GitHub.

build/

node_modules/

Create a new file in the root directory and name it .gitattributes . This will be used to add syntax color to our .sol files on GitHub.