Smart contracts are self-executing programs stored on blockchains that facilitate, verify, and enforce agreements without intermediaries. They enable transparent, automated transactions powered by immutable code.
This guide walks you through writing, testing, and deploying an ERC-20 token contract on Ethereum using Solidity. By the end, you’ll have a functional smart contract capable of:
- Minting tokens
- Tracking balances
- Enabling peer-to-peer transfers
👉 Ready to dive into blockchain development?
Why Smart Contracts Matter
Smart contracts hold over $142 billion in digital assets, revolutionizing industries like finance, supply chain, and real estate. Their advantages include:
- Trustless execution: Code automates agreements, removing third-party reliance.
- Efficiency: Transactions settle in real-time with minimal overhead.
- Transparency: All actions are recorded on-chain for public verification.
Prerequisites
- MetaMask Wallet: Install the Chrome or Firefox extension.
- Test ETH: Obtain Kovan testnet ETH from a faucet.
- Solidity Basics: Review the official docs.
- Remix IDE: Use this web-based editor.
Designing the Smart Contract
Our MyToken contract will:
- Adhere to the ERC-20 standard for interoperability.
- Initialize a 1 million token supply assigned to the deployer.
- Enable transfers with balance validation.
Writing the Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
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);
}
contract MyToken is IERC20 {
string public constant name = "MyToken";
string public constant symbol = "MTKN";
uint8 public constant decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) private balances;
constructor() {
totalSupply = 1000000 * (10 ** decimals);
balances[msg.sender] = totalSupply;
}
function totalSupply() public override view returns (uint256) {
return totalSupply;
}
function balanceOf(address tokenOwner) public override view returns (uint256) {
return balances[tokenOwner];
}
function transfer(address receiver, uint256 numTokens) public override returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] -= numTokens;
balances[receiver] += numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
}Key Components:
- Interface:
IERC20ensures compliance with standard functions. - State Variables: Track metadata (
name,symbol) and balances. - Constructor: Mints initial supply to the deployer.
- Core Functions:
transfer()handles token movements between addresses.
Compiling and Deploying
- Compile in Remix to generate ABI/bytecode.
- Connect MetaMask to Kovan Testnet.
- Deploy via "Injected Web3" and confirm the transaction.
👉 Explore advanced deployment tools like Hardhat
Interacting with the Contract
- Check Balances: Call
balanceOf()with your address. - Transfer Tokens: Use
transfer()to send tokens to another account.
Extending Functionality
Consider adding:
- Burn mechanism to remove tokens from circulation.
- Minting controls for managing supply.
function burn(uint256 amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
totalSupply -= amount;
}Security Best Practices
- Prevent re-entrancy with checks like
require(). - Use access controls (e.g.,
onlyOwnermodifiers). - Audit arithmetic operations for overflows.
Beyond ERC-20
- NFTs: Explore ERC-721 for unique assets.
- DeFi: Build lending/borrowing protocols.
FAQ
Q: How much does deploying a smart contract cost?
A: Fees depend on contract complexity and network congestion. Testnets like Kovan use free ETH.
Q: Can I update a deployed contract?
A: No. Smart contracts are immutable—plan upgrades via proxy patterns.
Q: What’s the difference between ETH and tokens?
A: ETH is the native cryptocurrency, while tokens (like ERC-20) are assets built atop Ethereum.
Conclusion
You’ve now deployed a functional ERC-20 token! From here, explore:
- Integrating with decentralized exchanges (DEXs).
- Developing dApps that interact with your contract.
Smart contracts unlock endless possibilities for decentralized innovation. Happy coding!