Metamask: MetaMask eth_requestAccounts not retrieving wallet address after signing in
Here is a design of the article:
Metamask: Metamask eth_requestaccounts" after registration in
do not call up a item of the item
I integrate Metamask into my next. JS app with Wagmi and meet a problem in which the item of the items' address is not called up immediately after connecting to the metamasking. The app asks for signing via Web3 browser before requesting the address of the wallet.
The problem:
If a user establishes a connection to Metamask, he sends an inquiry fromETH_REQUESTACCOUNTS ‘to call up his item of items. However, this process usually takes about 1-2 seconds to complete and return the items to the item pocket in the JSON format. Unfortunately, after registration at Metamast, the app does not immediately request the items of items with the web3 browser provided by Wagmi.
The problem:
As a result, my Next.JS -app asks every time I try to sign with Metamask via the API call from eth_requestAccounts'. This can be:
* Frequent inquiries : The user is asked to be authenticated every second or something again, which can annoy his workflow.
* Unnecessary errors : If the item of the item does not be called up correctly, my app can raise errors if you try to access it.
How to repair:
To fix this problem, you must treat the answerETH_REQUESTACCOUNTS ‘from Metamask properly. Here are some approaches:
1. Use OnSuccess
Hook
You can use the “OnSuccess” hook provided by Wagmi to wait for the item of the item after you have requested it with ETH_REQUESTACCOUTS". This ensures that your app is waiting for the address before asking again after the signing.
JSX
Import {usewagmi} from ‘Wagmi’;
Import the metamask provider of ‘Metamask provider’;
Const myapp = ({component, pageprops}) => {
Const {Connect} = Usewagmi ();
return (
);
};
Export Standard MyApp;
`
2. Use Oneror
Hook
Alternatively, you can use the “Oneror” hook provided by Wagmi to process errors regarding metamask requests.
`JSX
Import {usewagmi} from ‘Wagmi’;
Import the metamask provider of ‘Metamask provider’;
Const myapp = ({component, pageprops}) => {
Const {Connect} = Usewagmi ();
return (
);
};
Export Standard MyApp;
`
3. Use ETH_REQUESTACOUNTS
If you prefer to treat the answer yourself, you can wait "Settimeout () 'for the address before you sign again.
JSX
Import {useeffect, usestate} from ‘react’;
Import the metamask provider of ‘Metamask provider’;
Const myapp = ({component, pageprops}) => {
Const [Walletdadress, Set Wallertaddress] = Usustate (”);
Const {Connect} = Usewagmi ();
useeffect (() => {
Fetch (
.then (response => response.json ())
.then (data => setwallertaddress (data.address))
.Catch (error => console.error ('error:', error));
}, [Walletddress]);
Const Handlesign = () => {
fetch (
Method: ‘Post’,
Header: {‘INTALT type’: ‘Application/Json’},
Body: json.stringify ({{{
by: balletadress,
To: ”,
Data: ”,
}),
})
.then (response => response.json ())
.then (data => console.log (‘signed:’, data))
.Catch (error => console.error (‘error:’, error));
};
return (
Metamask provider>
);
};
`
These are just a few examples of how you can handle Wagmi with the answer from `ETH_REQUESTACSTACSTACTACOUNTS from METAMASK.