Skip to main content

Hardhat

In this quickstart, we'll create a basic Hardhat project. Here's a video walkthrough:

Prerequisites

Before you begin, ensure you set up your environment using Hardhat's recommended instructions](https://hardhat.org/tutorial/setting-up-the-environment#2.-setting-up-the-environment).

Create a Hardhat project

To create an empty Hardhat project:

  1. Create your project directory:

    mkdir linea-tutorial; cd linea-tutorial
  2. Initialize your Node.js project:

    npm init
  3. Install Hardhat:

    npm install --save-dev hardhat
  4. Initialize the Hardhat project:

    npx hardhat init

    In the menu that appears, select Create a JavaScript project and press Enter. Accept all the default values in the questionnaire.

You should now have a sample contract that deploys and tests a Lock contract that locks funds for a set time.

note

The default script in ignition/modules/Lock.js locks 1 GWEI in the contract upon deployment, you can modify this value in the script.

Deploy the contract

You can use the public endpoints or node provider (such as Infura) to deploy your contract to the Linea mainnet or or testnets.

important

The public endpoints are rate limited and not meant for production systems. We recommend using a node provider such as Infura. You can sign up for a free Infura account to get an API key.

The sample project already includes the deployment script. To deploy on Linea, you'll just need to make a few modifications to the hardhat.config.js file:

  1. Create a .env file in the root folder with your wallet's private key and Infura API (if using Infura).

    PRIVATE_KEY=YOUR_PRIVATE_KEY
    INFURA_API_KEY=INFURA_API_KEY
    warning

    Please do not check your keys into source control. Please add .env to your .gitignore

  2. Download and install dotenv

    npm i -D dotenv
  3. Add Linea to your hardhat.config.js file. The following example shows how to add an Infura endpoint and public endpoint to the configuration file.

    require("@nomicfoundation/hardhat-toolbox");
    require("dotenv").config();
    const { PRIVATE_KEY, INFURA_API_KEY } = process.env;

    module.exports = {
    solidity: "0.8.19",
    networks: {
    linea_sepolia: {
    url: `https://linea-sepolia.infura.io/v3/${INFURA_API_KEY}`,
    accounts: [PRIVATE_KEY],
    },
    linea_mainnet: {
    url: `https://linea-mainnet.infura.io/v3/${INFURA_API_KEY}`,
    accounts: [PRIVATE_KEY],
    },
    },
    };
  4. Deploy your contract. Replace NETWORK with a relevant network defined in the hardhat.config.js file. For example linea_mainnet.

    npx hardhat ignition deploy ./ignition/modules/Lock.js --network NETWORK

Your output should look something like this:

Deployed Addresses

LockModule#Lock - 0x2c73d6f093A2032D3371bFB9a29f7cE666080c4A

Next, you can optionally verify your contract on the network.