ICO Procedure — “Ethereum ERC20” — through OpenZeppelin

Eun Woo Nam
RayonProtocol
Published in
7 min readJun 27, 2018

Since Vitalik Buterin (Russian-Canadian programmer and writer primarily known as the co-founder of Ethereum and as the co-founder of Bitcoin Magazine) published the official manual of Ethereum, Ethereum has been spotlighted within the Blockchain community. As a result, many blockchain-based projects have been issuing prolific “Coins” based on Ethereum’s ERC 20 standard.

“Truffle” is an integrated development environment for Ethereum-based Smart Contract projects, and “OpenZeppelin” is an open-source library — which includes several ICO related contracts.

In this Article, we are going to explore how a blockchain project utilizes Truffle and OpenZeppelin for a successful ICO process in the Ethereum Testnet.

During the ICO process that we are going to walk through, a new ERC 20-based “MyCoin” will be registered and distributed. When Ethers are sent to the Ethereum Smart Contract, MyCoins will be delivered to the crowdsale buyers.

Set-up Environment

Install MetaMask

For Ether-related transactions, we used MetaMask (which is currently available in the Chrome Web Store).

# Truffle Install/Initialization
npm i -g truffle
mkdir my-ico && cd my-ico
truffle init
# Initialize Yarn
yarn init -y
yarn add -E zeppelin-solidity
yarn add truffle-hdwallet-provider

At first, truffle has to be installed globally, and it should be initialized in a newly created directory for the ICO. ‘init’ will automatically generate contracts, migrations and test folders as well as ‘truffle.js’ and ‘truffle-config.js’.

Then, yarn has to be initialized through the ‘init’ command, and ‘zeppelin-solidity’ as well as ‘truffle-hdwallet-provider’ have to be installed.

  1. When installing Zeppelin-solidity, ‘-E’ option will enable the folder to directly import files from the Smart Contract via the “zeppelin” path.
  2. In case of using Python 3.0 for the above, OpenZeppelin might not be successfully installed. So, please use the command below.
$ npm config set python python2.7

We will explain how to use each module and functionality — in the following chapters.

Smart Contract Implementation

Token — MyCoin

Here is the structure that we designed for MyCoin. Thanks to the code from OpenZeppelin — from ERC20Basic to MintableToken — , we simply inherit them to implement a custom coin. In other words, we can customize a coin through a few lines of code — avoiding any convoluted Validations.

  1. ERC20Basic(interface) : Total quantities, remaining balances in each account, delivery size
  2. ERC20(interface) : Approval of transmit mandate, confirmation of quantities that are available for transmit mandate
  3. BasicToken : Implementation of ERC20Basic
  4. StandardToken : Implementation of ERC20
  5. MintableToken : Issuing token before the deadline, adding to the total quantity
  6. Ownerble : History of contract owners, general authentication process

Above is the source code. As the parent Contract possesses all the essential functionalities (including transmit and mandate through name, symbol, and decimals), no heavy lines of code are needed. A user defined token can be generated with only 3 lines of code.

This code should be placed under the ‘my-ico/contract’ directory.

Token Sale — MyCrowdsale

MyCoinCrowdSale is also created through the inheritance from OpenZeppelin’s CrowdSale. It happens when calling the parent’s constructor and a Contract has been generated through establishing an internal variable.

  1. Crowdsale : Token, Ratio, Wallet (Constructor components), atoken has been offered when calling ‘buyTokens’
  2. TimedCrowdsale : Time limitation
  3. MintedCrowdsale : Token offered through calling buyTokens

As previously explained, MyCoinCrowdsale constructor makes the Crowdsale process through the following properties:

  1. openingTime : Beginning time of Crowdsale (no earlier than the current time)
  2. closingTime : Crowdsale closing time
  3. rate : Ether/Token Ratio (e.g. rate 5 means 5 Ethers are equivalent to 1 token)
  4. wallet : A wallet address that Ether will be delivered to (Ether sent from investors)
  5. token : Custom token address that a user created

This Codes should also be placed under the ‘my-ico/contract’ folder.

Release — Smart Contract

Several set-ups are required to distribute the Smart Contract inside Truffle. A series of files will be released in ascending order, starting from index 1. The 1st is a default set-up file — so, we are going to create ‘2_depoy_contract.js’ that will be functioning afterwards.

Above is the source code. MyCoin should be released first as it will be passed (as a parameter) to MyCoinCrowdsale. Then, MyCoinCrowdsale will be distributed after the beginning time, closing time, ratio and wallet address are assigned accordingly.

The parameters are the same as the ones for MyCoinCrowdsale. Additionally, we printed a log for checking the time and behavior of the start time.

  1. startTime : Beginning time of Crowdsale (no earlier than the current time)
  2. closeTime : Crowdsale closing time
  3. rate : Ether/Token Ratio (e.g. rate 5 means 5 Ethers are equivalent to 1 token)
  4. wallet : A wallet address that Ether will be delivered to (Ether sent from investors)

