Best JS library to use with Web3auth: Viem, ethers or web3.js
In the rapidly evolving world of web development and blockchain technology, choosing the right JavaScript library is crucial for building efficient and secure applications. With the integration of blockchain functionalities becoming more prevalent, libraries like Viem, Web3.js, and Ethers have become essential tools for developers. This article explores these libraries in the context of Web3Auth, a decentralized authentication platform that leverages the blockchain for secure user authentication without compromising privacy.
Viem: A rising star in blockchain integration
Viem, although relatively new to the scene, is quickly making a name for itself among developers who prioritize smooth integration with different blockchains. Viem offers a robust framework that simplifies the process of building decentralized applications (dApps). Its architecture is tailored for high performance and is particularly beneficial for projects that require real-time data from the blockchain.
web: https://viem.sh/
github: https://github.com/wevm/viem
docs: https://viem.sh/docs/getting-started
Web3.js: The Veteran Player
Web3.js has been the backbone of Ethereum-related web projects for many years. As a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket, it provides the necessary tools to send transactions, interact with smart contracts, and access blockchain data. Its widespread adoption and support have made it a reliable choice for developers integrating Web3Auth. However, it's worth noting that despite its robust features and reliability, Viem has recently surpassed Web3.js in terms of downloads, indicating a shift in developer preferences.
web: https://web3js.org/
github: https://github.com/web3/web3.js
docs: https://docs.web3js.org/
Ethers.js: Leading the pack
Ethers.js is the most downloaded library among the three, signifying its widespread popularity and trust within the developer community. It is a compact and complete library designed to interact with the Ethereum Blockchain and its ecosystem. The library is renowned for its ease of use, comprehensive features, and fine-tuned control over blockchain operations. Ethers.js supports fully typed custom contracts and has a straightforward accessible API.
web: https://ethers.org/
github: https://github.com/ethers-io/ethers.js
docs: https://docs.ethers.org/
Comparison and Considerations
When choosing a JavaScript library for Web3Auth integration, several factors need consideration:
Popularity and Community Support: Ethers.js leads with the highest download numbers, suggesting a large community and continuous updates. But web3.js has a lot of stars on Github. So both communities are working great and having two big libraries is great for decentralization and not only depending on one.
Ease of Use: Ethers.js and Viem are praised for their user-friendly APIs, which are crucial for developers aiming for quick deployments and maintenance.
Feature Set: All three libraries offer comprehensive features, but Ethers.js often receives praise for its detailed and developer-friendly documentation and features.
Integration with Wagmi and Rainbowkit: Wagmi was built on top of Viem and Rainbowkit was built on top of Wagmi, so if you are going to choose any of these libraries. It would be an easy decision to go for Viem.
Code
Let’s make a simple comparison of how to get the public address:
Ethers
import { ethers } from "ethers";
…
const ethersProvider = new ethers.BrowserProvider(this.provider);
const signer = await ethersProvider.getSigner();
// Get the user's Ethereum public address
const address = signer.getAddress();
web3.js
import Web3 from "web3";
…
const web3 = new Web3(this.provider);
// Get the user's Ethereum public address
const address = await web3.eth.getAccounts();
Viem
import { createWalletClient, custom} from 'viem'
import { mainnet } from 'viem/chains'
…
const walletClient = createWalletClient({
chain: mainnet,
transport: custom(this.provider)
});
const address = await walletClient.getAddresses();
How to implement in Web3auth PnP SDK
In our EVM examples, you can choose which file you are going to use and decide the library to connect the blockchain. Comment and uncomment your preferred library and start using it.
import RPC from "./web3RPC"; // for using web3.js
//import RPC from "./ethersRPC"; // for using ethers.js
//import RPC from "./viemRPC"; // for using viem
Conclusion
In conclusion, while Web3.js has been a staple in Ethereum development, Ethers.js currently leads in popularity and downloads, reflecting its robustness and developer trust. However, Viem, with its recent surge in downloads surpassing Web3.js, is proving to be a strong contender, especially for developers looking for libraries that integrate Wagmi. Each library has its strengths and specialties, and the choice largely depends on specific project requirements, developer familiarity, and the particular features needed for integration with Web3Auth.
Subscribe to my newsletter
Read articles from Tomas Rawski directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Tomas Rawski
Tomas Rawski
code, arts and earth keeper