0
0
Blockchain / Solidityprogramming~20 mins

Transfer and approve flow in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Transfer and Approve Flow
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ERC-20 approve and transferFrom sequence?

Consider the following simplified Solidity code snippet for an ERC-20 token:

mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;

function approve(address spender, uint amount) public returns (bool) {
    allowed[msg.sender][spender] = amount;
    return true;
}

function transferFrom(address from, address to, uint amount) public returns (bool) {
    require(balances[from] >= amount, "Insufficient balance");
    require(allowed[from][msg.sender] >= amount, "Allowance exceeded");
    balances[from] -= amount;
    balances[to] += amount;
    allowed[from][msg.sender] -= amount;
    return true;
}

// Initial state:
balances["Alice"] = 100;

// Sequence:
approve("Bob", 50) called by Alice
transferFrom("Alice", "Charlie", 30) called by Bob
transferFrom("Alice", "Dave", 25) called by Bob

What happens at the last transferFrom call?

AThe call fails with "Allowance exceeded" error.
BThe call succeeds and Dave receives 25 tokens.
CThe call fails with "Insufficient balance" error.
DThe call succeeds but allowance is not updated.
Attempts:
2 left
💡 Hint

Remember that allowance decreases after each transferFrom call.

🧠 Conceptual
intermediate
1:30remaining
Why is the approve and transferFrom pattern used in ERC-20 tokens?

In ERC-20 tokens, why do we use the approve and transferFrom functions instead of just allowing direct transfers?

ATo enable tokens to be transferred only once per account.
BTo prevent any transfers unless approved by the contract owner.
CTo automatically burn tokens after transfer.
DTo allow a third party to spend tokens on behalf of the owner with a set limit.
Attempts:
2 left
💡 Hint

Think about how a decentralized exchange or contract might move tokens for a user.

🔧 Debug
advanced
2:00remaining
Identify the bug in this approve function implementation

Review this Solidity approve function snippet:

function approve(address spender, uint amount) public returns (bool) {
    require(amount > 0, "Amount must be positive");
    allowed[msg.sender][spender] = amount;
    emit Approval(msg.sender, spender, amount);
    return true;
}

What is the main issue with this implementation?

AIt disallows setting allowance to zero, which is needed to reset approvals.
BIt does not check if spender is a valid address.
CIt does not update the balances mapping.
DIt emits the Approval event before updating allowance.
Attempts:
2 left
💡 Hint

Think about how users revoke or change allowances safely.

📝 Syntax
advanced
1:30remaining
Which option correctly implements transferFrom allowance check?

Choose the correct Solidity code snippet that properly checks allowance before transferring tokens in transferFrom:

Arequire(allowed[from][msg.sender] > amount, "Allowance exceeded");
Brequire(allowed[from][msg.sender] >= amount, "Allowance exceeded");
Crequire(allowed[from][msg.sender] == amount, "Allowance exceeded");
Drequire(allowed[from][msg.sender] <= amount, "Allowance exceeded");
Attempts:
2 left
💡 Hint

The spender should be allowed to spend exactly or more than the requested amount.

🚀 Application
expert
3:00remaining
Calculate final balances after multiple approve and transferFrom calls

Given the following initial balances and approvals:

balances = {"Alice": 100, "Bob": 50, "Charlie": 0}
approvals:
  Alice approved Bob for 60 tokens
  Bob approved Charlie for 30 tokens

Sequence of calls:

  1. Bob calls transferFrom("Alice", "Bob", 40)
  2. Charlie calls transferFrom("Bob", "Charlie", 20)
  3. Bob calls transferFrom("Alice", "Charlie", 25)

What are the final balances of Alice, Bob, and Charlie?

A{"Alice": 60, "Bob": 50, "Charlie": 40}
B{"Alice": 35, "Bob": 90, "Charlie": 25}
C{"Alice": 60, "Bob": 70, "Charlie": 20}
D{"Alice": 35, "Bob": 70, "Charlie": 45}
Attempts:
2 left
💡 Hint

Track each transferFrom call carefully, updating balances and allowances.