Introduction to ERC-20 Tokens
An ERC-20 token is a standard for creating fungible tokens on the Ethereum blockchain. Fungible tokens are interchangeable, meaning each token is identical in value and functionality to any other token of the same type. This uniformity makes ERC-20 tokens ideal for:
- Medium of exchange: Digital currencies like stablecoins.
- Voting rights: Governance tokens in decentralized organizations.
- Staking: Tokens used in proof-of-stake mechanisms.
OpenZeppelin Contracts simplifies ERC-20 token development by providing pre-audited, secure implementations. Their API reference offers detailed documentation on properties and usage.
Building an ERC-20 Token Contract
Example: Creating a "Gold" (GLD) Token
Below is a step-by-step implementation of a hypothetical GLD token using OpenZeppelin’s ERC20 template:
// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract GLDToken is ERC20 {
constructor(uint256 initialSupply) ERC20("Gold", "GLD") {
_mint(msg.sender, initialSupply);
}
}Key Components:
- Inheritance: The contract inherits from
ERC20, leveraging OpenZeppelin’s prebuilt functions likename,symbol, anddecimals. - Constructor: Mints an
initialSupplyof tokens to the deployer’s address.
Querying and Transferring Tokens:
> GLDToken.balanceOf(deployerAddress) // Check balance
1000000000000000000000
> GLDToken.transfer(otherAddress, 300000000000000000000) // Transfer tokens
> GLDToken.balanceOf(otherAddress)
300000000000000000000Decimals and Token Divisibility
The Challenge
Solidity doesn’t support fractional numbers natively. To enable transfers like 1.5 GLD, ERC-20 uses a decimals field to manage precision.
How It Works:
- Display:
decimals(default:18) dictates how UI formats values (e.g.,50might display as5.0 GLD). Arithmetic: All operations use integers internally. For example:
- Transferring
1.5 GLD= Sending15units ifdecimals = 1.
- Transferring
Customizing Decimals:
Override the decimals() function to change precision:
function decimals() public view virtual override returns (uint8) {
return 16; // Sets decimals to 16
}Practical Example:
To send 5 GLD with 18 decimals:
transfer(recipient, 5 * (10 ** 18));FAQ Section
1. What is the purpose of the decimals field in ERC-20?
It defines how many decimal places a token can be divided into for display purposes, ensuring compatibility with wallets/exchanges.
2. Can I change the default decimals value in OpenZeppelin’s ERC-20?
Yes, override the decimals() function in your contract (e.g., return 16;).
3. How do I calculate token amounts for transfers?
Multiply the desired amount by 10 ** decimals. For example, 5 * (10 ** 18) for 5 tokens with 18 decimals.
👉 Explore advanced tokenomics strategies
Key Takeaways
- ERC-20 standardizes fungible tokens on Ethereum.
- OpenZeppelin accelerates development with secure, reusable contracts.
- Decimals enable fractional transactions via integer scaling.
By mastering these concepts, you can create versatile tokens for diverse blockchain applications. For further reading, check out OpenZeppelin’s ERC-20 documentation.