Implementing a Token
transfer and approve.
Implementing a Token is a free Web3 & DApp Development Fundamentals lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web3 & DApp Development Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Building a Token
Now you will implement the core of an ERC-20 token from scratch. Understanding the internals helps you debug and audit real tokens, even when you later use libraries.
We will focus on the state, transfer, and approve.
Core State Variables
A token needs to track balances and total supply. Balances are stored in a mapping from address to amount.
<code>contract Token {
string public name = 'MyToken';
string public symbol = 'MTK';
uint8 public decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
}</code>The Constructor
The constructor mints an initial supply to the deployer. We increase totalSupply and credit the creator's balance.
<code>constructor(uint256 initialSupply) {
totalSupply = initialSupply;
balanceOf[msg.sender] = initialSupply;
}</code>The Events
Declare the two required events. We will emit Transfer on every movement and Approval when an allowance is set.
<code>event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);</code>Implementing transfer
The transfer function moves tokens from the caller to a recipient. It must check the caller has enough balance and emit the Transfer event.
<code>function transfer(address to, uint256 amount) public returns (bool) {
require(balanceOf[msg.sender] >= amount, 'insufficient balance');
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}</code>Guarding Against Zero Address
Sending tokens to address(0) effectively destroys them by accident. Many implementations reject it explicitly.
<code>function transfer(address to, uint256 amount) public returns (bool) {
require(to != address(0), 'transfer to zero');
require(balanceOf[msg.sender] >= amount, 'insufficient');
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}</code>The allowance Mapping
To support delegated transfers we add a nested mapping: owner to spender to approved amount.
<code>mapping(address => mapping(address => uint256)) public allowance;</code>Implementing approve
The approve function lets the caller authorize a spender to move up to a certain amount of their tokens. It records the allowance and emits Approval.
<code>function approve(address spender, uint256 amount) public returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}</code>Overflow Safety
Since Solidity 0.8, arithmetic reverts on overflow and underflow automatically. The subtraction in transfer is safe: if the balance is too low it reverts, though the explicit require gives a clearer error.
Why Return bool
The standard specifies a bool return for compatibility. We return true on success. On failure we revert, which is the modern convention and safer than silently returning false.
Putting It Together
With state, events, transfer, and approve in place you have a functioning fungible token. The next lesson adds transferFrom to complete the allowance flow.
<code>// state + constructor + events + transfer + approve
// = a minimal working ERC-20</code>Quick Check
Test your understanding of implementing a token.
Recap
You implemented the core of an ERC-20 token:
- State: name, symbol, decimals, totalSupply, balanceOf mapping
- Constructor mints the initial supply to the deployer
transferchecks balance, updates state, emitsTransferapproverecords an allowance and emitsApproval- Guard against the zero address and rely on 0.8 overflow safety
Frequently asked questions
Is the “Implementing a Token” lesson free?
Yes — the full text of “Implementing a Token” is free to read here on the web, and the Web3 & DApp Development Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web3 & DApp Development Fundamentals course, upgrade to CoddyKit PRO.
What will I learn in “Implementing a Token”?
transfer and approve. You practise Web3 & DApp Development Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Web3 & DApp Development Fundamentals?
No prior experience is required. Web3 & DApp Development Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Implementing a Token” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Web3 & DApp Development Fundamentals lesson?
Yes. Every Web3 & DApp Development Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The ERC-20 Standard
- Implementing a Token
- Allowances
- Minting and Burning