Challenge - 5 Problems
MetaMask Connection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when MetaMask is not installed?
Consider this code snippet that tries to connect to MetaMask. What will it print if MetaMask is not installed in the browser?
Blockchain / Solidity
async function connectWallet() { if (typeof window.ethereum === 'undefined') { console.log('MetaMask is not installed'); return; } try { const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); console.log('Connected account:', accounts[0]); } catch (error) { console.log('User rejected the connection'); } } connectWallet();
Attempts:
2 left
💡 Hint
Check what happens if window.ethereum is undefined.
✗ Incorrect
If MetaMask is not installed, window.ethereum is undefined, so the code prints 'MetaMask is not installed' and returns early.
❓ Predict Output
intermediate2:00remaining
What happens if user rejects MetaMask connection request?
Given this code to connect MetaMask wallet, what will be printed if the user rejects the connection request?
Blockchain / Solidity
async function connectWallet() { if (!window.ethereum) { console.log('MetaMask not found'); return; } try { const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); console.log('Connected:', accounts[0]); } catch (error) { console.log('User rejected the connection'); } } connectWallet();
Attempts:
2 left
💡 Hint
What does the catch block do?
✗ Incorrect
If the user rejects the connection, the request promise throws an error caught by catch, which logs 'User rejected the connection'.
🔧 Debug
advanced2:00remaining
Why does this MetaMask connection code fail with a TypeError?
Look at this code snippet. It throws a TypeError: window.ethereum.request is not a function. What is the cause?
Blockchain / Solidity
async function connect() { if (!window.ethereum) { console.log('No MetaMask'); return; } try { const accounts = await window.ethereum.send('eth_requestAccounts'); console.log('Account:', accounts[0]); } catch (e) { console.log('Error:', e.message); } } connect();
Attempts:
2 left
💡 Hint
Check the MetaMask API for the correct method to request accounts.
✗ Incorrect
The send method is deprecated and may not exist or behave differently. The correct method is window.ethereum.request with the method name.
🧠 Conceptual
advanced1:30remaining
What is the purpose of 'eth_requestAccounts' method in MetaMask?
In MetaMask integration, what does calling window.ethereum.request({ method: 'eth_requestAccounts' }) do?
Attempts:
2 left
💡 Hint
Think about what connecting a wallet means.
✗ Incorrect
The method asks the user to approve connection and returns their wallet addresses if approved.
❓ Predict Output
expert3:00remaining
What is the output of this MetaMask connection code with event listener?
This code connects to MetaMask and listens for account changes. What will it print if the user switches to account '0xABC123' after connection?
Blockchain / Solidity
async function connectAndListen() { if (!window.ethereum) { console.log('MetaMask missing'); return; } try { const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); console.log('Connected:', accounts[0]); window.ethereum.on('accountsChanged', (newAccounts) => { console.log('Account changed to:', newAccounts[0]); }); } catch { console.log('Connection failed'); } } connectAndListen(); // Simulate user switching account after 2 seconds setTimeout(() => { window.ethereum.emit('accountsChanged', ['0xABC123']); }, 2000);
Attempts:
2 left
💡 Hint
The event listener logs when accounts change after initial connection.
✗ Incorrect
First it logs the original connected account, then when the event fires, it logs the new account.