0
0
Blockchain / Solidityprogramming~10 mins

ERC-20 fungible token standard in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ERC-20 fungible token standard
Contract Deployment
Initialize totalSupply
Balances mapping setup
User calls transfer
Check sender balance >= amount?
NoFail transaction
Yes
Subtract amount from sender
Add amount to receiver
Emit Transfer event
Return success
Other functions: approve, transferFrom, allowance
Update allowances and balances accordingly
Emit Approval event
Return success
End
This flow shows how an ERC-20 token contract is deployed, manages balances, and processes transfers and approvals step-by-step.
Execution Sample
Blockchain / Solidity
contract SimpleToken {
    mapping(address => uint) balances;
    uint totalSupply = 1000;

    constructor() {
        balances[msg.sender] = totalSupply;
    }

    function transfer(address to, uint amount) public returns (bool) {
        if (balances[msg.sender] >= amount) {
            balances[msg.sender] -= amount;
            balances[to] += amount;
            return true;
        }
        return false;
    }
}
This code defines a simple ERC-20 token transfer function that moves tokens if the sender has enough balance.
Execution Table
StepActionSender balanceAmountReceiver balanceCondition (sender balance >= amount)ResultReturn value
1Initial balances set: sender=1000, receiver=01000N/A0N/ASetup completeN/A
2Call transfer(to=receiver, amount=200)100020001000 >= 200 is TrueBalances updatedtrue
3Subtract 200 from sender8002000N/ASender balance updatedN/A
4Add 200 to receiver800200200N/AReceiver balance updatedN/A
5Return true to caller800200200N/ATransfer successfultrue
6Call transfer(to=receiver, amount=900)800900200800 >= 900 is FalseTransfer failedfalse
7Return false to caller800900200N/ATransfer unsuccessfulfalse
💡 Execution stops when transfer returns false due to insufficient sender balance.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
balances[sender]10001000800800800800800
balances[receiver]000200200200200
amountN/A200200200200900900
return valueN/AN/AN/AN/AtrueN/Afalse
Key Moments - 3 Insights
Why does the transfer fail when the amount is 900 but the sender balance is 800?
Because the condition 'sender balance >= amount' is false (800 >= 900 is false), so the transfer does not proceed and returns false, as shown in step 6 of the execution_table.
What happens to the balances if the transfer amount is exactly equal to the sender's balance?
The transfer succeeds because the condition 'sender balance >= amount' is true when equal. The sender's balance becomes zero, and the receiver's balance increases by that amount, similar to step 2-5 but with amount equal to sender balance.
Why do we emit events like Transfer or Approval in ERC-20?
Events notify external apps about token movements or approvals. Though not shown in the simple code, emitting events after balance changes helps wallets and explorers track token activity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the sender's balance after step 4?
A1000
B200
C800
D0
💡 Hint
Check the 'balances[sender]' row in variable_tracker after step 4.
At which step does the transfer function return false?
AStep 5
BStep 7
CStep 3
DStep 2
💡 Hint
Look at the 'Return value' column in execution_table for the false return.
If the sender's initial balance was 500 instead of 1000, what would happen at step 2 when transferring 200?
ATransfer would succeed and sender balance becomes 300
BTransfer would succeed and sender balance becomes 700
CTransfer would fail because 500 < 200
DTransfer would fail because 200 > 500
💡 Hint
Compare the condition 'sender balance >= amount' with new balance 500 and amount 200.
Concept Snapshot
ERC-20 is a standard for fungible tokens on Ethereum.
It defines functions like transfer, approve, transferFrom, and events.
Balances are tracked in a mapping from address to uint.
Transfers check sender balance before moving tokens.
Approvals allow others to spend tokens on your behalf.
Events notify external apps of token changes.
Full Transcript
This visual execution trace shows how an ERC-20 token contract works step-by-step. First, the contract is deployed with a total supply and balances are set. When a user calls transfer, the contract checks if the sender has enough tokens. If yes, it subtracts the amount from the sender and adds it to the receiver, then returns true. If not, it returns false and the transfer fails. The variable tracker shows how balances change after each step. Key moments clarify why transfers fail or succeed and the role of events. The quiz tests understanding of balances and return values during execution.