The window.ethereum
object is injected into the browser by Ethereum-enabled browser extensions like MetaMask. It exposes various methods to interact with the Ethereum network.
Here are some commonly used methods available on window.ethereum
:
- Requests access to the user's Ethereum accounts (MetaMask will ask the user to allow the request).
- Usage:
await window.ethereum.request({ method: 'eth_requestAccounts' });
- Returns the current chain ID of the connected network.
- Usage:
const chainId = await window.ethereum.request({ method: 'eth_chainId' }); console.log(chainId); // Example: '0x1' for Ethereum mainnet
- Returns a list of Ethereum accounts available in the user's wallet (MetaMask).
- Usage:
const accounts = await window.ethereum.request({ method: 'eth_accounts' }); console.log(accounts); // List of accounts
- Returns the current gas price in wei.
- Usage:
const gasPrice = await window.ethereum.request({ method: 'eth_gasPrice' }); console.log(gasPrice); // Gas price in wei
- Returns the balance of a given address in wei.
- Usage:
const balance = await window.ethereum.request({ method: 'eth_getBalance', params: ['0xAddressHere', 'latest'] }); console.log(balance); // Balance in wei
- Sends a transaction to the Ethereum network (you need to pass a transaction object).
- Usage:
const tx = await window.ethereum.request({ method: 'eth_sendTransaction', params: [{ from: '0xYourAddressHere', to: '0xRecipientAddressHere', value: '0xAmountInWei', // For example: '0x29a2241af62c0000' for 0.1 ETH }], }); console.log(tx); // Transaction hash
- Signs a message using the selected account.
- Usage:
const message = 'Hello, Ethereum!'; const signature = await window.ethereum.request({ method: 'eth_sign', params: ['0xYourAddressHere', message], }); console.log(signature); // The signature of the message
- Used for signing typed data (e.g., for EIP-712 compatible signatures).
- Usage:
const typedData = { types: { EIP712Domain: [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' }, ], }, domain: { name: 'MyApp', version: '1' }, message: { from: '0xYourAddressHere' }, }; const signature = await window.ethereum.request({ method: 'eth_signTypedData', params: ['0xYourAddressHere', typedData], }); console.log(signature); // The signature
- Used to subscribe to certain Ethereum events (e.g., new blocks, pending transactions, etc.).
- Usage:
const subscription = await window.ethereum.request({ method: 'eth_subscribe', params: ['newHeads'] }); console.log(subscription);
- This method was used in older versions of MetaMask to request account access. It is now deprecated in favor of
eth_requestAccounts
.
- Checks if the current provider (e.g., MetaMask) is connected.
- Usage:
const isConnected = window.ethereum.isConnected(); console.log(isConnected); // true if connected, false if not
- In newer versions of MetaMask (and other providers),
request
is a generic method that combines the functionality of previous methods likesend
andsendAsync
. - Usage:
const accounts = await window.ethereum.request({ method: 'eth_accounts' }); console.log(accounts); // List of accounts
- You can listen for various events like account changes and network changes.
- Usage:
window.ethereum.on('accountsChanged', (accounts) => { console.log('Accounts changed:', accounts); }); window.ethereum.on('chainChanged', (chainId) => { console.log('Chain changed:', chainId); });
These are some of the most commonly used methods in window.ethereum
. They allow you to interact with the Ethereum blockchain and perform actions like account management, sending transactions, and subscribing to events.
let me know if there is any changes