Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No chainId property on UseWalletProvider #169

Closed
panukettu opened this issue Dec 7, 2021 · 2 comments
Closed

No chainId property on UseWalletProvider #169

panukettu opened this issue Dec 7, 2021 · 2 comments

Comments

@panukettu
Copy link
Contributor

panukettu commented Dec 7, 2021

I mean, am I crazy? But there is no chainId prop to insert into UseWalletProvider but the docs seem to mention it everywhere.

This is the definition for the props from the distributed package 0.13.1

declare type UseWalletProviderProps = {
    children: ReactNode;
    connectors: {
        [key: string]: Connector | ConnectorConfig;
    };
    autoConnect: boolean;
    pollBalanceInterval: number;
    pollBlockNumberInterval: number;
};

What am I missing? My use-case is simple wallet_switchEthereumChain/wallet_addEthereumChain and reflect it on the useWallethook.

@panukettu
Copy link
Contributor Author

Well I managed to solve this by reconnecting to the current connector after doing the switchChain:


export const useChain = () => {
  const [currentChain, setCurrentChain] = useState<EthereumChain>(chains.kovan)

  const { ethereum, connect, connector } = useWallet()

  const handleChainSwitch = async (newChain: EthereumChain) => {
    try {
      await ethereum.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: newChain.chainId }],
      })
      setCurrentChain(newChain)
      if (connector) {
        connect(connector)
      }
    } catch (switchError: any) {
      // user does not have the chain in wallet
      if (switchError.code === CHAIN_NOT_ADDED_ERROR) {
        try {
          await ethereum.request({
            method: 'wallet_addEthereumChain',
            params: [newChain],
          })
        } catch (addError: any) {
          console.log('Chain add failed', addError)
        }
      }
    }
  }

  return {
    currentChain,
    setChain: handleChainSwitch,
  }
}

@jspeirs
Copy link

jspeirs commented Mar 18, 2022

I was trying to figure this out too and found in some other comments that the method for assigning chainId has changed and it hasn't been updated in the docs. Below worked for me.

#173

<UseWalletProvider
    connectors={{
        injected: {
            chainId: [1, 4]
        },
        walletlink: { 
            chainId: [1, 4],
            url: '', 
            appName: '', 
            appLogoUrl: ''
        },
    }}
>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants