• Core
  • Ethers.js Adapters

Ethers.js Adapters

It is recommended for projects to migrate to viem when using wagmi, but there are some cases where you might still need to use ethers.js in your project:

  • You may want to incrementally migrate ethers.js usage to viem
  • Some third-party libraries & SDKs may only support ethers.js

We have provided reference implementations for viem → ethers.js adapters that you can copy + paste in your project.

Public Client → Provider

Reference Implementation

Copy the following reference implementation into a file of your choice:

src/ethers.ts
import { type PublicClient, getPublicClient } from '@wagmi/core'
import { providers } from 'ethers'
import { type HttpTransport } from 'viem'
 
export function publicClientToProvider(publicClient: PublicClient) {
  const { chain, transport } = publicClient
  const network = {
    chainId: chain.id,
    name: chain.name,
    ensAddress: chain.contracts?.ensRegistry?.address,
  }
  if (transport.type === 'fallback')
    return new providers.FallbackProvider(
      (transport.transports as ReturnType<HttpTransport>[]).map(
        ({ value }) => new providers.JsonRpcProvider(value?.url, network),
      ),
    )
  return new providers.JsonRpcProvider(transport.url, network)
}
 
/** Action to convert a viem Public Client to an ethers.js Provider. */
export function getEthersProvider({ chainId }: { chainId?: number } = {}) {
  const publicClient = getPublicClient({ chainId })
  return publicClientToProvider(publicClient)
}

Usage

Now you can use the getEthersProvider function in your components:

src/example.ts
import { getEthersProvider } from './ethers'
 
function example() {
  const provider = getEthersProvider()
  ...
}

Wallet Client → Signer

Reference Implementation

Copy the following reference implementation into a file of your choice:

src/ethers.ts
import { type WalletClient, getWalletClient } from '@wagmi/core'
import { providers } from 'ethers'
 
export function walletClientToSigner(walletClient: WalletClient) {
  const { account, chain, transport } = walletClient
  const network = {
    chainId: chain.id,
    name: chain.name,
    ensAddress: chain.contracts?.ensRegistry?.address,
  }
  const provider = new providers.Web3Provider(transport, network)
  const signer = provider.getSigner(account.address)
  return signer
}
 
/** Action to convert a viem Wallet Client to an ethers.js Signer. */
export async function getEthersSigner({ chainId }: { chainId?: number } = {}) {
  const walletClient = await getWalletClient({ chainId })
  if (!walletClient) return undefined
  return walletClientToSigner(walletClient)
}

Usage

Now you can use the getEthersSigner function in your components:

src/example.ts
import { getEthersSigner } from './ethers'
 
function example() {
  const signer = getEthersSigner()
  ...
}