Photo by Andre Francois on Unsplash

There are two types of cryptocurrencies, one is a coin, like Bitcoin, which operates on its own blockchain, and the other one is token that lives on top of an existing blockchain infrastructure like Ethereum. Through this post and tutorial, you will see how to create contracts on Ethereum blockchain to make your own tokens.

What is blockchain and how it works for Bitcoin?

On blockchain we have a block with data, and each block have to be approved by the network in order to be added to the blockchain. For example on the Bitcoin blockchain new block is calculated every 10 minutes. The block contains transactions people want to conduct. Just like in a bank transfer, transaction includes information about sender address (threat it like you bank account number), receiver address and amount to be transferred. Once the block is added to the blockchain it can’t changed, transactions are irreversible and public — we can say that blockchain is immutable and very often it’s called a distributed ledger.

Above process describes consensus algorithm. In the world of blockchain different implementations of this algorithm can be found. Just to name a few:

PoW — Proof of Work — Bitcoin / Ethereum

— Proof of Work — Bitcoin / Ethereum PoS — Proof of Stake — Ethereum (soon) / Neo

— Proof of Stake — Ethereum (soon) / Neo BFT — Byzantine Fault Tolerance — Ripple / Stellar / Hyperledger

If you’re interested in more details, I can highly recommend a series of Anders Brownworth videos on youtube:

However, on the Bitcoin blockchain, there are no smart contracts, which were introduced by Ethereum. This means you can run code on the blockchain that is being computed on the nodes of Ethereum network (decentralised super computer). The node is basically running on someone’s hardware using their computing power, and that is why you have to pay for each transaction that makes changes to the blockchain or computes data (reading data is free).

What is Aurity Coin and how we will use it?

For some time now we were discussing making Aurity Coin. We would give it to our employees where each one of us later could spend it on conferences, travel, food or anything else we decide. Here are the steps we used to make our coin that will help you to make your own.

Building a smart contract

In this part we will cover writing, testing and deploying the contract. To start coding contracts you can setup your environment, or use online Solidity compiler called remix. Through this, you can write and compile contracts online that you can run on test network later.

With the first line of the code, we define which version of Solidity we want to use. In our case its pragma solidity ^0.4.17 . On the next line, we specify that we are writing the contract and its name. Our contract will comply with ERC20 standard for Ethereum tokens.

Within the contract we define following constants:

string public name — name of the token — defined as public so it can be read by any user of the network.

— name of the token — defined as so it can be read by any user of the network. string public symbol — symbol of the token that identifies our token on the network. I.e OmiseGO token have its name defined as OmiseGO but its symbol is OMG.

— symbol of the token that identifies our token on the network. I.e OmiseGO token have its name defined as OmiseGO but its symbol is OMG. uint8 public decimals — how many decimals our token has. In most cases tokens use 18 decimals. In our case, to simplify code and calculations we set decimals to 0. uint8 is the definition for unsigned integer with the maximum size of 8 bits. That means it allows to store only positive number with maximum value of 2⁸=256

— how many decimals our token has. In most cases tokens use 18 decimals. In our case, to simplify code and calculations we set decimals to 0. is the definition for unsigned integer with the maximum size of 8 bits. That means it allows to store only positive number with maximum value of 2⁸=256 uint16 public totalSupply — The maximum number of tokens in circulation.

Mappings

To define the number of tokens each user poses and to keep a list of our employees, we will use mapping which is similar to hashmap in other languages. With the use of this data type we can map one value to another. In our case we need to define two mappings:

mapping(address => bool) public employees — mapping of user address to boolean values. If address is mapped with true value, that means the user is our employee and can use restricted contract functions.

mapping(address => uint16) public balances — mapping of user address to number of tokens that belongs to this user.

Our contract will contain following functions:

Constructor

function AurityCoin() public — The Constructor part of the code will be invoked when the contract is created on the network. Note that the name of the constructor must have the same name as the contract.

In the contract, we can access global object called msg . That gives us access to useful information about our contract. In the constructor we want to store msg.sender value, which is represented by address from where the contract was sent.

Right after, we set balance of the owner to total supply of the coin, so it can be distributed only by the owner of the contract.

isOwner

As the contract, we made is public we want to make sure that the owner of the contract could only invoke these functions. To accomplish that we wrote the modifier function named isOwner . The modifer is a keyword that allows to use the function in definition of other functions. It should be considered always when we need function to be reusable.

At the first glance the definition of this function might look weird, lets investigate it line by line.

modifier isOwner() {

require(owner == msg.sender); (1)

_; (2)

}

First line uses globally available function require which makes sure that the condition is met before executing next line of the code. In that case we check if function is invoked by the owner.

At the second line by using _ we want to say: take the body of the function that is used with this modifier function and paste it in the place of _

balanceOf

function balanceOf(address _address) public view returns(uint16) — returns the balance for specific address. It returns only the balance without changing the state of the contract, that is why we can mark the function with special keyword view .

transfer

function transfer(address _to, uint16 _value) public isOwner — a function for distributing tokens to users (employees). This function requires address of the user wallet and number of tokens we want to send. As we can see, it’s defined with isOwner modifier, hence it can be called only by the creator of the contract.

After invoking the function, we start by checking that the owner has enough tokens to transfer:

require(balances[owner] >= _value)

In the next step, the number of tokens should be decreased from the owner’s account:

balance[owner] = balance[owner] — _value

After the amount is taken from the owner’s account, we want to assign it to another user:

balance[_to] = balance[_to] + _value

The last step is to broadcast the transfer to the network. (More on that in next paragraph)

Transfer(owner, _to, _value)

Transfer event

event Transfer(address indexed from, address indexed to, uint16 value)

After the transfer is made we want to broadcast this to the whole Ethereum network so that the other services can listen to this event and react accordingly. In example Ethplorer is tracking this event and shows the data on its page. Because each transaction is public, we can easily see that transactions from one account to another one actually happen.

approveEmployee

As we don’t want everyone to use our contracts we required from users to register before they can get or spend any tokens. The list of users is stored in previously mentioned employees mapping.

Registered user needs to be approved by the owner of the contract. For that we are using approveEmployee function:

function approveEmployee(address _address) public isOwner {

employees[_address] = true;

}

pay

function pay(uint16 _value) public — If the user is registered and approved by admin, he/she will be able to buy something with tokens he/she has. ,This function requires that the user has enough amount of the token. Once user received the tokens from the owner and did the payment, we decrease the amount from his/her account and send that event to the network using the similar code we had for transfer function.

When you finish your contract you can test it on the test network. To test the contract we need a wallet that can interact with Ethereum network from within our browser. For that case, we have used MetaMask plugin for chrome. More information on how to use it for testing, you can find in my complete Video Tutorial below.

There are three options to run the script we wrote:

JavaScript VM Injected Web3 Web3 Provider

If you are using a VM and deploy the contract, each event or function that is stated on the blockchain will be invoked immediately. If however, you use Inject Werb3 or Web3 Provider you will have to wait for the functions and event to be processed (just like on the real network, it can take up to a few seconds). Every change on blockchain take some time to be processed and approved, and it requires some amount of money.

In the tutorial video below I covered additional questions like

Does the network validate my contract?

Are fraud actions possible on blockchain?

How is the price of the token defined?

Is there any certificate that states that my transfer function works well?

How could AurityCoin be used in real life?

Creating Your Own Token On The Ethereum Blockchain

After watching this tutorial, you should be able to understand how to create and deploy contracts made on Ethereum network and how to test and transfer coins you have. In the next part, I will show you how to make the admin panel for managing the transfer of the coins more easily.

Stay tuned!