Recommended resources
The freely available resources this lesson will direct you to.
Hello! Let's get started with today's lesson.
In our last session, we focused on using the constructor to set up your smart contract's initial state at the moment of its creation. You learned how to pass arguments to it, which is essential for deploying customized contracts. Now, we'll connect that concept to the real world by answering the question: "How do we actually run the deployment and pass those arguments?"
Today, we will learn how to write and execute a simple deployment script using Hardhat. This is the final and most crucial step in getting your Solidity code from your editor onto a blockchain network. For you, as a backend developer, you can think of this as the equivalent of a deployment script that pushes your application to a server—it's the bridge between development and a live environment.
This lesson completes our module on the Solidity development environment, bringing together everything from setting up Hardhat to writing and compiling a contract.
The Deployment Process: From Code to Live Contract
Before we dive into the code, let's look at the big picture. Deploying a contract is a multi-step process.

A deployment script is a file, typically written in JavaScript or TypeScript, that automates these final steps. It instructs Hardhat how to compile your contract, which account to use for deployment, what network to deploy to, and what arguments (if any) to pass to the constructor.
Writing a Standard Hardhat Deployment Script
Hardhat projects that are initialized with the sample project come with a scripts folder. This is the conventional place to store your deployment scripts. These scripts use the ethers.js library, a powerful tool for interacting with the Ethereum blockchain, which is automatically integrated into your Hardhat environment.
Let's walk through the entire process, from configuration to final deployment command, with a detailed video guide.
How to Write, Compile & Deploy a Smart Contract using Hardhat - Alchemy University
This excellent video from Alchemy University will guide us through the entire process of writing, compiling, and deploying a smart contract with Hardhat. It's a very practical demonstration that shows how all the pieces we've discussed fit together.
Please watch from 04:31 to 17:20. The video covers these key stages: Configuration (04:31 - 10:09): How to correctly place your private key and RPC URL into the .env file and then configure the hardhat.config.js file to use them for a testnet (Goerli in this case, but the process is identical for Sepolia). Writing the Script (10:18 - 13:59): How to take the default deploy.js script that comes with a Hardhat project and modify it to deploy your own custom contract (Counter.sol). Pay close attention to how the contract name is changed. Execution and Debugging (13:59 - 17:20): How to run the deployment script using npx hardhat run and how to use the --network flag to specify your target network. The video also shows how to solve a common Solidity version error, which is a valuable debugging skill.
Deconstructing the Deployment Script
As you saw in the video, the deployment script follows a clear and repeatable pattern. Let's break down the code from a similar example to solidify your understanding. The following script deploys a simple HelloWorld contract.
How to Create and Deploy a Smart Contract with Hardhat
This article from QuickNode provides a very clear, text-based example of a deployment script. It's a great reference to review the code line-by-line.
Read the section 'Deploying the Hello World Smart Contract'. Focus on the code inside the deploy.js file and the explanation provided for each part of the script.
Let's analyze the core logic of the script from the article:
async function main() {
// 1. Get the deployer's account
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
// 2. Get the ContractFactory
const HelloWorld = await ethers.getContractFactory("HelloWorld");
// 3. Deploy the contract
const contract = await HelloWorld.deploy();
// 4. Wait for the deployment to finish
// Note: Modern Hardhat with ethers v6 uses `await contract.waitForDeployment()`
// The guide uses an older version with `contract.deployed()`, but the principle is the same.
console.log("Contract deployed at:", contract.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Here’s a breakdown of the key lines:
await ethers.getSigners(): Thisethersfunction gets a list of the wallet accounts Hardhat has access to. In a deployment script, the first one ([deployer]) is typically the account you'll use to sign the transaction and pay the gas fees.await ethers.getContractFactory("HelloWorld"): AContractFactoryis an abstraction used to deploy new smart contracts. You provide the name of the contract (e.g.,"HelloWorld") as a string, andethersfinds the compiled artifact for it.await HelloWorld.deploy(): This is the magic moment! Thedeploy()method on the factory object creates the deployment transaction and sends it to the network. If your contract's constructor requires arguments, you pass them here, like so:await MyContract.deploy(arg1, arg2).
Running the Deployment Script
Once your script is ready, you execute it from your terminal using the npx hardhat run command.
# General syntax
npx hardhat run <path-to-your-script> --network <network-name>
<path-to-your-script>is typicallyscripts/deploy.js.--network <network-name>is an optional flag.- If you omit it, Hardhat deploys to its temporary Hardhat Network. This is a local, in-memory blockchain that's perfect for quick testing. It's incredibly fast, but all state is lost when the script finishes.
- If you include it (e.g.,
--network sepolia), Hardhat will look for thesepoliaconfiguration in yourhardhat.config.jsand deploy to that live test network. This is how you'll deploy your token for others to interact with.
Test your understanding!
In the last lesson, you created a contract called Configurable.sol with a constructor that looked like this: constructor(string memory _message, uint256 _initialValue).
How would you modify the HelloWorld.deploy() line in the script above to deploy your Configurable contract with the message "Hello Blockchain" and an initial value of 42?
Show answer
First, you'd get the correct contract factory:const Configurable = await ethers.getContractFactory("Configurable");
Then, you would pass the arguments to the deploy() method:const contract = await Configurable.deploy("Hello Blockchain", 42);
The values "Hello Blockchain" and 42 are passed directly to the constructor during deployment.
Conclusion
Congratulations on completing the second module! You've now gone through the full, fundamental workflow of a smart contract developer: setting up a professional environment, writing a contract, compiling it, and now, deploying it using a script.
Key Takeaways:
- Deployment scripts, stored in the
/scriptsdirectory, automate the process of deploying your smart contracts. - The core pattern involves using
ethers.getContractFactory()to get a "blueprint" of your contract and then calling.deploy()on it. - Constructor arguments are passed directly into the
.deploy()function (e.g.,MyContract.deploy(arg1, arg2)). - The
npx hardhat runcommand executes your script. - The
--networkflag allows you to choose your target, from the fast local Hardhat Network to a public testnet like Sepolia.
Preview of the Next Lesson:
So far, our contracts have assumed everything goes right. But in the real world, we need to handle errors and protect our contracts from invalid inputs. In the first lesson of our next module, "Advanced Solidity & Contract Logic," we will explore how to handle contract errors and revert transactions using require, assert, and revert. This is the foundation of writing secure and robust smart contracts.