What is ERC20 token

There are a lot of ETH tokens on the market, they all comply with REC20 protocol, so we need to know what REC20 protocol is.

An overview of the

Tokens represent digital assets and have value, but not all conform to specific specifications.

Erc20-based currencies are more interchangeable and work equally well on Dapps.

The new standard could make tokens more compatible and allow for other features, including vote tokenization. The operation is more like a vote operation

Token holders have full control over the assets, and erC20-compliant tokens can track how many tokens anyone has at any one time. Eth contract based sub-currency, so easy to implement. You have to transfer it yourself.

Standardization is very beneficial, meaning that these assets can be used across different platforms and projects that would otherwise only be used in specific situations.

ERC20 Token Standard (Github)

The preface

EIP: 20

Title: ERC-20 Token Standard

Author: Fabian Vogelsteller
[email protected], Vitalik Buterin [email protected]

Type: Standard

Category: ERC

Status: Accepted

Created: 2015-11-19

conclusion

Token interface standard

abstract

The following standards allow markup apis to be implemented in smart contracts. The standard provides basic functionality for transferring tokens and allows tokens to be approved so that they can be used by another online third party.

motivation

The standard interface allows any token on Ethereum to be reused by other applications: from wallets to distributed exchanges.

The rules

Token

methods

Note: The caller must handle returns (bool success) that return false. The caller must never assume that a return of false does not exist.

name

Return the name of the token, such as “MyToken”.

Optional – This method can be used to improve availability, but interfaces and other contracts cannot expect these values to exist.

function name(a) constant returns (string name)Copy the code

symbol

Returns the symbol of the token, such as HIX.

Optional – This method can be used to improve availability, but interfaces and other contracts cannot expect these values to exist.

function symbol(a) constant returns (string symbol)Copy the code

decimals

Returns the number of decimal places to which the token is used, such as 8, indicating that the number of tokens allocated is 100000000

Optional – This method can be used to improve availability, but interfaces and other contracts cannot expect these values to exist.

function decimals(a) constant returns (uint8 decimals)Copy the code

totalSupply

Returns the total supply of tokens.

function totalSupply(a) constant returns (uint256 totalSupply)Copy the code

balanceOf

Return the account balance for the account with address _owner.

function balanceOf(address _owner) constant returns (uint256 balance)Copy the code

transfer

Transfer _value token quantity to address _to, and Transfer event must be triggered. If the _from account balance does not have enough tokens to spend, the function should be thrown.

The token contract that creates the new token should set the _FROM address to 0x0 when the token is created to trigger the transport event.

Note that the transfer of a value of 0 must be treated as a normal transfer and trigger the transfer event.

function transfer(address _to, uint256 _value) returns (bool success)Copy the code

transferFrom

A Transfer event must be triggered to send tokens of value _from to address _to.

The transferFrom method is used to extract the workflow, allowing the contract to transfer tokens on your behalf. This can be used for example to allow contracts to transfer tokens on your behalf and/or charge fees in sub-currencies. The function ** should **throw, except that the _from account has intentionally authorized the sender of the message through some mechanism.

Note that the transfer of a value of 0 must be treated as a normal transfer and trigger the transfer event.

function transferFrom(address _from, address _to, uint256 _value) returns (bool success)Copy the code

approve

Allow _spender to retrieve your account multiple times, up to a _value amount. If this function is called again, it overrides the current margin with _value.

Note: To prevent vector attacks, the client needs to make sure that the user interfaces are created in such a way that they are set to 0 and then set to another value for the same spender. Although the contract itself should not be enforced, backward compatibility with previously deployed contract compatibility is allowed

function approve(address _spender, uint256 _value) returns (bool success)Copy the code

allowance

Returns the amount that _spender is still allowed to extract from _owner.

function allowance(address _owner, address _spender) constant returns (uint256 remaining)Copy the code

Events

Transfer

When a token is transferred (including a value of 0), it must be triggered.

event Transfer(address indexed _from, address indexed _to, uint256 _value)Copy the code

Approval

Must be triggered after any successful call to approve(address _spender, uint256 _value).

event Approval(address indexed _owner, address indexed _spender, uint256 _value)Copy the code

The implementation of

A large number of ERC20-compliant tokens are deployed on the Ethereum network. Various teams with different trade-offs have written different implementations, ranging from gas savings to improved security.

Sample implementations are available at

  • Github.com/ConsenSys/T…
  • Github.com/OpenZeppeli…

Add force 0 implementation “approved” before calling

ERC20 Token standard interface

Here is an interface contract that states the functions and events required to comply with the ERC20 standard:

