📖 Scenario: You want to create your own digital currency on the Ethereum blockchain. This currency will follow the ERC-20 standard, which means it can be easily used by wallets and exchanges.
🎯 Goal: Build a simple ERC-20 token contract that tracks balances, allows transfers, and lets users approve others to spend tokens on their behalf.
📋 What You'll Learn
Create a contract named SimpleToken
Define state variables for name, symbol, decimals, and totalSupply
Create a mapping called balances to track token balances
Create a mapping called allowances to track approved spending
Implement transfer, approve, and transferFrom functions
Emit Transfer and Approval events appropriately
💡 Why This Matters
🌍 Real World
ERC-20 tokens are the most common type of tokens on Ethereum. They represent digital assets like currencies, points, or shares.
💼 Career
Understanding ERC-20 tokens is essential for blockchain developers working on decentralized finance (DeFi), NFTs, and token-based applications.
Progress0 / 4 steps
1
Set up the basic contract and state variables
Write a Solidity contract named SimpleToken. Inside it, declare public variables name as "MyToken", symbol as "MTK", decimals as 18, and totalSupply as 1000000 * 10 ** 18. Also, create a public mapping balances from address to uint256.
Blockchain / Solidity
Hint
Start by declaring the contract and the variables exactly as named.
2
Initialize the total supply and add allowances mapping
Add a constructor that assigns the entire totalSupply to the contract deployer's balances. Also, declare a public nested mapping allowances from address to address to uint256.
Blockchain / Solidity
Hint
Use msg.sender to assign the total supply to the deployer.
3
Implement transfer, approve, and transferFrom functions
Write three public functions: transfer that moves tokens from the caller to another address, approve that allows a spender to spend tokens on the caller's behalf, and transferFrom that allows a spender to transfer tokens from an owner to a recipient. Make sure to update balances and allowances accordingly and return bool success.
Blockchain / Solidity
Hint
Use require to check balances and allowances. Emit events after state changes.
4
Test the transfer function output
Add a simple test in the contract that transfers 100 tokens from the deployer to address 0x0000000000000000000000000000000000000001 and then print the balance of that address using a public view function. Write a public view function balanceOf that returns the balance of a given address. Then call balanceOf for 0x0000000000000000000000000000000000000001 and print the result.
Blockchain / Solidity
Hint
Write a balanceOf function and a testTransfer function that transfers 100 tokens and returns the new balance.
Practice
(1/5)
1. What is the main purpose of the ERC-20 standard in Ethereum?
easy
A. To ensure tokens have a common set of functions for compatibility
B. To create unique non-fungible tokens
C. To define the Ethereum network consensus rules
D. To manage smart contract deployment costs
Solution
Step 1: Understand ERC-20 purpose
ERC-20 defines a common interface for fungible tokens to work uniformly.
Step 2: Identify the correct purpose
This standard allows wallets and apps to interact with tokens easily by using the same functions.
Final Answer:
To ensure tokens have a common set of functions for compatibility -> Option A
Quick Check:
ERC-20 = Common token interface [OK]
Hint: ERC-20 standardizes token functions for easy use [OK]
Common Mistakes:
Confusing ERC-20 with NFT standards
Thinking ERC-20 controls Ethereum network rules
Assuming ERC-20 manages deployment costs
2. Which of the following is the correct function signature to check a token balance in ERC-20?
easy
A. function balanceOf(address owner) external view returns (uint256);
B. function getBalance(address owner) public returns (uint256);
C. function balance(address owner) external returns (bool);
D. function checkBalance(address owner) public view returns (bool);
Solution
Step 1: Recall ERC-20 balance function
The standard defines balanceOf as a view function returning a uint256 balance.
Step 2: Match function signature
function balanceOf(address owner) external view returns (uint256); matches the exact signature: external view returns (uint256).
Final Answer:
function balanceOf(address owner) external view returns (uint256); -> Option A
Quick Check:
balanceOf = correct signature [OK]
Hint: balanceOf returns uint256 and is a view function [OK]
Common Mistakes:
Using wrong function name like getBalance
Missing view keyword for read-only function
Returning bool instead of uint256
3. Given this Solidity snippet from an ERC-20 token contract:
function transfer(address to, uint256 amount) public returns (bool) {
if (balances[msg.sender] < amount) {
return false;
}
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
What will transfer return if the sender has 50 tokens and tries to send 60 tokens?
medium
A. true
B. false
C. It will throw an error
D. It will return zero
Solution
Step 1: Check sender balance condition
The function checks if sender's balance is less than amount; if yes, returns false.
Step 2: Apply given values
Sender has 50 tokens but tries to send 60, so condition is true and function returns false.
Final Answer:
false -> Option B
Quick Check:
Insufficient balance returns false [OK]
Hint: Transfer returns false if balance is insufficient [OK]
Common Mistakes:
Assuming it throws error instead of returning false
Thinking it returns true regardless
Confusing return value with event emission
4. Identify the error in this ERC-20 approve function snippet:
function approve(address spender, uint256 amount) public returns (bool) {
allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
What is the common security issue with this implementation?
medium
A. It does not emit the Approval event
B. It does not check if spender is zero address
C. It allows race condition by directly setting allowance
D. It returns false instead of true
Solution
Step 1: Understand approve race condition
Directly setting allowance can cause race conditions if spender uses old and new allowance simultaneously.
Step 2: Identify missing mitigation
Safe implementations require resetting allowance to zero before changing it to new value to avoid this issue.
Final Answer:
It allows race condition by directly setting allowance -> Option C
Quick Check:
approve race condition = direct allowance set [OK]
Hint: Always reset allowance to zero before changing it [OK]
Common Mistakes:
Thinking missing zero address check is main issue
Assuming Approval event is missing
Believing return value is incorrect
5. You want to create an ERC-20 token that charges a 1% fee on every transfer and sends it to a fee collector address. Which modification to the transfer function correctly implements this?
hard
A. Send full amount to recipient, then send 1% from sender to collector
B. Add 1% to amount, send full amount to recipient, send fee separately
C. Send full amount to recipient and fee collector equally
D. Deduct 1% from amount, send fee to collector, send rest to recipient
Solution
Step 1: Calculate fee and net transfer
Calculate 1% fee from amount, subtract it to get net amount to recipient.
Step 2: Transfer fee and net amount correctly
Send fee to fee collector address and net amount to recipient to implement fee deduction.
Final Answer:
Deduct 1% from amount, send fee to collector, send rest to recipient -> Option D
Quick Check:
Fee deducted and sent correctly = Deduct 1% from amount, send fee to collector, send rest to recipient [OK]
Hint: Subtract fee from amount before sending tokens [OK]
Common Mistakes:
Adding fee on top of amount instead of deducting
Sending full amount to recipient and fee separately from sender
Splitting amount equally between recipient and fee collector
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.