Connecting MetaMask lets your website talk to your Ethereum wallet. This helps you use blockchain features like sending money or checking your balance.
When you want users to log in with their Ethereum wallet on your website.
When you need to read the user's Ethereum address to personalize their experience.
When you want to let users send transactions or interact with smart contracts.
When building decentralized apps (dApps) that require blockchain access.
When you want to check if the user has MetaMask installed and ready.
A. Missing await before window.ethereum.request call
B. Incorrect method name, should be 'requestAccounts'
C. window.ethereum.request does not exist
D. accounts is not an array
Solution
Step 1: Check the request call usage
window.ethereum.request returns a Promise, so it must be awaited or handled with then().
Step 2: Identify missing await
The code calls request without await, so accounts is a Promise, not an array, causing accounts[0] to be undefined.
Final Answer:
Missing await before window.ethereum.request call -> Option A
Quick Check:
Async calls need await to get resolved value [OK]
Hint: Always await async calls like window.ethereum.request [OK]
Common Mistakes:
Forgetting await on async calls
Passing method as string instead of object
Assuming request returns array directly
5. You want to connect a MetaMask wallet and display the connected account or an error message if MetaMask is not installed. Which code snippet correctly handles both cases?
async function connect() {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
console.log('Connected account:', accounts[0]);
} catch (error) {
console.log('Connection error:', error.message);
}
} else {
console.log('MetaMask is not installed');
}
}
connect();
hard
A. Correctly checks for MetaMask and handles connection and errors
B. Fails to check if window.ethereum exists before requesting accounts
C. Does not handle user rejection errors
D. Uses deprecated enable() method instead of request()
Solution
Step 1: Check for MetaMask presence
The code correctly checks if window.ethereum exists before trying to connect.
Step 2: Handle connection and errors properly
It uses try-catch to handle user rejection or other errors and logs appropriate messages.
Final Answer:
Correctly checks for MetaMask and handles connection and errors -> Option A
Quick Check:
Check existence + try-catch = robust connection [OK]
Hint: Always check window.ethereum before connecting [OK]
Common Mistakes:
Not checking if MetaMask is installed
Ignoring errors from user rejection
Using deprecated methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.