ERC-20 Token Standard Function & Events

Charan
4 min readDec 7, 2022
ERC-20 Tokens

ERC20 is a type of token that is based on the Ethereum blockchain. It is a technical standard that defines a set of rules that all Ethereum-based tokens must follow. This allows for the creation of tokens that are interoperable, meaning that they can be used with other Ethereum-based tokens and smart contracts.

The ERC20 standard defines a set of functions that all ERC20 tokens must implement. These functions include the ability to transfer tokens, check the balance of a token holder, and allow others to approve the transfer of tokens on the token holder’s behalf. This allows for the creation of tokens that can be easily traded on Ethereum-based exchanges and used in a variety of applications.

The ERC-20 standard specifies six different functions that a smart contract must implement in order to be considered ERC-20 compliant. These functions include:

  • totalSupply: This function returns the total supply of tokens.
  • balanceOf: This function takes an address as input and returns the number of tokens that are owned by that address.
  • transfer: This function is used to transfer a specified number of tokens from one address to another.
  • transferFrom: This function is used to transfer a specified number of tokens from one address to another, with the approval of the token owner.
  • approve: This function is used to approve another address to transfer tokens on behalf of the token owner.
  • allowance: This function returns the number of tokens that an address is allowed to transfer on behalf of the token owner.

In addition to these functions, ERC-20 also defines two events that must be included in the smart contract:

  • Transfer: This event is triggered whenever tokens are transferred from one address to another. It includes the addresses of the sender and recipient, as well as the number of tokens that were transferred.
  • Approval: This event is triggered whenever the approve function is called, and includes the address of the token owner, the approved address, and the number of tokens that are approved for transfer.

These functions and events are used to enable the transfer and management of ERC-20 tokens on the Ethereum blockchain.

pragma solidity ^0.6.0;

contract ERC20 {
// Define the required variables and events
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

// Implement the required functions
function ERC20(string memory _name, string memory _symbol, uint8 _decimals, uint256 _totalSupply) public {
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _totalSupply;
balanceOf[msg.sender] = totalSupply;
}

function totalSupply() public view returns (uint256) {
return totalSupply;
}

function balanceOf(address _owner) public view returns (uint256) {
return balanceOf[_owner];
}

function transfer(address _to, uint256 _value) public {
require(balanceOf[msg.sender] >= _value && _value > 0);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
}

function transferFrom(address _from, address _to, uint256 _value) public {
require(balanceOf[_from] >= _value && allowance[_from][msg.sender] >= _value && _value > 0);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
}

function approve(address _spender, uint256 _value) public {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
}

function allowance(address _owner, address _spender) public view returns (uint256) {
return allowance[_owner][_spender];
}
}

In this example, the ERC20 contract constructor takes four parameters: _name, _symbol, _decimals, and _totalSupply. These are used to initialize the contract with the name, symbol, number of decimal places, and total supply of the token.

The totalSupply function simply returns the value of the totalSupply variable, which represents the total number of tokens in circulation. The balanceOf function takes an address as input and returns the number of tokens that are owned by that address.

The transfer function is used to transfer a specified number of tokens from the contract owner to another address. It requires that the sender has enough tokens to make the transfer, and emits a Transfer event to log the transfer.

The transferFrom function is similar to transfer, but it allows the transfer of tokens on behalf of another address with the approval of the token.

One of the key advantages of ERC20 tokens is that they are easily integrated with other Ethereum-based systems. This means that developers can create applications and smart contracts that can interact with ERC20 tokens without having to write a lot of custom code. This greatly simplifies the process of creating and using tokens on the Ethereum platform.

Another advantage of ERC20 tokens is that they are highly customizable. Developers can create tokens that have different supply models, such as fixed-supply or inflationary, and can also specify other parameters, such as the name of the token and the number of decimal places. This allows for the creation of tokens that are tailored to specific use cases and can be easily integrated into various applications.

Despite their many advantages, ERC20 tokens do have some limitations. For example, they are not able to support complex business logic or perform computations, as this is outside the scope of the ERC20 standard. Additionally, the Ethereum blockchain itself has scalability issues that can affect the performance of ERC20 tokens, particularly when they are used in applications with high transaction volumes.

Overall, ERC20 tokens are an important part of the Ethereum ecosystem and have enabled the creation of many innovative applications and services. They provide a standard set of rules for the creation and use of tokens on the Ethereum platform, making it easier for developers to create and integrate tokens into their applications.

--

--