Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Web3.js vs ethers.js in Blockchain / Solidity - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Web3.js vs ethers.js Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Web3.js contract call
What will be the output of this Web3.js code snippet when calling a contract method that returns a string?
Blockchain / Solidity
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
const contract = new web3.eth.Contract(abi, contractAddress);

async function getName() {
  const name = await contract.methods.name().call();
  console.log(name);
}

getName();
A"MyToken"
Bundefined
CTypeError: contract.methods.name is not a function
DPromise { <pending> }
Attempts:
2 left
💡 Hint
Remember that .call() returns the actual value asynchronously.
Predict Output
intermediate
2:00remaining
Output of ethers.js contract call
What will this ethers.js code print when calling a contract method that returns a number?
Blockchain / Solidity
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
const contract = new ethers.Contract(contractAddress, abi, provider);

async function getTotalSupply() {
  const supply = await contract.totalSupply();
  console.log(supply.toString());
}

getTotalSupply();
ABigNumber { _hex: '0x0f4240', _isBigNumber: true }
Bundefined
C1000000
DTypeError: contract.totalSupply is not a function
Attempts:
2 left
💡 Hint
ethers.js returns BigNumber objects for big integers; use toString() to print.
🔧 Debug
advanced
2:00remaining
Identify the error in Web3.js event subscription
What error will this Web3.js code produce when trying to subscribe to a contract event?
Blockchain / Solidity
const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_PROJECT_ID');
const contract = new web3.eth.Contract(abi, contractAddress);

contract.events.Transfer((error, event) => {
  if (error) console.error(error);
  else console.log(event);
});
ATypeError: contract.events.Transfer is not a function
BNo error, logs Transfer events correctly
CError: Invalid JSON RPC response
DError: WebSocket connection failed
Attempts:
2 left
💡 Hint
Check if the WebSocket URL and event subscription syntax are correct.
Predict Output
advanced
2:00remaining
ethers.js signer transaction output
What will this ethers.js code print after sending a transaction?
Blockchain / Solidity
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
const signer = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, abi, signer);

async function sendTx() {
  const txResponse = await contract.transfer('0xRecipientAddress', 1000);
  console.log(txResponse.hash);
}

sendTx();
ATypeError: contract.transfer is not a function
Bundefined
CError: signer.transfer is not a function
D0x123abc... (transaction hash string)
Attempts:
2 left
💡 Hint
Transaction methods return a transaction response object with a hash property.
🧠 Conceptual
expert
2:00remaining
Key difference between Web3.js and ethers.js providers
Which statement best describes a key difference between Web3.js and ethers.js providers?
Aethers.js providers return BigNumber objects for numeric values, Web3.js returns strings or numbers.
BWeb3.js providers automatically parse all contract events, ethers.js providers do not support events.
CWeb3.js only supports HTTP providers, ethers.js only supports WebSocket providers.
Dethers.js requires manual ABI encoding for all contract calls, Web3.js does this automatically.
Attempts:
2 left
💡 Hint
Think about how each library handles large numbers from the blockchain.

Practice

(1/5)
1. Which of the following is a key difference between Web3.js and ethers.js?
easy
A. ethers.js is a backend-only library, Web3.js is frontend-only.
B. Web3.js only works with Bitcoin, ethers.js only with Ethereum.
C. ethers.js cannot send transactions, Web3.js can.
D. Web3.js is larger and older, while ethers.js is lighter and simpler.

Solution

  1. Step 1: Understand library origins

    Web3.js is an older, larger library designed for Ethereum interaction.
  2. Step 2: Compare library features

    ethers.js is newer, designed to be lightweight and simpler to use.
  3. Final Answer:

    Web3.js is larger and older, while ethers.js is lighter and simpler. -> Option D
  4. Quick Check:

    Library size and age = A [OK]
Hint: Remember: Web3.js is big and old; ethers.js is small and new [OK]
Common Mistakes:
  • Thinking ethers.js only works on backend
  • Confusing blockchain support (Bitcoin vs Ethereum)
  • Believing ethers.js can't send transactions
