ERC-20, stands for Ethereum Request for Comment, and 20 is the number that was assigned to this request, is a standard to build smart contract on the Ethereum blockchain for implementing tokens.
ERC-20 standard based tokens must follow a list of rules to create digital tokens before they can be available to public. ERC-20 standard was proposed on November 19, 2015 by Fabian Vogelsteller. It defines a common list of rules that an Ethereum token has to implement, giving developers the ability to program how new tokens will function within the Ethereum ecosystem.
The ERC20 Token Standard Interface
Following is an interface contract declaring the required functions and events to meet the ERC20 standard:
-
-
-
-
- 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);
- }
How Does A Token Contract Work?
Following is a fragment of a token contract to demonstrate how a token contract maintains the token balance of Ethereum accounts:
- contract TokenContractFragment {
-
-
- mapping(address => uint256) balances;
-
-
- mapping(address => mapping (address => uint256)) allowed;
-
-
- function balanceOf(address tokenOwner) public constant returns (uint balance) {
- return balances[tokenOwner];
- }
-
-
- function transfer(address to, uint tokens) public returns (bool success) {
- balances[msg.sender] = balances[msg.sender].sub(tokens);
- balances[to] = balances[to].add(tokens);
- Transfer(msg.sender, to, tokens);
- return true;
- }
-
-
-
-
-
-
-
- function transferFrom(address from, address to, uint tokens) public returns (bool success) {
- balances[from] = balances[from].sub(tokens);
- allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
- balances[to] = balances[to].add(tokens);
- Transfer(from, to, tokens);
- return true;
- }
-
-
-
- function approve(address spender, uint tokens) public returns (bool success) {
- allowed[msg.sender][spender] = tokens;
- Approval(msg.sender, spender, tokens);
- return true;
- }
- }
ERC-20 is available on
Github.
References:
https://theethereum.wiki/w/index.php/ERC20_Token_Standard