[Beginner Guide] Write and deploy your first ICO smart contract under 30 minutes (stage 1)

Written by jeffhu | Published 2018/06/06
Tech Story Tags: ethereum | blockchain | cryptocurrency | ico | ico-smart-contract

TLDRvia the TL;DR App

Write a smart contract that can issue tokens and transfer tokens

Image credit to bitcoinmagazine.com

This super simple tutorial will walk you through very basic steps of writing a smart contract that can ISSUE TOKENS, like an real ICO, and transfer tokens. Though it is not ERC20 compatible, which makes it insufficient to do an legal ICO so far. This is still a guide that can get you started pretty fast.

Things you need

Go visit the place where you write codes

Visit Remix compiler from your Chrome browser, which is a great online compiler that you can write smart contracts, deploy contracts, and interact with contracts.

Remix compiler (http://remix.ethereum.org/)

Start writing your first smart contract!

Today we will be writing a simple smart contract that has only 4 features:

  • Data: Record the token balances of each user
  • Method: Check the token balance of a specific user
  • Method: Issue all the tokens
  • Method: Transfer tokens from one user to the other user

Now, in the white box of Remix, delete everything and let’s start from fresh.

White box is right there circled by the green border

Step 1:

Specify the solidity version:

pragma solidity ^0.4.21;

Solidity is the programming language that we use to write smart contract. And we are writing in Solidity right now! Here, we have specify the solidity version to be 0.4.21, with has the compatibility up to 0.4.x (x > 21). Don’t worry if you cannot understand by this stage.

Step 2:

Create the contract body:

contract Token {// Write more codes here}

We create a smart contract with a name “Token”.

Step 3:

Now we are getting to the first feature. Data: Record the token balances of each user. We need a dictionary in the type of mapping to record the balances for each of the users.

// Write this inside the "contract Token" body

mapping (address => uint256) private balances;

This line declare a variable with the type mapping , which can take an address of a user, and return a uint256 value of that user’s token balance. So it’s like an dictionary that you look for an address to get its corresponding token balance. The address type stores a user or a deployed smart contract. The uint256 type stands for unsigned integer (256 bits) , which is simply a integer number holder.

The private keyword hides this variable from being accessed externally. The balances is just the name of the variable we are creating. Lot’s of things to catch up. But I promise you that it will be fun!

Step 4:

We have completed our very first feature. And now we are moving on to the second cool feature — Method: Check the token balance of a specific user.

Please add the following code below the mapping , but still inside the Token contract body:

We are creating a function with the name getBalance . This function asks for a input variable with type address that named _account (which is basically the user). And it returns a output answer with type uint256 (the token balance of that user). The function is public and constant , which opens to public access and can ONLY check the info from blockchain without changing them.

Inside the function, we return the answer by providing balances (the address book) with the _account (user address). It will return the token balance of that specific user _account .

Step 5:

Now we want to be a bit selfish by storing all the ICO tokens to our own wallet right after the deployment of the smart contract (launch of th ICO). By reaching that, we are now building the third feature — Method: Issue all the tokens.

Add the following code below the second feature:

This is a Constructor function, which will only be triggered once right at the moment of deployment. The name is Token , which must be the same as the smart contract created. The function takes in _initialSupply with the type of uint256 (the issue amount of the tokens! As much as you wish!).

Inside the function, we are setting the balances of the msg.sender to be the initial supply that I have just set! But who the hack is msg.sender ? It’s the user address who will be deploying the contract and .. it’s YOU! You now have all the tokens. :)

All the codes we have so far

Intermission wrap up

Now we have a smart contract that can:

  1. Store all the token balances of all the users
  2. Check the token balance of a specific user
  3. Issue a specific amount of tokens at start and send all to you!

Well done! That is super fast huh?

We will carry on to finish up the last feature — Method: Transfer tokens from one user to the other user. And show you how we gonna deploy and try out the ICO smart contract via Remix and Metamask.

Second guide is ready here!


Written by jeffhu | CEO @ Turing Chain, TuringCerts | Blockchain Scholar @ UC Berkeley | Fellow @ Clinton Foundation
Published by HackerNoon on 2018/06/06