Skip to main content

Indexing Greeter contract using Envio

This tutorial will take you through a step by step guide to indexing a live Greeter smart contract using Envio.

Background

Greeter contract

The Greeter contract is a very simple smart contract that allows a user to write a greeting message on the blockchain.

The Greeter contract is deployed on both the Polygon and the Linea blockchain. Following this tutorial will index events from both chains.

Envio

Envio is a framework for developing a backend to index and aggregate blockchain data into a graphQL query-able database.

Prerequisites

Environment tooling

  1. Node.js we recommend using something like fnm or nvm to install Node
  2. pnpm
  3. Docker Desktop

Install Envio

npm i -g envio

Step by step instructions

Initialize the project

Initialize the project using the Greeter template.

Run

envio init

Name your indexer

? Name your indexer:

Choose the directory where you would like to setup your project (default is the current directory)

? Set the directory:  (.) .

Select to start from a template

? Would you like to start from a template or migrate from a subgraph?
> "Template"
"SubgraphMigration"
[↑↓ to move, enter to select, type to filter]

Choose Greeter when prompted to choose template.

? Which template would you like to use?
"Blank"
> "Greeter"
"Erc20"
[↑↓ to move, enter to select, type to filter]

Then choose a language of your choice for the event handlers.

? Which language would you like to use?
> "Javascript"
"Typescript"
"Rescript"
[↑↓ to move, enter to select, type to filter]

Start the indexer

Dev note: 📢 make sure you have docker open

The following commands will start the docker and create databases for indexed data, make sure to re-run dev if you make changes to the files

Run

envio dev

The indexer will then start indexing the contract's specified in the config.yaml file from the start_block specified for each network.

Write to the contracts on Polygon and Linea using the block explorers

Once the indexer is running, you can call functions on the Greeter contract that is deployed on Polygon and Linea, using the respective network's blockchain explorer.

For Polygon, navigate to the contract on Polygonscan and the call setGreeting function.

For Linea, navigate to the contract on Lineascan and call the setGreeting function.

In the case of a multi-chain indexing example, you can call the setGreeting function on both contracts.

View the indexed results

You can view the indexed results on a local Hasura server.

open http://localhost:8080

The hasura admin-secret is testing and the tables can be viewed in the data tab or queried from the playground