Snapshot
In this tutorial, we will show you how to index Snapshot data using the Blockflow CLI. Blockflow provides a seamless and efficient solution for building "Serverless Backends" through our Managed Databases, Instances, and Custom APIs.
Snapshot Labs is an innovative platform that facilitates decentralized governance for blockchain projects. It lets token holders easily vote on proposals without paying gas fees, making the process super efficient. With its user-friendly interface, anyone can join in and have a say in decision-making. The platform also supports various blockchain protocols, making it flexible and perfect for diverse communities looking to manage their projects collaboratively.
Project Overview
The purpose of this tutorial is to teach you how to index Snapshot data by creating subgraphs with the aid of Blockflow and its utilities. A short breakdown for the project is -
Create a project directory and
cd
into it. Use the commandblockflow init
through the terminal and provide with the relevant contract address to be tracked.Provide with appropriate schema to the
studio.schema.ts
file depending upon the data emitted by the events to be tracked.Write the handler functions for tracking each event.
Test the project using the comand
blockflow test
and verify the data stored over the database.
Setting up the project
Download the Blockflow CLI using the command:
We can remove the
-g
tag if global installation is not required.Create a project directory and use the
cd
command over it to make it the working directory.Use the
blockflow init
command to initiate the project.Below are the fields that need to be entered for the project. You can also let the default values for each field by pressing [ENTER]. The last field requires you to select the instance trigger type with 4 choices. You can also add more than one trigger for a project.
Setting up the schema
The below mentioned schema is appropriate for indexing the events emitted by the Snapshot contract:
On completing the schema, use the command blockflow typegen
to generate types.
Writing the handler function
Now we move onto writing the handler functions but first we remove the unwanted event triggers in our studio.yaml file otherwise unnecessary handlers would be generated in our /src
directory. Below is the studio.yaml:
Use the command blockflow codegen
to generate handler templates in the /src/handlers
directory.
The handlers have been generated as such so we now move onto writing handler logic and updating the databases. In the /src/handlers/delegateregistry
directory, we find two files - ClearDelegate.ts and SetDelegate.ts. We proceed with writing the ClearDelegate handler. The boilerplate code is already present there. We now import the Delegation and other imp classes from the types/schema
and bind the database connections.
We create a delegation variable and use the await method to create the document in delegationDB and blockDB as follows.
To fill the fields, we use the emitted event data and blockflow's utils. Similarly we can write the handler logic for the SetDelegate.ts file as follows
Testing
We use the below command to test if the handler logic is correct and the data gets stored on our specified collection in Mongo.
The <block number> can be put of any range, but we can put it to latest
if no opinion provided. The — uri
holds the MongoDB connection URL which is tested locally shall be filled with mongodb://localhost:27017/blockflow_studio
.
Last updated