← Back to list

# How to Deploy a Smart Contract Using Hardhat: A Complete Guide

In this tutorial, we’ll walk you through the process of deploying a smart contract using **Hardhat**, one of the most popular development…

Hackmind · 2024-11-09 20:37 · 0 claps · 4.6 min read
#smart-con #swisstronik-testnet #swisstronik
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3

Smart Contract Using Hardhat

How to Deploy a Smart Contract Using Hardhat: A Complete Guide

In this tutorial, we’ll walk you through the process of deploying a smart contract using Hardhat, one of the most popular development frameworks for Ethereum-based applications. Hardhat makes it easy to test, deploy, and debug your smart contracts, and is a go-to tool for developers looking to work with Ethereum and other compatible blockchains.

By the end of this guide, you’ll be able to deploy your first smart contract to a local test network or an Ethereum testnet like Rinkeby or Goerli. Let’s dive in!

— -

What is Hardhat?

Hardhat is a development environment designed to simplify building and testing decentralized applications (dApps) on Ethereum and Ethereum-compatible blockchains. It provides a variety of features, including:

  • Local Ethereum network (Hardhat Network) for testing.
  • Easy contract deployment scripts.
  • Built-in debugging and error handling.
  • Integration with plugins like Ethers.js, OpenZeppelin, and more.

— -

Prerequisites

Before you start, make sure you have the following tools installed:

  • Node.js (version 14.x or higher)
  • npm (Node package manager)

If you don’t have them installed, you can download them from nodejs.org.

Additionally, this guide assumes that you have basic knowledge of smart contract development, Solidity, and how Ethereum works.

— -

Step 1: Setting Up a Hardhat Project

First, you need to set up a new Hardhat project. Here’s how:

1.1 Initialize a New Node.js Project

Create a new directory for your project and initialize it with npm:

mkdir my-hardhat-project
cd my-hardhat-project
npm init -y

1.2 Install Hardhat and Dependencies

Now, install Hardhat and the necessary dependencies:

npm install — save-dev hardhat

1.3 Create a New Hardhat Project

Once Hardhat is installed, create a new Hardhat project using the command below:

npx hardhat

You’ll be prompted with several options. Choose Create a basic sample project. This will generate some default files, including a sample contract, test script, and configuration files.

— -

Step 2: Write Your Smart Contract

For this tutorial, we’ll deploy a simple smart contract that stores a number.

2.1 Create the Contract

Navigate to the contracts folder and create a new file called SimpleStorage.sol. Here’s an example contract in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
 uint256 private storedNumber;

function set(uint256 _number) public {
 storedNumber = _number;
 }

function get() public view returns (uint256) {
 return storedNumber;
 }
}

This contract allows you to store a number and retrieve it.

2.2 Compile the Contract

To make sure your contract is syntactically correct, you need to compile it. Run the following command:

npx hardhat compile

This will compile all the contracts in the contracts/ folder and create an artifacts/ folder with the compiled contract artifacts.

— -

Step 3: Write a Deployment Script

Now that we have our contract written and compiled, let’s create a deployment script.

3.1 Create the Deployment Script

In the scripts folder, create a new file named deploy.js. The script will deploy the SimpleStorage contract to the network.

async function main() {
 const [deployer] = await ethers.getSigners();
 console.log(“Deploying contracts with the account:”, deployer.address);

// Get the ContractFactory for SimpleStorage
 const SimpleStorage = await ethers.getContractFactory(“SimpleStorage”);
 console.log(“Deploying SimpleStorage…”);

// Deploy the contract
 const simpleStorage = await SimpleStorage.deploy();
 console.log(“SimpleStorage contract deployed to:”, simpleStorage.address);
}

main()
 .then(() => process.exit(0))
 .catch((error) => {
 console.error(error);
 process.exit(1);
 });

This script:

  • Retrieves the deployer’s Ethereum account using ethers.getSigners().
  • Uses ethers.getContractFactory() to get a contract factory for SimpleStorage.
  • Deploys the contract and logs the contract address to the console.

— -

Step 4: Configure Hardhat for Network Deployment

Before you can deploy to a live network, you need to configure the network settings in Hardhat.

4.1 Set Up hardhat.config.js

