Topics In Demand
Notification
New

No notification found.

A step-by-step guide for Smart Contract deployment using Hardhat
A step-by-step guide for Smart Contract deployment using Hardhat

8

0

Blog 2/3 of our series focused on Web3 is here. If you're getting into Ethereum development, one of the first things you'll probably want to do is deploy a Smart Contract. And trust me, it's not as daunting as it sounds! In this blog, I'm going to walk you through the steps to deploy a Smart Contract on the Sepolia Ethereum testnet using Hardhat.

If you're unfamiliar with it, Sepolia is a test network where you can deploy contracts without using real Ether—perfect for testing and experimenting. Let's break it down step-by-step. By the end, you'll have your first contract running on Sepolia and be one step closer to becoming an Ethereum developer!

Prerequisites for deploying a Smart Contract

Before we start, you’ll need a few things ready:

  • MetaMask Wallet: You'll need MetaMask to interact with Ethereum and sign transactions. You can download the browser extension from here. Ensure you've also got some Sepolia ETH in your wallet (more on this in a second).
  • Sepolia ETH: Since Sepolia is a testnet, you need Sepolia ETH to pay for gas fees. You can easily get some by visiting a Sepolia faucet online. Your MetaMask wallet must be connected to the faucet to get the ETH.
  • Node.js: Hardhat runs on Node.js, so you need it installed. If you don't have it, download it from here.
  • Hardhat: This development framework makes deploying Smart Contracts super easy. We'll install it next!

Steps to Deploy a Smart Contract with Hardhat

Streamline your deployment process. Follow these straightforward steps to get your Smart Contract live

Step 1: Set up your Hardhat project

Let's set up your Hardhat project. Open your terminal (or command prompt) and follow these steps:

1. Create a new directory for your project. This keeps everything organized.


2. Initialize a new Node.js project. This step generates a package.json file, essential for managing your dependencies.


3. Install Hardhat as a development dependency.


4. Initialize the Hardhat project now that Hardhat is installed.


 

When you run this command, Hardhat will ask you a few questions. Don't worry about anything complicated; choose "Create a basic sample project" and hit Enter. This will set up a basic project with some sample code to get you started.

Step 2: Install the dependencies for Sepolia and Hardhat

Now that we have a basic Hardhat project, we need a few extra tools to help us deploy the contract to Sepolia.

Run this command to install everything we need:
 



 

Here’s what these packages do:

  • @nomiclabs/hardhat-ethers: This plugin allows Hardhat to work with the Ethers.js library, which is super useful for interacting with Ethereum.
  • ethers: This JavaScript library lets you connect to the Ethereum blockchain and send transactions.
  • @nomiclabs/hardhat-waffle: This gives us testing tools that make writing tests for your Smart Contracts easy (we'll use this later when you're ready to test contracts).

Step 3: Set up the Sepolia network configuration

We're almost there! Now, let's configure Hardhat to work with the Sepolia test network.

1. In your project folder, find the hardhat.config.js file and open it.
2. We need to add a configuration for Sepolia so Hardhat knows how to connect to it. Replace the contents of hardhat.config.js with the following:

Here’s the breakdown:

  • solidity: Specifies the version of Solidity we're using for the contract.
  • networks: Configures the networks where you can deploy your contracts. We add the Sepolia network here, with the Infura URL and your wallet's private key.
    • To connect to Sepolia, you'll need an Infura account. Go to Infura and create an account. Then, create a new project and grab your API key.
    • For security, never hardcode your private key directly into the configuration file. Instead, use environment variables (.env file).

Step 4: Write a simple Smart Contract

Now, let's write the actual Smart Contract that we'll deploy.
Inside your project folder, go to the contracts directory. You should see a file called Greeter.sol. You can edit this to create a simple contract, or create a new one.
Here’s a basic example of a simple contract:

This contract simply stores a greeting message and lets you update it.

Step 5: Deploy the Contract to Sepolia

Now the fun part: deployment!

1. Inside the scripts directory, you should see a sample-script.js file. You can replace its contents with the following script to deploy your contract:

2. Before running this, make sure you’ve set up your private key in an .env file. You can use the dotenv package to load the private key securely.

3. Once you’re ready, run the deployment script using:

 

Step 6: Interact with your contract

Congrats! Your contract is now live on Sepolia. You can interact with it by calling its functions using Hardhat or through a web interface like Etherscan or MetaMask.

If you want to interact programmatically with your deployed contract, you can use Hardhat scripts or build a front-end with Web3 or Ethers.js to interact with the contract in your browser. To familiarize yourself with Web3, read our previous blog that explains the basics of Web3 and how its seen as the future of the internet.  

Key takeaway

Using Hardhat, you've just deployed your first Smart Contract to the Sepolia test network. From here, you can experiment with more advanced contracts, write tests, and eventually deploy to the Ethereum mainnet. Remember, the key takeaway is that Hardhat makes Ethereum development much more manageable by streamlining the process of compiling, deploying, and testing contracts. With Sepolia as your testnet, you can experiment freely without the worry of losing real ETH. Happy coding, and good luck with your Ethereum journey!


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


© Copyright nasscom. All Rights Reserved.