Foundry

This guide walks you through setting up a complete local Ethereum development environment for Substreams development using Foundry. You'll deploy a sample Counter contract, generate transactions, and stream the events using Substreams.

Estimated time: 15-20 minutes

What You'll Build

  • Local Ethereum node (Geth in dev mode)

  • Firehose integration for block streaming

  • Counter smart contract with events

  • Substreams module to extract contract events

  • Complete Docker Compose orchestration

Prerequisites

Ensure you have the following installed:

  • Docker 20.10+ with Docker Compose v2.0+

  • Foundry (forge, cast, anvil) - Installation guide

  • Substreams CLI v1.7.0+ (installation guide)

  • Rust with wasm32-unknown-unknown target

  • curl for testing endpoints

Architecture Overview

The local environment consists of:

  • Geth (port 8545/8546) - Ethereum node in dev mode with 1-second block time

  • Substreams (port 9000) - Substreams Tier1 service providing gRPC streaming

  • Docker network - Connecting all services

Setup Instructions

1. Create Project Directory

2. Create Docker Compose Configuration

Create a docker-compose.yml file:

3. Start the Environment

Validation Commands

1. Check Docker Services

Verify all containers are running and healthy:

Expected output:

2. Test Substreams Connectivity

Test Substreams Tier1 gRPC connectivity:

Expected output:

Deploy Counter Contract with Foundry

1. Install Foundry

If you haven't installed Foundry yet:

2. Initialize Foundry Project

3. Configure Foundry

Add the following network configuration to foundry.toml (the file already exists from forge init):

4. Set Environment Variables

Set up the private key environment variable for easier command usage:

This makes subsequent forge and cast commands cleaner and easier to read. You'll need to run this in each new terminal session.

5. Use Default Counter Contract

Foundry already created a suitable Counter contract in src/Counter.sol. The default contract contains:

No need to modify this file - we'll use the default contract as-is.

6. Compile and Deploy

Example output:

Export the deployed contract address for easy reference:

Replace <DEPLOYED_ADDRESS> with the actual address from the deployment output (look for the Deployed to: field).

7. Verify Deployment

Test the deployed contract with the following sequence:

Expected output from the final call: 43 (42 + 1 from increment)

Create Substreams Module

1. Initialize Substreams Project

Follow the interactive prompts:

  • Chosen protocol: EVM

  • Chosen generator: evm-events-calls

  • Please enter the project name: counter

  • Please select the chain: Ethereum Mainnet (or the chain you are targeting)

  • Please enter the contract address: Use your deployed address (from $CONTRACT variable)

  • How do you want to provide the JSON ABI?: JSON in a local file

  • Input the full path of the JSON ABI: ./out/Counter.sol/Counter.json

  • Please enter the contract initial block number: 0

  • Choose a short name for the contract: counter

  • What do you want to track for this contract?: Both events and calls

  • Is this contract a factory: No

  • Add another contract?: No

  • In which directory do you want to download the project?: ./substreams

  • How would you like to consume the Substreams?: To Postgres (or choose any other one)

The IDL file at ./out/Counter.sol/Counter.json was automatically generated by Foundry during the build process. We reference this file when initializing the Substreams module.

This will generate the basic Substreams module structure with the necessary configuration for tracking your Counter program.

2. Build and Test Substreams

Look for the deployment block as it's the one that will contain some actual data. You can scan a specific range using -s <DEPLOYMENT_BLOCK> -t +10 to scan 10 blocks starting from the deployment block.

You can also leave the substreams run command running and open another terminal to run cast send $CONTRACT "increment()" --rpc-url local --private-key $PKEY to increment the counter and see the events appear live in your Substreams output.

You should see the Increment events from your contract deployment!

Cleanup

Congratulations! You've completed the tutorial and have a working local Foundry development environment for Substreams.

When you're done, you can clean up the Docker environment with:

This will stop all containers and remove all data, allowing you to start fresh if needed.

Troubleshooting

For common issues with Docker Compose, RPC connectivity, and Substreams, see the Local Development Troubleshooting guide.

Next Steps

Now that you have a working local environment:

  1. Try Other Platforms - Explore HardHat or Solana local development

  2. Advanced Substreams - Learn about modules, manifests, and data transformations

  3. Consuming Substreams - Connect to databases or streaming platforms

  4. Production Deployment - Move to production endpoints

Additional Resources

Last updated

Was this helpful?