As the start time for the Crowdsale should not be earlier than the current time, we added 3 minutes to meet the requirement.

Set-up for Release Network

For us to release Coins to the Testnet, Ropsten’s full nodes are needed. Yet it takes a lot of time and memory, so we are going to use ‘Infura’ which provides them through api. Below is a screen capture for registering Infura.

Register: Infura Main, Click “Get strated for free”

The registeration process is fairly simple — Infura needs only general information such as email, password, etc.

api key that is delivered to our email

With registration completion for Infura, an API key will be delivered to your email. This API should be remembered for later codes.

The Infura API key and a mnemonic (for later release of a new account) are to be stored in the variable. Then, set up network properties along with ‘development’ (development node) and ‘Ropsten’ (Testnet).

  1. provider: provides web3 that functions as a HD wallet, which is later used to sign transactions (from twelve different mnemonic addresses).
  2. gas: gas limit
  3. gasPrice: gas price
  4. network_id : Unique network id. [1 for MainNet, 2 for Kovan, 3 for Ropsten , 4 for Rinkeby].

These values can be set through Truffle — Network Option

When implementing codes for actual release to the MainNet, the mnemonics and API should be confidential and kept outside of the main code.

# Compile and Release
truffle compile && truffle migrate --network ropsten

To complete all set-up processes, let’s compile the Contract and release it. (Please use the above command)

Please note that a sufficient amount of Ether should be available in the account to pay a fixed fee. In case of insufficient Ether, you can get it for free from Ropsten faucet (http://faucet.ropsten.be:3001/).

Writing artifacts to ./build/contractsUsing network 'ropsten'.Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x065a670d4d13f1aab7e8404b63c959cee5424529418059e0eab4a27ca6b0102d
Migrations: 0x22d3ae685bdf627bdd4bcb2ede6f5eafb586a8b2
Saving successful migration to network...
... 0xd53b7a95a4451f43c2df59ce6ea1fec3b18d1b220db0893576420e03470d9a3d
Saving artifacts...
Running migration: 2_deploy_contracts.js
Running step...
Deploying MyCoin...
... 0x012f05d5b933bf6536339fe8c5ec7b7eb7fb95906de8be148d678125b8a0431a
MyCoin: 0x58155c5f41f71511e64179d73559fa323a2ccbe9
startTime 1529372295
Deploying MyCoinCrowdsale...
... 0x5821140355321f2d1ea6286c7017540b2b32f84be0d5a471bbfae4183e9e6cb3
MyCoinCrowdsale: 0x5d2c1f918816c2938c13dafe52cc5c4c7dbd3983
Saving successful migration to network...
... 0xc627381d4628962adb02d976674f09d472173aaff285fc510a795873ea7f552f
Saving artifacts...

The above is the result. The lines in bold are the Contract addresses (the one above is the address for the Coin and the other is for the CoinClowdsale). These are to be remembered for later use (e.g. function calls).

Crowdsale Management

Set MyCoin’s Owner as MyCoinCrowdsale

MyCoinCrowdsale, inherited from MintedCrowdsale, has a method called ‘_deliverTokens’ and it issues tokens on-demand. In this process, the owner of MyCoin should be MyCoinCrowdsale so that it can call MyCoin.mint.

Go to (https://www.myetherwallet.com/) and access the Contract tab.

Connecting to MetaMask

Select the account that released the Smart Contract in MetaMask. Then connect MyEtherWallet and MetaMask. Now we can call internal functions.

Enter Contract Information

Next, enter the MyCoin Contract address that we’ve obtained as well as API in myCoin/build/MyCoin.json — and click “Access”.

transfer ownership

We are going to run ‘transferOwnership’ to transfer the right to release tokens to Crowdsale. At the very bottom of the page, please select transferOwnership in the drop-down menu, and input the MyCoinCrowdSale address as the new Owner.

ICO Participation

Convert MetaMask into a virtual Investor

Convert MetaMask into a virtual investor. We can transmit Ether by pressing the “SEND” button.

Enter the MyCoinCrowdsale address and the amount of Ether to be sent. Click “Next”. For testing purpose, we transmit 0.2ETH (which is equivalent of 1 token; 1–5 Ratio).

Information on the transaction can be found in MetaMask. The (…) symbol and the picture shows the result of the transaction.

We are not using MyEtherWallet as fallback functions are already deployed internally — it is enough to make a transaction through MetaMask (‘buyTokens’ function is to be called).

Result

Token Addition

You can add a custom token (after the completion of a transaction) by clicking the “Add Token” button in Token tab. When entering the MyCoin Contract address, symbol and decimal will be automatically added.

Result

We confirmed that 1MCO has been added to the virtual investor’s account (5: 1 ratio with 0.2ETH).

In sum, we’ve explored the ICO process cycle that is to be released in the TestNet.

--

--