📖 Scenario: You are building a simple blockchain token system where users can transfer tokens to each other and approve others to spend tokens on their behalf.
🎯 Goal: Create a basic token transfer and approval flow using a dictionary to track balances and allowances.
📋 What You'll Learn
Create a dictionary called balances with exact user balances
Create a dictionary called allowances to track approved spending
Write a function transfer to move tokens between users
Write a function approve to set spending allowance
Write a function transfer_from to spend tokens using allowance
Print the final balances after transfers and approvals
💡 Why This Matters
🌍 Real World
This project models how cryptocurrencies and tokens manage transfers and approvals on blockchains.
💼 Career
Understanding transfer and approve flows is essential for blockchain developers working on smart contracts and token standards.
Progress0 / 4 steps
1
Set up initial balances
Create a dictionary called balances with these exact entries: 'Alice': 100, 'Bob': 50, 'Charlie': 75.
Blockchain / Solidity
Hint
Use curly braces to create a dictionary with keys and values exactly as shown.
2
Set up allowances dictionary
Create an empty dictionary called allowances to track approved spending.
Blockchain / Solidity
Hint
Use empty curly braces to create an empty dictionary.
3
Write transfer, approve, and transfer_from functions
Write three functions: transfer(sender, receiver, amount) to move tokens if sender has enough balance; approve(owner, spender, amount) to set allowance; and transfer_from(spender, owner, receiver, amount) to transfer tokens using allowance if allowed and owner has enough balance.
Blockchain / Solidity
Hint
Use dictionary methods like get to safely access balances and allowances.
4
Perform transfers, approvals, and print final balances
Call transfer('Alice', 'Bob', 20), then approve('Bob', 'Charlie', 15), then transfer_from('Charlie', 'Bob', 'Alice', 10). Finally, print the balances dictionary.
Blockchain / Solidity
Hint
Make sure to call the functions in the correct order and print the balances dictionary.
Practice
(1/5)
1. What is the main purpose of the approve function in a token contract?
easy
A. To allow another address to spend tokens on your behalf
B. To send tokens directly to another address
C. To check the balance of an address
D. To mint new tokens to an address
Solution
Step 1: Understand the role of approve
The approve function lets a token owner give permission to another address to spend tokens from their balance.
Step 2: Differentiate from other functions
transfer sends tokens directly, approve only sets allowance, so the spender can use transferFrom later.
Final Answer:
To allow another address to spend tokens on your behalf -> Option A
Quick Check:
approve = permission grant [OK]
Hint: Approve sets spending permission, transfer sends tokens [OK]
Common Mistakes:
Confusing approve with transfer
Thinking approve sends tokens immediately
Mixing approve with balance checking
2. Which of the following is the correct syntax to approve 100 tokens for spender address 0xABC... in Solidity?
easy
A. approve(100, 0xABC...);
B. approve(address spender, uint amount);
C. approve(0xABC..., 100);
D. approve(100);
Solution
Step 1: Recall approve function parameters
The approve function takes two parameters: the spender's address and the amount to approve.
Step 2: Match parameters order
The correct order is approve(address spender, uint amount), so calling approve(0xABC..., 100); is correct.
Final Answer:
approve(0xABC..., 100); -> Option C
Quick Check:
approve(spender, amount) syntax [OK]
Hint: approve(spender, amount) order matters [OK]
Common Mistakes:
Swapping parameters order
Using variable names instead of values
Calling approve with only one argument
3. Given the following Solidity code snippet, what will be the balance of recipient after execution?
B. transferFrom should be called by spender, not msg.sender
C. recipient address is missing
D. approve amount is less than transfer amount
Solution
Step 1: Check who calls transferFrom
transferFrom must be called by the spender who was approved, not the token owner (msg.sender here).
Step 2: Confirm approve and transferFrom roles
Owner calls approve, spender calls transferFrom to move tokens.
Final Answer:
transferFrom should be called by spender, not msg.sender -> Option B
Quick Check:
transferFrom caller = spender [OK]
Hint: transferFrom caller must be approved spender [OK]
Common Mistakes:
Calling transferFrom as token owner
Mixing approve and transferFrom order
Ignoring caller permissions
5. You want to allow a decentralized exchange (DEX) contract to spend exactly 200 tokens from your wallet, but only once. Which sequence of calls correctly implements this using the transfer and approve flow?
hard
A. Call approve(DEX, 200) once, then DEX calls transferFrom for 200 tokens
B. Call transfer(DEX, 200) directly, no approve needed
C. Call approve(DEX, 200) multiple times before each trade
D. Call transferFrom(DEX, yourAddress, 200) yourself
Solution
Step 1: Understand one-time spending permission
To let DEX spend tokens once, you approve exactly 200 tokens once.
Step 2: Let DEX transfer tokens
DEX uses transferFrom to move 200 tokens from your wallet to itself or others.
Final Answer:
Call approve(DEX, 200) once, then DEX calls transferFrom for 200 tokens -> Option A
Quick Check:
approve once, transferFrom once [OK]
Hint: Approve once, then let spender transferFrom once [OK]
Common Mistakes:
Transferring tokens directly without approval
Approving multiple times unnecessarily
Calling transferFrom as token owner
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.