Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

ERC-20 implementation in Blockchain / Solidity - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the total supply variable in an ERC-20 contract.

Blockchain / Solidity
uint256 public [1];
Drag options to blanks, or click blank then click option'
Aamount
Bbalance
Cowner
DtotalSupply
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names like balance or owner instead of totalSupply.
2fill in blank
medium

Complete the code to define the balance mapping for token holders.

Blockchain / Solidity
mapping(address => [1]) private balances;
Drag options to blanks, or click blank then click option'
Auint256
Bstring
Cbool
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using bool or address instead of uint256.
3fill in blank
hard

Fix the error in the transfer function signature to match ERC-20 standard.

Blockchain / Solidity
function transfer(address [1], uint256 amount) public returns (bool) {
Drag options to blanks, or click blank then click option'
Afrom
Brecipient
Cto
Dspender
Attempts:
3 left
💡 Hint
Common Mistakes
Using from or spender instead of to.
4fill in blank
hard

Fill both blanks to complete the allowance mapping declaration.

Blockchain / Solidity
mapping([1] => mapping([2] => uint256)) private allowances;
Drag options to blanks, or click blank then click option'
Aaddress
Buint256
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using uint256 or bool as keys instead of address.
5fill in blank
hard

Fill all three blanks to complete the transfer function body that updates balances and emits Transfer event.

Blockchain / Solidity
require(balances[msg.sender] >= [1], "Insufficient balance");
balances[msg.sender] -= [2];
balances[[3]] += amount;
emit Transfer(msg.sender, [3], amount);
return true;
Drag options to blanks, or click blank then click option'
Aamount
Bto
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names like from or mixing up amount and to.

Practice

(1/5)
1. What is the main purpose of the balanceOf function in an ERC-20 token contract?
easy
A. To approve another address to spend tokens
B. To transfer tokens from one address to another
C. To check the token balance of a specific address
D. To mint new tokens to an address

Solution

  1. Step 1: Understand the role of balanceOf

    The balanceOf function returns how many tokens a given address owns.
  2. Step 2: Compare with other functions

    Functions like transfer move tokens, approve allows spending, and minting creates tokens. These are different from checking balance.
  3. Final Answer:

    To check the token balance of a specific address -> Option C
  4. Quick Check:

    balanceOf = check balance [OK]
Hint: balanceOf always returns token count for an address [OK]
Common Mistakes:
  • Confusing balanceOf with transfer function
  • Thinking approve checks balances
  • Mixing minting with balance checking
2. Which of the following is the correct Solidity function signature for transferring tokens in an ERC-20 contract?
easy
A. function transfer(address to, uint256 amount) public returns (bool);
B. function transfer(uint256 amount, address to) public returns (bool);
C. function transfer(address to) public returns (uint256);
D. function transfer(address from, address to, uint256 amount) public;

Solution

  1. Step 1: Recall ERC-20 transfer signature

    The standard transfer function takes the recipient address first, then the amount, and returns a boolean.
  2. Step 2: Check each option

    function transfer(address to, uint256 amount) public returns (bool); matches the standard: transfer(address to, uint256 amount) public returns (bool). Others have wrong parameter order, missing return, or extra parameters.
  3. Final Answer:

    function transfer(address to, uint256 amount) public returns (bool); -> Option A
  4. Quick Check:

    transfer(to, amount) returns bool [OK]
Hint: transfer always has (address to, uint256 amount) and returns bool [OK]
Common Mistakes:
  • Swapping parameter order
  • Omitting return type
  • Adding extra parameters not in standard
3. Given this snippet from an ERC-20 contract:
mapping(address => uint256) balances;

function transfer(address to, uint256 amount) public returns (bool) {
    if (balances[msg.sender] >= amount) {
        balances[msg.sender] -= amount;
        balances[to] += amount;
        return true;
    }
    return false;
}

What will transfer return if msg.sender has 50 tokens and tries to send 60 tokens?
medium
A. true
B. Returns the remaining balance
C. Throws an error
D. false

Solution

  1. Step 1: Check balance condition

    The function checks if balances[msg.sender] is at least amount. Here, 50 < 60, so condition fails.
  2. Step 2: Determine function return

    Since condition fails, the function skips token transfer and returns false.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    Insufficient balance returns false [OK]
Hint: Transfer returns false if sender lacks enough tokens [OK]
Common Mistakes:
  • Assuming transfer throws error on insufficient balance
  • Thinking it returns true anyway
  • Confusing return value with balance amount
4. Identify the bug in this ERC-20 approve function snippet:
mapping(address => mapping(address => uint256)) allowances;

function approve(address spender, uint256 amount) public returns (bool) {
    allowances[msg.sender][spender] = amount;
    return true;
}
medium
A. No bug, function is correct
B. Should emit an Approval event after setting allowance
C. Should check if amount is less than balance
D. Should decrease allowance instead of setting it

Solution

  1. Step 1: Review ERC-20 approve requirements

    The standard requires emitting an Approval event after changing allowance for transparency.
  2. Step 2: Check function code

    The function sets allowance but does not emit the Approval event, missing a key part of the standard.
  3. Final Answer:

    Should emit an Approval event after setting allowance -> Option B
  4. Quick Check:

    approve must emit Approval event [OK]
Hint: approve must emit Approval event to follow standard [OK]
Common Mistakes:
  • Ignoring event emission
  • Checking balance in approve (not required)
  • Decreasing allowance instead of setting
5. You want to add a feature to your ERC-20 token that prevents transfers when the contract is paused. Which is the best way to implement this without breaking the ERC-20 standard?
hard
A. Add a paused boolean and require it false in transfer and transferFrom functions
B. Remove the transfer function and replace it with a new function that checks pause
C. Add a new function pauseTransfer that users must call before transferring
D. Modify the balanceOf function to return zero when paused

Solution

  1. Step 1: Understand pause feature integration

    To pause transfers, the contract needs a state variable like paused and checks in transfer functions.
  2. Step 2: Check ERC-20 compliance

    Removing or renaming transfer breaks the standard. Changing balanceOf output breaks expected behavior. Adding a separate function for pause is not standard.
  3. Final Answer:

    Add a paused boolean and require it false in transfer and transferFrom functions -> Option A
  4. Quick Check:

    Pause by require(!paused) in transfer functions [OK]
Hint: Use a paused flag and require it false in transfer methods [OK]
Common Mistakes:
  • Removing or renaming standard functions
  • Changing balanceOf behavior
  • Adding extra steps users must call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard