In Ethereum and other EVM blockchains, an ERC20 token is a smart contract that implements a robust standard to create fungible tokens.
Note: The standard only defines a set of functions and events that must be implemented in our contract; the implementation itself belongs to us.
Back in 2015, there was the need to virtually represent something inside the blockchain. With EIP-20 (Ethereum Improvement Proposal), they solved the problem by creating a common API interface to build a fungible token.
These tokens can represent anything, from USD pegged coins (such as USDT) to lottery tickets.
Initially, the tokens were widely used to create
An ERC20 token must implement the following functions:
balanceOf
: Provides the balance of a specific address.transfer
: Provides a way to transfer a number of tokens to an address.transferFrom
: Is the same as transfer
, but anyone is allowed to transfer a number of tokens from one address to another.approve
: Allows the spender to withdraw a number of tokens from a specific account.allowance
: Returns a set number of tokens from a spender to the owner.ERC20 tokens should also implement two events: Transfer and Approval, triggered on a transfer or approvals of the spender.
We can use the OpenZeppelin
library to make a simple ERC20 token, which provides a safe and audited set of libraries to build ERC-20 tokens.
In Remix IDE, we can just use the contract below to build one:
// SPDX-License-Identifier: MITpragma solidity ^0.8.2;import "@openzeppelin/contracts/token/ERC20/ERC20.sol";contract MyToken is ERC20 {constructor() ERC20("MyToken", "MTK") {}}
In this simple example, we import this contract and use it to extend our contract with the is
keyword.