0
0
Blockchain / Solidityprogramming~10 mins

Connecting MetaMask wallet in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Connecting MetaMask wallet
User clicks Connect button
Check if MetaMask is installed
Request account access
User approves or rejects
Get account address
Display connected account
This flow shows how a user connects their MetaMask wallet by clicking a button, the app checks MetaMask presence, requests access, and handles approval or rejection.
Execution Sample
Blockchain / Solidity
async function connectWallet() {
  if (!window.ethereum) {
    alert('MetaMask not installed');
    return;
  }
  try {
    const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
    console.log('Connected account:', accounts[0]);
  } catch (error) {
    console.error('User rejected the request or error occurred:', error);
  }
}
This code tries to connect to MetaMask and logs the first account if successful.
Execution Table
StepActionCheck/RequestResultNext Step
1User clicks Connect buttonN/ATrigger connectWallet()Check if MetaMask is installed
2Check MetaMask presencewindow.ethereum exists?YesRequest account access
3Request account accesseth_requestAccounts method calledUser approvesGet account address
4Get account addressaccounts array receivedaccounts[0] = '0xABC123...'Display connected account
5Display connected accountConsole logs accountOutput: Connected account: 0xABC123...End
💡 Execution stops after displaying the connected account or if MetaMask is not installed or user rejects access.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
window.ethereumundefined or objectobject (MetaMask present)objectobjectobject
accountsundefinedundefined['0xABC123...']['0xABC123...']['0xABC123...']
accounts[0]undefinedundefined'0xABC123...''0xABC123...''0xABC123...'
Key Moments - 3 Insights
What happens if MetaMask is not installed?
At Step 2 in the execution_table, if window.ethereum is undefined, the code alerts the user to install MetaMask and stops further execution.
What if the user rejects the connection request?
At Step 3, if the user rejects, the promise from eth_requestAccounts is rejected and the code should handle this error (now shown in sample), preventing account retrieval.
Why do we use accounts[0]?
At Step 4, accounts is an array of addresses; accounts[0] is the first connected account, which is usually the active wallet address.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of Step 2?
AUser rejects connection
BMetaMask is not installed
CMetaMask is installed (window.ethereum exists)
DAccount address received
💡 Hint
Check the 'Result' column in Step 2 of the execution_table.
At which step does the user approve the connection?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Next Step' and 'Result' columns in Step 3 of the execution_table.
If the user rejects the connection request, what should happen?
AThe connection process stops without account access
BThe app alerts to install MetaMask
CThe account address is displayed
DThe app automatically retries connection
💡 Hint
Refer to key_moments about user rejection and Step 3 in execution_table.
Concept Snapshot
Connecting MetaMask wallet:
- Check if window.ethereum exists (MetaMask installed)
- Request accounts with 'eth_requestAccounts'
- User approves or rejects connection
- On approval, get accounts array
- Use accounts[0] as connected wallet address
Full Transcript
This visual execution shows how a user connects their MetaMask wallet. First, the user clicks a connect button. The app checks if MetaMask is installed by looking for window.ethereum. If not found, it alerts the user to install MetaMask and stops. If MetaMask is present, the app requests account access using the 'eth_requestAccounts' method. The user can approve or reject this request. If approved, the app receives an array of accounts and uses the first account as the connected wallet address, displaying it. If rejected, the connection stops without access. Variables like window.ethereum and accounts change during these steps, reflecting the connection state.