Development tools setup (Hardhat, Remix) in Blockchain / Solidity - Time & Space Complexity
When setting up blockchain development tools like Hardhat or Remix, it's important to understand how the time to complete tasks grows as your project gets bigger.
We want to know how the setup and running of these tools scale with the size of your smart contracts and tests.
Analyze the time complexity of the following Hardhat test script snippet.
const { expect } = require("chai");
describe("Token contract", function () {
it("Deployment should assign total supply", async function () {
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
await token.deployed();
const totalSupply = await token.totalSupply();
expect(totalSupply).to.equal(1000000);
});
});
This code deploys a token contract and checks its total supply in a test.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running each test case which deploys contracts and calls functions.
- How many times: Once per test case; if you have many tests, this repeats for each.
As you add more tests or your contracts get larger, the time to deploy and test grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 tests | About 10 deployments and checks |
| 100 tests | About 100 deployments and checks |
| 1000 tests | About 1000 deployments and checks |
Pattern observation: The time grows roughly in direct proportion to the number of tests you run.
Time Complexity: O(n)
This means the time to run your tests grows linearly with the number of tests you have.
[X] Wrong: "Adding more tests won't affect the total time much because each test is small."
[OK] Correct: Each test deploys contracts and runs calls, which takes time. More tests add up and increase total time linearly.
Understanding how your development tools scale helps you write efficient tests and manage project growth confidently.
"What if we reused the same deployed contract instance for all tests instead of deploying each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand Hardhat's purpose
Hardhat is designed as a local tool to help developers write, test, and deploy smart contracts easily on their computer.Step 2: Compare options with Hardhat's role
Options A, B, and D describe other blockchain tools or devices, not Hardhat.Final Answer:
A local development environment to write and test smart contracts -> Option BQuick Check:
Hardhat = Local development tool [OK]
- Confusing Hardhat with hardware wallets
- Thinking Hardhat is an online platform
- Mixing Hardhat with blockchain explorers
Solution
Step 1: Recall Hardhat initialization command
The standard way to start a Hardhat project is by runningnpx hardhatwhich launches the setup wizard.Step 2: Evaluate other options
Options A, B, and C are not valid Hardhat commands for project initialization.Final Answer:
npx hardhat -> Option AQuick Check:
Initialize Hardhat with 'npx hardhat' [OK]
- Using 'hardhat create project' which is invalid
- Trying 'hardhat start' instead of 'npx hardhat'
- Using 'npx hardhat init' which is not a valid command
npx hardhat compile
Solution
Step 1: Understand the 'compile' command in Hardhat
The commandnpx hardhat compilecompiles all smart contracts in the project and outputs success or error messages.Step 2: Check other options
Throws an error: command not found is wrong because the command exists. Deletes all compiled files is incorrect as it does not delete files. Starts a local blockchain node is for running nodes, not compiling.Final Answer:
Compiles smart contracts and shows success message -> Option DQuick Check:
'npx hardhat compile' compiles contracts [OK]
- Expecting 'compile' to start blockchain node
- Thinking 'compile' deletes files
- Assuming command is invalid without setup
Error: Cannot find module '@nomiclabs/hardhat-ethers'What is the most likely fix?
Solution
Step 1: Identify the missing module error
The error says the plugin '@nomiclabs/hardhat-ethers' is missing, meaning it is not installed in the project.Step 2: Fix by installing the missing plugin
Runningnpm install --save-dev @nomiclabs/hardhat-ethersadds the plugin so Hardhat can use it.Final Answer:
Install the missing plugin with npm install --save-dev @nomiclabs/hardhat-ethers -> Option AQuick Check:
Missing module error = install plugin [OK]
- Restarting computer without installing packages
- Deleting node_modules without reinstalling
- Running compile without fixing dependencies
Solution
Step 1: Identify tools for quick testing without local setup
Remix is an online IDE that lets you write, compile, and deploy contracts directly in the browser.Step 2: Compare with other options
Hardhat requires local installation, hardware wallets store keys but don't write contracts, and blockchain explorers only view data.Final Answer:
Use Remix because it is an online editor that requires no local setup -> Option CQuick Check:
Quick test without install = Remix online editor [OK]
- Thinking Hardhat runs in browser without install
- Confusing hardware wallets with development tools
- Trying to deploy contracts via blockchain explorers
Ready to prove it?
Pass the test and this pattern moves unknown
