Hardhat
In this tutorial, we'll walk through creating a basic Hardhat project and deploying a token contract.
Here's a video walkthrough:
Prerequisites​
Before you begin, ensure you've:
- Set up your wallet
- Funded your wallet with goerli ETH
- Bridged Goerli ETH to Linea
- 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 or by running:
npm install --save-dev @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers
And adding the line require("@nomicfoundation/hardhat-toolbox");
to the top of hardhat.config.js
file.
Write the smart contract​
To write a smart contract, replace the file Lock.sol
with Token.sol
. Add the following code:
pragma solidity 0.8.18;
// SPDX-License-Identifier: MIT
contract Token {
string public name = "My Token";
string public symbol = "MTK";
uint8 public decimals = 18;
uint256 public totalSupply = 100000000;
mapping (address => uint256) public balances;
address public owner;
constructor() {
owner = msg.sender;
balances[owner] = totalSupply;
}
function transfer(address recipient, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance.");
balances[msg.sender] -= amount;
balances[recipient] += amount;
}
}
Then, to compile your contract, run npx hardhat compile
.
Create a deployment script​
To deploy your contract, we'll need to write a deployment script for Token.sol
. Replace the code in scripts/deploy.js
with:
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
console.log("Token address:", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy your contract​
You can deploy with Hardhat by adding Linea to your hardhat.config.js
, but you can also use Truffle Dashboard to deploy directly with your MetaMask wallet.
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:
Install Truffle using the recommended installation procedure
Run
truffle dashboard
in your terminal, which will open a window on port24012
.Navigate to
localhost:24012
in your browser. Please ensure that Dashboard is connected to the Linea testnet by connecting your MetaMask wallet to Linea. For reference, the Linea testnet network ID is59140
.Add the the the Dashboard network to your
hardhat.config.js
networks: {
// other networks
truffledashboard: {
url: "http://localhost:24012/rpc"
}
},In a separate terminal, in your Hardhat project, run
npx hardhat run scripts/deploy.js --network truffledashboard
Navigate back to
localhost:24012
. You should see a prompt asking your to confirm the deployment. Click Confirm.You should see something similar to:
Deploying contracts with the account: YOUR_ACCOUNT_NUMBER
Account balance: 71790921294697313
Token address: 0xD8b30F2B76361AD82d086414a1c47b0062DCBaf3
hardhat.config.js
​
To deploy to Linea, we'll need to add the network to our hardhat.config.js
. To do this:
Create a
.env
file in the root folder with your wallet's private key.PRIVATE_KEY=YOUR_PRIVATE_KEY_HERE
dangerPlease do not check your keys into source control. We recommend adding
.env
to your.gitignore
Download
dotenv
npm i -D dotenv
Add Linea to your
hardhat.config.js
file:require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.17",
networks: {
linea: {
url: `https://rpc.goerli.linea.build/`,
accounts: [PRIVATE_KEY],
},
},
};Call
npx hardhat run scripts/deploy.js --network linea
from the CLI. Your output should look something like this:Deploying contracts with the account: YOUR_ACCOUNT_NUMBER
Account balance: 71790921294697313
Token address: 0xD8b30F2B76361AD82d086414a1c47b0062DCBaf3
Next, you can optionally verify your contract on the network.
You may encounter rate limiting if you are connecting via the public endpoint. If your dapp needs full Infura node access, open a support ticket here.