0
0
Blockchain / Solidityprogramming~30 mins

Web3.js vs ethers.js in Blockchain / Solidity - Hands-On Comparison

Choose your learning style9 modes available
Comparing Web3.js and ethers.js for Ethereum Interaction
📖 Scenario: You want to interact with the Ethereum blockchain to check the balance of an Ethereum address. Two popular JavaScript libraries for this are Web3.js and ethers.js. This project will help you understand how to set up and use both libraries to get the balance of the same Ethereum address.
🎯 Goal: Build a script using both Web3.js and ethers.js to fetch and display the balance of the Ethereum address 0x742d35Cc6634C0532925a3b844Bc454e4438f44e using each library.
📋 What You'll Learn
Use the exact Ethereum address 0x742d35Cc6634C0532925a3b844Bc454e4438f44e
Create a provider connected to the Ethereum mainnet using Infura
Use both Web3.js and ethers.js
Fetch the balance in wei and convert it to ether
Print the balances as strings with the labels 'Balance with Web3.js:' and 'Balance with ethers.js:'
💡 Why This Matters
🌍 Real World
Developers use Web3.js and ethers.js to build web applications that interact with Ethereum blockchain, such as wallets, decentralized apps, and analytics tools.
💼 Career
Understanding these libraries is essential for blockchain developers, smart contract engineers, and anyone working on Ethereum-based projects.
Progress0 / 4 steps
1
Setup Web3.js with Infura Provider
Create a variable called Web3 by requiring the web3 library. Then create a variable called web3 that connects to the Ethereum mainnet using the Infura URL https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID. Also, create a variable called address and set it to the string '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'.
Blockchain / Solidity
Need a hint?

Use require('web3') to import Web3.js. Then create a new Web3 instance with the Infura URL. Set the address exactly as given.

2
Setup ethers.js with Infura Provider
Create a variable called ethers by requiring the ethers library. Then create a variable called provider using new ethers.providers.JsonRpcProvider with the same Infura URL https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID.
Blockchain / Solidity
Need a hint?

Use require('ethers') to import ethers.js. Then create a new JsonRpcProvider with the Infura URL.

3
Fetch Balance Using Both Libraries
Write an async function called getBalances. Inside it, use web3.eth.getBalance(address) to get the balance in wei and convert it to ether using web3.utils.fromWei. Also, use provider.getBalance(address) to get the balance with ethers.js and convert it to ether using ethers.utils.formatEther. Store the results in variables balanceWeb3 and balanceEthers respectively.
Blockchain / Solidity
Need a hint?

Use await to get balances asynchronously. Convert wei to ether using the utilities from each library.

4
Print the Balances
Inside the getBalances function, print the balances with console.log using the exact format: Balance with Web3.js: {balanceWeb3} and Balance with ethers.js: {balanceEthers}. Then call the getBalances function.
Blockchain / Solidity
Need a hint?

Use console.log with template strings to print the balances. Don't forget to call the getBalances function.