// https://github.com/ethereum/EIPs/issues/20
  contract ERC20 {
      function totalSupply(a) constant returns (uint totalSupply);
      function balanceOf(address _owner) constant returns (uint balance);
      function transfer(address _to, uint _value) returns (bool success);
      function transferFrom(address _from, address _to, uint _value) returns (bool success);
      function approve(address _spender, uint _value) returns (bool success);
      function allowance(address _owner, address _spender) constant returns (uint remaining);
      event Transfer(address indexed _from, address indexed _to, uint _value);
      event Approval(address indexed _owner, address indexed _spender, uint _value);
    }Copy the code

Most Ethereum primary tokens are ERC20 compliant.

Some tokens include further information describing the token contract:

string public constant name = "Token Name";
string public constant symbol = "SYM";
uint8 public constant decimals = 18;  // Most of them are 18Copy the code

How does it work?

The following is a snippet of the token contract to demonstrate how token contracts maintain token balances in Ethereum accounts

contract TokenContractFragment {

     Balances saves the balance of the address
     mapping(address => uint256) balances;

     // The account owner approves the transfer of the amount to another account
     mapping(address => mapping (address => uint256)) allowed;

      // What is the balance of a particular account?
      function balanceOf(address _owner) constant returns (uint256 balance) {
          return balances[_owner]; // Take the value from the array
      }

      // Transfer the balance from the owner account to another account
      function transfer(address _to, uint256 _amount) returns (bool success) {
          // Determine the condition sender balance >= value to be sent Value to be sent >0 Receiver balance + value to be sent > receiver balance
          if (balances[msg.sender] >= _amount 
              && _amount > 0
              && balances[_to] + _amount > balances[_to]) {
              balances[msg.sender] -= _amount;   // The sender's balance decreases
              balances[_to] += _amount;         // The recipient's balance increases
              return true;
         } else {
              return false; }}// Send _value number of tokens from address _from to address _to
      // The transferFrom method is used to extract the workflow that allows the contract to send tokens in your name, such as "deposit" to the contract address and/or charge in sub-currencies; This command should fail unless the _FROM account intentionally authorizes the sender of the message through some mechanism; We propose these standardized apis for approval:
      function transferFrom( address _from, address _to, uint256 _amount ) returns (bool success) {
          // Same validation rule as above
          if (balances[_from] >= _amount
              && allowed[_from][msg.sender] >= _amount
              && _amount > 0
              && balances[_to] + _amount > balances[_to]) {
              balances[_from] -= _amount;
              allowed[_from][msg.sender] -= _amount; // Reduce the number of senders' approvals
              balances[_to] += _amount;
              return true;
         } else {
             return false; }}// Allow _spender to exit your account multiple times, up to the _value amount. If this function is called again, it overrides the current margin with _value.
      function approve(address _spender, uint256 _amount) returns (bool success) {
          allowed[msg.sender][_spender] = _amount; // Overwrite the current margin
          return true; }}Copy the code

Token balance

Suppose there are two holders of the token contract

  • 0x1111111111111111111111111111111111111111There are 100 units
  • 0x2222222222222222222222222222222222222222There are 200 units

The balances structure of the contract stores the following

balances[0x1111111111111111111111111111111111111111] = 100
balances[0x2222222222222222222222222222222222222222] = 200Copy the code

Then, the balanceOf (…). The following result is returned

tokenContract.balanceOf(0x1111111111111111111111111111111111111111) will return100
tokenContract.balanceOf(0x2222222222222222222222222222222222222222) will return200Copy the code

Transfer token balance

If 0 x1111111111111111111111111111111111111111 want to transfer to 0 x2222222222222222222222222222222222222222 10 units, and then zero x1111111111111111111 11111111111111111 will execute the following function

tokenContract.transfer(0x2222222222222222222222222222222222222222.10)Copy the code

Transfer of token contracts (…) Method changes the information in the balances structure

balances[0x1111111111111111111111111111111111111111] = 90
balances[0x2222222222222222222222222222222222222222] = 210Copy the code

balanceOf(…) The call returns the following information

tokenContract.balanceOf(0x1111111111111111111111111111111111111111) will return90
tokenContract.balanceOf(0x2222222222222222222222222222222222222222) will return210Copy the code

Approval and transfer from token balance

If 0 x1111111111111111111111111111111111111111 wants to approve some token x222222222222222222 0 0 x2222222222222222222222222222222222222222 transmission 2222222222222222222222, then 0 x1111111111111111111111111111111111111111 will perform the following functions

tokenContract.approve(0x2222222222222222222222222222222222222222.30)Copy the code

Then the allowed structure stores the following

tokenContract.allowed[0x1111111111111111111111111111111111111111] [0x2222222222222222222222222222222222222222] = 30Copy the code

If 0 x2222222222222222222222222222222222222222 want late transfer token from 0 x1111111111111111111111111111111111111111 to himself, 0 x2222222222222222222222222222222222222222 will execute transferFrom (…). function

