Hardhat deployment scripts in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When deploying smart contracts with Hardhat, it's important to understand how the deployment time changes as the number of contracts grows.
We want to know how the time to deploy scales when we add more contracts to deploy.
Analyze the time complexity of the following deployment script.
async function main() {
const contracts = ["Token", "Marketplace", "Auction"];
for (const name of contracts) {
const ContractFactory = await ethers.getContractFactory(name);
const contract = await ContractFactory.deploy();
await contract.deployed();
console.log(`${name} deployed at ${contract.address}`);
}
}
This script deploys multiple contracts one after another using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Deploying each contract inside the loop.
- How many times: Once for each contract in the list.
As the number of contracts to deploy increases, the total deployment time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 deployments |
| 10 | 10 deployments |
| 100 | 100 deployments |
Pattern observation: Doubling the number of contracts roughly doubles the total deployment time.
Time Complexity: O(n)
This means the deployment time grows linearly with the number of contracts you deploy.
[X] Wrong: "Deploying multiple contracts in a loop happens instantly or all at once."
[OK] Correct: Each deployment is a separate transaction that takes time, so the total time adds up with each contract.
Understanding how deployment time scales helps you plan and write efficient deployment scripts, a useful skill when working on real blockchain projects.
"What if we deployed all contracts in parallel instead of one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand deployment scripts
Deployment scripts are used to automate the process of putting smart contracts on the blockchain.Step 2: Differentiate from other tasks
Writing contract logic, testing, and UI creation are separate tasks from deployment.Final Answer:
To automate deploying smart contracts to the blockchain -> Option CQuick Check:
Deployment script = automate deployment [OK]
- Confusing deployment with contract coding
- Thinking deployment scripts test contracts
- Assuming deployment scripts build UI
Solution
Step 1: Recall ethers.js method
The correct method to prepare a contract for deployment is ethers.getContractFactory with await.Step 2: Check syntax correctness
Options B, C, and D use incorrect method names or miss await keyword.Final Answer:
const Contract = await ethers.getContractFactory('MyContract'); -> Option AQuick Check:
Use getContractFactory with await [OK]
- Omitting await before getContractFactory
- Using wrong method names like getContract or deployContract
- Confusing contract factory with contract instance
const Token = await ethers.getContractFactory('Token');
const token = await Token.deploy();
await token.deployed();
const Sale = await ethers.getContractFactory('Sale');
const sale = await Sale.deploy(token.address);
await sale.deployed();
console.log(sale.address);What will be printed by
console.log(sale.address)?Solution
Step 1: Understand deployment sequence
The Token contract is deployed first, then its address is passed to Sale contract deployment.Step 2: Identify sale.address value
After deployment, sale.address holds the Sale contract's blockchain address, which is logged.Final Answer:
The deployed address of the Sale contract -> Option DQuick Check:
sale.address = Sale contract address [OK]
- Confusing token.address with sale.address
- Assuming sale.address is undefined before deployment
- Thinking passing token.address causes error
const Token = ethers.getContractFactory('Token');
const token = await Token.deploy();
await token.deployed();Solution
Step 1: Check getContractFactory usage
ethers.getContractFactory returns a promise, so it needs await.Step 2: Verify other awaits
Token.deploy() and token.deployed() correctly use await.Final Answer:
Missing await before ethers.getContractFactory -> Option AQuick Check:
Always await getContractFactory [OK]
- Forgetting await on getContractFactory
- Confusing which calls need await
- Assuming deploy() is synchronous
Token and Marketplace, where Marketplace needs the Token address in its constructor. Which is the correct way to write the deployment script?Solution
Step 1: Understand constructor dependency
Marketplace requires Token's address, so Token must be deployed first to get its address.Step 2: Deploy in correct order
Deploy Token, then deploy Marketplace passing token.address to its constructor.Step 3: Reject incorrect options
Deploying Marketplace first or simultaneously won't provide Token's address; omitting constructor args breaks dependency.Final Answer:
Deploy Token first, then deploy Marketplace passing token.address -> Option BQuick Check:
Deploy dependencies first, then dependent contracts [OK]
- Deploying dependent contract before dependency
- Not passing required constructor arguments
- Deploying contracts simultaneously ignoring dependencies
