Uniswap / web3-react

A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
https://web3-react-mu.vercel.app/
GNU General Public License v3.0
5.54k stars 1.52k forks source link

Eagerly connect walletconnect on v6 #455

Open davidthomasparks opened 2 years ago

davidthomasparks commented 2 years ago

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;
}
adamxyzxyz commented 2 years ago

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

dsldsl commented 1 year ago

bump - any feedback on this? thanks!

stayForward09 commented 1 year ago

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

SmartDev-0205 commented 1 year ago

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