ENS
The default values for the blockflow init commandIn this tutorial, we will show you how to index the ENS protocol using the Blockflow CLI. Blockflow provides a seamless and efficient solution for building "Serverless Backends" through our Managed Databases, On-chain Instances, and Custom APIs.
The Ethereum Name Service (ENS) is a decentralized protocol on the Ethereum blockchain that converts complex, alphanumeric addresses into human-readable names. This system simplifies cryptocurrency transactions and interactions with decentralized applications (dApps) by making them more user-friendly. ENS also supports linking names to various cryptocurrency addresses, content hashes, and metadata.
Project Overview
The purpose of this project is to teach you how to index ENS by creating subgraphs with the aid of Blockflow and its utilities. A short breakdown for the project is below -
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.Supply the appropriate schema for depending upon the data emitted by the events in the contract.
Write the handler function for each event for tracking.
Test the project using the command
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 to track the relevant data from CCTP:
On completing the schema, use the command blockflow typegen
to generate types.
Writing the handler function
Now, we move onto writing the handler functions for the project, before doing so, we move onto removing unwanted handler and keeping the necessary ones only into the studio.yaml file. As shown below, the studio.yaml will let us track the NameRegistered event:
Use the command blockflow codegen
to generate handler templates in /src/handlers directory.
On going into the handler template file created, we can see 3 events emitted which are id, owner, expires. We import the Registration class from /types/schema and bind the database connections as shown below:
We don't need to create an Id variable as in other tutorials because we already have it emitted in the event data. We still need a string holding a date value during the time of the transction, so we create it as follows:
On being done with all of this, we move onto doing CRUD operations onto the DB. We declare a variable registration
and look for a certain ID over the registrationDB
.The first if
statement gets triggered if the ID is not found in the DB and it creates a new document with the speicified ID. We assign either default or event values to all the fields in the documents accordingly. The else statement
block comes into play if a document is found for the ID being searched in the registrationDB
. The following values are updated according to the event data - registarant and events. The last line includes the await method to save the data updated in the registration variable.
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
.
And, we're good to go, to have a look at the complete indexing and all the necessary handlers for indexing ENS using blockflow CLI, do check the github repository.
Last updated