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:
-
Funded your wallet with Linea ETH on either the testnet or mainnet
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.
These instructions use API keys and private keys inline. We highly recommend hiding them in .env
files by following the instructions below.
- Infura
- Public Endpoint
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-mainnet.infura.io/v3/INFURA_API_KEY src/Counter.sol:Counter --private-key PRIVATE_KEY
The public endpoints are rate limited and not meant for production systems.
On testnet:
forge create --rpc-url https://rpc.goerli.linea.build/ src/Counter.sol:Counter --private-key YOUR_PRIVATE_KEY
On mainnet:
forge create --rpc-url https://rpc.linea.build/ src/Counter.sol:Counter --private-key YOUR_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-mainnet.infura.io/v3/${INFURA_API_KEY}"
Now, in order to deploy, you can simply run:
- Mainnet
- Testnet
forge create --rpc-url linea-mainnet src/Counter.sol:Counter --private-key $PRIVATE_KEY
forge create --rpc-url linea-testnet src/Counter.sol:Counter --private-key $PRIVATE_KEY
Next, you can optionally verify your contract on the network.