Skip to main content

Hardhat

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.

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
    warning

    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],
},
},
};
  1. 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.