What is ERC-20 in smart contracts?

ERC-20 is the standard interface for fungible tokens on Ethereum and other EVM chains: every token exposes the same functions for balances, transfers and allowances, so any wallet or protocol can hold it. The standard, by Fabian Vogelsteller and Vitalik Buterin, was created in November 2015 (EIP-20).

Last updated . 2 minute read. By the QuillAudits Team.

What does the ERC-20 interface include?

Six functions hold the state: totalSupply, balanceOf, transfer, transferFrom, approve and allowance. Two events, Transfer and Approval, record changes, and newly created tokens emit a Transfer from the zero address. name, symbol and decimals are optional, and other contracts must not expect them (EIP-20).

Why is calling an ERC-20 token risky?

Behavior Why it matters
Returns false instead of reverting EIP-20 says callers must handle false, so an unchecked call can count a failed transfer as paid
Returns nothing A call that expects a bool reverts on the empty return
Takes a fee on transfer The amount received is less than the amount sent
Rebases Balances change without any transfer, which breaks cached accounting
Allowance changed in flight A spender can use the old allowance before the new one lands, then the new one too

EIP-20 asks user interfaces to set an allowance to 0 before changing it (EIP-20), and the OpenZeppelin approve docs describe the same ordering risk. OpenZeppelin SafeERC20 reverts when a token returns false and also supports tokens that return no value (OpenZeppelin).

How do you transfer an unknown token safely?

A safe transfer helper in the style of SafeERC20 (illustrative, not audited code) SafeTransferLite.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

library SafeTransferLite {
    error TransferFailed();

    function safeTransfer(address token, address to, uint256 amount) internal {
        (bool ok, bytes memory data) =
            token.call(abi.encodeWithSignature("transfer(address,uint256)", to, amount));
        if (!ok) revert TransferFailed();
        if (data.length == 0) {
            if (token.code.length == 0) revert TransferFailed(); // no contract, no transfer
        } else if (!abi.decode(data, (bool))) {
            revert TransferFailed(); // the token said no
        }
    }
}

Measure what arrived by the balance change when a token might charge a fee, and never assume decimals is 18.

The unchecked return value is a standard audit finding: a contract calls token.transfer(to, amount), ignores the bool it gets back, and records the payment as made even when a token answered false. The fix is a helper like the one above, used for every transfer.

FAQ

Is a token that returns nothing from transfer compliant?

Not by the letter of EIP-20, which declares a bool return. OpenZeppelin SafeERC20 still supports tokens that return no value (OpenZeppelin), and a protocol that accepts arbitrary tokens should handle both cases, as the helper above does.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts