Skip to main content

Ethereum Quick Start - ENS (Complex)

SubQuery TeamAbout 2 min

Ethereum Quick Start - ENS (Complex)

This project can be use as a starting point for developing your new Ethereum SubQuery project, it indexes all ENS Records in the ENS registry.

Important

We suggest starting with the Ethereum Gravatar example. The Ethereum PancakeSwap project is a lot more complicated and introduces some more advanced concepts

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.

Now, let's move forward and fork the example code for this project from hereopen in new window

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

The main concepts in this ENS project is that it only indexes logs from ENS' various smart contracts, LogHandlers are the most common type of handlers for Ethereum, and it shows here in this example project. There are a total of 31 different log handlers in this project.

Secondly, note that there are 7 different ABIs imported into this project. We give each different ABI it's own section under datasources since they all have their own unique smart contract address.

{
  dataSources: [
    // ENSRegistry
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 9380380,

      options: {
        // Must be a key of assets
        abi: "EnsRegistry",
        address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
      },
      assets: new Map([["EnsRegistry", { file: "./abis/Registry.json" }]]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // ENSRegistryOld
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 3327417,

      options: {
        // Must be a key of assets
        abi: "EnsRegistry",
        address: "0x314159265dd8dbb310642f98f50c066173c1259b",
      },
      assets: new Map([["EnsRegistry", { file: "./abis/Registry.json" }]]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // Resolver
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 3327417,

      options: {
        // Must be a key of assets
        abi: "Resolver",
      },
      assets: new Map([["Resolver", { file: "./abis/PublicResolver.json" }]]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // BaseRegistrar
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 9380410,

      options: {
        // Must be a key of assets
        abi: "BaseRegistrar",
        address: "0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85",
      },
      assets: new Map([
        ["BaseRegistrar", { file: "./abis/BaseRegistrar.json" }],
      ]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // EthRegistrarControllerOld
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 9380471,
      options: {
        // Must be a key of assets
        abi: "EthRegistrarControllerOld",
        address: "0x283Af0B28c62C092C9727F1Ee09c02CA627EB7F5",
      },
      assets: new Map([
        [
          "EthRegistrarControllerOld",
          { file: "./abis/EthRegistrarControllerOld.json" },
        ],
      ]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // EthRegistrarController
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 3327417,
      options: {
        // Must be a key of assets
        abi: "EthRegistrarController",
      },
      assets: new Map([
        [
          "EthRegistrarController",
          { file: "./abis/EthRegistrarController.json" },
        ],
      ]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
    // NameWrapper
    {
      kind: EthereumDatasourceKind.Runtime,
      startBlock: 3327417,
      options: {
        // Must be a key of assets
        abi: "NameWrapper",
      },
      assets: new Map([["NameWrapper", { file: "./abis/NameWrapper.json" }]]),
      mapping: {
        file: "./dist/index.js",
        handlers: [...],
      },
    },
  ],
}

Note

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.

You'll see that there are 33 GraphQL entities in the ENS project with many foreign key relationships between them. Take for example the Domain and DomainEvent entities. There is a one to many relationship between Domain and DomainEvent, and there is also a one to many relationship that Domain has with itself (via the parent property), we've event created a virtual subdomains field that can be used to navigate via the GraphQL entities.

type Domain @entity {
  id: ID! # The namehash of the name
  name: String # The human readable name, if known. Unknown portions replaced with hash in square brackets (eg, foo.[1234].eth)
  parent: Domain # The namehash (id) of the parent name
  subdomains: [Domain] @derivedFrom(field: "parent") # Can count domains from length of array
  events: [DomainEvent] @derivedFrom(field: "domain")
  ...
}

type DomainEvent @entity {
  id: ID!
  domain: Domain!
  ...
}

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
yarn 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:

All entites can be imported from the following directory:

// Import entity types generated from the GraphQL schema
import {
  Account,
  Domain,
  Resolver,
  NewOwner,
  Transfer,
  NewResolver,
  NewTTL,
} from "../types";
import {
  NewOwnerEvent,
  TransferEvent,
  NewResolverEvent,
  NewTTLEvent,
} from "../types/contracts/Registry";

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.

They operate in a similar way to SubGraphs, and you can see wiht ENS that they are contained in 4 different files with the addition of a helper utils.ts.

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
yarn 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
yarn 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:

  1. Open your browser and head to http://localhost:3000.

  2. You will see a GraphQL playground in the browser and the schemas which are ready to query.

  3. 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.

query {
  domains(first: 5, orderBy: SUBDOMAIN_COUNT_DESC) {
    nodes {
      id
      name
      labelName
      subdomains(first: 5) {
        totalCount
        nodes {
          id
          name
          labelName
        }
      }
    }
  }
}
{
  "data": {
    "domains": {
      "nodes": [
        {
          "id": "0x0000000000000000000000000000000000000000000000000000000000000000",
          "name": null,
          "labelName": null,
          "subdomains": {
            "totalCount": 2,
            "nodes": [
              {
                "id": "0x825726c8cd4176035fe52b95bc1aef3c27e841545bd3a431079f38641c7ba88c",
                "name": "0xdec08c9dbbdd0890e300eb5062089b2d4b1c40e3673bbccb5423f7b37dcf9a9c",
                "labelName": "0xdec08c9dbbdd0890e300eb5062089b2d4b1c40e3673bbccb5423f7b37dcf9a9c"
              },
              {
                "id": "0xd1b0e2eec983ad6a7fb21f6fc706af8717b12b8814d2596016750ea73e00b57f",
                "name": "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0",
                "labelName": "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0"
              }
            ]
          }
        },
        {
          "id": "0x352b3a53b6861a6c39477ba530d607cc922b3469121b1b1cb533c2b66805007c",
          "name": null,
          "labelName": "0xe5e14487b78f85faa6e1808e89246cf57dd34831548ff2e6097380d98db2504a",
          "subdomains": {
            "totalCount": 0,
            "nodes": []
          }
        },
        {
          "id": "0x79700a4bad07bddf30b55c0c41297f727c853ae7ac64667e009df49a9ab68dfd",
          "name": null,
          "labelName": "0xc384f2a2b2ac833e2abf795bf38a38f0865833233b8f67cecd7598bd108a2859",
          "subdomains": {
            "totalCount": 0,
            "nodes": []
          }
        },
        {
          "id": "0x825726c8cd4176035fe52b95bc1aef3c27e841545bd3a431079f38641c7ba88c",
          "name": "0xdec08c9dbbdd0890e300eb5062089b2d4b1c40e3673bbccb5423f7b37dcf9a9c",
          "labelName": "0xdec08c9dbbdd0890e300eb5062089b2d4b1c40e3673bbccb5423f7b37dcf9a9c",
          "subdomains": {
            "totalCount": 0,
            "nodes": []
          }
        },
        {
          "id": "0xd1b0e2eec983ad6a7fb21f6fc706af8717b12b8814d2596016750ea73e00b57f",
          "name": "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0",
          "labelName": "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0",
          "subdomains": {
            "totalCount": 0,
            "nodes": []
          }
        }
      ]
    }
  }
}

Note

The final code of this project can be found hereopen in new window.

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.