Anchor

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

Estimated time: 20-25 minutes

What You'll Build

  • Local Solana validator (test mode)

  • Firehose integration for block streaming

  • Counter Anchor program with events

  • Substreams module to extract program events

  • Complete Docker Compose orchestration

Prerequisites

Ensure you have the following installed:

Architecture Overview

The local environment consists of:

  • Solana Validator (port 8899/8900) - Test validator with unlimited SOL

  • 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 Program with Anchor

1. Install Required Tools

Anchor installation requires several prerequisites. Before proceeding, familiarize yourself with the Anchor installation requirements.

Install AVM (Anchor Version Manager):

Install and use the latest Anchor version:

Verify installation:

2. Configure Solana CLI

3. Initialize Anchor Project

4. Review Generated Program

The anchor init counter command created a basic program for us. The generated program source is located at programs/counter/src/lib.rs:

This simple program demonstrates the basic structure of an Anchor program with an initialize instruction.

5. Build and Deploy Program

Build the program:

Deploy to your local cluster (as configured in Anchor.toml):

After deployment, you'll see output similar to this:

Export the Program ID from the deployment output:

Replace <PROGRAM_ID> with the address from the Program Id: line (e.g., E2sTdp1aDaKbyh26BQkpYUH338u52fzwaiFbnym8MTk4) and <IDL_ACCOUNT> with the address from the Idl account created: line (e.g., G5GLdJwfDagZ7jfWjDZPBDwFNmZWtsY6WA3Lgbw6txmF).

8. Verify Deployment

Interact with the Counter Program

Now that the program is deployed, let's interact with it to generate some on-chain activity by calling the initialize instruction.

Start the Anchor shell:

In the Anchor shell, run the following commands to call the initialize instruction:

You should see a transaction signature returned, indicating the initialize instruction was successfully called.

Exit the shell:

This will create transactions on the local Solana validator that you can later observe when running your Substreams module.

Create Substreams Module

1. Initialize Substreams Project

Navigate back to the parent directory:

Initialize the Substreams module:

Follow the interactive prompts:

  • Chosen protocol: Solana

  • Chosen generator: sol-anchor-beta

  • Please enter the project name: counter

  • Please select the chain: Solana Mainnet (or choose another chain)

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

  • Input the full path of your JSON IDL in your filesystem: ./counter/target/idl/counter.json

  • Do you want to proceed with this IDL?: Yes

  • At what block do you want to start indexing data?: 0

  • How would you like to consume the Substreams?: To Postgres

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

The IDL file at ./counter/target/idl/counter.json was automatically generated by Anchor 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 block where you called the initialize instruction as it's the one that will contain some actual data. You can scan a specific range using -s <BLOCK_NUMBER> -t +10 to scan 10 blocks starting from a specific block.

You can also leave the substreams run command running and open another terminal to run anchor shell and execute await anchor.workspace.Counter.methods.initialize().accounts({}).signers([]).rpc() to call the initialize instruction and see the events appear live in your Substreams output.

You should see the Counter program events from your deployment and interactions!

Cleanup

Congratulations! You've completed the tutorial and have a working local Anchor 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 Solana development environment:

  1. Try Other Platforms - Explore HardHat or Foundry 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?