Hardhat
In this quickstart, we'll create a basic Hardhat project.
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:
-
Create your project directory:
mkdir linea-tutorial; cd linea-tutorial
-
Initialize your Node.js project:
npm init
-
Install Hardhat:
npm install --save-dev hardhat
-
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.
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.
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:
-
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_KEYwarningPlease do not check your keys into source control. Please add
.env
to your.gitignore
-
Download and install
dotenv
npm i -D dotenv
-
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.- Infura
- Public endpoint
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],
},
},
};require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.24",
networks: {
linea_sepolia: {
url: `https://rpc.sepolia.linea.build/`,
accounts: [PRIVATE_KEY],
},
linea_mainnet: {
url: `https://rpc.linea.build/`,
accounts: [PRIVATE_KEY],
},
},
}; -
Deploy your contract. Replace
NETWORK
with a relevant network defined in thehardhat.config.js
file. For examplelinea_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.