Testing with ethers.js in Blockchain / Solidity - Time & Space Complexity
When testing smart contracts with ethers.js, it's important to know how the time to run tests changes as the number of tests or contract calls grows.
We want to understand how the test execution time scales when we add more test cases or interact more with the blockchain.
Analyze the time complexity of the following ethers.js test code snippet.
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("SimpleContract", function () {
it("should return correct value", async function () {
const contract = await ethers.getContractFactory("SimpleContract").then(factory => factory.deploy());
await contract.deployed();
for (let i = 0; i < 100; i++) {
const value = await contract.getValue(i);
expect(value).to.equal(i * 2);
}
});
});
This code deploys a contract and calls a function 100 times in a loop, checking the returned value each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop calling
contract.getValue(i)100 times. - How many times: Exactly 100 times in this example, but this number can grow with test size.
As the number of calls in the loop increases, the total test time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 contract calls |
| 100 | 100 contract calls |
| 1000 | 1000 contract calls |
Pattern observation: Doubling the number of calls roughly doubles the total execution time.
Time Complexity: O(n)
This means the test time grows linearly with the number of contract calls in the loop.
[X] Wrong: "Adding more calls inside the test loop won't affect test time much because calls are fast."
[OK] Correct: Each contract call involves communication with the blockchain simulator or network, which takes time. More calls add up and increase total test time linearly.
Understanding how test execution time grows helps you write efficient tests and shows you can think about performance beyond just code correctness.
"What if we batch multiple calls into one contract function instead of calling individually? How would the time complexity change?"
Practice
ethers.js in testing smart contracts?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 DQuick Check:
ethers.js tests contract behavior = A [OK]
- Confusing ethers.js with UI design tools
- Thinking ethers.js mines blocks
- Assuming ethers.js creates blockchains
Solution
Step 1: Recall ethers.js signer method
The correct method to get a signer from a provider isgetSigner()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 BQuick Check:
Correct method is getSigner() = A [OK]
- Using wrong casing like signer() instead of getSigner()
- Misspelling method names
- Confusing signer with sign
const tx = await contract.setValue(42); await tx.wait(); const value = await contract.getValue(); console.log(value);What will be printed to the console?
Solution
Step 1: Understand transaction and wait
The code callssetValue(42)on the contract, then waits for the transaction to confirm withtx.wait().Step 2: Retrieve stored value
After confirmation, it callsgetValue()which returns the stored value 42.Final Answer:
42 -> Option AQuick Check:
Confirmed setValue(42) then getValue() = 42 [OK]
- Not awaiting tx.wait() before reading value
- Expecting transaction hash instead of value
- Assuming getValue() is undefined
const tx = contract.setValue(10); await tx.wait();What is the likely cause of the error?
Solution
Step 1: Check async call to contract method
contract.setValue(10) returns a Promise, so it must be awaited to get the transaction object.Step 2: Identify missing await issue
Withoutawait, tx is a Promise, which does not have await()method, causing the error.Final Answer:
Missing await before contract.setValue(10) -> Option AQuick Check:
Await async call before tx.wait() = C [OK]
- Forgetting await before async contract calls
- Assuming tx is transaction without awaiting
- Not checking provider connection first
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?Solution
Step 1: Await the transaction call
Useawaitto get the transaction object fromcontract.transfer.Step 2: Wait for transaction confirmation and get receipt
Calltx.wait()to get the receipt which contains event logs.Step 3: Verify event in receipt
Checkreceipt.events[0].eventequals 'Transfer' to confirm event emitted.Final Answer:
const tx = await contract.transfer(addr, 100); const receipt = await tx.wait(); expect(receipt.events[0].event).to.equal('Transfer'); -> Option CQuick Check:
Await tx, wait receipt, check event = D [OK]
- Not awaiting transaction before wait()
- Trying to access events on tx instead of receipt
- Ignoring event verification after transaction
Ready to prove it?
Pass the test and this pattern moves unknown
