Reader small image

You're reading from  Hands-On Blockchain for Python Developers

Product typeBook
Published inFeb 2019
Reading LevelExpert
PublisherPackt
ISBN-139781788627856
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Arjuna Sky Kok
Arjuna Sky Kok
author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok

Right arrow

Creating Token in Ethereum

In this chapter, you are going to learn how to create a token on top of Ethereum. Tokens have a variety of uses; it may be for local currency in a local community, it may represent a physical good, it may be virtual money in a game, or it may be loyalty points. With this token, you can build a new cryptocurrency. While Ethereum is a cryptocurrency itself, you can build a new cryptocurrency on top of it. Ethereum makes it far easier to create a new token, and this fueled the creation of many new cryptocurrencies in 2017.

This chapter will cover the following topics:

  • How to create a simple token smart contract
  • ERC 20 (Ethereum token standard)
  • How to sell your token
  • How to customize your token

Token smart contract

Creating a token on top of Ethereum with Vyper is easy. Let's follow the initial steps to prepare our development environment before we build a token.

Start by ensuring that you have geth installed and that the geth program is in the $PATH environment variable (meaning that you can call geth without its full path):

$ virtualenv -p python3.6 token-venv
$ source token-venv/bin/activate
(token-venv) $ pip install eth-abi==1.2.2
(token-venv) $ pip install eth-typing==1.1.0
(token-venv) $ pip install py-evm==0.2.0a33
(token-venv) $ pip install web3==4.7.2
(token-venv) $ pip install -e git+https://github.com/ethereum/populus#egg=populus
(token-venv) $ pip install vyper
(token-venv) $ mkdir token_project
(token-venv) $ cd token_project
(token-venv) $ mkdir tests contracts
(token-venv) $ cp ../token-venv/src/populus/populus/assets/defaults.v9.config.json project.json

Add...

ERC20

More than likely, you will have heard of ERC20. When a new cryptocurrency is out, the first question that usually arises is—is it an ERC20 token? People assume incorrectly regarding the ERC20 token; they think it is a cryptocurrency based on Ethereum. Well, technically speaking, this is true, but it does not tell the whole story. ERC20 is a standard in Ethereum for creating a token. The simple token that we have just created does not fulfill the ERC20 standard. Yes, it is a digital token smart contract, but it is not an ERC20 token. ERC20 is one of many reasons why we have seen an increase in the number of new cryptocurrencies in 2017. However, ERC20 is not a requirement for creating a token on top of Ethereum.

To create an ERC20 token, you must implement the following methods:

function totalSupply() public view returns (uint256)
function balanceOf(address _owner...

Selling a token

Now that we have a token, it's time to sell the coins. We want to sell our custom token for ethers. A crowdsourcing token is very easy do create on the Ethereum platform compared to the Bitcoin platform. You already know how to create a method in a smart contract to accept ethers. You also know how to increase the token balance of some accounts. To sell tokens, you must combine those two things. That's all.

This is the core of the Initial Coin Offering (ICO). The currency of Ethereum is valuable. Although the price of ether fluctuates, 1 ether is valued at around USD 100. People would pay real money for some ethers, but not our custom token. To make our custom token worthy, we have to make it useful first, or at least make it appear useful. But to do that, we need capital. So why not sell some of our tokens (say 60%) to early adopters? They can then...

Stable coin

You have created a digital token that can be sold autonomously. However, you should not restrict yourself to the generic token. You could be more creative in your token smart contract by adding more methods to spice up your smart contract. What methods you should add are dependent on your smart contract's purpose. The token smart contract that is used in a game as currency will have different methods to the token smart contract that is used in a supply chain tracking system.

Let's create a stable coin smart contract. This is a token smart contract that is pegged to fiat money, such as the US dollar. We also want this smart contract to be a bank where we, as an owner, can freeze an account.

We can base our work on an ERC 20 token smart contract. We just need to add three methods—a method to freeze an account, a method to add some coins, and a method...

Summary

In this chapter, you have learned how to create a token on top of Ethereum. You used Mist an Ethereum wallet, to deploy the contract and interact with the token smart contract. Then, you implemented the ERC 20 standard in creating the token smart contract by creating implementations of certain methods. You also saw how these standard methods help Mist to recognize your token. Then, you created a method to sell tokens for ethers. You put a deadline in this smart contract and then you used a time travel method to simulate the expired deadline in the test of the smart contract. Finally, you added other methods to freeze and unfreeze other accounts. The example you used is a stable coin that pegs coins to real-world assets, such as fiat.

In the next chapter, you are going to create a cryptocurrency wallet that can handle ethers and ERC20 tokens.

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019Publisher: PacktISBN-13: 9781788627856
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok