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.
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))
| Step | Action | Input | Ledger State | Balance Found | Output |
|---|---|---|---|---|---|
| 1 | Receive user address | 0xABC123 | {'0xABC123': 100, '0xDEF456': 50} | N/A | N/A |
| 2 | Query ledger for address | 0xABC123 | {'0xABC123': 100, '0xDEF456': 50} | 100 | N/A |
| 3 | Return balance | 0xABC123 | {'0xABC123': 100, '0xDEF456': 50} | 100 | 100 |
| 4 | Print balance | N/A | N/A | N/A | 100 |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| user_address | N/A | '0xABC123' | '0xABC123' | '0xABC123' | '0xABC123' |
| ledger | N/A | {'0xABC123': 100, '0xDEF456': 50} | {'0xABC123': 100, '0xDEF456': 50} | {'0xABC123': 100, '0xDEF456': 50} | {'0xABC123': 100, '0xDEF456': 50} |
| balance | N/A | N/A | 100 | 100 | 100 |
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
What does balance checking in blockchain primarily tell you?
Which of the following is the correct way to call a function getBalance for an address addr in a blockchain library?
?
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?
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)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)Pass the test and this pattern moves unknown