TRC20

Introduction

TRC20 (TRC20 Token Standard) is a token standard based on the implementation of smart contract when using TRON network to issue a token.

In order to swap, transfer, share token and be supported by digital wallets, all the TRC20 tokens follow a series of specifications which include 6 required items and 3 optional items.

Implementation Rules

3 Optional Items:

  1. Token Name

string public constant name = “TRONEuropeRewardCoin”;

2. Token Abbreviation

For example, TERC is the abbreviation of TRONEuropeRewardCoin. TRONEuropeRewardCoin and TERC represent the same token, but TERC is more simple.string public constant symbol = “TERC”;

3. Token Precision

Token precision is the minimum divisible unit. Precision 0 means the minimum divisible unit is 1. Precision 2 means the minimum divisible unit is 0.01. The maximum value of precision is 18.uint8 public constant decimals = 6;

6 Required Items

The code below is a brief TRC20 smart contract:contract TRC20 {function totalSupply() constant returns (uint theTotalSupply);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);}

  1. totalSupply()

This function returns the total supply of the token.contract MyTRCToken { uint256 _totalSupply = 1000000; function totalSupply() constant returns (uint256 theTotalSuppl { theTotalSupply = _totalSupply; return theTotalSupply; }}

2. balanceOf()

This function returns the token balance of the specific account.contract MyTRCToken { mapping(address => uint256) balances; address public owner; // Owner of this contract function balanceOf(address _owner) constant returns (uint256 balance) { return balances[_owner]; }}

3. transfer()

This function is used to transfer an amount of tokens from the smart contract to a specific address.contract MyTRCToken { mapping(address => uint256) balances; 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); // trigger event return true; } else { return false; } }}

4. approve()

This function is used to authorize the third party (like a DAPP smart contract) to transfer token from the token owner’s account.contract MyTRCToken { mapping(address => mapping (address => uint256)) allowed; function approve(address _spender, uint256 _amount) returns (bool success) { allowed[msg.sender][_spender] = _amount;
// msg.sender is the third party’s account Approval(msg.sender, _spender, _amount);
// trigger event return true; } }

5. transferFrom()

This function is used to allow the third party to transfer token from an owner account to a receiver account. The owner account must be approved to be called by the third party.

Compared to transfer(), what’s the meaning of transferFrom()?

By using transferFrom(), the third party can transfer token from your account to another account automatically.contract MyTRCToken { mapping(address => uint256) balances; 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; balances[_to] += _amount; Transfer(_from, _to, _amount); return true; } else { return false; } }}

6. allowance()

This function is used to query the remaining amount of tokens the third party can transfer.

Approve() can authorize the third pay to transfer token from the owner account, then through transferFrom() it can transfer token to a receiver account.

For example:

Alice owns 1000 TRX, if he wants to allow the third part contract C to manipulate 100 TRX from his account:

  1. Call approve(C, 100) using Alice’s account
  2. Contract C wants to transfer 10 TRX to Bob from Alice. Call transferFrom(Alice, Bob, 10) using contract C’s account.
  3. Call allowance(Alice, C) to query the remaining tokens that the contract C can transfer from Alice’s account.

2 Event Functions

event Transfer(address indexed _from, address indexed _to, uint256 _value)

When token is successfully transferred, it has to trigger Transfer Event.event Approval(address indexed _owner, address indexed _spender, uint256 _value)

When approval() is successfully called, it has to trigger Approval Event.

References

https://github.com/tronprotocol/tips/blob/master/tip-20.md
https://www.jianshu.com/p/cc81e7b66f8e

For more information

Github: https://github.com/tronprotocol

Telegram: https://t.me/troncoredevscommunity