0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 modes available
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.