ERC 20 Smart Contracts
An ERC20 smart contract is a token enabled contract that follows the ERC20 protocol and is standardized on the Ethereum blockchain. It is smart contract that enable the creation of fungible tokens which are exchangeable with another token. These tokens serve as a representation of any transferable, non-unique object, right, ownership, access, cryptocurrency, or other such object.
The ERC 20 Standard
According to EIP20 in the Ethereum Improvement Proposals, ERC 20 standard allows for the implementation of a standard API for tokens within smart contracts. This standard provides basic functionality to transfer tokens, manage tokens, as well as allow tokens to be approved so they can be spent by another on-chain third party.
A smart contract is said to be ERC-20 compliant only if it implements all of the events and compulsory functions listed in ERC-20 standard. While events define an activity, these functions (called methods in the ERC) specify what needs to be contained in this smart contracts.
Required functions that must be present in an ERC20 smart contract:
totalSupply: This function returns the total number of tokens that will ever be in circulation or in existence. It help the caller to know the total number of tokens that was minted.
function totalSupply() public view returns (uint256)
balanceOf: This function returns the number of tokens held by a specific token holder's address. It allows the caller to know the amount of token the specified address has.
function balanceOf(address _owner) public view returns (uint256 balance)
transfer: This function transfers a specified number of tokens from the total supply to a specified address. This function is called to send some amount of token from the total supply to the address that is passed to the function.
function transfer(address _to, uint256 _value) public returns (bool success)
transferFrom: This function transfers a specified number of tokens from one address to another, with the approval of the token holder. It throws an error if the token holder has not deliberately authorized the sender of the message. This function basically allows other person to move or transfer tokens on the token holder's behalf.
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
approve: This function allows a spender to withdraw a certain number of tokens from a owner's account, up to a specified amount.
function approve(address _spender, uint256 _value) public returns (bool success)
allowance: This function returns the amount of token a spender is still allowed to withdraw from the owner's account.
function allowance(address _owner, address _spender) public view returns (uint256 remaining)
Required event that must be present in an ERC20 smart contract:
Transfer: This is an event that must be emitted after tokens are transferred.
event Transfer(address indexed _from, address indexed _to, uint256 _value)
Approval: This is an event that must be emitted on any successful call to the approve function.
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
Optional functions that may be present in an ERC20 smart contract:
name: This function returns the name of the token when it is called.
function name() public view returns (string)
symbol: This function returns the symbol of the token.
function symbol() public view returns (string)
decimals: This function returns the number of decimals the token uses.
function decimals() public view returns (uint8)
Conclusions
ERC 20 smart contracts standard allow us to create token and help us make sure that compatible tokens on the Ethereum blockchain are interchangeable.
Subscribe to my newsletter
Read articles from Olayiwola Saheed Akorede directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Olayiwola Saheed Akorede
Olayiwola Saheed Akorede
Mobile Developer | Web Developer | Smart Contract Developer | Blockchain | Ethereum | Solidity | Typescript | Flutter | React | Node js