📖 Scenario: Imagine you are learning about blockchain accounts. There are two main types: Externally Owned Accounts (EOA), which are controlled by people, and Contract Accounts, which are controlled by code (smart contracts).We will create a simple program to represent these accounts and check their types.
🎯 Goal: You will build a small program that stores a list of accounts with their types and then filters out only the EOAs.
📋 What You'll Learn
Create a dictionary called accounts with account addresses as keys and their types ('EOA' or 'Contract') as values.
Create a variable called filter_type and set it to the string 'EOA'.
Use a dictionary comprehension to create a new dictionary called eoa_accounts that contains only the accounts of type 'EOA'.
Print the eoa_accounts dictionary.
💡 Why This Matters
🌍 Real World
Blockchain developers often need to distinguish between EOAs and contract accounts to manage transactions and smart contract interactions correctly.
💼 Career
Understanding account types is essential for blockchain engineers, smart contract developers, and security analysts working with Ethereum or similar platforms.
Progress0 / 4 steps
1
Create the accounts dictionary
Create a dictionary called accounts with these exact entries: '0xabc123': 'EOA', '0xdef456': 'Contract', '0xghi789': 'EOA', '0xjkl012': 'Contract'.
Blockchain / Solidity
Hint
Use curly braces {} to create a dictionary with the exact keys and values.
2
Set the filter type
Create a variable called filter_type and set it to the string 'EOA'.
Blockchain / Solidity
Hint
Assign the string 'EOA' to the variable named filter_type.
3
Filter EOAs using dictionary comprehension
Use a dictionary comprehension to create a new dictionary called eoa_accounts that contains only the accounts from accounts where the value equals filter_type. Use for address, acc_type in accounts.items() in your comprehension.
Blockchain / Solidity
Hint
Use {key: value for key, value in dict.items() if condition} to filter the dictionary.
4
Print the filtered EOAs
Write print(eoa_accounts) to display the dictionary of EOAs.
Blockchain / Solidity
Hint
Use the print function to show the eoa_accounts dictionary.
Practice
(1/5)
1. Which of the following best describes an Externally Owned Account (EOA) on a blockchain?
easy
A. An account that can only receive tokens but not send them.
B. An account that only stores data but cannot send transactions.
C. An account that automatically executes code when called.
D. An account controlled by a private key, used by people to send transactions.
Solution
Step 1: Understand what an EOA is
An EOA is controlled by a private key and represents a user or person on the blockchain.
Step 2: Identify EOA capabilities
EOAs can send transactions and initiate actions, unlike contract accounts which run code.
Final Answer:
An account controlled by a private key, used by people to send transactions. -> Option D
Quick Check:
EOA = user-controlled account [OK]
Hint: EOAs are user accounts with private keys [OK]
Common Mistakes:
Confusing EOAs with contract accounts
Thinking EOAs run code automatically
Believing EOAs cannot send transactions
2. Which syntax correctly represents a contract account address in Solidity?
easy
A. address payable contractAddr = 0x1234567890abcdef1234567890abcdef12345678;
B. contractAddr = '0x1234567890abcdef1234567890abcdef12345678';
C. address contractAddr = 1234567890abcdef1234567890abcdef12345678;
D. address contractAddr = 0xGHIJKL7890abcdef1234567890abcdef12345678;
Solution
Step 1: Check Solidity address syntax
Addresses must be hexadecimal starting with 0x and enclosed in proper type declaration.
Step 2: Validate each option
The declaration 'address payable contractAddr = 0x1234567890abcdef1234567890abcdef12345678;' uses correct type and hex format. String assignments without type declaration are invalid, missing 0x prefix fails, and invalid hex characters like 'GHI' are not allowed.
Final Answer:
address payable contractAddr = 0x1234567890abcdef1234567890abcdef12345678; -> Option A
B. True if sender is a contract account, false if EOA.
C. Always true because msg.sender is a contract.
D. Always false because msg.sender has no code.
Solution
Step 1: Understand msg.sender.code.length
This checks the length of the code at the sender's address; contracts have code, EOAs do not.
Step 2: Interpret the boolean value
If code length > 0, sender is a contract account; otherwise, it's an EOA.
Final Answer:
True if sender is a contract account, false if EOA. -> Option B
Quick Check:
Code length > 0 means contract [OK]
Hint: Check code length to distinguish contract vs EOA [OK]
Common Mistakes:
Assuming msg.sender is always EOA
Confusing code length meaning
Thinking EOAs have code
4. You have this Solidity function:
function sendEther(address recipient) public {
recipient.transfer(1 ether);
}
When calling this function with an EOA address, it fails with an error. What is the likely cause?
medium
A. The recipient address is not payable, so transfer fails.
B. EOA addresses cannot receive ether.
C. The function lacks the payable modifier.
D. transfer() only works with contract accounts.
Solution
Step 1: Understand transfer() requirements
transfer() requires the recipient address to be payable to receive ether.
Step 2: Analyze recipient type
If recipient is declared as address (not address payable), transfer will fail even if it's an EOA.
Final Answer:
The recipient address is not payable, so transfer fails. -> Option A
Quick Check:
Use address payable for ether transfers [OK]
Hint: Use address payable to send ether [OK]
Common Mistakes:
Thinking EOAs can't receive ether
Missing payable keyword on function
Assuming transfer only works with contracts
5. You want to write a Solidity function that sends 1 ether only if the recipient is an EOA (not a contract). Which code snippet correctly implements this check?
hard
A. require(recipient.balance > 0, "Recipient must have balance"); recipient.transfer(1 ether);
B. require(recipient.code.length > 0, "Recipient must be contract"); payable(recipient).transfer(1 ether);
C. require(recipient.code.length == 0, "Recipient must be EOA"); payable(recipient).transfer(1 ether);
D. require(msg.sender == recipient, "Sender must be recipient"); recipient.transfer(1 ether);
Solution
Step 1: Check recipient code length to identify EOA
EOAs have no code, so recipient.code.length == 0 confirms EOA.
Step 2: Use require to enforce condition and transfer ether
If recipient is EOA, convert to payable and transfer 1 ether safely.
Final Answer:
require(recipient.code.length == 0, "Recipient must be EOA"); payable(recipient).transfer(1 ether); -> Option C
Quick Check:
Code length 0 means EOA, then transfer [OK]
Hint: Check code length == 0 to confirm EOA before sending [OK]
Common Mistakes:
Checking for code length > 0 to find EOAs
Not converting address to payable before transfer
Using balance check instead of code length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.