0
0
Blockchain / Solidityprogramming~20 mins

Connecting MetaMask wallet in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MetaMask Connection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
AMetaMask is not installed
BTypeError: window.ethereum.request is not a function
CUser rejected the connection
DConnected account: 0x123...abc
Attempts:
2 left
💡 Hint
Check what happens if window.ethereum is undefined.
Predict Output
intermediate
2: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();
AMetaMask not found
BConnected: 0xabc123...
CUser rejected the connection
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
What does the catch block do?
🔧 Debug
advanced
2: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();
Awindow.ethereum.send is deprecated; use window.ethereum.request instead
Bwindow.ethereum is undefined, so request method is missing
CThe method 'eth_requestAccounts' is invalid
DMissing await keyword before window.ethereum.send
Attempts:
2 left
💡 Hint
Check the MetaMask API for the correct method to request accounts.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of 'eth_requestAccounts' method in MetaMask?
In MetaMask integration, what does calling window.ethereum.request({ method: 'eth_requestAccounts' }) do?
AIt sends a transaction to transfer Ether from the user to the dApp
BIt disconnects the user's wallet from the dApp
CIt fetches the current balance of the user's wallet
DIt requests the user to connect their wallet and returns an array of their account addresses
Attempts:
2 left
💡 Hint
Think about what connecting a wallet means.
Predict Output
expert
3: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);
A
Connected: 0xABC123
Account changed to: 0xOriginal
B
Connected: 0xOriginal
Account changed to: 0xABC123
CMetaMask missing
DConnection failed
Attempts:
2 left
💡 Hint
The event listener logs when accounts change after initial connection.