Smart Contracts • Token Standard
ERC-20 Token Standard
Learn how to create, deploy, and interact with ERC-20 tokens on Tetreum Testnet.
What is ERC-20?
ERC-20 is the most widely used token standard on Ethereum-compatible blockchains like Tetreum. It defines a common set of rules for fungible tokens, ensuring interoperability across different platforms.
Key Features
- • Fungible tokens (each token is identical)
- • Transferable between addresses
- • Allowance system for third-party spending
- • Total supply tracking
- • Balance queries
Use Cases
- • Utility tokens for dApps
- • Governance tokens for DAOs
- • Stablecoins and cryptocurrencies
- • Reward tokens and loyalty points
- • Asset tokenization
Required Functions
Every ERC-20 token must implement these mandatory functions:
function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address to, uint256 amount) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 amount) external returns (bool); function transferFrom(address from, address to, uint256 amount) external returns (bool); event Transfer(address indexed from, address indexed to, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value);
View Functions
- •
totalSupply()- Total token supply - •
balanceOf()- Account token balance - •
allowance()- Approved spending amount
State-Changing Functions
- •
transfer()- Send tokens directly - •
approve()- Approve spending allowance - •
transferFrom()- Spend approved tokens
Basic Implementation
Here's a simple ERC-20 token implementation using OpenZeppelin:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
uint256 private constant INITIAL_SUPPLY = 1000000 * 10**18; // 1M tokens
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, INITIAL_SUPPLY);
}
// Optional: Mint new tokens (only owner)
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
// Optional: Burn tokens
function burn(uint256 amount) public {
_burn(msg.sender, amount);
}
// Optional: Burn tokens from another account (with allowance)
function burnFrom(address account, uint256 amount) public {
_spendAllowance(account, msg.sender, amount);
_burn(account, amount);
}
}Advanced Features
Enhance your ERC-20 token with additional functionality:
Pausable Token
import "@openzeppelin/contracts/security/Pausable.sol";
contract PausableToken is ERC20, Pausable, Ownable {
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal override whenNotPaused {
super._beforeTokenTransfer(from, to, amount);
}
}Capped Supply
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
contract CappedToken is ERC20Capped, Ownable {
constructor() ERC20("CappedToken", "CAP") ERC20Capped(1000000 * 10**18) {
_mint(msg.sender, 500000 * 10**18); // Mint 50% initially
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount); // Will revert if cap is exceeded
}
}Snapshot Token
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Snapshot.sol";
contract SnapshotToken is ERC20Snapshot, Ownable {
function snapshot() public onlyOwner returns (uint256) {
return _snapshot();
}
// Get balance at specific snapshot
function balanceOfAt(address account, uint256 snapshotId)
public view returns (uint256) {
return super.balanceOfAt(account, snapshotId);
}
}Deployment Steps
- 1
Install Dependencies
Install OpenZeppelin contracts and Hardhat
npm install @openzeppelin/contracts hardhat - 2
Configure Network
Add Tetreum testnet to hardhat.config.js
- 3
Deploy Contract
Run deployment script to Tetreum testnet
npx hardhat run scripts/deploy.js --network tetreum - 4
Verify Contract
Verify your token contract on Tetreum Explorer
Build Your Token Economy
Create powerful tokenized applications and economies using ERC-20 tokens on Tetreum Testnet.