tokenContract.transferFrom(0x1111111111111111111111111111111111111111.20)Copy the code

The sub message becomes the sub message

tokenContract.balances[0x1111111111111111111111111111111111111111] = 70
tokenContract.balances[0x2222222222222222222222222222222222222222] = 230Copy the code

Allowed then becomes the following

tokenContract.allowed[0x1111111111111111111111111111111111111111] [0x2222222222222222222222222222222222222222] = 10Copy the code

0 x2222222222222222222222222222222222222222 can still transfer from 0 x1111111111111111111111111111111111111111 10 units.

tokenContract.balanceOf(0x1111111111111111111111111111111111111111) will return 70
tokenContract.balanceOf(0x2222222222222222222222222222222222222222) will return 230Copy the code

Simple fixed token contract

Here is a sample token contract with a fixed supply of 1,000,000 units initially allocated to the contract owner:

pragma solidity ^0.48.;

  // ----------------------------------------------------------------------------------------------
  // Sample fixed supply token contract
  // Enjoy. (c) BokkyPooBah 2017. The MIT Licence.
  // ----------------------------------------------------------------------------------------------

   // ERC Token Standard #20 Interface
  // https://github.com/ethereum/EIPs/issues/20
  contract ERC20Interface {
      // Get total support
      function totalSupply(a) constant returns (uint256 totalSupply);

      // Get the balance of other addresses
      function balanceOf(address _owner) constant returns (uint256 balance);

      // Send tokens to other addresses
      function transfer(address _to, uint256 _value) returns (bool success);

      // Send the balance from one address to another
      function transferFrom(address _from, address _to, uint256 _value) returns (bool success);

      // Allows _spender to transfer the balance of _value from your account. Multiple calls overwrite the available amount. Some DEX functions require this feature
      function approve(address _spender, uint256 _value) returns (bool success);

      // Returns the amount of balance that _spender still allows to exit from _owner
      function allowance(address _owner, address _spender) constant returns (uint256 remaining);

      // The token transfer is completed
      event Transfer(address indexed _from, address indexed _to, uint256 _value);

      // trigger after approve(address _spender, uint256 _value) is called
      event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  }

   // Inherits the interface instance
   contract FixedSupplyToken is ERC20Interface {
      string public constant symbol = "FIXED"; / / unit
      string public constant name = "Example Fixed Supply Token"; / / name
      uint8 public constant decimals = 18; // The number of decimal places
      uint256 _totalSupply = 1000000; // Total number of issues

      // Owner of the smart contract
      address public owner;

      // The balance of each account
      mapping(address => uint256) balances;

      // The account owner approves the transfer of the amount to another account. From the description above, we can learn that the account is allowed[an account where money is transferred]
      mapping(address => mapping (address => uint256)) allowed;

      // a method that can only be called by the owner of the smart contract
      modifier onlyOwner() {
          if(msg.sender ! = owner) {throw;
          }
          _;
      }

      // constructor
      function FixedSupplyToken(a) {
          owner = msg.sender;
          balances[owner] = _totalSupply;
      }

      function totalSupply(a) constant returns (uint256 totalSupply) {
          totalSupply = _totalSupply;
      }

      // The balance of a specific account
      function balanceOf(address _owner) constant returns (uint256 balance) {
          return balances[_owner];
      }

      // Transfer the balance to another account
      function transfer(address _to, uint256 _amount) returns (bool success) {
          if (balances[msg.sender] >= _amount 
              && _amount > 0
              && balances[_to] + _amount > balances[_to]) {
              balances[msg.sender] -= _amount;
              balances[_to] += _amount;
              Transfer(msg.sender, _to, _amount);
              return true;
          } else {
              return false; }}// Transfer from one account to another, as long as there is a balance that can be transferred
      function transferFrom( address _from, address _to, uint256 _amount ) returns (bool success) {
          if (balances[_from] >= _amount
              && allowed[_from][msg.sender] >= _amount
              && _amount > 0
              && balances[_to] + _amount > balances[_to]) {
              balances[_from] -= _amount;
              allowed[_from][msg.sender] -= _amount;
              balances[_to] += _amount;
              Transfer(_from, _to, _amount);
              return true;
          } else {
              return false; }}// Allows an account to transfer balances from the current user to that account, overwritten by multiple calls
      function approve(address _spender, uint256 _amount) returns (bool success) {
          allowed[msg.sender][_spender] = _amount;
          Approval(msg.sender, _spender, _amount);
          return true;
      }

      // Return the number of balances allowed to transfer
      function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
          returnallowed[_owner][_spender]; }}Copy the code

Note that in the last example, the allowed parameter in the second dimension is the number of transfers by the caller, while in the beginning example, the number of receivers.

The resources