In the previous lession (https://medium.com/@yenthanh/ether101-lession-2-create-a-simple-token-cryptocurrency-with-ethereum-c6c21fde2ea2) we already know how to put the first step in programming with Ethereum and create for us a simple token which can’t do much. In this lession, we will learn how to create a real token which can be used in real production.

Exchanging value between our TOKEN and ETHER
The token we created in the previous lession doesn’t have any value (unless someone accept to use the token for exchanging real thing), there are many ways to create value for your own token but it is about business problem. One of the simple way is to setup a relation between your token and a value that already has value — ETHER. So we will try to create a method which helps people exchange between our token and ETHER.
Let’s take a look at the below smart contract code:
In this contract, we are having 2 new function: buy
and sell
. The buy
function is a method to help people exchange from ETHER to your TOKEN, (i.e. buy your TOKEN using ETHER), and the sell
function is vice versa.
First let’s start with the buy
function, the function has a modifier function
named payable
which helps people to know that they can use this function to send ETHER to your contract’s address with the value stored in msg.value
. Then we can define a value for own TOKEN = bitCoinPrice
which is how much ETHER for 1 TOKEN. There is one important note: The value stored in msg.value
is not ETHER but WEI — the smallest unit in Ethereum, 1 ETHER = 10¹⁸ WEI. That’s why we need to define the value of our TOKEN bitCoinPrice
in WEI unit. So in the above example code, we set 1 TOKEN = 1 ETHER. Then the following logic is just calculating the number of TOKEN and do the logic as the transfer
function. Remember that this function send TOKEN from the contract to USER, so we need to make sure the contract has enough TOKEN to send to the buyer, the ETHER from the buyer is then belong to the contract.
The sell
function has the same logic but vice versa. We calculate how much ETHER must be sent to the seller and do the logic, we call function msg.sender.send
to send ETHER. Remember that we need to make sure the contract has enough ETHER to send to the seller.
This is just a simple demo how to define a value for our TOKEN. In the real product, the value of our TOKEN must be dynamic and being decided by voting methods or maybe some mystery financial system.
Now, open ETHEREUM WALLET and deploy our contract, note that we need to send all TOKEN value from the creator to the contract first. After that we can use the buy
and sell
function to exchange between our TOKEN and ETHER.
Manage events in the smart
You may see the above contract has defined a special event:
event Transfer(address indexed from, address indexed to, uint256 value);
This is a special event defined by Ethereum to help smart contracts notify about transfering events in Ethereum system. When ever a transfer event happen in our smart contract, we can call this event to notify Ethereum’s people about it. (Line 16, 26 and 36)
Inheritance in smart contract
People call programming in Ethereum is COP (Contract — oriented programming), just like OOP (you know what it is). Logically, our Smart contracts can have some similar behaviours as Classes.
Following the above example about creating a TOKEN which can be exchanged with ETHER. Now we are going to create a new contract with the same behaviour, but with a new function setPrices
to setup new price for the TOKEN, of course we only allow a specific persion (address) to call it. So let’s see how we can do that:
First we create a contract named owned
which some methods for managing ownership for our smart contract. You can see the modifier
function onlyOwner
.
modifier onlyOwner {
require(msg.sender == owner);
_;
}
This is how we can create a modifier
(such as payable
modifier we used above). You may notice the line with _;
. This is where the code of the function using this modified being inserted into. So you can see the onlyOwner
modifier will insert require(msg.sender == owner)
into our setPrices
function, which only allow the owner can call this function.
Now let’s deploy the contract, use the owner address to change the prices and playing with other functions to understand more about it.
ERC-20 TOKEN Standard
An ERC-20 TOKEN is a standard for all tokens implemented in Ethereum system, it has all the functions, properties which help Ethereum system can recognize the TOKEN and show it correctly to people. The ERC-20 TOKEN require below implementation:
If you are creating your own TOKEN in real product, or using the TOKEN for ICO purpose, you need to implement it follow this standard. This is the minimum requirement to make people believe that the TOKEN has a real value (but not a scam).
After that, you can create your own token, with your own functions and property. Bellow is an advanced implementation for a TOKEN with many functions. You may want to deploy it and play with it some more.
Now you already know how to create a real token in Ethereum, let’s create some interesting Token and use the power of Ethereum and Blockchain to make cool apps. In the next lessions, we will learn how to create own first ICO with the TOKEN we have created.
Feel free to comment and discuss with me about Ethereum and Blockchain. Cheers!