Querying Ethereum Data using The Graph (ENS examples)

Unegma
3 min readJul 12, 2021

12/Jul/2021

Indexing

Indexing is essential when working with large data sets. When you ask a question using a search engine, the search engine does not, in that moment, scan every single web page (server) on the internet and deliver you a result -that would take far too long! - instead, a database belonging to the search engine company is scanned so that results are delivered to you much quicker.

Whilst each web server on the internet is not scanned at the moment of your query, prior to your query this has happened, and search engine companies do in fact constantly scan new web pages made available on the internet and add the data to their indexes so that we can all find things quickly. It sort of like of asking a librarian where a book is instead of looking yourself.

Indexing Blockchains

Blockchain networks are no exception when it comes to needing some sort of indexing. The Ethereum network, for example, is constantly processing new transactions which are put through smart contracts, and the result is a giant ledger of data ordered in a certain way, but not optimised for returning the answers to certain queries such as “How many NFTs do I have?”

In order to answer “How many NFTs do I have?” without an indexing service, a developer would have to check every single NFT smart contract in existence for transactions from a specific wallet address; the current ‘state’ of who owns what would then need to be deciphered and by the time this was done, more transactions would have already occurred.

The Graph

The Graph is one service which aims to solve the need for answering our questions in a speedy way. They describe themselves as an “Indexing protocol for querying networks like Ethereum and IPFS”; and as “APIs for a vibrant decentralized future”, where “Anyone can build and publish open APIs, called subgraphs, making data easily accessible”.

The Graph makes it easy to host what are called ‘Subgraphs’, with API endpoints to indexed data which is: “Stored and processed on open networks with verifiable integrity” (such as IPFS). There are plenty of Subgraphs with queryable endpoints already available for most well known Web3 services such as ENS, Aave or Uniswap.

ENS and GraphQL

ENS is a service which makes it easy to map a ‘human rememberable’ name to a specific wallet address; i.e. it is much easier to say to someone: “Send to unegma.eth” vs: “To 0xEF9D542Cd93c6300b5BB755dff4033Eb0c8f8e01”. We will look at some examples below using the ENS Subgraph to query data (and therefore answer questions) about the unegma.eth ENS address.

When querying a Subgraph, a query language called GraphQL is used. This is a widely used format for querying data and is not blockchain specific. The idea behind GraphQL, is that a developer queries for the exact data which they need, usually in a JSON-like format. e.g. querying for { hero { name } } would return: { "hero": { "name": "Vitalik Buterin" } }.

Examples using ENS

The ‘Playground’ tab under any Subgraph Explorer lets developers search and experiment with available data in an easy to use way, however, a service such as Postman can also be used by putting a query into ‘Body’ and then selecting ‘GraphQL’. (Please note, that for querying GraphQL endpoints, the request needs to be a POST request). Ultimately, this will be done in code.

We will now find the expiry date of an ENS domain by querying the ENS Subgraph endpoint. First we need to get the labelhash property from the domain, so we query the Domain type by name requesting the labelhash:

{
domains(where:{name:"unegma.eth"})
{
name
labelhash
}
}

Resulting in:

{
"data": {
"domains": [{
"labelhash":"0x93af1d2e6382871bfb..",
"name": "unegma.eth"
}]
}
}

We must then query the Registration type in order to get the registration data connected to the ENS domain. We do this by passing the labelhash as the id when requesting data about registrations:

{
registrations(first: 1, where: {id: "0x93af1d2e6382871bfb.."})
{
id
registrationDate
expiryDate
}
}

Resulting in:

{
"data": {
"registrations": [{
"expiryDate": "1657395303",
"id":"0x93af1d2e6382871bfb4ec7d..",
"registrationDate": "1625838351"
}]
}
}

We now have the expiry and registration date (albeit in UNIX timestamp format, so remember to convert into a friendlier format using either a date library or: https://www.unixtimestamp.com/index.php)

What Next

And that is it for this short introduction to querying The Graph for ENS specific data. In future posts, we will look into more complicated queries for returning other data related to ENS domains. Thanks for reading, and thanks to Makoto, Jeff and Jamie for making this article possible. Check out Ethereum London for upcoming Ethereum events in London.

https://unegma.com/

--

--