Skip to main content
Create your own

Deploying Your First Smart Contract with Hardhat

Welcome to the final lesson in our "Solidity Fundamentals" module. In our previous session, we explored Solidity events, the crucial mechanism for logging state changes and communicating with the world outside the blockchain. You learned how to define, emit, and design events with searchable indexed parameters, a concept that mirrors indexing in the databases you're familiar with.

This lesson marks the exciting transition from writing code to running it. We will take a complete smart contract, compile it into a format the Ethereum Virtual Machine (EVM) understands, and deploy it to a running blockchain on your own machine. This is the capstone skill for this module, bridging the gap between theory and a tangible, running decentralized application. Your goal is to become proficient with the full development lifecycle, and this is a critical part of that process.

By the end of our 60 minutes, you will be able to compile any Solidity smart contract using Hardhat and deploy it to a local network, completing the foundational workflow for any blockchain developer.

1. From Solidity to Bytecode: The Compilation Step

So far, you've been writing Solidity code, which is human-readable. However, the EVM cannot execute Solidity directly. It needs to be translated into low-level instructions called bytecode. The process of converting your .sol files into EVM bytecode is called compilation.

Hardhat provides a simple, built-in task for this: compile. When you run this command, Hardhat does two important things:

  1. Generates EVM Bytecode: This is the machine code that will be deployed to the blockchain.
  2. Generates the ABI (Application Binary Interface): This is a JSON file that serves as a detailed description of your contract's public interface—its functions, parameters, return values, and events. Given your background, you can think of the ABI as the equivalent of an OpenAPI or Swagger specification for a web API. It tells external applications (like scripts or user interfaces) how to format calls to the contract's bytecode and how to interpret the data it returns.

These generated files, known as artifacts, are stored in the artifacts/ directory of your Hardhat project.

The Moonbeam documentation provides a clear walkthrough of this process. It includes a simple Box.sol contract that we will use for the rest of this lesson.

Deploy Contracts with Hardhat - Moonbeam Docs

This guide walks through the fundamental Hardhat workflow. We'll start with the sections on creating a contract and compiling it.

First, create the simple Box.sol contract as described in the section The Contract File. Save this in your contracts/ directory. Next, read the section on compilation, Compile the Contract. Follow the instruction to run npx hardhat compile in your terminal. After it completes, take a moment to inspect the newly created artifacts/ directory in your project to see the resulting JSON files.

After running the command, you've successfully turned your Solidity code into something the EVM can understand. Now, we need a network to deploy it to.

2. Setting the Stage: The Hardhat Local Network

Hardhat comes with a built-in local Ethereum network designed for development. It mimics the behavior of a live blockchain but runs entirely on your machine, so you don't need real money for transactions. You can interact with it in two main ways:

  1. Implicit Network: When you run a Hardhat task like test or run without specifying a network, Hardhat automatically starts a fresh, in-memory blockchain for the duration of that task and shuts it down afterward. This is fast and ideal for automated testing, as each run is isolated.
  2. Persistent Node: You can also run a standalone blockchain node that persists in a terminal window. This is extremely useful for deploying your contracts and then interacting with them manually, or for connecting a front-end application.

For today's goal, we will use the persistent node. You can start it with a simple command:

npx hardhat node

When you run this, Hardhat will start a local blockchain and print a list of 20 pre-funded accounts with their private keys. This local node is now ready to accept deployments and transactions. You should leave this terminal window running in the background.

3. Deploying the Contract with Hardhat Ignition

Deploying a contract involves sending a special transaction to the blockchain that contains the contract's bytecode. While this can be done manually, a robust project uses deployment scripts to automate and manage this process. This is analogous to the automated deployment scripts you might use for releasing updates to business intelligence tools or data pipelines.

The modern, recommended way to handle deployments in Hardhat is with Hardhat Ignition, a declarative deployment system. Instead of writing step-by-step procedural instructions, you simply declare what you want deployed. Ignition then figures out the necessary steps, executes them, and keeps track of what has already been deployed to avoid redundant work.

An Ignition deployment is defined in a module, which is a TypeScript or JavaScript file typically placed in the ignition/modules/ directory.

The official Hardhat documentation provides the clearest explanation of how to write an Ignition module and use it to deploy to a local node.

Deploying a contract | Hardhat 3

This official tutorial covers the essentials of deploying with Hardhat Ignition. We'll focus on writing the module and deploying to the local node you just started.

First, create the directory structure ignition/modules/. Inside, create a new file named DeployBox.ts. Now, read the section Writing a deployment. Adapt the example to create a module that deploys our Box contract instead of Counter. Your DeployBox.ts file should look like this:

```typescript
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("BoxModule", (m) => {
  const box = m.contract("Box");

  return { box };
});
```

3. Finally, read the section Deploying to a local development node. With your npx hardhat node still running in one terminal, open a second terminal and run the deployment command, pointing it to your local node:

`npx hardhat ignition deploy ignition/modules/DeployBox.ts --network localhost`

After running this command, you will see output in both terminals. The deployment terminal will confirm the successful deployment and print the address of your newly created Box contract. The node terminal will show the transaction being received and processed. Congratulations, your contract is now live on your local blockchain!

4. Visualizing the Full Workflow

You have now completed the entire development loop: writing, compiling, and deploying. The flowchart below illustrates all the components you've just worked with and how they fit together.

A high-level view of the Hardhat project structure and deployment process. You wrote the **Smart Contract** (`Box.sol`), used Hardhat to compile it into **Contract Artifacts** (the ABI and bytecode), wrote a **Deployment Module** (your Ignition script), and executed it to deploy the contract to a **Blockchain Network** (your local Hardhat node).

Let's trace your steps on this diagram:

  • You started with the "Greeter Smart Contract" (in our case, Box.sol).
  • npx hardhat compile transformed it into "Contract Artifacts" (the JSON file in the artifacts directory).
  • You wrote the "Greeter Deployment Module" (DeployBox.ts) which uses those artifacts.
  • The "Hardhat Configuration" file (hardhat.config.ts) tells Hardhat which compiler version to use and defines the localhost network.
  • Finally, npx hardhat ignition deploy used your deployment module to send the transaction that created the contract on the "Blockchain Network EVM".

This entire structure is what makes Hardhat such a powerful and complete development environment.

Conclusion

In this lesson, you successfully navigated the path from source code to a live application on a local blockchain. You've mastered the final and most rewarding step of our "Solidity Fundamentals" module.

Here are your key takeaways:

  • Compilation (npx hardhat compile) is the process of converting human-readable Solidity into EVM bytecode and an ABI. The ABI is the contract's API specification in JSON format.
  • The Hardhat Node (npx hardhat node) provides a persistent local blockchain, perfect for development and testing.
  • Deployment Scripts, written using Hardhat Ignition, provide a reliable and automated way to deploy contracts.
  • The command npx hardhat ignition deploy <module> --network localhost executes your deployment script against the running local node.

You now have the complete set of fundamental skills to build and deploy a basic smart contract. In our next module, "Smart Contract Testing with Hardhat," we will build directly on what you learned today. You'll see how Hardhat leverages these same compilation and deployment steps to create an automated testing environment, allowing you to write scripts that deploy your contract and systematically verify that every function and event works exactly as you expect.

Can't find a good explanation? Sign up and we'll make it for you

Sign up