web3-react icon indicating copy to clipboard operation
web3-react copied to clipboard

Eagerly connect walletconnect on v6

Open davidthomasparks opened this issue 3 years ago • 4 comments
trafficstars

Hi there,

I've refrained from upgrading to v8 until there's some documentation, so still using v6.

I'm curious if there is a method to eagerly connect wallet connect after an initial activation. For whatever reason on subsequent page reloads it clears localStorage and makes me scan my QR code again.

I've tried adding an additional hook to the useEagerConnect example but it fails once localStorage is cleared, and the walletConnect connector does not have access to the isAuthorized() method the default injected connector does on v6.

Any ideas?

import { useWeb3React } from "@web3-react/core";
import { useEffect, useState } from "react";
import { injected } from "../connectors";
import { walletConnectInjected, rainbowWallectConnect } from "../connectors";

export default function useEagerConnect() {
  const { activate, active, library } = useWeb3React();

  const [tried, setTried] = useState(false);

  console.log(rainbowWallectConnect)

  useEffect(() => {

    injected.isAuthorized().then((isAuthorized) => {
      if (isAuthorized) {
        activate(injected, undefined, true).catch(() => {
          setTried(true);
        });
      } else {

        setTried(true);
      }
    });
  }, [activate]);

  useEffect(() => {
    const walletConnect = window.localStorage.getItem("walletconnect");
    if (walletConnect) {
      const parsedJson = JSON.parse(walletConnect)
      if (parsedJson.connected) {
        activate(rainbowWallectConnect, undefined, true).catch((error) => {
          console.log(error)
          setTried(true);
        });
      }
    }
  }, [activate]);

  // if the connection worked, wait until we get confirmation of that to flip the flag
  useEffect(() => {
    if (!tried && active) {
      setTried(true);
    }
  }, [tried, active]);

  return tried;
}

davidthomasparks avatar Mar 08 '22 02:03 davidthomasparks

hey did you ever solve this? I am having the exact same issue

adamxyzxyz avatar Apr 28 '22 21:04 adamxyzxyz

bump - any feedback on this? thanks!

dsldsl avatar Nov 30 '22 18:11 dsldsl

upgrade web3-react/core from v6 to v8 https://github.com/Uniswap/web3-react/tree/main#upgrading-from-v6 use latest WalletConnect -v2 (v1 depreciated) @web3-react/walletconnect-v2 https://docs.walletconnect.com/2.0/advanced/migration-from-v1.x/dapps/#web3-react

stayForward09 avatar Aug 09 '23 08:08 stayForward09

https://github.com/SmartDev-0205/web3-react-walletconnectv2-dex It is working on my side. Try to check and follow me.

SmartDev-0205 avatar Oct 21 '23 08:10 SmartDev-0205