Here is a draft of the article:
Metamask: MetaMask eth_requestAccounts
does not extract wallet address after login
I’m integrating MetaMask into my Next.js app using Wagmi and I’m running into an issue where the wallet address isn’t being retrieved immediately after connecting to MetaMask. The application will request a signature through a Web3 browser before requesting the wallet address.
Problem:
When a user connects to MetaMask, it sends an eth_requestAccounts
request to get the wallet address. However, this process usually takes about 1-2 seconds and returns wallet addresses in JSON format. Unfortunately, after logging in to MetaMask using the Wagmi web browser, the application does not immediately request wallet addresses.
Problem:
As a result, my Next.js application requests a signature via the eth_requestAccounts
API call every time I ask to perform a signature using MetaMask. This can lead to:
- Frequent requests: The user is asked to re-authenticate approximately every second, which can be annoying and disrupt his workflow.
- Unnecessary errors
: If the wallet address is loaded incorrectly, my app may throw errors when trying to access it.
How to fix:
To solve this problem, it is necessary to correctly process the eth_requestAccounts
response from MetaMask. Here are some approaches:
1. Use the onSuccess
hook
You can use the onSuccess
hook provided by Wagmi to wait for the wallet address after requesting it with eth_requestAccounts
. This ensures that your application waits for the address before requesting the signature again.
import { useWagmi } from 'wagmi';
import MetaMaskProvider from 'metamask-provider';
const MyApp = ({ Component, pageProps }) => {
const { connect } = useWagmi();
return (
);
};
export MyApp by default;
2. Use the onError
hook
Alternatively, you can use the onError
hook provided by Wagmi to handle errors related to MetaMask requests.
import { useWagmi } from 'wagmi';
import MetaMaskProvider from 'metamask-provider';
const MyApp = ({ Component, pageProps }) => {
const { connect } = useWagmi();
return (
);
};
export MyApp by default;
3. Use eth_requestAccounts
with a timeout
If you prefer to handle the response yourself, you can use setTimeout()
to wait for the address and then request the signature again.
{
import { useEffect, useState } from 'react';
import MetaMaskProvider from 'metamask-provider';
const MyApp = ({ Component, pageProps }) => {
const [walletaddress, set walletaddress] = useState('');
const { connect } = useWagmi();
useEffect(() => {
download (
.then(response => response.json())
.then(data => setWalletAddress(data.address))
.catch(error => console.error('Error:', error));
}, [wallet address]);
const handleSign = () => {
fetch(
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
from: wallet address,
to: '',
data: '',
}),
})
.then(response => response.json())
.then(data => console.log('Signed by:', data))
.catch(error => console.error('Error:', error));
};
return (
);
};
These are just a few examples of how you can process an eth_requestAccounts
response from MetaMask in a Next.js application using Wagmi.