0
0
Blockchain / Solidityprogramming~10 mins

Hardhat deployment scripts in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hardhat deployment scripts
Start script
Load Hardhat Runtime Environment
Get contract factory
Deploy contract
Wait for deployment
Log deployed address
End script
The deployment script loads Hardhat, prepares the contract, deploys it, waits for confirmation, then logs the address.
Execution Sample
Blockchain / Solidity
async function main() {
  const hre = require('hardhat');
  const Contract = await hre.ethers.getContractFactory("MyContract");
  const contract = await Contract.deploy();
  await contract.deployed();
  console.log("Deployed to:", contract.address);
}
main();
This script deploys 'MyContract' to the blockchain and prints its address.
Execution Table
StepActionEvaluationResult
1Load Hardhat Runtime Environment (hre)hre.ethers availableReady to interact with contracts
2Get contract factory for 'MyContract'hre.ethers.getContractFactory('MyContract')Contract factory object obtained
3Deploy contractContract.deploy()Deployment transaction sent
4Wait for deployment to completecontract.deployed()Contract deployed and address assigned
5Log deployed contract addressconsole.log(contract.address)Address printed to console
6Script endsNo further actionsDeployment complete
💡 Script ends after logging deployed contract address
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
hreundefinedHardhat Runtime Environment objectSameSameSame
ContractundefinedContract factory for MyContractSameSameSame
contractundefinedundefinedDeployment transaction objectDeployed contract instance with addressSame
Key Moments - 3 Insights
Why do we use 'await' before deploy() and deployed()?
Because deploy() sends a transaction that takes time to confirm, and deployed() waits until the contract is fully deployed. See execution_table steps 3 and 4.
What is the contract factory and why do we need it?
The contract factory is like a blueprint to create new contract instances. We get it in step 2 to prepare deployment.
Why do we log contract.address after deployment?
Because the address is only assigned after deployment completes (step 4), so logging it before would be undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'contract' after step 3?
ADeployment transaction object
BContract factory object
CDeployed contract instance with address
DUndefined
💡 Hint
Check variable_tracker row for 'contract' after Step 3
At which step does the script wait for the contract to be fully deployed?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
See execution_table action 'Wait for deployment to complete'
If we remove 'await' before contract.deployed(), what happens?
AScript waits correctly for deployment
BDeployment will fail
Ccontract.address will be undefined when logged
DScript will throw a syntax error
💡 Hint
Refer to key_moments about why 'await' is needed before deployed()
Concept Snapshot
Hardhat deployment script steps:
1. Load Hardhat Runtime Environment (hre)
2. Get contract factory with hre.ethers.getContractFactory('ContractName')
3. Deploy contract with await deploy()
4. Wait for deployment with await deployed()
5. Log deployed contract address
Use async/await to handle asynchronous blockchain operations.
Full Transcript
This visual execution shows how a Hardhat deployment script runs step-by-step. First, the script loads the Hardhat Runtime Environment (hre) to access blockchain tools. Then it gets a contract factory for the contract named 'MyContract'. Next, it deploys the contract by sending a transaction and waits for the deployment to complete. After deployment, the contract address is available and logged to the console. Variables like 'contract' change from undefined to a deployment transaction object, then to a deployed contract instance with an address. Key points include using 'await' to wait for asynchronous steps and logging the address only after deployment finishes. The quiz tests understanding of these steps and variable states.