Skip to main content

Foundry

In this tutorial, we'll walk through creating a basic Foundry project.

Here's a video walkthrough:

Prerequisites​

Before you begin, Ensure you've:

  1. Set up your wallet

  2. Funded your wallet with Linea ETH

    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

Deploy a smart contract​

Running forge init sets you up with a sample contract, test, and script for Counter.sol. To build it, simply run forge build.

To deploy a smart contract, we highly recommend using an Infura endpoint, as the public endpoint may experience rate limiting.

caution

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

To use Infura, you'll need to get an API key

On testnet:

forge create --rpc-url https://linea-goerli.infura.io/v3/INFURA_API_KEY src/Counter.sol:Counter --private-key PRIVATE_KEY

On mainnet:

forge create --rpc-url https://linea.infura.io/v3/INFURA_API_KEY src/Counter.sol:Counter --private-key PRIVATE_KEY

Your output should look a little something like this:

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

Using .env to store private keys​

It is dangerous to directly paste your private key into the command line. One workaround is to use .env files to store private information such as your wallet's private keys or API keys. In order to do so, create a .env file and add it to your .gitignore file. Then, fill it with the following information:

PRIVATE_KEY=YOUR_PRIVATE_KEY
INFURA_API_KEY=YOUR_INFURA_API_KEY

Then, run:

source .env

Finally, we can modify the foundry.toml file to conveniently store the various rpc endpoints we might be working with. Add this section:

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

Now, in order to deploy, you can simply 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.