ERC-20 Contract.
Table of contents
- ERC-20 Token Standard
- How Do Token Standards Work?
- ERC-20
- Why is the ERC-20 token standard important?
- What Solidity functions are mandatory for all ERC-20 tokens?
- 1. totalSupply
- 2. balanceOf
- 3. transfer
- 4. approve
- 5. transferFrom
- 6. allowance
- Events
- What Solidity functions are optional for all ERC-20 tokens?
- 1. Name
- 2. Symbol
- 3. Decimals
- Conclusion
ERC-20 Token Standard
What is the ERC-20 token standard?
Actually, let’s start with the basics. What is a token standard?
A token standard is a set of specific rules and protocols that determine the way a cryptocurrency token is created, behaves, and functions on a blockchain network. Cryptocurrency tokens represent digital assets that can be created, transferred, mutated, destroyed and stored on a blockchain. They enable various on-chain functionalities, such as paying transaction fees, securing the network via staking, running liquidity pools, and voting on governance proposals.
In the Ethereum world, anyone can create their own ERC token. ERC stands for Ethereum Request for Comment and is a set of technical documents that contain guidelines for developing a smart contract.
To ensure that tokens are standardized to the blockchain protocol on which they run and can interoperate across applications, different token standards have emerged. These standards ensure that tokens are compatible and can be integrated seamlessly across different blockchain protocols. This facilitates the creation of decentralized applications (dApps), which use tokens to function, and fosters liquidity and interoperability between tokens.
How Do Token Standards Work?
Token standards set out the parameters within which smart contracts must operate in order to perform basic functions on a blockchain protocol. This allows any developer to create their own cryptocurrency token, as the interfaces and features are standardized. Developers can design smart contracts for tokens that interact with each other because this standardization ensures that data types, functions, and behaviors are always consistent.
Token standards are set by proposals put forward by the blockchain developers and community. For example, the Ethereum blockchain, which pioneered smart contracts and remains the top platform for many applications, uses Ethereum Improvement Proposals (EIPs) to set standards for the core protocol, application programming interfaces (APIs) for nodes and client software, and smart contracts that run the Ethereum Virtual Machine (EVM).
Ethereum Requests for Comment (ERC) are proposals that define standards for Ethereum-based applications, including smart contract and token standards. They act as a set of technical documents that contain the guidelines for smart contract development.
ERC-20
The ERC-20 (Ethereum Request for Comments 20), proposed by Fabian Vogelsteller in November 2015, is a Token Standard that implements an API for tokens within Smart Contracts. They have a property that makes each Token be exactly the same (in type and value) as another Token. For example, an ERC-20 Token acts just like the ETH, meaning that 1 Token is and will always be equal to all the other Tokens.
Example functionalities ERC-20 provides:
transfer tokens from one account to another
get the current token balance of an account
get the total supply of the token available on the network
approve whether an amount of token from an account can be spent by a third-party account
If a Smart Contract implements the following methods and events it can be called an ERC-20 Token Contract and, once deployed, it will be responsible to keep track of the created tokens on Ethereum.
ERC-20 is the Ethereum standard for fungible tokens, covering different types of cryptocurrencies, such as utility tokens, security tokens, and stablecoins. It is widely used by projects developing utility and governance tokens for dApps running on the Ethereum blockchain or sidechains.
ERC-20 is the most widely adopted Ethereum token standard. It provides details of six required functions and three optional functions that a token must apply to be compliant.
The six primary functions define the total supply, wallet balance, ownership transfer, token transfer on a user’s behalf, and approval for the maximum number of tokens that a smart contract can withdraw. The optional functions can help improve the token’s usability, including the token name, ticker symbol, and number of decimal places it can support.
The standard allows developers to create customized fungible tokens that have utility, such as staking or representing voting rights and can be supported by many different cryptocurrency exchanges and wallets.
All ERC-20 tokens can interoperate with each other and compatible services such as the MetaMask wallet.
Examples of ERC-20 tokens include uniswap (UNI), shiba inu (SHIB), sandbox (SAND), and polygon (MATIC).
Why is the ERC-20 token standard important?
The ERC-20 token standard introduces a specific approach for fungible tokens, ensuring that they all share similar properties.
Since its proposal in 2015, the development of the ERC-20 token standard allows for other protocols, platforms, and developers to create smart contracts that can use any token following the standard without creating special logic for each new token. The standardization for smart contracts enhances the development process and in turn, greatly benefits the entire crypto ecosystem.
What Solidity functions are mandatory for all ERC-20 tokens?
The ERC-20 standard contains a set of methods and events that must be present in every implementation of the standard, including methods to transfer value, lookup balances for addresses and retrieve other metadata.
1. totalSupply
The totalSupply method returns the current circulating total supply of the tokens.
function totalSupply() external view returns (uint256);
2. balanceOf
The balanceOf method returns the account balance of another account with address _owner
.
function balanceOf(address _owner) external view returns (uint256 balance);
3. transfer
Transfers _value
amount of tokens to address _to
, and MUST fire the Transfer
event. The function SHOULD throw
if the message caller’s account balance does not have enough tokens to spend.
Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer
event.
function transfer(address _to, uint256 _value) public returns (bool success)
4. approve
Allows _spender
to withdraw from your account multiple times, up to the _value
amount. If this function is called again it overwrites the current allowance with _value
.
NOTE: To prevent attack vectors like the one described here and discussed here, clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to 0
before setting it to another value for the same spender. THOUGH The contract itself shouldn’t enforce it, to allow backwards compatibility with contracts deployed before
function approve(address _spender, uint256 _value) public returns (bool success)
5. transferFrom
Transfers _value
amount of tokens from address _from
to address _to
, and MUST fire the Transfer
event.
The transferFrom
method is used for a withdraw workflow, allowing contracts to transfer tokens on your behalf. This can be used for example to allow a contract to transfer tokens on your behalf and/or to charge fees in sub-currencies. The function SHOULD throw
unless the _from
account has deliberately authorized the sender of the message via some mechanism.
Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer
event.
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
6. allowance
Returns the amount which _spender
is still allowed to withdraw from _owner
.
function allowance(address _owner, address _spender) public view returns (uint256 remaining)
Events
Transfer
MUST trigger when tokens are transferred, including zero value transfers.
A token contract which creates new tokens SHOULD trigger a Transfer event with the _from
address set to 0x0
when tokens are created.
event Transfer(address indexed _from, address indexed _to, uint256 _value)
Approval
MUST trigger on any successful call to approve(address _spender, uint256 _value)
.
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
What Solidity functions are optional for all ERC-20 tokens?
Names, symbols, and decimals are all optional functions serving as an extension to the base interface of the ERC-20 token contract. Although these functions are not required, they provide additional details to the contract beneficial to understanding its objective. This core interface can serve as the base for additional customization and extensions to the smart contract.
1. Name
The name is a human-readable name that defines the purpose of the contract. it returns the name of the token — e.g. "MyToken"
.
function name() public view returns (string)function name() public view returns (string)
2. Symbol
The symbol is a human-readable ticker of the token that can be used to represent it. Similar to ETH, BTC, etc.
function symbol() public view returns (string)
3. Decimals
Returns the number of decimals the token uses — e.g. 8
, means to divide the token amount by 100000000
to get its user representation.
OPTIONAL — This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present.
function decimals() public view returns (uint8)
Conclusion
For smart contracts running on blockchain networks to operate effectively, token standards are necessary to guide the implementation of smart contracts that provide the programming interfaces on which decentralized applications can be built.
Subscribe to my newsletter
Read articles from Franklin Ejezie directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by