Introduction to ERC-20 Tokens
ERC-20 tokens are a cornerstone of the Ethereum ecosystem, enabling the creation of fungible digital assets. Any smart contract adhering to the ERC-20 standard qualifies as an ERC-20 token. These tokens facilitate:
- Peer-to-peer token transfers
- Delegated transfers (via approvals)
ERC-20 Interface Breakdown
Below is the standard interface for ERC-20 tokens in Solidity:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, 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 sender, address recipient, uint256 amount) external returns (bool);
}Core Functions Explained
totalSupply(): Returns the token's circulating supply.balanceOf(): Checks an address's token balance.transfer(): Moves tokens between addresses.allowance(): Views approved spending limit.approve(): Authorizes third-party transfers.transferFrom(): Executes delegated transfers.
Implementing an ERC-20 Token
Here's a complete ERC-20 contract example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "./IERC20.sol";
contract ERC20 is IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name;
string public symbol;
uint8 public decimals;
constructor(string memory _name, string memory _symbol, uint8 _decimals) {
name = _name;
symbol = _symbol;
decimals = _decimals;
}
// ... (Functions implementation continues)
}๐ Learn advanced token economics
Creating Custom Tokens with OpenZeppelin
Using OpenZeppelin's library simplifies ERC-20 development:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "./ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol, uint8 decimals)
ERC20(name, symbol, decimals)
{
_mint(msg.sender, 100 * 10 ** uint256(decimals));
}
}Token Swap Mechanism
The TokenSwap contract enables atomic swaps between ERC-20 tokens:
contract TokenSwap {
IERC20 public token1;
address public owner1;
uint256 public amount1;
// ... (Other variables)
constructor(address _token1, address _owner1, uint256 _amount1, /* ... */) {
// Initialization logic
}
function swap() public {
// Validation and transfer logic
}
}Swap Process Walkthrough
- Approval: Both parties approve the swap contract.
- Execution: Either party triggers
swap(). - Atomic Transfer: Tokens exchange simultaneously.
FAQs About ERC-20 Tokens
What makes ERC-20 tokens special?
ERC-20 provides a standardized interface for creating interchangeable tokens on Ethereum, ensuring compatibility across wallets and exchanges.
How do token approvals work?
Approvals allow smart contracts to spend tokens on your behalf, essential for DEXs and other DeFi protocols.
Can ERC-20 tokens represent non-financial assets?
Yes! They can symbolize anything from loyalty points to voting rights.
๐ Explore DeFi token strategies
Key Takeaways
- ERC-20 standards enable interoperable token creation
- OpenZeppelin reduces development overhead
- Atomic swaps require careful approval sequencing
Developers should prioritize security audits when implementing token contracts, especially for swap mechanisms.