By Gregory McCubbin · June 2025
Introduction to ERC-20 Tokens
Ethereum enables developers to create custom cryptocurrencies using smart contracts. The ERC-20 standard governs how these tokens function, ensuring compatibility with wallets, exchanges, and decentralized applications (dApps).
Key Features of ERC-20 Tokens:
- Standardized Interface: Uniform functions like
transfer()andapprove(). - Interoperability: Works seamlessly with Ethereum-based platforms.
- Trackable Balances: Publicly verifiable ownership via blockchain.
How Crowdsales (ICOs) Work
Initial Coin Offerings (ICOs) allow projects to raise funds by selling tokens. Here’s the process:
- Smart Contract Deployment: Rules for token distribution are coded into the contract.
- Investor Participation: Users send ETH to the contract in exchange for tokens.
- Automatic Distribution: Tokens are instantly transferred to buyers’ wallets.
Crowdsale Structure Example:
- Token Price: Fixed or dynamic (e.g., 1 ETH = 100 tokens).
- Funding Caps: Hard limits to prevent overselling.
- Admin Controls: Ability to pause/finalize the sale.
Building an ERC-20 Token
Smart Contract Code (Solidity):
pragma solidity ^0.4.2;
contract DappToken {
string public name = "DApp Token";
string public symbol = "DAPP";
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
function DappToken(uint256 _initialSupply) public {
balanceOf[msg.sender] = _initialSupply;
totalSupply = _initialSupply;
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
} Key Functions:
transfer(): Moves tokens between addresses.balanceOf: Tracks token holdings per account.
Creating a Crowdsale Contract
Crowdsale Smart Contract:
pragma solidity ^0.4.2;
import "./DappToken.sol";
contract DappTokenSale {
address admin;
DappToken public tokenContract;
uint256 public tokenPrice;
uint256 public tokensSold;
event Sell(address _buyer, uint256 _amount);
function DappTokenSale(DappToken _tokenContract, uint256 _tokenPrice) public {
admin = msg.sender;
tokenContract = _tokenContract;
tokenPrice = _tokenPrice;
}
function buyTokens(uint256 _numberOfTokens) public payable {
require(msg.value == _numberOfTokens * tokenPrice);
require(tokenContract.transfer(msg.sender, _numberOfTokens));
tokensSold += _numberOfTokens;
emit Sell(msg.sender, _numberOfTokens);
}
function endSale() public {
require(msg.sender == admin);
tokenContract.transfer(admin, tokenContract.balanceOf(this));
selfdestruct(admin);
}
} Features:
- Token Sales: Accepts ETH and distributes tokens.
- Admin Controls: Finalize sale and withdraw funds.
Deployment Steps
- Test Locally: Use Ganache for a personal Ethereum blockchain.
- Deploy Contracts: Truffle migrations to Ethereum testnet/mainnet.
- Build Frontend: Web3.js to interact with smart contracts.
👉 Explore advanced Ethereum tools
FAQ
1. What’s the difference between ERC-20 and other token standards?
ERC-20 is for fungible tokens (e.g., currencies), while ERC-721 is for NFTs (unique assets).
2. How do I set a token price?
Define it in the crowdsale contract (e.g., 1 ETH = 1000 tokens).
3. Can I modify the token supply later?
No—total supply is fixed at deployment.
4. How do investors claim tokens?
Tokens are automatically sent to their Ethereum address.
5. Is KYC required for ICOs?
Depends on jurisdiction; smart contracts can enforce whitelists.
6. What happens to unsold tokens?
Admin can reclaim them via endSale().
Conclusion
This guide covered:
- ERC-20 token creation.
- Crowdsale smart contract development.
- Deployment and testing strategies.
Next Steps:
- Extend functionality with tiered sales or vesting schedules.
- Integrate with a React frontend for user-friendly ICOs.
For full source code, visit GitHub repo.