3

I'm currently working on "How to build an NFT Marketplace from Scratch" and, Implementing wallet connection.

The wallet connect Button is always shows as "connected" even if I disconnect wallet.

useEffect(() => {
let val = window.ethereum.isConnected();
console.log(val);
if (val) {
  console.log("here");
  getAddress();
  toggleConnect(val);
  updateButton();
}

window.ethereum.isConnected() is always retuning true. How can I fix it?always shows as connected

2 Answers 2

4

window.ethereum.isConnected() not refers to connection about user account!

In details, this function refers if the provider can make RPC requests to the current chain.

From documentation:

Note that this method has nothing to do with the user's accounts.

You may often encounter the word "connected" in reference to whether a web3 site can access the user's accounts. In the provider interface, however, "connected" and "disconnected" refer to whether the provider can make RPC requests to the current chain.

You can read more here.

1
  • Thank you for the information.
    – 吉川遼
    Aug 29, 2022 at 6:50
2

the problem is you are only toggling once in the if-statement (if: true / isConnected = true) but not the other way around (isConncted = false) does not trigger toggleConnect. In effect, you are just setting the button's color to green/connected, but never to red/disconnected since your negative case is missing in the code.

What you really need is a updateButton(val) function without the checking the cases beforehand - then passing on the result of window.ethereum.isConnected() to that function directly.

A one-liner with modifications in your updateButton function could be:

useEffect(() => {
  updateButton(window.ethereum.isConnected());
}

You should rename the updateButton function to something more meaningful like updateConnectionButton()

3
  • I rewrote update function, But it doesn't work. I think the problem is window.ethereum.isConnected() always returning true even if I disconnect wallet. It shows true in console.
    – 吉川遼
    Aug 23, 2022 at 13:41
  • You could use the browser console to isolate the problem. Open up your inspection tools in FF/Chrome, type window.ethereum.isConnected() and you'll get false or true. If it is still always connected, I'd suggest uninstallting metamask and ANY other wallet provider... do you have multiple installed? Try another "freshly" installed browser please.
    – AirUp
    Aug 23, 2022 at 14:48
  • In Chrome console, It shows true. So I uninstalled metamsk and phantom. The site gets error and show nothing. Maybe I should write logic for connecting wallet from scratch. Thanks for tips and help. That was so helpful.
    – 吉川遼
    Aug 23, 2022 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.