Skip to main content

Indexing a live Greeter contract using Envio

ยท 3 min read

Blockchains excel at decentralizing data and continue to revolutionize traditional systems of trust and record-keeping, but data retrieval is often inefficient and majorly time-consuming for developers hampering the overall development experience.

Indexers like Envio allow developers to sift through blockchain data, tidy it up, and store it in a useful intermediary format that is easily accessible. Applications can then use this format for potent queries on indexed data, ensuring scalability and production readiness.

This tutorial will take you through a step by step guide to indexing a Greeter smart contract deployed on Linea 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.

Envioโ€‹

Envio is a real-time indexer built specifically for EVM-compatible blockchains, providing developers with a seamless and efficient indexing solution. Designed to optimize the user experience, Envio offers automatic code generation and flexible language support. Indexers on Envio can be written in JavaScript, TypeScript or ReScript. Easily 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"
"ERC-20"
[โ†‘โ†“ 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 indexing 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.

Write to the contract on Lineascanโ€‹

Once the indexer is running, you can call functions on the Greeter contract that is deployed on Linea.

Navigate to the contract on Lineascan and call the setGreeting function.

View the indexed resultsโ€‹

You can view the indexed results on your 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


Check out the full Envio docs here.

Our engineers are available to help you with your indexing use case and where Envio can provide value. Hop into our Discord or send us an email at hello@envio.dev.