Code Your Own Cryptocurrency on Ethereum: Build an ERC-20 Token and Crowdsale Website

·

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:

👉 Master Ethereum development


How Crowdsales (ICOs) Work

Initial Coin Offerings (ICOs) allow projects to raise funds by selling tokens. Here’s the process:

  1. Smart Contract Deployment: Rules for token distribution are coded into the contract.
  2. Investor Participation: Users send ETH to the contract in exchange for tokens.
  3. Automatic Distribution: Tokens are instantly transferred to buyers’ wallets.

Crowdsale Structure Example:


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:


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:


Deployment Steps

  1. Test Locally: Use Ganache for a personal Ethereum blockchain.
  2. Deploy Contracts: Truffle migrations to Ethereum testnet/mainnet.
  3. 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:

Next Steps:

For full source code, visit GitHub repo.