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
intermediate2: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();
Attempts:
2 left
💡 Hint
Remember that .call() returns the actual value asynchronously.
✗ Incorrect
The .call() method in Web3.js returns a Promise that resolves to the value returned by the contract method. Here, it prints the string returned by the name() method.
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
ethers.js returns BigNumber objects for big integers; use toString() to print.
✗ Incorrect
The totalSupply() method returns a BigNumber. Calling toString() converts it to a readable string number.
🔧 Debug
advanced2: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); });
Attempts:
2 left
💡 Hint
Check if the WebSocket URL and event subscription syntax are correct.
✗ Incorrect
The code correctly subscribes to the Transfer event using Web3.js with a valid WebSocket provider, so it logs events without error.
❓ Predict Output
advanced2: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();
Attempts:
2 left
💡 Hint
Transaction methods return a transaction response object with a hash property.
✗ Incorrect
The transfer method sends a transaction and returns a transaction response. The hash property is the transaction hash string.
🧠 Conceptual
expert2:00remaining
Key difference between Web3.js and ethers.js providers
Which statement best describes a key difference between Web3.js and ethers.js providers?
Attempts:
2 left
💡 Hint
Think about how each library handles large numbers from the blockchain.
✗ Incorrect
ethers.js uses BigNumber objects to safely handle large integers, while Web3.js often returns strings or numbers which can lose precision.