DEV Community

lio
lio

Posted on

ERC20 Smart Contracts: A Primer

  • Smart contracts are programmable code that are stored on the Ethereum blockchain and self-execute when they are called by externally-owned accounts (EOA) or other smart contracts. They can receive Ether (ETH) transfers and require gas fees to perform their various operations.

  • Tokens are blockchain-based entities that can be owned and represent assets, currency, identity, resources, and access rights. Tokens have intrinsic, significant value in blockchain networks. They can be programmed to have different applications and conform to various standards, including the ERC20 standard.

  • ERC20 smart contracts are contracts (usually written in Solidity) that are built following the specifications of the EIP-20 standard. The standard implements functions that allows users to transfer tokens to other users and approves tokens to be spent by other on-chain entities, such as a wallet or decentralized exchange.

  • More accurately, the ERC20 smart contract is a base contract that implements all the functions in the IERC20 interface. The ERC20Detailed contract is another core ERC20 contract that extends the base implementation and adds optional methods, including the token's name, symbol, and decimal properties.

  • Organizations like OpenZeppelin have further extended the ERC20 base contract and created related ERC20 contracts that add advanced features, e.g.:

    • ERC20Burnable: This defines an ERC20 token that allows the destruction of its native token.
    • ERC20Mintable: This extension allows creation of addresses than can create new tokens
    • ERC20Capped
    • ERC20Pausable
  • The ERC20 contract family also contains multiple utilities they interact with to provide additional functionalities, including SafeERC20 and TokenTimeLock.

  • The IERC20 defines a list of functions and events that the ERC20 smart contract must implement correctly in order to ensure full functionality.

FUNCTIONS:

  1. totalSupply:
    function totalSupply() public view returns (uint256)

    This function returns the total token supply of the implemented token.

  2. balanceOf:
    function balanceOf(address _owner) public view returns (uint256 balance)

    This function returns the amount of tokens or the account balance of the account with address _owner.

  3. transfer:
    function transfer(address _to, uint256 _value) public returns (bool success)

    This function transfers tokens of _value amount to _to address and fires the Transfer event. It should throw an error if the caller of the transaction does not have enough tokens in their account.

  4. transferFrom:
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)

    This function transfers _value amount of tokens from the _from address to the _to address and also fires the Transfer event.

  5. approve:
    function approve(address _spender, uint256 _value) public returns (bool success)

    This function allows the _spender address to spend _value amount of tokens on the owner's behalf.

  6. allowance:
    `function allowance(address _owner, address _spender) public view returns (uint256 remaining)

    This function returns the amount that the _spender address is allowed to withdraw form the _owner address.

EVENTS:

  1. Transfer:
    event Transfer(address indexed _from, address indexed _to, uint256 _value)

    This event is triggered when a new ERC20 token is created by sending to the 0x00 (zero) address.

  2. Approval:
    event Approval(address indexed _owner, address indexed _spender, uint256 _value)

    This event is triggered on every successful call to the approve function.

  • Accurate implementation of these functions and events will ensure that your ERC20 token performs as intended. However, more functions can be added to your custom ERC20 token for extra functionality.

Top comments (0)