If you have been following the previous chapters, you should now have a pretty good understanding of Ethereum Smart Contracts and how to interact with them through the web3.js APIs.
One exciting feature of Ethereum Smart Contracts is Ethereum tokens. Tokens are digital assets built on top of the Ethereum blockchain. Using tokens, developers can use it to pay for services performed by Smart Contracts, as well as use it as a mean for fund raising. Tokens also drive demands for Ether, the native cryptocurrency on the Ethereum blockchain.
In this chapter, you will learn what tokens are, how they are created, how to buy them, and how to add them to your MetaMask accounts.
What Are Tokens?
The stalls need not deal with cash; this will prevent the owner of the stalls (usually employees of the carnival owner) from pocketing the cash.
The owner of the carnival receives cash up front before you even play the games; and all unused tokens cannot be refunded.
The owner of the carnival wants to sell you more tokens than you need by giving you incentives to buy more up front.
Coins vs. Tokens
In the cryptocurrency world, there has been two terms that have been used interchangeably – coins and tokens. So are they the same? First, the definition of a coin is that it is an asset that is native to its own blockchain. Examples of coins are Bitcoin, Litecoin, and Ether. Each of these coins exists on their own blockchain. Tokens, on the other hand, are created on existing blockchains. The most common token platform is Ethereum, which allows developers to create their own tokens using the ERC20 standard (more on this in the later section of this chapter). Using Ether (which is the coin native to the Ethereum blockchain), users can exchange them for specific tokens on the Ethereum blockchain.
Hence, strictly speaking, coins are not the same as tokens. In fact, tokens are based on coins.
How Tokens Are Implemented?
Now that you have a clear understanding of tokens and how they work, let’s see how tokens are implemented in Ethereum.
Minting New Tokens
Burning Tokens
Units Used Internally in Token Contracts
Internally, for a token with n-decimals of precision, the balance is represented using this formula: token_internal = token_external * 10n
For example, a user may own 4.497 GoldToken, but internally, the token contract stores its balance as 4497.
ERC20 Token Standard
In order for tokens created on the Ethereum blockchain to be accepted by Smart Contracts, tokens must adhere to some particular standards. In the case of Ethereum tokens, that standard is ERC20. ERC stands for Ethereum Request for Comments. In ERC20, the number 20 refers to the proposal ID number. The proposal ERC20 defines a set of rules which need to be met for a token to be able to interact with each other.
Tip
ERC20 standard is a specific set of functions developers must use in their tokens.
Get the total token supply
Get the account balance
Transfer the token from one account to another
Approve the use of token as a monetary asset
totalSupply – Returns the total token supply.
balanceOf(address _owner) – Returns the account balance of _owner.
transfer(address _to, uint256 _value) – Transfers _value to _to and fire the Transfer event. The function should revert if the _from account does not have enough tokens to spend.
approve(address _spender, uint256 _value) – Allows _spender to withdraw from the account several times, up to the _value amount.
transferFrom(address _from, address _to, uint256 _value) – Transfers _value from _from to _to and fire the Transfer event. The function should revert unless the _from account has deliberately authorized the sender of the message via some mechanism.
allowance(address _owner, address _spender) – Returns the amount which the _spender is still allowed to withdraw from the _owner.
Transfer(address indexed _from, address indexed _to, uint256 _value) – Must trigger when tokens are transferred, including zero-value transfers.
Approval(address indexed _owner, address indexed _spender, uint256 _value) – Must trigger on any successful call to approve(address _spender, uint256 _value).
Creating Token Contracts
As mentioned in the earlier section, to create tokens, you need to create a token contract. Fortunately, you can make use of some standard token contract that is already written for you. For our example, we are going to write our token contract
based on the contract by Token-Factory ( https://github.com/ConsenSys/Token-Factory/tree/master/contracts ).
Paste the following token contract into the Remix IDE:
Tip
You will be able to download the following token contract from the Apress support web site for this book.
Caution
For this contract, you need to use the older 0.4 Solidity compiler.
The preceding statements specify the price of the token (10 tokens for 1 Ether). It also specifies that the raised ETH would go to the owner (the one who published the contract).
The payable() function is called when Ether is sent to the contract. It converts the Ether sent to tokens and credit the tokens to the respective buyers. It also sends the Ether sent to the contract to the owner of the contract.
Deploying the Token Contract
A total of 2000 tokens
18 decimal places of precision
Token Name of “Lee Token”
Token symbol of “LWM”
For my example, the address of my token contract is 0x4c5167a4df68f05722d0afdf57d734a575d6744c.
Adding Tokens to MetaMask
Buying Tokens
When the token contract is deployed, all the initial tokens would belong to the account that deployed the token contract (which is Account 1 in this example). If a user wants to buy tokens, all he needs to do is to send some Ethers to the token contract, and the token contract will automatically transfer some tokens to his account (all balances are maintained by the token contract). For this example, we shall get a user (Account 2) to buy some tokens.
Before you buy any tokens, take note of the balance of Account 1 (which you have used to deploy the token contract). For my own record, my Account 1 at this moment has 7.8327 Ethers.
Tip
When Account 2 sends some Ether to the token contract, the Ethers will be credited into Account 1 (who owns the tokens).
Creating an ICO Page
Now that you have created a token, it is time to spread the word and get people to buy it. In the cryptocurrency world, an Initial Coin Offering (ICO) is a fundraising event that allows companies to raise funds through the sale of their coins (or tokens).
Tip
Be sure to replace the token contract address with that of your own. The abi variable contains the ABI of the MyToken contract.
Summary
In this chapter, you learned how tokens work and created one yourself using a token contract. You also learned how to add tokens to your MetaMask account and how you can buy tokens by sending Ethers to the token contract. Finally, you also learned how to display a page showing how much a token has sold.