📖 Scenario: You are building a simple smart contract that stores a number. You want to write tests using ethers.js to check if the contract works correctly.
🎯 Goal: Write a test script using ethers.js that deploys the contract, sets a number, and checks if the stored number is correct.
📋 What You'll Learn
Create a variable called SimpleStorage to get the contract factory
Create a variable called simpleStorage to deploy the contract
Create a variable called tx to store the transaction when setting the number
Use await tx.wait() to wait for the transaction to be mined
Use const storedNumber = await simpleStorage.retrieve() to get the stored number
Use console.log(storedNumber.toString()) to print the stored number
💡 Why This Matters
🌍 Real World
Testing smart contracts before deploying them to the blockchain helps avoid costly mistakes and bugs.
💼 Career
Blockchain developers use ethers.js and testing frameworks to ensure their contracts work correctly and securely.
Progress0 / 4 steps
1
Setup ethers.js and get the contract factory
Write const SimpleStorage = await ethers.getContractFactory('SimpleStorage') to get the contract factory for SimpleStorage.
Blockchain / Solidity
Hint
Use ethers.getContractFactory with the contract name 'SimpleStorage'.
2
Deploy the contract
Write const simpleStorage = await SimpleStorage.deploy() to deploy the contract and store it in simpleStorage.
Blockchain / Solidity
Hint
Call deploy() on SimpleStorage and assign it to simpleStorage.
3
Set a number in the contract
Write const tx = await simpleStorage.store(42) to call the store function with the number 42 and save the transaction in tx. Then write await tx.wait() to wait for the transaction to be mined.
Blockchain / Solidity
Hint
Call store(42) on simpleStorage and wait for the transaction to complete.
4
Retrieve and print the stored number
Write const storedNumber = await simpleStorage.retrieve() to get the stored number. Then write console.log(storedNumber.toString()) to print it.
Blockchain / Solidity
Hint
Call retrieve() on simpleStorage and print the result as a string.
Practice
(1/5)
1. What is the main purpose of using ethers.js in testing smart contracts?
easy
A. To create new blockchain networks
B. To design the user interface of a blockchain app
C. To mine new blocks on the blockchain
D. To interact with smart contracts and verify their behavior programmatically
Solution
Step 1: Understand ethers.js role
ethers.js is a library that helps developers interact with Ethereum smart contracts in code.
Step 2: Identify testing purpose
In testing, ethers.js lets you call contract functions and check if they behave as expected.
Final Answer:
To interact with smart contracts and verify their behavior programmatically -> Option D
Quick Check:
ethers.js tests contract behavior = A [OK]
Hint: Remember: ethers.js tests contracts by calling their functions [OK]
Common Mistakes:
Confusing ethers.js with UI design tools
Thinking ethers.js mines blocks
Assuming ethers.js creates blockchains
2. Which of the following is the correct way to get a signer from a provider in ethers.js?
easy
A. const signer = provider.signer();
B. const signer = provider.getSigner();
C. const signer = provider.getsigner();
D. const signer = provider.sign();
Solution
Step 1: Recall ethers.js signer method
The correct method to get a signer from a provider is getSigner() with capital S.
Step 2: Check method spelling and casing
Options B, C, and D have incorrect method names or casing.
Final Answer:
const signer = provider.getSigner(); -> Option B
Quick Check:
Correct method is getSigner() = A [OK]
Hint: Method names are case-sensitive; use getSigner() exactly [OK]
Common Mistakes:
Using wrong casing like signer() instead of getSigner()
contract.setValue(10) returns a Promise, so it must be awaited to get the transaction object.
Step 2: Identify missing await issue
Without await, tx is a Promise, which does not have a wait() method, causing the error.
Final Answer:
Missing await before contract.setValue(10) -> Option A
Quick Check:
Await async call before tx.wait() = C [OK]
Hint: Always await contract calls returning transactions before wait() [OK]
Common Mistakes:
Forgetting await before async contract calls
Assuming tx is transaction without awaiting
Not checking provider connection first
5. You want to test a smart contract function transfer(address to, uint amount) that emits an event Transfer. Which ethers.js testing approach correctly waits for the transaction and verifies the event was emitted?
hard
A. await contract.transfer(addr, 100);
expect(tx.events[0].event).to.equal('Transfer');
B. const receipt = await contract.transfer(addr, 100);
expect(receipt.events[0].event).to.equal('Transfer');