What is ERC-20

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:
  1. // ----------------------------------------------------------------------------  
  2. // ERC Token Standard #20 Interface  
  3. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md  
  4. // ----------------------------------------------------------------------------  
  5. contract ERC20Interface {  
  6.     function totalSupply() public constant returns (uint);  
  7.     function balanceOf(address tokenOwner) public constant returns (uint balance);  
  8.     function allowance(address tokenOwner, address spender) public constant returns (uint remaining);  
  9.     function transfer(address to, uint tokens) public returns (bool success);  
  10.     function approve(address spender, uint tokens) public returns (bool success);  
  11.     function transferFrom(address from, address to, uint tokens) public returns (bool success);  
  13.     event Transfer(address indexed from, address indexed to, uint tokens);  
  14.     event Approval(address indexed tokenOwner, address indexed spender, uint tokens);  
  15. }  
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:
  1. contract TokenContractFragment {  
  3.     // Balances for each account  
  4.     mapping(address => uint256) balances;  
  6.     // Owner of account approves the transfer of an amount to another account  
  7.     mapping(address => mapping (address => uint256)) allowed;  
  9.     // Get the token balance for account `tokenOwner`  
  10.     function balanceOf(address tokenOwner) public constant returns (uint balance) {  
  11.         return balances[tokenOwner];  
  12.     }  
  14.     // Transfer the balance from owner's account to another account  
  15.     function transfer(address to, uint tokens) public returns (bool success) {  
  16.         balances[msg.sender] = balances[msg.sender].sub(tokens);  
  17.         balances[to] = balances[to].add(tokens);  
  18.         Transfer(msg.sender, to, tokens);  
  19.         return true;  
  20.     }  
  22.     // Send `tokens` amount of tokens from address `from` to address `to`  
  23.     // The transferFrom method is used for a withdraw workflow, allowing contracts to send  
  24.     // tokens on your behalf, for example to "deposit" to a contract address and/or to charge  
  25.     // fees in sub-currencies; the command should fail unless the _from account has  
  26.     // deliberately authorized the sender of the message via some mechanism; we propose  
  27.     // these standardized APIs for approval:  
  28.     function transferFrom(address from, address to, uint tokens) public returns (bool success) {  
  29.         balances[from] = balances[from].sub(tokens);  
  30.         allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);  
  31.         balances[to] = balances[to].add(tokens);  
  32.         Transfer(from, to, tokens);  
  33.         return true;  
  34.     }  
  36.     // Allow `spender` to withdraw from your account, multiple times, up to the `tokens` amount.  
  37.     // If this function is called again it overwrites the current allowance with _value.  
  38.     function approve(address spender, uint tokens) public returns (bool success) {  
  39.         allowed[msg.sender][spender] = tokens;  
  40.         Approval(msg.sender, spender, tokens);  
  41.         return true;  
  42.     }  
  43. }  
ERC-20 is available on Github.