Learning About ERC20 Standard Through GoCryptobot’s GCC Token
GCC is an ERC20 Token used within GoCryptobot for buying parts or participating in the PvP. As a blockchain game, users can send their GCC to an external Ethereum wallet. Furthermore, they can exchange their GCC for Ether.
What is ERC20 Token?
You can make a variety of apps on the Ethereum network. For example, just like GoCryptobot’s PvP, it is also possible to create an app that takes the user’s part information from the blockchain, calculates and returns the score, and even issues a currency like the Bitcoin. At this point, the currency issued on the Ethereum side is called a token. In order to use tokens as currency, users must be allowed to check the quantity of their tokens and send these tokens to other people. The tokens created for the ERC20 interface adopted by Ethereum as the standard are called ERC20 tokens. It must be implemented in accordance with ERC20 so that it can be compatible with Etherscan and Ethereum wallets, etc. GoCryptobot’s GCC is an ERC20 token that meets these criteria.
Firstly, ERC20 token’s standard interface is defined as follows:
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
There are methods for performing token functions, such as viewing balance and transferring. You can easily guess the method’s functionality just by looking at the method’s name and its respective parameters.
- totalSupply() : returns the total issued amount of tokens
- balanceOf(address tokenOwner) : returns the balance of tokenOwner
- allowance(address tokenOwner, address spender) : returns the number of tokens spender can take from the account of tokenOwner.
- transfer(address to, uint tokens) : sends tokens amount of tokens to address, to.
- approve(address spender, uint tokens) : grands the spender the right to take tokens amount of tokens from the account.
- transferFrom(address from, address to, uint tokens) : sends tokens amount of tokens from from to to.
- event Transfer(address indexed from, address indexed to, uint tokens) : event that occurs after transferFrom and transfer
- event Approval(address indexed tokenOwner, address indexed spender, uint tokens) : event that happens after approve.
There are two token transfer methods in ERC20. The first method is to use transfer(). With transfer(), you can instantly transfer the desired number of tokens to another user, and the receiving user can immediately see that the token has been added to his or her account.
In the case of transfer(), it is not a problem to send tokens to a user. However, there is a problem when that token is transferred in return for calling a smart contract. For instance, GoCryptobot’s exchange receives GCC from the user and exchanges it for Ether. If the user directly transfers GCC to the address of the exchange, the exchange will know how much GCC every user has sent and how much Ether to return to each respective user.
Using the approve() and transferFrom() functions specified in ERC20, you can solve the problems mentioned above. First, a user who wants to send tokens should use the approve() function to allow the spender to take any number of tokens as many times as the spender wants. Then the spender can use the transferFrom() function to get the amount of money he/she wants from the allowed amount.
Now, the exchange at GoCryptobot user can add the GCC amount to be exchanged into allowed pile, and the exchange will use transferFrom() function in order to exchange the amount of money that is in allowed.
The GoCryptobot exchange will be described in more detail in the next blog.
Let’s Take a Closer Look through the GCC Implementation
GCC consists of the following contracts. Each contract is modularized in a way that sequentially inherits other tracks in order. In other words, the contract called GoCryptobotCoinCore contains all the other contracts.
GCC has implemented ERC827 in a somewhat unusual way. ERC827 is an extended interface of ERC20 and includes the function to accept data with the parameters of approve() and transferFrom() and perform a contract call at the end of the function. With this feature, approve() and transferFrom() functions can be called sequentially in a transaction. I will explain ERC827 in detail in my next article when discussing GoCryptobot’s exchange.
Most codes related to ERC20 are derived from OpenZeppelin’s code.
contract GoCryptobotCoinERC20
contract GoCryptobotCoinERC827 is GoCryptobotCoinERC20
contract GOCryptobotCoinCore is GoCryptobotCoinERC827
GoCryptobotCoinERC20: A Contract that has All of ERC20 Interface Implemented
Just by looking at the contract’s name, you can tell that this contract is an ERC20 interface implementation.
string public constant name = "GoCryptobotCoin";
string public constant symbol = "GCC";
uint8 public constant decimals = 3;
This contains name, symbol, and decimals, which explains the token’s contract. Decimals represent the decimal point. Since decimals is 3 here, 1 Token equals 0.001GCC within the contract. Ether and other tokens generally have decimals of 18 digits. In order to prevent the token from disappearing due to floating-point errors, the blockchain expresses the decimal point by storing the token quantity as an integer only and specifying the decimals.
ERC20 has a function that takes the sum of the tokens and the user’s balance according to its specification.
The transfer function that transfers tokens to another user returns a success or failure value. If the address input is wrong or a larger amount of tokens than that of the balance is attempted to be sent, it will result in the failure of the require function.
require is used to check the user's input.If the require parameter is false, the remaining gas is returned and the transaction fails.
The transfer function can be implemented simply by subtracting _value from msg.sender’s balance and then adding it to _to. At the end of the function, the log is recorded by leaving the Transfer event.
library SafeMath {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
}balances[msg.sender] = balances[msg.sender].sub(_value);
We are using the sub() function of the SafeMath library to reduce the balance. If the balances of msg.sender are less than _value, the assert() within the sub() function will revert the previous state and the transactions will fail, so the balance can be safely deducted.
The transferFrom() and approve() functions mentioned earlier are also implemented as above.
The ERC20 standard specifies that approve() should be re-transmitted to change the allowed tokens through approve(). However, if approve() is re-transmitted, there is a possibility of an attack. Assume a situation in which a transaction that approve() 30GCC is released and then changed to 20GCC. In the moment you approve 30GCC, the spender can call the transferFrom() function to move 30GCC to the spender’s balance. The user does not know about this situation and calls approve() to change to 20GCC. Then the spender can use a total of 50GCC. In order to prevent this situation, ERC20 standard recommends that changing the allowed value to 0 before calling approve to change the GCC amount. When it comes to GCC, increaseApproval() and decreaseApproval() functions are implemented to provide safe editing of the allowed amount.
By using these two functions, one can stop potential attacks described above.
GCC has implemented the ERC20 interface splendidly, and has enhanced its security by using increaseApproval() and decreaseApproval(). It even includes ERC827, which further propels its usability.
The contents thus far could be summarized as follows:
- ERC20 has an approve / transferFrom function to send tokens to the Smart Contract.
- GCC uses approve when sending tokens to exchange contracts and it also has an extended functionality of ERC827.
ERC20 token sounds like something grandiose, but if you dig into it, implementation is quite simple and you can even create your own token by simply changing the token’s name of already implemented code. I believe that the effort that went into creating Ethereum’s ERC20 standard will have a positive impact in the DAPP ecosystem since anyone can easily create compatible currencies.
You can download GoCryptobot through the links below:
- App Store: https://apple.co/2KbMipn
- Google Play: https://bit.ly/2K5tYhx
(For the Korean version of this article, click here)