0
0
Blockchain / Solidityprogramming~30 mins

Testing with ethers.js in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Testing with ethers.js
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Call retrieve() on simpleStorage and print the result as a string.