Skip to main content

Foundry

In this quickstart, we'll create a basic Foundry project. Here's a video walkthrough:

Prerequisites​

Before you begin, ensure you:

  1. Set up your wallet

  2. Fund your wallet with Linea ETH on either the testnet, or mainnet

  3. Download and install Foundry:

    curl -L https://foundry.paradigm.xyz | bash

    Then, open a new terminal, and call foundryup to install the latest release.

Create a Foundry project​

To create a Foundry project, run:

forge init linea-tutorial

And change into the directory:

cd linea-tutorial

Running forge init sets up a sample contract, test, and script for Counter.sol.

Deploy a smart contract​

To deploy a smart contract, we highly recommend using an Infura endpoint, as the public endpoint may experience rate limiting and not meant for production use.

Sign up for an Infura account to get an API key that provides access the Linea endpoints. Assign the Linea endpoints you want to access, to your API key.

caution

These instructions use API keys and private keys inline. We highly recommend hiding them in .env files

Deploy your contract using the following syntax:

forge create --rpc-url YOUR_LINEA_ENDPOINT src/Counter.sol:Counter --private-key YOUR_PRIVATE_KEY

In the command:

Your output should look similar to:

Deployer: YOUR_ACCOUNT_NUMBER
Deployed to: 0xED0Ff7E8B655dFFfCA471ea3B6B649ce7C2C1b83
Transaction hash: 0x967e1290b285e67b3d74940ee19925416734c345f58bd1ec64dcea134647d7ee

Deploy a smart contract using a .env file​

Directly pasting your private key into the command line poses security risks. To avoid exposing sensitive information such as wallet private keys or API keys, use files with the .env extension to store private data. Create a .env file, then add the file to the .gitignore file to prevent committing it. Populate the .env file with the relevant private information:

PRIVATE_KEY=YOUR_PRIVATE_KEY
INFURA_API_KEY=YOUR_INFURA_API_KEY

Then, run:

source .env

Finally, modify the foundry.toml file to store the various RPC endpoints we might be working with. For example:

[rpc_endpoints]
linea-goerli = "https://linea-goerli.infura.io/v3/${INFURA_API_KEY}"
linea-sepolia = "https://linea-sepolia.infura.io/v3/${INFURA_API_KEY}"
linea-mainnet = "https://linea-mainnet.infura.io/v3/${INFURA_API_KEY}"

To deploy the smart contract, run:

forge create --rpc-url linea-mainnet src/Counter.sol:Counter --private-key $PRIVATE_KEY

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