2. Which of the following is the correct way to create a provider using ethers.js?
easy
A. const provider = new ethers.providers.JsonRpcProvider();
B. const provider = new Web3.providers.HttpProvider();
C. const provider = new ethers.Web3Provider();
D. const provider = new Web3.eth.JsonRpcProvider();

Solution

  1. Step 1: Recall ethers.js provider syntax

    ethers.js uses ethers.providers.JsonRpcProvider() to create a JSON RPC provider.
  2. Step 2: Identify correct syntax

    const provider = new ethers.providers.JsonRpcProvider(); matches the correct ethers.js syntax; others mix Web3.js or incorrect classes.
  3. Final Answer:

    const provider = new ethers.providers.JsonRpcProvider(); -> Option A
  4. Quick Check:

    ethers.js provider creation = D [OK]
Hint: ethers.js uses ethers.providers.JsonRpcProvider() [OK]
Common Mistakes:
  • Mixing Web3.js and ethers.js syntax
  • Using Web3 classes with ethers.js
  • Incorrect capitalization or namespaces
3. What will be the output of this ethers.js code snippet?
const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider();
(async () => {
  const blockNumber = await provider.getBlockNumber();
  console.log(blockNumber);
})();
medium
A. An error because getBlockNumber() is not a function.
B. Undefined because provider is not initialized.
C. The current Ethereum block number as a number.
D. A string 'blockNumber' printed to console.

Solution

  1. Step 1: Understand provider and method

    The JsonRpcProvider connects to Ethereum and getBlockNumber() returns the latest block number as a number.
  2. Step 2: Analyze async function output

    The code logs the block number to console, so output is a number representing current block.
  3. Final Answer:

    The current Ethereum block number as a number. -> Option C
  4. Quick Check:

    getBlockNumber() returns number [OK]
Hint: getBlockNumber() returns a number, not error or string [OK]
Common Mistakes:
  • Expecting a string instead of number
  • Thinking getBlockNumber() is missing
  • Assuming provider is uninitialized
4. Identify the error in this Web3.js code snippet:
const Web3 = require('web3');
const web3 = new Web3();
(async () => {
  const balance = await web3.eth.getBalance('0x123...');
  console.log(balance);
})();
medium
A. Missing provider URL when creating Web3 instance.
B. getBalance() is not an async function.
C. The address format is incorrect.
D. console.log cannot print balance.

Solution

  1. Step 1: Check Web3 instance creation

    Web3 requires a provider URL (like HTTP or WebSocket) when instantiated to connect to Ethereum.
  2. Step 2: Identify missing provider

    The code creates new Web3() without a provider, so calls like getBalance will fail.
  3. Final Answer:

    Missing provider URL when creating Web3 instance. -> Option A
  4. Quick Check:

    Web3 needs provider URL [OK]
Hint: Always pass provider URL to Web3 constructor [OK]
Common Mistakes:
  • Thinking getBalance is not async
  • Assuming address format is wrong
  • Believing console.log can't print balance
5. You want to send a transaction using ethers.js and wait for it to be mined. Which code snippet correctly does this?
hard
A. const tx = await provider.sendTransaction(txData); await tx.wait();
B. const tx = await signer.sendTransaction(txData); await tx.wait(); console.log('Mined:', tx.hash);
C. const tx = signer.sendTransaction(txData); console.log('Mined:', tx.hash);
D. const tx = await signer.send(txData); await tx.wait();

Solution

  1. Step 1: Identify correct method to send transaction

    In ethers.js, signer.sendTransaction() sends a transaction and returns a transaction response.
  2. Step 2: Wait for transaction mining

    Calling tx.wait() waits for the transaction to be mined before proceeding.
  3. Step 3: Confirm correct usage

    const tx = await signer.sendTransaction(txData; await tx.wait(); console.log('Mined:', tx.hash); correctly awaits sending, then waits for mining, then logs the hash.
  4. Final Answer:

    const tx = await signer.sendTransaction(txData); await tx.wait(); console.log('Mined:', tx.hash); -> Option B
  5. Quick Check:

    sendTransaction + wait() = C [OK]
Hint: Use sendTransaction() then wait() to confirm mining [OK]
Common Mistakes:
  • Not awaiting sendTransaction()
  • Using provider instead of signer to send
  • Calling non-existent send() method