DEV Community

easyoon
easyoon

Posted on • Originally published at devblogeasyoon.xyz

Comparing ethers.js and web3.js as Web3 Regains Popularity

Original Article Link


Comparing ethers.js and web3.js as Web3 Regains Popularity

As Web3 regains attention, interest in ethers.js and web3.js, the primary JavaScript libraries used for Ethereum-based DApp (Decentralized Application) development, is also increasing. While both libraries enable interaction with the Ethereum blockchain, they have some key differences, especially in their development approach. This document compares the two libraries, exploring their characteristics, advantages, disadvantages, and differences in development style.


web3.js

web3.js is an older library that emerged in the early days of the Ethereum ecosystem. It provides a broad range of functionalities, offering all methods for interacting with the blockchain from a single web3 object. It primarily uses a callback function-based API style.

Advantages:

  • It has a long history and is used in many legacy projects.
  • Offers a wider range of features compared to ethers.js.

Disadvantages:

  • Relatively large and heavy, which can impact performance.
  • The API is somewhat complex, leading to a steeper learning curve.
  • Updates are slower compared to ethers.js.
  • Callback-based API can make asynchronous code writing somewhat complex.

ethers.js

ethers.js is a relatively newer library that adheres to modern JavaScript standards and focuses on providing a better developer experience. It is concise and lightweight, offering a modularized API. In particular, it improves development flexibility and security by clearly separating Provider and Signer. It uses a Promise-based API, allowing for concise asynchronous code.

Advantages:

  • Concise and lightweight, providing faster performance.
  • Has a clear API structure separated into Signer and Provider.
    • Signer: Manages private keys and handles transaction signing (enhanced security).
    • Provider: Manages blockchain network connections (easy support for various networks).
  • Provides enhanced security features and pays more attention to private key management.
  • Actively developed and maintained, with rapid reflection of the latest features.
  • Provides excellent documentation.
  • Promise-based API makes asynchronous code concise and readable.

Disadvantages:

  • As a relatively new library, it is not used in as many legacy projects as web3.js.

Provider and Signer: Core Concepts of ethers.js and web3.js

In the blockchain, especially the Ethereum ecosystem, Provider and Signer are crucial concepts. They define how DApps interact with the blockchain. ethers.js and web3.js handle these two concepts differently, leading to significant differences in development approach.


Provider: Read-Only Connection to the Blockchain

The Provider provides read-only access to the blockchain network. It is like a librarian. You can read books (blockchain data) and get information, but you cannot add or modify content in the books.

Key Functions:

  • Retrieving block information (block height, timestamp, etc.)
  • Retrieving transaction information
  • Checking account balances
  • Calling read-only functions (view functions) of smart contracts
  • Checking network status

Signer: Transaction Signing and Execution

The Signer provides the ability to sign transactions using a private key and submit them to the blockchain. It is like someone with a seal. Just as a document (transaction) becomes effective only when stamped, the Signer signs transactions so that they can be recorded on the blockchain.

Key Functions:

  • Private key management (secure storage and access)
  • Transaction creation and signing
  • Calling state-changing functions of smart contracts
  • Sending Ether

Provider and Signer in ethers.js

ethers.js structures its API by clearly separating Provider and Signer. This greatly enhances development flexibility and security.

Provider: Provides various Providers through the ethers.providers module. You can connect using services like Infura, Alchemy, Etherscan, or directly using an RPC URL.

  • Example: const provider = new ethers.providers.InfuraProvider("mainnet", "YOUR_INFURA_PROJECT_ID");

Signer: You can manage private keys using the ethers.Wallet class or connect with wallets like MetaMask.

  • Example (using private key): const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);
  • Example (connecting MetaMask): const provider = new ethers.providers.Web3Provider(window.ethereum); const signer = provider.getSigner();

By separating Provider and Signer in ethers.js, you can gain the following advantages:

  • Enhanced Security: Private keys can be managed securely through wallets without direct management.
  • Increased Flexibility: Various Providers can be easily switched and used.
  • Easy Testing: You can perform tests using mock Signers in the test environment.

Provider and Signer in web3.js

web3.js does not clearly separate Provider and Signer. Although it manages accounts and signs transactions through web3.eth.accounts, it is not as clearly separated as ethers.js.

Provider: Sets the Provider using web3.setProvider().

  • Example: const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_RPC_URL'));

Signer: Signs transactions using web3.eth.accounts.signTransaction(). In this process, you often have to use the private key directly, which can create security vulnerabilities. You can also use wallets like MetaMask, but the integration is not as clean as in ethers.js.


Summary Comparison

Feature ethers.js web3.js
Provider Clearly separated, supports various Providers (Infura, Alchemy, etc.) Set with web3.setProvider()
Signer Clearly separated, Wallet class, easy wallet integration Managed through web3.eth.accounts, may require direct private key management
Security Secure private key management, enhanced security Risk of private key exposure
Flexibility High flexibility, supports various Providers and wallets Relatively low flexibility

ethers.js greatly improves development flexibility, security, and convenience by clearly separating Provider and Signer. On the other hand, web3.js does not have this clear separation, which can make development somewhat complex and create security vulnerabilities. Therefore, when starting a new Web3 project, using ethers.js is generally recommended.


Differences in Development Style

Feature web3.js ethers.js
API Style Single web3 object, callback-based Signer and Provider separated, Promise-based
Asynchronous Processing Handles asynchronous code using callback functions, which can reduce code readability Can write asynchronous code concisely and clearly using Promises (easy to use async/await)
Private Key Management Requires direct private key management (potential security vulnerabilities) Abstracted private key management through Signer (enhanced security)
Network Connection Connection setup using web3.setProvider() Supports various networks and connection methods through Provider (Infura, Alchemy, etc.)

Conclusion

When starting a new Web3 project, using ethers.js is recommended. It provides a better development experience, performance, security, and the latest features. In particular, the separation of Provider and Signer and the Promise-based API are in line with modern development practices and improve code readability and maintainability. However, web3.js may still be a good choice for maintaining existing web3.js projects or in specific situations.


References

Top comments (0)