ICO Smart Contracts – Part 1

September 13, 2018

Author: Akarsh Agarwal

Tutorial - MLG Blockchain

With the dawn of Blockchain applications, many new companies prefer raising money through an ICO (Initial Coin Offering) rather than an IPO (Initial Public Offering) or VC (Venture Capitalists). The main reason being that the founder of the company retains their equity and is still are able to raise funds for the development processes.

Although ICOs are popular, there are some nuances which needs to be taken care of while having an ICO. Some of the most important include, selecting the Blockchain platform to hold the funds and writing the token and crowdsale smart contracts. The Ethereum Blockchain is currently the most prominent platform for companies to conduct an ICO.

With that in mind, we are publishing a 3-phase article, which describes the cycle of the CrowdSale Token Smart Contract (SC). We will delve into details of how to write a basic, but secure CrowdSale Token SC.

So, let’s get started!!

Using the ERC20 Token Contract As The Main Interface

Most of the tokens available on the Ethereum Blockchain are ERC20 tokens. These tokens have some pre-defined interfaces where they interact using functions for basic operations like transfer tokens, getting the balances, etc. So, we have used the same interface for our implementation too.

We have also used some of the contract published by Open-Zeppelin community to ensure a secure and safe smart contract for token sale. So, I would suggest you to go through ERC20 interface and Open-Zeppelin Package on Github, via the following links. Also, the source-code of the contracts used in the tutorial below is also present on the Github for access.

  •  https://github.com/ethereum/EIPs/issues/20
  •  https://github.com/OpenZeppelin/zeppelin-solidity

You can also find a link to the complete contract code from this example on our GitHub

  • https://github.com/MLG-Blockchain/Standard-ico-crowdsale

Creating the Token Related Functions

Now that we have looked at the basic interfaces we are going to use below, lets outline what we are trying to achieve here. We are trying to declare a generic token contract, which can be used by any ICO as a foundation for their own token.

Below is a snippet of the code, which describes some of the variables, essential to define the unique identify of the token:

string public tokenName; // Defines the name of the token.
string public tokenSymbol; // Defines the symbol of the token.
uint256 public decimals; // Number of decimal places for the token.

Explanation of the variables:

tokenName: This is the name of your unique Token. It can be single/multiple words, like United State Dollar.tokenSymbol: This is an abbreviation for your token, something like USD. decimals: The number of decimal places allowed for your token, like 2 decimal places for USD.

Now, let’s move onto the next snippet of the code:

function Token(string _name, string _symbol, uint256 _initialSupply, uint256 _decimals){
require(_initialSupply > 0);
tokenName = _name;
tokenSymbol = _symbol;
decimals = _decimals;
totalSupply = _initialSupply ** decimals;
}

This is a constructor function call, which instantiates the variables, along with the Total Number of Tokens existing for that application.

Next, we move to executing the transfer tokens function, where we store the amount of ETHs invested by the Investor, so as to issue corresponding amount of tokens to the recipient.

function transferTokens(address _recipient, uint256 _value, uint256 _ratePerETH) returns (bool) {
uint256 finalAmount = _value.mul(_ratePerETH);
return transfer(_recipient, finalAmount);
}

There is a variable, _ratePerETH, which specifies, how many tokens can be bought per Ether. This is required, as this enables us to set different prices for the Pre-Sale and ICO Sale Period. This will ensure that we store only the total money invested by the individual and not the number of tokens, as they may have been bought at different rates.

Next, we have a function which when called, refunds the amount of money invested by the caller. This is required, if the company was not able to raise at least the minimum required Ethers during the ICO.

function refundedAmount(address _recipient) returns (bool) {
require(balances[_recipient] != 0);
balances[_recipient] = 0;
return true;
}

We first check that the balance of the caller is already not equal to 0. If it is, then it means that either caller has already received the refund or is calling the same function to get an extra refund. Then we re-initialize the amount invested as 0, to avoid any further double redeeming of the balances, via repeated calling.

Now, these above functions are just the tip of the iceberg which we define to uniquely identify our token on the Blockchain. Next, we’ll move towards writing a real world CrowdSale application SC, taking care of nuances and incorporating some of the basic functions of the ICO into it.

About MLG Blockchain

MLG Blockchain is a global blockchain consulting and development firm headquartered in Toronto with a distributed team across North America, Europe and Asia that is focused on building next generation applications using blockchain and smart contract technology. We speed up your team’s understanding of the blockchain and its potential opportunities for your business and help you to create a blockchain strategy you can use today.

If you are part of the executive team of a new or existing token, please reach out at hello@mlgblockchain.com to be profiled.