0
0
Blockchain / Solidityprogramming~10 mins

Balance checking in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Balance checking
Start
Input: User Address
Query Blockchain Ledger
Find User Balance
Return Balance
End
The process starts by receiving a user address, then queries the blockchain ledger to find and return the user's balance.
Execution Sample
Blockchain / Solidity
def check_balance(address, ledger):
    balance = ledger.get(address, 0)
    return balance

ledger = {'0xABC123': 100, '0xDEF456': 50}
user_address = '0xABC123'
print(check_balance(user_address, ledger))
This code looks up the balance for a given user address in the ledger and returns it.
Execution Table
StepActionInputLedger StateBalance FoundOutput
1Receive user address0xABC123{'0xABC123': 100, '0xDEF456': 50}N/AN/A
2Query ledger for address0xABC123{'0xABC123': 100, '0xDEF456': 50}100N/A
3Return balance0xABC123{'0xABC123': 100, '0xDEF456': 50}100100
4Print balanceN/AN/AN/A100
💡 Balance found and returned for user address; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
user_addressN/A'0xABC123''0xABC123''0xABC123''0xABC123'
ledgerN/A{'0xABC123': 100, '0xDEF456': 50}{'0xABC123': 100, '0xDEF456': 50}{'0xABC123': 100, '0xDEF456': 50}{'0xABC123': 100, '0xDEF456': 50}
balanceN/AN/A100100100
Key Moments - 2 Insights
Why do we use ledger.get(address, 0) instead of ledger[address]?
Using ledger.get(address, 0) safely returns 0 if the address is not found, avoiding errors. This is shown in Step 2 of the execution_table where balance is found or defaults to 0.
What happens if the user address is not in the ledger?
The balance defaults to 0 because ledger.get(address, 0) returns 0 when the address is missing, preventing crashes. This is implied in Step 2 where balance would be 0 if address missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the balance found at Step 2 for address '0xABC123'?
A100
B50
C0
DUndefined
💡 Hint
Check the 'Balance Found' column in Step 2 of the execution_table.
At which step is the balance returned to the caller?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column for the step where balance is returned.
If the ledger did not have the user address, what would the balance be at Step 2?
A100
B50
C0
DError
💡 Hint
Refer to the key_moments explanation about ledger.get(address, 0) default value.
Concept Snapshot
Balance checking in blockchain:
- Input user address
- Query ledger dictionary with .get(address, 0)
- Return found balance or 0 if missing
- Prevents errors if address not found
- Simple lookup returns user's token or coin balance
Full Transcript
Balance checking means finding how much money or tokens a user has on the blockchain. We start by getting the user's address. Then we look up this address in the blockchain ledger, which stores balances. If the address exists, we get the balance. If not, we return zero to avoid errors. Finally, we return this balance to the user. This process is simple and safe because it handles missing addresses gracefully.