Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Transfer and approve flow 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 transfer function signature.

Blockchain / Solidity
function transfer(address [1], uint256 amount) public returns (bool);
Drag options to blanks, or click blank then click option'
Afrom
Bto
Cspender
Downer
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' instead of 'to' for the recipient address.
2fill in blank
medium

Complete the code to approve a spender to spend tokens.

Blockchain / Solidity
function approve(address [1], uint256 amount) public returns (bool);
Drag options to blanks, or click blank then click option'
Aspender
Bowner
Crecipient
Dto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'owner' instead of 'spender'.
3fill in blank
hard

Fix the error in the transferFrom function parameter name.

Blockchain / Solidity
function transferFrom(address [1], address to, uint256 amount) public returns (bool);
Drag options to blanks, or click blank then click option'
Ato
Bspender
Cowner
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to' or 'spender' instead of 'from'.
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 transferFrom function body snippet.

Blockchain / Solidity
require(_allowances[[1]][[2]] >= amount, "Allowance too low");
_allowances[[3]][[2]] -= amount;
Drag options to blanks, or click blank then click option'
Afrom
Bspender
Cto
Downer
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'to' and 'spender' keys.

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

  1. 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.
  2. Step 2: Differentiate from other functions

    transfer sends tokens directly, approve only sets allowance, so the spender can use transferFrom later.
  3. Final Answer:

    To allow another address to spend tokens on your behalf -> Option A
  4. 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

  1. Step 1: Recall approve function parameters

    The approve function takes two parameters: the spender's address and the amount to approve.
  2. Step 2: Match parameters order

    The correct order is approve(address spender, uint amount), so calling approve(0xABC..., 100); is correct.
  3. Final Answer:

    approve(0xABC..., 100); -> Option C
  4. 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?
address owner = msg.sender;
address spender = 0x123...;
uint amount = 50;

// Owner approves spender
approve(spender, amount);

// Spender transfers tokens from owner to recipient
transferFrom(owner, recipient, 30);
medium
A. 80 tokens
B. 50 tokens
C. 0 tokens
D. 30 tokens

Solution

  1. Step 1: Understand approve and transferFrom interaction

    The owner approves spender to spend 50 tokens. Then spender transfers 30 tokens to recipient.
  2. Step 2: Calculate recipient balance after transferFrom

    Since spender transfers 30 tokens, recipient's balance increases by 30 tokens.
  3. Final Answer:

    30 tokens -> Option D
  4. Quick Check:

    transferFrom moves approved amount [OK]
Hint: transferFrom moves approved tokens only [OK]
Common Mistakes:
  • Assuming full approved amount is transferred
  • Confusing approve amount with transfer amount
  • Ignoring transferFrom effect on recipient balance
4. Identify the error in this Solidity code snippet for transferring tokens using approve and transferFrom:
approve(spender, 100);
transferFrom(msg.sender, recipient, 50);
medium
A. approve should be called after transferFrom
B. transferFrom should be called by spender, not msg.sender
C. recipient address is missing
D. approve amount is less than transfer amount

Solution

  1. Step 1: Check who calls transferFrom

    transferFrom must be called by the spender who was approved, not the token owner (msg.sender here).
  2. Step 2: Confirm approve and transferFrom roles

    Owner calls approve, spender calls transferFrom to move tokens.
  3. Final Answer:

    transferFrom should be called by spender, not msg.sender -> Option B
  4. 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

  1. Step 1: Understand one-time spending permission

    To let DEX spend tokens once, you approve exactly 200 tokens once.
  2. Step 2: Let DEX transfer tokens

    DEX uses transferFrom to move 200 tokens from your wallet to itself or others.
  3. Final Answer:

    Call approve(DEX, 200) once, then DEX calls transferFrom for 200 tokens -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard