This repo contains the code for the providers-wtf npm package. This package is a utility package that allows you to convert easily between viem PublicClient and WalletClient to ethers providers (JsonRpcProvider, Web3Provider and WebsocketProvider).
The reason why I created this package is because there are a lot of web3 SDKs (such as Uniswap, Li.Fi, etc.) that currently do not support viem as a signer/provider, and instead they still work using ethers.
Since a lot of the frontend libraries have switched to viem, this package allows you to easily convert between the two without having to duplicate your code, or to lose your mind to do so.
You can install this package with your favorite package manager:
Using npm:
npm install providers-wtfUsing yarn:
yarn add providers-wtfUsing pnpm:
pnpm add providers-wtfCurrently this package works with the following versions of the viem and ethers libraries:
- viem ^1.4.1
- ethers ^5.7.0
Please note that this package does not support ethers v6 until it will become stable.
The package exports some useful functions to perform the conversion between the providers, allowing you to have a seamless experience between the two.
These are the currently available methods, with more coming soon:
-
toEthersJsonRpcProvider- converts aPublicClientto aJsonRpcProvider -
toEthersWeb3Provider- converts aPublicClientto aWeb3Provider -
toEthersWsProvider- converts aPublicClientto aWebsocketProvider -
toEthersWeb3ProviderWithSigner- converts aWalletClientto aWeb3Provider -
toViemPublicClient- converts aJsonRpcProviderto aPublicClient -
toViemWsPublicClient- converts aWebsocketProviderto aPublicClient
import { createPublicClient, http } from 'viem'
import { gnosis } from 'viem/chains'
import { toEthersJsonRpcProvider } from 'providers-wtf'
const publicClient = createPublicClient({
chain: gnosis,
transport: http(<YOUR_PROVIDER_URL>)
})
const ethersProvider = toEthersJsonRpcProvider(publicClient)import { createPublicClient, http } from 'viem'
import { gnosis } from 'viem/chains'
import { toEthersWeb3Provider } from 'providers-wtf'
const publicClient = createPublicClient({
chain: gnosis,
transport: http(<YOUR_PROVIDER_URL>)
})
const ethersProvider = toEthersWeb3Provider(publicClient)import { createPublicClient, websocket } from 'viem'
import { gnosis } from 'viem/chains'
import { toEthersWsProvider } from 'providers-wtf'
const publicClient = createPublicClient({
chain: gnosis,
transport: websocket(<YOUR_PROVIDER_URL>)
})
const ethersProvider = toEthersWsProvider(publicClient)import { createWalletClient, custom } from "viem";
import { gnosis } from "viem/chains";
import { toEthersWeb3ProviderWithSigner } from "providers-wtf";
const client = createWalletClient({
chain: gnosis,
transport: custom(window.ethereum),
});
const provider = toEthersWeb3ProviderWithSigner(client);import { providers } from "ethers";
import { toViemPublicClient } from "providers-wtf";
const provider = new providers.JsonRpcProvider(<YOUR_PROVIDER_URL>)
const publicClient = await toViemPublicClient(provider)import { providers } from "ethers";
import { toViemWsPublicClient } from "providers-wtf";
const provider = new providers.WebsocketProvider(<YOUR_PROVIDER_URL>)
const publicClient = await toViemWsPublicClient(provider)Please have a look in the CONTRIBUTING.md file for more information.
Please have a look in the CODE_OF_CONDUCT.md file for more information.