0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.