How To Deploy Smart Contracts on Linea Blockchain
Linea is an EVM-compatible Layer-2 scaling solution for Ethereum. Linea aims to provide users with a faster and cheaper way to transact while enjoying the security of the underlying layer-1 network by vertically scaling Ethereum. Linea uses Zero Knowledge Proofs (zkProofs) and offline computation techniques to achieve high throughput and offload the traffic from the parent layer-1 blockchain, Ethereum.
Linea is type 1 zkEVM, which means it is EVM equivalent and does not require changing any part of the system, making it perfectly compatible with Ethereum, which allows developers to use a lot of existing infrastructure.
Learn more about Linea zkEVM blockchain.
Linea's out-of-the-box compatibility with the Ethereum Virtual Machine enables an easy porting and deployment of pre-existing smart contracts and dapps and the creation of new ones that would otherwise be costly on Mainnet.
Let’s jump into smart contract deployment on Linea —
Before we dive into the deployment process, you need a few prerequisites to have in place:
- Wallet: To deploy a smart contract, you need a Web3 wallet. For this tutorial, we will be using the Metamask browser wallet. You can download Metamask from here.
- Wallet Funds: To need to pay a gas fee to deploy contracts to the blockchain. You need to fund your wallet with ETH to cover those gas fees for contract deployment. You can follow this guide to bridge funds to the Linea blockchain.
- For this tutorial, we will be deploying contracts to the Linea blockchain using Hardhat. Use these resources to set up your environment according to Hardhat's recommended instructions.
Create A Hardhat project
Create an empty Hardhat project by running the following commands:
Select Create an empty hardhat.config.js in the next menu and hit Enter.
Hardhat recommends installing the @nomicfoundation/hardhat-toolbox plugin. You can download the plugin by entering Y during the project creation process.
Alternatively, you can run the following command in your project directory to install @nomicfoundation/hardhat-toolbox plugin —
You should now have a sample project from Hardhat that deploys and tests a Lock contract which locks funds for a set amount of time.
Note: Upon deployment, 0.001 ETH is automatically locked in the contract by the default script in scripts/deploy.js. If you want to avoid this, modify const lockedAmount = hre.ethers.parseEther("0.001"); to const lockedAmount = 0;.
Deploy The Contract
The above sample project already includes the deployment script. You just need to make a few modifications to deploy on Linea zkEVM.
To directly deploy the contract with your MetaMask wallet, use Truffle Dashboard. Otherwise, you can deploy with the Hardhat CLI by adding Linea to your hardhat.config.js.
Deploying the contract using Truffle Dashboard
The main advantage of using Truffle Dashboard is that it allows you to avoid saving your private keys locally and instead, you can use MetaMask wallet for deployments.
To deploy with Truffle Dashboard, download the Truffle-Hardhat integration plugin.
1. Install Truffle by following this guide.
2. Run following command
3. Import the plugin into hardhat.config.js by running
4. Run the Truffle Dashboard in your terminal, which should open a window on port 24012. Next, navigate to localhost:24012 in your browser.
Ensure that the Truffle Dashboard is connected to the Linea mainnet by adding Linea to your MetaMask wallet.
5. Run the following command in your Hardhat project using a separate terminal
6. Navigate back to localhost:24012. Confirm the deployment by clicking Confirm in the prompt on your screen.
7. Once confirmed, you should see something similar to the following
Deploying the contract using hardhat.config.js
To deploy to Linea zkEVM, add the network to hardhat.config.js by following these instructions:
1. Create a .env file in the root folder with your wallet's private key by running:
2. Download dotenv
3. Add Linea to your hardhat.config.js file by entering your Infura API Key, run:
Get an Infura API Key from here.
Next, modify hardhat.config.js as follows:
4. Call npx hardhat run scripts/deploy.js --network linea_mainnet from the CLI.
You should get an output something like this:
Verifying The Deployed Smart Contract
1. Add the @nomicfoundation/hardhat-toolbox or @nomicfoundation/hardhat-verify plugin to your project
2. Get and add your Lineascan API Key to your hardhat.config.js.
3. Add the custom Linea chain to your hardhat.config.js.
Note: The Etherscan apiKey and network name for your custom chain must be the same as under networks in your hardhat.config.js.
4. Finally, to verify your contract, run the following command:
npx hardhat verify --network linea_mainnet <DEPLOYED_CONTRACT_ADDRESS> <CONTRACT_ARGUMENTS>
After a successful verification, you should see something similar to the following
You can check if your contract was verified correctly by navigating to the Linea mainnet block explorer and pasting in the deployed contract address.
By following this guide, you should be able to understand how to deploy and verify the smart contracts on the Linea zkEVM blockchain.