Intagity Metamask: Window.etherum.request ({method:" account eth_request "})
do not open
As a web developer who uses React, you’re probably experiencing frustration when you try to interact with the Metamask wallet. The problem lies in the way Metamask integrates with the browser web platform. Here is an article that explains the problem and proposes a solution.
The problem: `Window.etherum.request ({method: "account eth_request"})
do not open
When clicking on the button to activateWindow.etherum.request ({method:” account et_request “}), tries to perform the request. However, this request does not seem to open Metamask correctly.
Because the request does not open
The reason for this problem is thatWindow.ethetheum.request () is expected to be a specific URL as a topic, which is used by Metamask to establish a connection with the browser. The name of the provider method "" eth_request accesses "
does not correspond to the effective request made by Metamask when clicking on the button.
The solution: using Window.etherum.Send ({method: 'eth_request accounts'})
To solve this problem, it is necessary to use the correct function that corresponds to your request. The `Send () 'method is used to send requests to the Ethereum network. In this case, you are trying to get an account from Metamask.
Here's how you can change your code:
Jsx
Import React, {Usestate} from "react";
Import window from "window";
Import {Ethers} from 'Metamask/APPS';
Function App () {
Const [Account, set account] = Usestate ('');
// Get the User's Ethereum account address
Const Getcount = Async () => {
Attempt {
Const Account = Wait Window.etherum.request ({method: "eth_request accounts"});
IF (Accounts.Length> 0) {
Account set (account [0] .dress);
}
} Catch (error) {
console.error (error);
}
};
// button to activate the account request
Constance Randombuttonclick = () => {
Getacoun ();
}
Return (
{Account? (
Account: {Account}
): Null}
);
}
Export the default app;
Changes made
We made the following changes to our code:
- We have replacedWindow.etherum.request ({method:” Et_request accounts “})
with
Window.etherum.Send ({method: ‘eth_request account’}).
- Inside theGet Account ()
, we are using the correct URL for the request for Metamask.
- After obtaining the user’s account address, we display it on the page.
By making these changes, now you should be able to get an Ethereum account from Metamask when clicking on the button.