Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

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

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

Practice

(1/5)
1.

What does balance checking in blockchain primarily tell you?

easy
A. The private key of a wallet
B. The transaction history of an address
C. How much cryptocurrency an address currently holds
D. The mining difficulty of the blockchain

Solution

  1. Step 1: Understand the purpose of balance checking

    Balance checking is used to find out the amount of cryptocurrency held by a blockchain address.
  2. Step 2: Differentiate from other blockchain data

    Transaction history, private keys, and mining difficulty are different concepts unrelated to balance checking.
  3. Final Answer:

    How much cryptocurrency an address currently holds -> Option C
  4. Quick Check:

    Balance checking = cryptocurrency amount [OK]
Hint: Balance checking shows funds held by an address [OK]
Common Mistakes:
  • Confusing balance with transaction history
  • Thinking balance reveals private keys
  • Mixing balance with blockchain mining info
2.

Which of the following is the correct way to call a function getBalance for an address addr in a blockchain library?

?
easy
A. getBalance(addr)
B. getBalance = addr
C. getBalance.addr()
D. getBalance->addr()

Solution

  1. Step 1: Identify correct function call syntax

    In most programming languages, calling a function with an argument uses parentheses: functionName(argument).
  2. Step 2: Check each option

    getBalance(addr) uses correct syntax: getBalance(addr). Options A, C, and D use invalid or incorrect syntax.
  3. Final Answer:

    getBalance(addr) -> Option A
  4. Quick Check:

    Function call syntax = getBalance(addr) [OK]
Hint: Function calls use parentheses with arguments [OK]
Common Mistakes:
  • Using assignment instead of function call
  • Using dot notation incorrectly for function calls
  • Using arrow notation which is not valid here
3.

Given this JavaScript code snippet using a blockchain API:

async function check() {
  const balance = await blockchainAPI.getBalance('0x123');
  console.log(balance);
}
check();

What will be printed if the address has 500 tokens?

medium
A. undefined
B. 500
C. Error: getBalance is not a function
D. null

Solution

  1. Step 1: Understand async/await usage

    The function uses await to get the balance asynchronously, so balance will hold the returned value.
  2. Step 2: Check what is logged

    console.log(balance) will print the balance value, which is 500 tokens for the given address.
  3. Final Answer:

    500 -> Option B
  4. Quick Check:

    Async balance fetch = 500 [OK]
Hint: Await returns the actual balance value [OK]
Common Mistakes:
  • Ignoring async and expecting immediate value
  • Assuming undefined if await is missing
  • Confusing error messages with correct output
4.

Find the error in this Python code that tries to get a balance:

def get_balance(address):
    balance = blockchain.getBalance(address)
    return balance

print(get_balance)
medium
A. Using print instead of return
B. Incorrect function name getBalance
C. Missing import statement for blockchain
D. Missing parentheses when calling get_balance in print

Solution

  1. Step 1: Check how get_balance is used in print

    The code prints get_balance without parentheses, so it prints the function object, not the result.
  2. Step 2: Correct usage to call the function

    Adding parentheses and an argument like print(get_balance('0xabc')) calls the function and prints the balance.
  3. Final Answer:

    Missing parentheses when calling get_balance in print -> Option D
  4. Quick Check:

    Function call needs () [OK]
Hint: Call functions with () to get results [OK]
Common Mistakes:
  • Printing function name instead of calling it
  • Assuming function runs without parentheses
  • Ignoring required function arguments
5.

You want to check balances of multiple addresses and store them in a dictionary in Python. Which code correctly does this?

addresses = ['0x1', '0x2', '0x3']
balances = {}
for addr in addresses:
    balances[addr] = blockchain.getBalance(addr)
print(balances)
hard
A. This code correctly stores balances for each address in a dictionary
B. This code will cause a syntax error due to missing colon
C. This code overwrites balances instead of storing all
D. This code does not call getBalance correctly

Solution

  1. Step 1: Analyze the loop and dictionary usage

    The for loop iterates over each address and assigns the balance to balances[addr], storing all balances.
  2. Step 2: Check syntax and function call

    Syntax is correct with colon and indentation; getBalance is called properly with each address.
  3. Final Answer:

    This code correctly stores balances for each address in a dictionary -> Option A
  4. Quick Check:

    Loop + dict assignment = correct balances [OK]
Hint: Use a loop to assign balances to dictionary keys [OK]
Common Mistakes:
  • Forgetting colon after for statement
  • Overwriting dictionary instead of adding keys
  • Calling getBalance without argument
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