Indexing Greeter Contract Using Envio
Introduction
This tutorial provides a step-by-step guide to indexing a simple Greeter smart contract deployed on multiple blockchains. You'll learn how to set up and run a multichain indexer using Envio's template system.
What is the Greeter Contract?
The Greeter contract is a straightforward smart contract that allows users to store greeting messages on the blockchain. For this tutorial, we'll be indexing instances of this contract deployed on both Polygon and Linea.
What You'll Build
By the end of this tutorial, you'll have:
- A functioning multichain indexer that tracks greeting events
- The ability to query these events through a GraphQL endpoint
- Experience with Envio's core indexing functionality
Prerequisites
Before starting, ensure you have the following installed:
- Node.js (v22 or newer recommended)
- pnpm (recommended but not required)
- Docker Desktop (required to run the Envio indexer locally)
Note: Docker is specifically required to run your blockchain indexer locally. You can skip Docker installation if you plan only to use Envio Cloud.
Step 1: Initialize Your Project
First, let's create a new project using Envio's Greeter template:
- Open your terminal and run:
pnpx envio init
- When prompted for a folder name, press Enter to use the current directory or type another path:
? Specify a folder name (ENTER to skip): (.)
- Choose the Evm ecosystem:
? Choose blockchain ecosystem
> Evm
Svm
Fuel
- Select the Greeter template:
? Choose an initialization option
From Address - Lookup ABI from block explorer
From ABI File - Use your own ABI file
Template: ERC20
> Template: Greeter
Feature: External Calls
Feature: Factory Contract
- Add your Envio API token. Pick Create a new API token to open the token page, or Add an existing API token, then paste it at the
? Add your API token:prompt:
? Add an Envio API token to your .env file?
> Create a new API token (Opens https://envio.dev/app/api-tokens)
Add an existing API token
After completing these steps, Envio will generate all the necessary files for your indexer project, with event handlers in TypeScript.
If you are running this through an AI agent or a script, skip the prompts with the non-interactive equivalent. It needs an Envio API token, so create one at envio.dev/app/api-tokens and export it first, or pass --api-token <token>.
export ENVIO_API_TOKEN=your_token_here
pnpx envio init template -t greeter -l typescript -d .
Step 2: Understanding the Generated Files
Let's examine the key files that were created:
config.yaml
This configuration file defines which chains and contracts to index:
# Partial example
chains:
- id: 137 # Polygon
# ... Polygon chain settings
contracts:
- name: Greeter
address: "0x9D02A17dE4E68545d3a58D3a20BbBE0399E05c9c"
# ... contract settings
- id: 59144 # Linea
# ... Linea chain settings
contracts:
- name: Greeter
address: "0xdEe21B97AB77a16B4b236F952e586cf8408CF32A"
# ... contract settings
schema.graphql
This schema defines the data structures for the indexed events:
type User {
id: ID!
greetings: [String!]!
latestGreeting: String!
numberOfGreetings: Int!
}
src/handlers/Greeter.ts
This file contains the logic to process events emitted by the Greeter contract.
Step 3: Start Your Indexer
Important: Make sure Docker Desktop is running before proceeding.
- Start the indexer with:
pnpm dev
This command:
- Launches Docker containers for the database and Hasura
- Sets up your local development environment
- Begins indexing data from the specified contracts
- Opens a terminal UI to monitor indexing progress
The indexer will retrieve data from both Polygon and Linea blockchains, starting from the blocks specified in your config.yaml file.
Step 4: Interact with the Contracts
To see your indexer in action, you can write new greetings to the blockchain:
For Polygon:
- Visit the contract on Polygonscan
- Connect your wallet
- Use the
setGreetingfunction to write a new greeting - Submit the transaction
For Linea:
- Visit the contract on Lineascan
- Connect your wallet
- Use the
setGreetingfunction to write a new greeting - Submit the transaction
Since this is a multichain example, you can interact with both contracts to see how Envio handles data from different blockchains simultaneously.
Step 5: Query the Indexed Data
Now you can explore the data your indexer has captured:
- Open Hasura at http://localhost:8080
- When prompted for authentication, use the password:
testing - Navigate to the Data tab to browse the database tables
- Or use the API tab to write GraphQL queries
Example Query
Try this query to see the users with the most greetings:
query GetUsers {
User(limit: 10, order_by: { numberOfGreetings: desc }) {
id
chainId
latestGreeting
numberOfGreetings
greetings
}
}
Step 6: Deploy to Production (Optional)
When you're ready to move from local development to production:
- Visit Envio Cloud
- Follow the steps to deploy your indexer
- Get a production GraphQL endpoint for your application
For detailed deployment instructions, see the Envio Cloud documentation.
What You've Learned
By completing this tutorial, you've learned:
- How to initialize an Envio project from a template
- How indexers process data from multiple blockchains
- How to query indexed data using GraphQL
- The basic structure of an Envio indexing project
Next Steps
Now that you've mastered the basics, you can:
- Try the Contract Import feature to index any deployed contract
- Customize the event handlers to implement more complex indexing logic
- Add relationships between entities in your schema
- Explore Preload Optimization for faster handlers
- Create aggregated statistics from your indexed data
For more tutorials and examples, visit the Envio Documentation or join our Discord community for support.