Skip to main content

Hardhat

In this tutorial, we'll walk through creating a basic Hardhat project and deploying their sample contract.

Here's a video walkthrough:

Prerequisites​

Before you begin, ensure you've:

  1. Set up your wallet
  2. Funded your wallet with Linea ETH on either the testnet or mainnet
  3. Set up your environment using Hardhat's recommended instructions.

Create a Hardhat project​

To create an empty Hardhat project, run the following commands:

mkdir linea-tutorial
cd linea-tutorial
npm init
npm install --save-dev hardhat
npx hardhat

In the menu that appears, select Create an empty hardhat.config.js and press Enter.

Hardhat recommends using their plugin, @nomicfoundation/hardhat-toolbox, which can be downloaded by entering Y during the project creation process.

You should now have a sample project from Hardhat, the deploys, and test a Lock contract that locks funds for a set amount of time.

note

The default script in scripts/deploy.js locks 0.001 ETH in the contract upon deployment. If you don't want to do this, modify const lockedAmount = hre.ethers.parseEther("0.001"); to const lockedAmount = 0;

Deploy the contract​

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

You can use Truffle Dashboard to deploy directly with your MetaMask wallet or deploy with the Hardhat CLI by adding Linea to your hardhat.config.js.

Use Truffle Dashboard​

Truffle Dashboard allows you to forgo saving your private keys locally, instead connecting to your MetaMask wallet for deployments. To deploy with Truffle Dashboard using the Hardhat plugin:

  1. Install Truffle using the recommended installation procedure

  2. Run:

    npm install --save-dev @truffle/dashboard-hardhat-plugin
  3. Import the plugin into hardhat.config.js, with:

    import "@truffle/dashboard-hardhat-plugin";

    or:

    require("@truffle/dashboard-hardhat-plugin");
  4. Run truffle dashboard in your terminal, which will open a window on port 24012.

  5. Navigate to localhost:24012 in your browser. Please ensure that your Truffle Dashboard is connected to the Linea testnet or mainnet by connecting your MetaMask wallet to Linea. For reference, the Linea testnet network ID is 59140.


  6. In a separate terminal, in your Hardhat project, run

    npx hardhat run scripts/deploy.js --network truffleDashboard
  7. Navigate back to localhost:24012. You should see a prompt asking you to confirm the deployment. Click Confirm.


  8. You should see something similar to:

    Preparing Truffle compilation
    Sending Truffle compilation to dashboard
    Compilation successfully sent!
    Lock with 0.0ETH and unlock timestamp 1692377128 deployed to 0x6230404e79E2082A6454d5b1efF58DacA0C330C7

Use hardhat.config.js​

To deploy to Linea, we'll need to add the network to our hardhat.config.js. To do this:

  1. Create a .env file in the root folder with your wallet's private key.

    PRIVATE_KEY=YOUR_PRIVATE_KEY
    danger

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

  2. Download dotenv

    npm i -D dotenv
  3. Add Linea to your hardhat.config.js file.

    To use Infura, you'll need to get an API key. Add it to the .env file as follows:

    PRIVATE_KEY=YOUR_PRIVATE_KEY
    INFURA_API_KEY=INFURA_API_KEY

    Then, modify the hardhat.config.js like so:

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

    module.exports = {
    solidity: "0.8.19",
    networks: {
    linea_testnet: {
    url: `https://linea-goerli.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. Call npx hardhat run scripts/deploy.js --network linea_testnet or npx hardhat run scripts/deploy.js --network linea_mainnet from the CLI.

    Your output should look something like this:

    Lock with 0.0ETH and unlock timestamp 1692381910 deployed to 0xaD6C134BD9EA1E174CC8EA77755BF79C86148d38

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