wevm/wagmi

useEthersProvider Example TS error

Pfed-prog opened this issue · 2 comments

Describe the bug

Code from https://wagmi.sh/core/guides/ethers

import { type Config, getClient } from '@wagmi/core'
import { FallbackProvider, JsonRpcProvider } from 'ethers'
import type { Client, Chain, Transport } from 'viem'

export function clientToProvider(client: Client<Transport, Chain>) {
  const { chain, transport } = client
  const network = {
    chainId: chain.id,
    name: chain.name,
    ensAddress: chain.contracts?.ensRegistry?.address,
  }
  if (transport.type === 'fallback') {
    const providers = (transport.transports as ReturnType<Transport>[]).map(
      ({ value }) => new JsonRpcProvider(value?.url, network),
    )
    if (providers.length === 1) return providers[0]
    return new FallbackProvider(providers)
  }
  return new JsonRpcProvider(transport.url, network)
}

/** Action to convert a viem Client to an ethers.js Provider. */
export function getEthersProvider(
  config: Config,
  { chainId }: { chainId?: number } = {},
) {
  const client = getClient(config, { chainId })
  return clientToProvider(client)
}

Provides a typescript error,
image

It definitely fixed the warning per gitroll, but thats an error.

image

Link to Minimal Reproducible Example

No response

Steps To Reproduce

No response

Wagmi Version

latest

Viem Version

latest

TypeScript Version

^5.4.5

Check existing issues

Anything else?

No response

export function useEthersProvider({ chainId }: { chainId?: number } = {}) {
  const client = useClient<Config>({ chainId });

  // Define the provider outside of the conditional statement
  const provider = useMemo(() => {
    if (client) {
      return clientToProvider(client);
    }
  }, [client]);

  return provider

this might fix, not sure, but please try