Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

huangapple go评论113阅读模式
英文:

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

问题

我正在尝试通过在单击按钮时启动登录流程来连接到Chrome上的Metamask扩展。但是,如果用户在单击按钮后关闭登录弹出窗口,Chrome不会抛出代码4001异常,控制台日志完全为空。之后,当我再次单击按钮时,它不再弹出登录弹出窗口,而是抛出“RPC错误:已经在处理eth_requestAccounts,请等待。”

但是,当我在Edge中尝试时,控制台会正常抛出异常(这是预期的结果)。刷新Chrome页面不起作用,必须关闭并重新打开整个浏览器才能重置它。

查看扩展图标时,我注意到在Edge中,每当我关闭登录按钮时,表示挂起交易的蓝色“1”通知将消失。但是在Chrome中,情况并非如此,蓝色“1”通知仍然存在。

以下是按钮的相关代码,如果有帮助的话。非常感谢您的协助:

import { Card, CardContent, Button, Grid } from '@material-ui/core';
import React, { useEffect, useState } from "react";
import MetaMaskOnboarding from '@metamask/onboarding';

function MetaMaskConnectButton(props) {
    const forwarderOrigin = 'http://localhost:3000';
    const onboarding = new MetaMaskOnboarding({ forwarderOrigin });
    const { ethereum } = window;
    const [buttonText, setButtonText] = useState('Connect MetaMask');
    const [isButtonDisabled, setIsButtonDisabled] = useState(false);
    const [mmAccounts, setMmAccounts] = useState('');

    useEffect(() => {
        if (!isMetaMaskInstalled()) {
            setButtonText('Click here to install MetaMask!');
        }
    }, []);

    useEffect(() => {
        if (mmAccounts == null || mmAccounts.length === 0)
            setIsButtonDisabled(false);
        else
            setIsButtonDisabled(true);
    }, [mmAccounts]);

    async function metaMaskClientCheck() {
        if (!isMetaMaskInstalled()) {
            await metamaskInstall();
            setIsButtonDisabled(true);
        } else {
            await metamaskConnect();
            setIsButtonDisabled(false);
        }
    }

    const isMetaMaskInstalled = () => {
        return Boolean(ethereum && ethereum.isMetaMask);
    }

    const metamaskInstall = async () => {
        setButtonText('Onboarding in progress...');
        setIsButtonDisabled(true);
        onboarding.startOnboarding();
    }

    const metamaskConnect = async () => {
        setIsButtonDisabled(true);
        setButtonText('Connecting to MetaMask...');
        try {
            let accounts = await ethereum.request({ method: 'eth_requestAccounts' });
            console.log('Ethereum Request over');
            let account = accounts[0];
            setMmAccounts(account);
            setButtonText('MetaMask Connected');
        } catch (error) {
            console.error(error);
            setIsButtonDisabled(false);
            setButtonText('Connect MetaMask');
        }
    };

    return (
        <Grid container xs={12} sm={12}>
            <Grid container xs={6} sm={6} justify='flex-start'>
                <Button
                    disabled={isButtonDisabled}
                    variant='contained'
                    color='secondary'
                    className='button-blue originalText'
                    onClick={() => {
                        metaMaskClientCheck()
                    }}
                    classes={{ root: 'ng-button-filled-secondary' }}
                >
                    {buttonText}
                </Button>
            </Grid>
        </Grid>
    )
}

export default MetaMaskConnectButton;

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

英文:

I am trying to connect to the Metamask extension on Chrome by having a button that initiates the log in process on click. However in the event the user closes the log in pop up after clicking the button, no code 4001 exception is thrown in Chrome, the console log is completely empty. After that when I click the button again, it will no longer bring up the log in pop up but instead throw "RPC Error: Already processing eth_requestAccounts. Please wait."
Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

But when I tried in Edge, it throws just fine in the console (this is the expected result):
Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

Refreshing the Chrome page does not help, I have to close and reopen the entire browser to reset it.

Looking into the extension icon, I noticed that in Edge, whenever I close the login button, the blue "1" notification which signifies pending transactions will be gone:

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

