Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Transfer and approve flow in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Transfer and approve flow
User A calls approve(spender, amount)
↓
Contract records allowance
↓
User B calls transferFrom(User A, User C, amount)
↓
Check allowance and balance
↓
Transfer tokens
↓
Update balances and allowance
↓
Done
User A allows User B to spend tokens by approve. Then User B uses transferFrom to move tokens from A to C if allowed.
Execution Sample
Blockchain / Solidity
approve(spender, 100)
transferFrom(owner, recipient, 50)
// Check allowance and balance
// Update balances and allowance
User A approves User B to spend 100 tokens, then User B transfers 50 tokens from A to recipient.
Execution Table
StepActionAllowance BeforeBalance Owner BeforeCondition CheckAllowance AfterBalance Owner AfterBalance Recipient AfterResult
1User A calls approve(User B, 100)01000N/A10010000Allowance set to 100
2User B calls transferFrom(User A, User C, 50)1001000Allowance >= 50 and Balance >= 50: True5095050Transfer successful
3User B calls transferFrom(User A, User C, 60)50950Allowance >= 60 and Balance >= 60: False5095050Transfer rejected - insufficient allowance
4User B calls transferFrom(User A, User C, 50)50950Allowance >= 50 and Balance >= 50: True0900100Transfer successful
5User B calls transferFrom(User A, User C, 1)0900Allowance >= 1 and Balance >= 1: False0900100Transfer rejected - allowance zero
💡 Execution stops when allowance or balance is insufficient for transferFrom.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Allowance (User B)0100505000
Balance Owner (User A)10001000950950900900
Balance Recipient (User C)005050100100
Key Moments - 3 Insights
Why does transferFrom fail at step 3 even though User A has enough tokens?
Because the allowance for User B is only 50 tokens after step 2, and step 3 tries to transfer 60 tokens which exceeds allowance (see execution_table row 3).
What happens to allowance after a successful transferFrom?
Allowance decreases by the transferred amount, as shown in execution_table rows 2 and 4 where allowance reduces from 100 to 50, then to 0.
Can transferFrom succeed if allowance is zero?
No, transferFrom will reject the transaction if allowance is zero, as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the allowance for User B after step 2?
A0
B50
C100
D150
💡 Hint
Check the 'Allowance After' column in row 2 of the execution_table.
At which step does transferFrom fail due to insufficient allowance?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Result' and 'Condition Check' columns in execution_table row 3.
If User A approves 200 tokens instead of 100 at step 1, what would be the allowance after step 4?
A0
B100
C150
D50
💡 Hint
Allowance after step 4 is original allowance minus transferred amounts (see variable_tracker and execution_table).
Concept Snapshot
Transfer and approve flow:
- User approves a spender allowance with approve(spender, amount).
- Spender uses transferFrom(owner, recipient, amount) to move tokens.
- transferFrom checks allowance and owner's balance.
- On success, balances and allowance update.
- On failure, transfer is rejected.
Full Transcript
This visual execution shows how the transfer and approve flow works in blockchain token contracts. First, User A calls approve to allow User B to spend tokens on their behalf. The contract records this allowance. Then User B calls transferFrom to move tokens from User A to another user. The contract checks if User B has enough allowance and if User A has enough balance. If both are true, the transfer happens and balances and allowance update accordingly. If not, the transfer is rejected. The execution table traces each step with allowance and balances before and after, showing when transfers succeed or fail. The variable tracker shows how allowance and balances change over time. Key moments clarify common confusions about allowance usage and transfer failures. The quiz tests understanding of allowance changes and failure conditions. This flow is essential for safe token spending delegation.

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