Open hardhat.config.js and configure the networks section to support the network you’re deploying to, such as Rinkeby or Goerli (Ethereum testnets), or a local Hardhat network.

To deploy to Rinkeby, for example, modify your hardhat.config.js file like this:

require(‘[@nomiclabs/hardhat-ethers](http://twitter.com/nomiclabs/hardhat-ethers)’);
require(‘dotenv’).config();

module.exports = {
 solidity: “0.8.0”,
 networks: {
 hardhat: {},
 rinkeby: {
 url: `[https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`](https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`),
 accounts: [`0x${process.env.PRIVATE_KEY}`]
 }
 }
};

In this configuration:

  • We require the [@nomiclabs/hardhat-ethers](http://twitter.com/nomiclabs/hardhat-ethers) plugin for interaction with the Ethereum network.
  • We load environment variables from a .env file using dotenv to store sensitive information like your private key and Infura project ID.

4.2 Set Up .env File

Create a .env file in the root directory of your project to store sensitive data like your Infura Project ID and private key:

INFURA_PROJECT_ID=your_infura_project_id
PRIVATE_KEY=your_wallet_private_key

Replace your_infura_project_id with your Infura project ID and your_wallet_private_key with the private key of the account you will use to deploy.

Note: Never share your private key publicly. Always keep it secure.

— -

Step 5: Deploy the Contract

Now that everything is set up, it’s time to deploy your contract. You can deploy it to the local Hardhat network or any other Ethereum testnet (like Rinkeby or Goerli).

5.1 Deploy to the Hardhat Network

To deploy to the local Hardhat network, simply run:

npx hardhat run scripts/deploy.js — network hardhat

This command deploys the contract to the local Hardhat network, and you’ll see the contract address in the console.

5.2 Deploy to a Testnet

If you want to deploy your contract to a testnet like Rinkeby, run the following command:

npx hardhat run scripts/deploy.js — network rinkeby

Make sure you’ve added testnet ETH to your wallet (using a faucet) and have enough funds to pay for the deployment transaction.

— -

Step 6: Interact with Your Deployed Contract

Once your contract is deployed, you can interact with it by writing scripts that call the contract’s functions.

Here’s an example of how to interact with your deployed SimpleStorage contract using Hardhat:

6.1 Create an Interaction Script

Create a new script file in the scripts folder called interact.js. This script will set and get a stored number from the contract.

async function main() {
 const [deployer] = await ethers.getSigners();
 console.log(“Interacting with contract using the account:”, deployer.address);

// The contract address from the deployment
 const contractAddress = “YOUR_DEPLOYED_CONTRACT_ADDRESS”;

// Get the deployed contract
 const SimpleStorage = await ethers.getContractAt(“SimpleStorage”, contractAddress);

// Set a new number
 const setTx = await SimpleStorage.set(42);
 await setTx.wait();

// Get the stored number
 const storedNumber = await SimpleStorage.get();
 console.log(“Stored Number:”, storedNumber.toString());
}

main()
 .then(() => process.exit(0))
 .catch((error) => {
 console.error(error);
 process.exit(1);
 });

To run the script:

npx hardhat run scripts/interact.js — network rinkeby

This will set the number to 42 and retrieve it.

— -

Conclusion

You’ve now successfully deployed a smart contract using Hardhat! This guide covered the basics of setting up a Hardhat project, writing a smart contract, deploying it to a network, and interacting with it.

Hardhat is a powerful tool that allows you to test, debug, and deploy contracts efficiently, and is a must-have in the toolkit of any blockchain developer.

If you found this tutorial helpful, be sure to check out the official Hardhat documentation for more advanced features and use cases.

Happy coding! 🎉

— -

Feel free to share this guide with anyone interested in learning how to deploy smart contracts using Hardhat!


메타데이터
post_id
6bb283ec9879
slug
how-to-deploy-a-smart-contract-using-hardhat-a-complete-guide-6bb283ec9879
url
https://medium.com/@hackmind39/how-to-deploy-a-smart-contract-using-hardhat-a-complete-guide-6bb283ec9879
canonical_url
https://medium.com/@hackmind39/how-to-deploy-a-smart-contract-using-hardhat-a-complete-guide-6bb283ec9879
author_url
https://medium.com/@hackmind39
status
ok
fetched_at
2026-08-11 22:12:39