Polygon Quick Start - Lens Protocol
Polygon Quick Start - Lens Protocol
This article's purpose is to provide a clear, step-by-step guide on setting up an indexer for the Lens Protocol on the Polygon blockchain. By the end of this guide, you will understand what Lens Protocol is, why its smart contract data is valuable, and how to set up a SubQuery indexer to track and index events like profile creation, post, and follow.
Please initialise a Polygon project
In the earlier Quickstart section , you should have taken note of three crucial files. To initiate the setup of a project from scratch, you can proceed to follow the steps outlined in the initialisation description.
As a prerequisite, you will need to generate types from the ABI files of each smart contract. Additionally, you can kickstart your project by using the EVM Scaffolding approach (detailed here). You'll find all the relevant events to be scaffolded in the documentation for each type of smart contract.
Note
The final code of this project can be found here.
Your Project Manifest File
The Project Manifest file is an entry point to your project. It defines most of the details on how SubQuery will index and transform the chain data.
For EVM chains, there are three types of mapping handlers (and you can have more than one in each project):
- BlockHanders: On each and every block, run a mapping function
- TransactionHandlers: On each and every transaction that matches optional filter criteria, run a mapping function
- LogHanders: On each and every log that matches optional filter criteria, run a mapping function
In this Lens Protocol indexing project, our primary objective is to configure the indexer to specifically gather data from a single smart contract: 0xDb46d1Dc155634FbC732f92E853b10B288AD5a1d
, which you can find on this page. You can copy the entire JSON and save as a file LensHub.abi.json
in the root directory.
Important
We use Ethereum packages, runtimes, and handlers (e.g. @subql/node-ethereum
, ethereum/Runtime
, and ethereum/*Handler
) for Polygon. Since Polygon is a layer-2 scaling solution, we can use the core Ethereum framework to index it.
For a more comprehensive understanding of how the fundamental mechanisms of this protocol work, you can refer to the official Lens documentation.
Then, you need to update the datasources
section as follows:
{
dataSources: [
{
kind: EthereumDatasourceKind.Runtime,
startBlock: 28384641, // This is the block that the contract was deployed on
options: {
// Must be a key of assets
abi: "LensHub",
address: "0xDb46d1Dc155634FbC732f92E853b10B288AD5a1d",
},
assets: new Map([["LensHub", { file: "./abis/LensHub.abi.json" }]]),
mapping: {
file: "./dist/index.js",
handlers: [
{
kind: EthereumHandlerKind.Event,
handler: "handlePostCreated",
filter: {
topics: [
"PostCreated(uint256,uint256,string,address,bytes,address,bytes,uint256)",
],
},
},
{
kind: EthereumHandlerKind.Event,
handler: "handleProfileCreated",
filter: {
topics: [
"ProfileCreated(uint256,address,address,string,string,address,bytes,string,uint256)",
],
},
},
{
kind: EthereumHandlerKind.Event,
handler: "handleFollowed",
filter: {
topics: ["Followed(address,uint256[],bytes[],uint256)"],
},
},
],
},
},
];
}
This setup establishes an manifest file to gather and manage information from a particular smart contract on the Polygon network, specified by its unique address. It has three handlers:
handlePostCreated
: This handler is responsible for processing data associated with thePostCreated
event, which includes details about newly created posts.handleProfileCreated
: This handler is dedicated to managing data originating from theProfileCreated
event, which pertains to the creation of user profiles.handleFollowed
: This handler is designed to handle data stemming from theFollowed
event, which tracks user interactions related to following other users.
Check out our Manifest File documentation to get more information about the Project Manifest (project.ts
) file.
Update Your GraphQL Schema File
The schema.graphql
file determines the shape of your data from SubQuery due to the mechanism of the GraphQL query language. Hence, updating the GraphQL Schema file is the perfect place to start. It allows you to define your end goal right at the start.
Remove all existing entities and update the schema.graphql
file as follows:
type Profile @entity {
id: ID!
handle: String
imageURI: String
creator: Account
owner: Account
followNFT: String
followNFTURI: String
posts: [Post!] @derivedFrom(field: "profile")
createdAt: BigInt
follows: [Follow]! @derivedFrom(field: "toProfile")
}
type Post @entity {
id: ID!
contentURI: String
profile: Profile
collectModule: String
collectModuleReturnData: String
timestamp: BigInt
}
type Account @entity {
id: ID!
profiles: [Profile!] @derivedFrom(field: "owner")
}
type Follow @entity {
id: ID!
fromAccount: Account
toProfile: Profile
timestamp: BigInt
}
Important
When you make any changes to the schema file, please ensure that you regenerate your types directory.
From three logs we're working with, there's a wealth of data to extract. Notably, the following entities can be derived:
Profile
that represents a user profile on the Lens Protocol. Attributes include an ID, user handle, profile image URI, creator account, owner account, follow NFT information, follow NFT URI, creation timestamp, and an array of posts associated with this profile. Thefollows
attribute establishes a one-to-many relationship with theFollow
entity, linking profiles to the users they are followed by.Post
which represents a post. Attributes include an ID, content URI, the profile that created the post, the collect module used, collect module return data, and a timestamp for when the post was created.Account
which represents an individual Polygon account. It is linked to one or more profiles (one-to-many relationship), indicating that an account can own multiple user profiles.Follow
that represents a follow action. Attributes include an ID, the account that initiated the follow action, the profile that was followed, and a timestamp for when the follow action occurred.
Note
Importantly, these relationships can not only establish one-to-many connections but also extend to include many-to-many associations. To delve deeper into entity relationships, you can refer to this section. If you prefer a more example-based approach, our dedicated Hero Course Module can provide further insights.
SubQuery simplifies and ensures type-safety when working with GraphQL entities, smart contracts, events, transactions, and logs. The SubQuery CLI will generate types based on your project's GraphQL schema and any contract ABIs included in the data sources.
yarn codegen
npm run-script codegen
This action will generate a new directory (or update the existing one) named src/types
. Inside this directory, you will find automatically generated entity classes corresponding to each type defined in your schema.graphql
. These classes facilitate type-safe operations for loading, reading, and writing entity fields. You can learn more about this process in the GraphQL Schema section.
It will also generate a class for every contract event, offering convenient access to event parameters, as well as information about the block and transaction from which the event originated. You can find detailed information on how this is achieved in the EVM Codegen from ABIs section. All of these types are stored in the src/types/abi-interfaces
and src/types/contracts
directories.
You can conveniently import all these types:
import { Account, Post, Profile, Follow } from "../types";
import {
PostCreatedLog,
ProfileCreatedLog,
FollowedLog,
} from "../types/abi-interfaces/LensHubAbi";
Check out the GraphQL Schema documentation to get in-depth information on schema.graphql
file.
Now that you have made essential changes to the GraphQL Schema file, let’s proceed ahead with the Mapping Function’s configuration.
Add a Mapping Function
Mapping functions define how blockchain data is transformed into the optimised GraphQL entities that we previously defined in the schema.graphql
file.
Follow these steps to add a mapping function:
Navigate to the default mapping function in the src/mappings
directory. You will be able to see three exported functions: handleBlock
, handleLog
, and handleTransaction
. Replace these functions with the following code (note the additional imports):
import {
PostCreatedLog,
ProfileCreatedLog,
FollowedLog,
} from "../types/abi-interfaces/LensHubAbi";
import { Account, Post, Profile, Follow } from "../types";
import assert from "assert";
export async function getOrCreateAccount(
accountAddress: string,
): Promise<Account> {
let account = await Account.get(accountAddress);
if (!account) {
account = Account.create({
id: accountAddress,
});
}
return account;
}
export async function getOrCreateFollow(
accountAddress: string,
): Promise<Follow> {
let follow = await Follow.get(accountAddress);
if (!follow) {
follow = Follow.create({ id: accountAddress });
}
return follow;
}
export function getNewPublicactionId(profileId: BigInt, pubId: BigInt): string {
return profileId.toString().concat("-").concat(pubId.toString());
}
export async function getOrCreatePost(pubId: BigInt): Promise<Post> {
let post = await Post.get(pubId.toString());
if (!post) {
post = await Post.create({
id: pubId.toString(),
});
}
return post;
}
export async function getOrCreateProfile(profileId: string): Promise<Profile> {
let profile = await Profile.get(profileId);
if (!profile) {
profile = Profile.create({
id: profileId,
});
}
return profile;
}
export async function handleProfileCreated(
event: ProfileCreatedLog,
): Promise<void> {
logger.warn("Handling ProfileCreatedLog");
assert(event.args, "No log args");
logger.warn(event.args.profileId.toString());
let profile = await getOrCreateProfile(event.args.profileId.toString());
let creator = await getOrCreateAccount(event.args.creator);
let to = await getOrCreateAccount(event.args.to);
profile.creatorId = creator.id;
profile.ownerId = (await getOrCreateAccount(event.args.to)).id;
profile.followNFTURI = event.args.followNFTURI;
profile.handle = event.args.handle;
profile.imageURI = event.args.imageURI;
Promise.all([creator.save(), to.save(), profile.save()]);
}
export async function handlePostCreated(event: PostCreatedLog): Promise<void> {
logger.warn("Handling PostCreatedLog");
assert(event.args, "No log args");
let post = await getOrCreatePost(event.args.pubId.toBigInt());
let profile = await getOrCreateProfile(event.args.profileId.toString());
post.profileId = profile.id;
post.timestamp = event.args.timestamp.toBigInt();
post.contentURI = event.args.contentURI;
Promise.all([profile.save(), post.save()]);
}
export async function handleFollowed(event: FollowedLog): Promise<void> {
logger.warn("Handling FollowedLog");
assert(event.args, "No log args");
for (let index in event.args.profileIds) {
let profileId = event.args.profileIds[index];
logger.warn(profileId.toString());
let profile = await getOrCreateProfile(profileId.toString());
let follow = await getOrCreateFollow(
event.args.follower
.concat("-")
.concat(event.transaction.hash)
.concat("-")
.concat(profileId.toString()),
);
let follower = await getOrCreateAccount(event.args.follower);
follow.fromAccountId = follower.id;
follow.toProfileId = profile.id;
follow.timestamp = event.args.timestamp.toBigInt();
Promise.all([profile.save(), follow.save(), follower.save()]);
}
}
Let's dive into an explanation of the code above. The code includes three distinct handlers, each of which leverages some helper functions for interacting with different entities:
handleProfileCreated
which either creates a new profile entity or updates an existing one. It assigns key attributes such as the creator's ID, owner's ID, follow NFT URI, user handle, and profile image URI.handlePostCreated
which performs a similar task as the first handler, creating or updating a post entity. This includes specifying the profile ID, timestamp, content URI, and other pertinent attributes.handleFollowed
. For each profile that's being followed, it either creates a new profile entity or updates an existing one, and a follow entity is established. It associates the follower, the profile being followed, and the timestamp with the follow event.
🎉 At this point, we've effectively integrated all the desired entities that can be retrieved from Lens smart contract. For each of these entities, there is a corresponding mapping handler to structure and store the data in a format that can be easily queried and utilised.
Note
Check the final code repository here to observe the integration of all previously mentioned configurations into a unified codebase.
Note
For more information on mapping functions, please refer to our Mappings documentation.
Build Your Project
Next, build your work to run your new SubQuery project. Run the build command from the project's root directory as given here:
yarn build
npm run-script build
Important
Whenever you make changes to your mapping functions, you must rebuild your project.
Now, you are ready to run your first SubQuery project. Let’s check out the process of running your project in detail.
Whenever you create a new SubQuery Project, first, you must run it locally on your computer and test it and using Docker is the easiest and quickiest way to do this.
Run Your Project Locally with Docker
The docker-compose.yml
file defines all the configurations that control how a SubQuery node runs. For a new project, which you have just initialised, you won't need to change anything.
However, visit the Running SubQuery Locally to get more information on the file and the settings.
Run the following command under the project directory:
yarn start:docker
npm run-script start:docker
Note
It may take a few minutes to download the required images and start the various nodes and Postgres databases.
Query your Project
Next, let's query our project. Follow these three simple steps to query your SubQuery project:
Open your browser and head to
http://localhost:3000
.You will see a GraphQL playground in the browser and the schemas which are ready to query.
Find the Docs tab on the right side of the playground which should open a documentation drawer. This documentation is automatically generated and it helps you find what entities and methods you can query.
Try the following queries to understand how it works for your new SubQuery starter project. Don’t forget to learn more about the GraphQL Query language.
Posts and Profiles
Request
{
posts(first: 1) {
nodes {
id
contentURI
collectModule
profile {
id
owner {
id
}
followNFTURI
creator {
id
}
}
}
}
}
Reponse
{
"data": {
"posts": {
"nodes": [
{
"id": "354",
"contentURI": "ar://ujK-ZBiv9QfVDW4a-VVj3USz5LFGjMwTpvN1A09kbq0",
"collectModule": null,
"profile": {
"id": "98203",
"owner": null,
"followNFTURI": null,
"creator": null
}
}
]
}
}
}
Follows
Request
{
follows(first: 1) {
nodes {
id
timestamp
fromAccount {
id
}
toProfile {
id
owner {
id
}
followNFTURI
creator {
id
}
}
}
}
}
Reponse
{
"data": {
"follows": {
"nodes": [
{
"id": "0x57B7bf6f792a6181Ec5aFB88cE7bcE330a9d1b67-0x7ea8308689589c49a2ba71593a697841466dc2a2609f4cad03caa956c911fa65-119173",
"timestamp": "1696602888",
"fromAccount": {
"id": "0x57B7bf6f792a6181Ec5aFB88cE7bcE330a9d1b67"
},
"toProfile": {
"id": "119173",
"owner": null,
"followNFTURI": null,
"creator": null
}
}
]
}
}
}
Note
The final code of this project can be found here.
What's next?
Congratulations! You have now a locally running SubQuery project that accepts GraphQL API requests for transferring data.
Tip
Find out how to build a performant SubQuery project and avoid common mistakes in Project Optimisation.
Click here to learn what should be your next step in your SubQuery journey.