However in Chrome that is not the case, the blue "1" notification remains:

Metamask not returning "code 4001 User rejected the request." when closing login popup in Chrome, but works in Edge

Here is the relevant code for the button if it helps. Any assistance is greatly appreciated:

import { Card, CardContent, Button, Grid } from &#39;@material-ui/core&#39;
import React, { useEffect, useState } from &quot;react&quot;;
import MetaMaskOnboarding from &#39;@metamask/onboarding&#39;;
function MetaMaskConnectButton(props) {&#160; &#160; 
const forwarderOrigin = &#39;http://localhost:3000&#39;;&#160; &#160; 
const onboarding = new MetaMaskOnboarding({ forwarderOrigin });&#160; &#160; 
const { ethereum } = window;&#160; &#160; 
const [buttonText, setButtonText] = useState(&#39;Connect MetaMask&#39;)&#160; &#160; 
const [isButtonDisabled, setIsButtonDisabled] = useState(false)&#160; &#160; 
const [mmAccounts, setMmAccounts] = useState(&#39;&#39;)&#160; &#160; 
useEffect(() =&gt; {&#160; &#160; &#160; &#160; 
if (!isMetaMaskInstalled()) {&#160; &#160; &#160; &#160; &#160; &#160; 
setButtonText(&#39;Click here to install MetaMask!&#39;)&#160; &#160; &#160; &#160; 
}
}, [])&#160; &#160; 
useEffect(() =&gt; {&#160; &#160; &#160; &#160; 
if (mmAccounts == null || mmAccounts.length === 0)
&#160; &#160; &#160; &#160; &#160; &#160; setIsButtonDisabled(false) &#160; &#160; &#160; 
else&#160; &#160; &#160; &#160; &#160; &#160; 
setIsButtonDisabled(true)&#160; &#160; 
}, [mmAccounts])&#160; &#160; 
async function metaMaskClientCheck() {&#160; &#160; &#160; &#160; 
if (!isMetaMaskInstalled()) {&#160; &#160; &#160; &#160; &#160; &#160; 
await metamaskInstall()&#160; &#160; &#160; &#160; &#160; &#160; 
setIsButtonDisabled(true)&#160; &#160; &#160; &#160; 
} 
else {&#160; &#160; &#160; &#160; &#160; &#160; 
await metamaskConnect()&#160; &#160; &#160; &#160; &#160; &#160; 
setIsButtonDisabled(false)&#160; &#160; &#160; &#160; 
}&#160; &#160; 
}&#160; &#160; 
//Function checks if the MetaMask extension is installed&#160; &#160; 
const isMetaMaskInstalled = () =&gt; {&#160; &#160; &#160; &#160; 
//Have to check the ethereum binding on the window object to see if it&#39;s installed
&#160; &#160; &#160; &#160;   return Boolean(ethereum &amp;&amp; ethereum.isMetaMask);&#160; &#160; 
}&#160; &#160; 
const metamaskInstall = async () =&gt; {&#160; &#160; &#160; &#160; 
setButtonText(&#39;Onboarding in progress...&#39;)&#160; &#160; &#160; &#160; 
setIsButtonDisabled(true)&#160; &#160; &#160; &#160; 
onboarding.startOnboarding();&#160; &#160; 
}&#160; &#160; 
const metamaskConnect = async () =&gt; {&#160; &#160; &#160; &#160; 
setIsButtonDisabled(true)&#160; &#160; &#160; &#160; 
setButtonText(&#39;Connecting to MetaMask...&#39;)&#160; &#160; &#160; &#160; 
try {&#160; &#160; &#160; &#160; &#160; &#160; 
// Will open the MetaMask UI&#160; &#160; &#160; &#160; &#160; &#160; 
let accounts = await ethereum.request({ method: &#39;eth_requestAccounts&#39; })&#160;
console.log(&#39;Ethereum Request over&#39;)&#160; &#160; &#160; &#160; &#160; &#160; 
let account = accounts[0]&#160; &#160; &#160; &#160; &#160; &#160; 
setMmAccounts(account)&#160; &#160; &#160; &#160; &#160; &#160; 
setButtonText(&#39;MetaMask Connected&#39;)&#160; &#160; &#160; &#160; 
} catch (error) {&#160; &#160; &#160; &#160; &#160; &#160; 
console.error(error);&#160; &#160; &#160; &#160; &#160; &#160; 
setIsButtonDisabled(false)&#160; &#160; &#160; &#160; &#160; &#160; 
setButtonText(&#39;Connect MetaMask&#39;)&#160; &#160; &#160; &#160; 
}&#160; &#160; 
};&#160; &#160; 
return (&#160; &#160; &#160; &#160; 
&lt;Grid container xs={12} sm={12}&gt;
&lt;Grid container xs={6} sm={6} justify=&#39;flex-start&#39;&gt;
&lt;Button&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 
disabled={isButtonDisabled}&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 
variant=&#39;contained&#39;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 
color=&#39;secondary&#39;
className=&#39;button-blue originalText&#39;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; onClick={() =&gt; {
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; metaMaskClientCheck()
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }}
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; classes={{ root: &#39;ng-button-filled-secondary&#39; }}
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; {buttonText}
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;/Button&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &lt;/Grid&gt;
&#160; &#160; &#160; &#160; &lt;/Grid&gt;
&#160; &#160; )}
export default MetaMaskConnectButton

答案1

得分: 1

Chrome中的Metamask表现出奇怪的行为。我刚刚在Metamask Chrome上遇到了一个问题,在此处提问。在Mozilla和Brave上运行正常,但在Chrome上却不行。我已经禁用了所有的扩展(你可以尝试这样做),但错误仍然发生。我的主要计算机是Linux,当我在macOS的Chrome上启动相同的应用程序时,它可以正常运行。

你可以在这里阅读详细的讨论。我认为这个问题是与你的Chrome浏览器和设备有关的。

英文:

Chrome metamask behaves funky. I just had an issue with metamask chrome asked here. It was working on mozilla and brave but not on chrome. I disabled all the extensions (you should try this) but error still occurred. My main machine is Linux and when I started the same app on macos chrome, it worked.

You can read a long discussion here. I think the issue is specific to your chrome and device

答案2

得分: 1

你可以在当前代码中添加超时,并像这样抛出错误:

let timeout;
async function metamaskConnect() {
  setIsButtonDisabled(true);
  setButtonText('连接到 MetaMask...');
  try {
    let accounts = await ethereum.request({ method: 'eth_requestAccounts' });
    console.log('以太坊请求完成');
    let account = accounts[0];
    setMmAccounts(account);
    setButtonText('MetaMask 已连接');
    timeout = setTimeout(() => {
      setIsButtonDisabled(false);
      setButtonText('连接 MetaMask');
    }, 5000);
  } catch (error) {
    console.error(error);
    setIsButtonDisabled(false);
    setButtonText('连接 MetaMask');
  }
}

或者作为另一种选择,也可以使用 Promise.reject 或抛出新的错误。

英文:

You can add a timeout to your current code and thrown an error like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

   let timeout;
async function metamaskConnect() {
setIsButtonDisabled(true);
setButtonText(&#39;Connecting to MetaMask...&#39;);
try {
let accounts = await ethereum.request({ method: &#39;eth_requestAccounts&#39; });
console.log(&#39;Ethereum Request over&#39;);
let account = accounts[0];
setMmAccounts(account);
setButtonText(&#39;MetaMask Connected&#39;);
timeout = setTimeout(() =&gt; {
setIsButtonDisabled(false);
setButtonText(&#39;Connect MetaMask&#39;);
}, 5000);
} catch (error) {
console.error(error);
setIsButtonDisabled(false);
setButtonText(&#39;Connect MetaMask&#39;);
}
}

<!-- end snippet -->

Or maybe use Promise.reject or thrown new Error as another option.

huangapple
  • 本文由 发表于 2023年3月7日 11:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定