0
0
Blockchain / Solidityprogramming~10 mins

Connecting MetaMask wallet in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if MetaMask is installed.

Blockchain / Solidity
if (typeof window.ethereum [1] 'undefined') {
  console.log('MetaMask is installed!');
} else {
  console.log('Please install MetaMask.');
}
Drag options to blanks, or click blank then click option'
A===
B!==
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of !== causes wrong detection.
Using = instead of comparison operator.
2fill in blank
medium

Complete the code to request account access from MetaMask.

Blockchain / Solidity
const accounts = await window.ethereum.request({ method: '[1]' });
Drag options to blanks, or click blank then click option'
Aeth_requestAccounts
Beth_accounts
Ceth_getBalance
Deth_sendTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'eth_accounts' only returns accounts if already connected.
Using 'eth_getBalance' is for balance, not accounts.
3fill in blank
hard

Fix the error in the code to handle MetaMask connection errors.

Blockchain / Solidity
try {
  const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
  console.log('Connected account:', [1]);
} catch (error) {
  console.error('User rejected the request');
}
Drag options to blanks, or click blank then click option'
Aaccount[0]
Baccount
Caccounts
Daccounts[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'account' instead of 'accounts'.
Using 'accounts' directly prints the array, not the first account.
4fill in blank
hard

Fill both blanks to listen for account changes and log the new account.

Blockchain / Solidity
window.ethereum.on('[1]', (accounts) => {
  console.log('Account changed to:', [2]);
});
Drag options to blanks, or click blank then click option'
AaccountsChanged
BchainChanged
Caccounts[0]
Daccounts
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'chainChanged' event instead of 'accountsChanged'.
Logging the whole accounts array instead of the first account.
5fill in blank
hard

Fill all three blanks to create a function that connects MetaMask and returns the first account.

Blockchain / Solidity
async function connectMetaMask() {
  if (typeof window.ethereum [1] 'undefined') {
    try {
      const accounts = await window.ethereum.request({ method: '[2]' });
      return [3];
    } catch (error) {
      console.error('Connection failed');
      return null;
    }
  } else {
    console.log('MetaMask not installed');
    return null;
  }
}
Drag options to blanks, or click blank then click option'
A!==
B===
Ceth_requestAccounts
Daccounts[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of !== for existence check.
Using wrong method name for request.
Returning the whole accounts array instead of first account.