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

useEthersProvider Example TS error #3923

Closed
1 task done
Pfed-prog opened this issue May 13, 2024 · 2 comments
Closed
1 task done

useEthersProvider Example TS error #3923

Pfed-prog opened this issue May 13, 2024 · 2 comments

Comments

@Pfed-prog
Copy link

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

@Pfed-prog
Copy link
Author

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

@tmm tmm closed this as completed in 6bf5592 May 13, 2024
Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Wagmi version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant