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

WalletConnect on activate returns incorrect provider.getBalance()

Open uyrussell21 opened this issue 3 years ago • 4 comments

When connecting WalletConnect using activate method, provider.getBalance returns incorrect balance. This is due to how defaultChainId uses whichever has the lowest chainId from the rpc options

this.defaultChainId = defaultChainId ?? Number(Object.keys(this.rpc)[0])

However, when connecting via connectEagerly method, provider.getBalance returns the correct balance with the current chain.

Is this a bug? I understand errors will be raised if RPC chain ID is not included in the rpc configuration, but what about supported chain IDs?

So my hacky workaround was to reassign eagerConnection to the walletConnect connector with the correct chainId

    await walletConnect.activate();
    const chainId = await walletConnect.provider?.request({ method: "eth_chainId" });
    // walletConnect.rpc is a private property
    if (!chainId || !Object.keys(walletConnect.rpc).includes(chainId.toString())) return;
    // Reset eagerConnection
    walletConnect.eagerConnection = undefined; // private property
    await walletConnect.isomorphicInitialize(chainId); // private method
    // update useWeb3React states
    await walletConnect.connectEagerly();

uyrussell21 avatar Jul 20 '22 09:07 uyrussell21

When connecting WalletConnect using activate method, provider.getBalance returns incorrect balance. This is due to how defaultChainId uses whichever has the lowest chainId from the rpc options

this.defaultChainId = defaultChainId ?? Number(Object.keys(this.rpc)[0])

However, when connecting via connectEagerly method, provider.getBalance returns the correct balance with the current chain.

Is this a bug? I understand errors will be raised if RPC chain ID is not included in the rpc configuration, but what about supported chain IDs?

So my hacky workaround was to reassign eagerConnection to the walletConnect connector with the correct chainId

    await walletConnect.activate();
    const chainId = await walletConnect.provider?.request({ method: "eth_chainId" });
    // walletConnect.rpc is a private property
    if (!chainId || !Object.keys(walletConnect.rpc).includes(chainId.toString())) return;
    // Reset eagerConnection
    walletConnect.eagerConnection = undefined; // private property
    await walletConnect.isomorphicInitialize(chainId); // private method
    // update useWeb3React states
    await walletConnect.connectEagerly();

648253 avatar Jul 20 '22 21:07 648253

I have the same issue. How do we make the same supportedChain Ids functionality work in this version 8? Version is quite confusing for me.

niraj-khatiwada avatar Aug 08 '22 18:08 niraj-khatiwada

When connecting WalletConnect using activate method, provider.getBalance returns incorrect balance. This is due to how defaultChainId uses whichever has the lowest chainId from the rpc options

this.defaultChainId = defaultChainId ?? Number(Object.keys(this.rpc)[0])

However, when connecting via connectEagerly method, provider.getBalance returns the correct balance with the current chain.

Is this a bug? I understand errors will be raised if RPC chain ID is not included in the rpc configuration, but what about supported chain IDs?

So my hacky workaround was to reassign eagerConnection to the walletConnect connector with the correct chainId

    await walletConnect.activate();
    const chainId = await walletConnect.provider?.request({ method: "eth_chainId" });
    // walletConnect.rpc is a private property
    if (!chainId || !Object.keys(walletConnect.rpc).includes(chainId.toString())) return;
    // Reset eagerConnection
    walletConnect.eagerConnection = undefined; // private property
    await walletConnect.isomorphicInitialize(chainId); // private method
    // update useWeb3React states
    await walletConnect.connectEagerly();

Did you figure out the solution?

niraj-khatiwada avatar Aug 08 '22 18:08 niraj-khatiwada

I have the same issue. How do we make the same supportedChain Ids functionality work in this version 8? Version is quite confusing for me.

I have only used v8 and not previous versions. I am currently using this workaround and is good enough for me

uyrussell21 avatar Aug 10 '22 06:08 uyrussell21