providers-wtf v1.0.5
providers-wtf
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
).
❓ Why is this even a package
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.
📦 Installation
You can install this package with your favorite package manager:
Using npm:
npm install providers-wtf
Using yarn:
yarn add providers-wtf
Using pnpm:
pnpm add providers-wtf
🔨 Compatibility
Currently 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.
📖 Usage
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 aPublicClient
to aJsonRpcProvider
toEthersWeb3Provider
- converts aPublicClient
to aWeb3Provider
toEthersWsProvider
- converts aPublicClient
to aWebsocketProvider
toEthersWeb3ProviderWithSigner
- converts aWalletClient
to aWeb3Provider
toViemPublicClient
- converts aJsonRpcProvider
to aPublicClient
toViemWsPublicClient
- converts aWebsocketProvider
to aPublicClient
PublicClient
to JsonRpcProvider
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)
PublicClient
to Web3Provider
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)
PublicClient
to WebsocketProvider
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)
WalletClient
to Web3Provider
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);
JsonRpcProvider
to PublicClient
import { providers } from "ethers";
import { toViemPublicClient } from "providers-wtf";
const provider = new providers.JsonRpcProvider(<YOUR_PROVIDER_URL>)
const publicClient = await toViemPublicClient(provider)
WebsocketProvider
to PublicClient
import { providers } from "ethers";
import { toViemWsPublicClient } from "providers-wtf";
const provider = new providers.WebsocketProvider(<YOUR_PROVIDER_URL>)
const publicClient = await toViemWsPublicClient(provider)
🤝 Contributing
Please have a look in the CONTRIBUTING.md file for more information.
📝 Code of Conduct
Please have a look in the CODE_OF_CONDUCT.md file for more information.