Efficiently Search Multiple ENS Domains in a Single Request
Overview
In this blog, we'll explore how to resolve addresses from ENS and vice versa using subgraphs on The Graph Network. Developers had to rely on JSON RPC calls directly to the ENS contracts in order to resolve these ENS names. To resolve multiple names, multiple calls were sent over the wire, making the process slow.
With the ENS Subgraph on The Graph Network, it is possible to resolve multiple ENS names with one request that resolves very fast. Thanks to the multiple Indexers competing for query fees with the fastest response.
What is ENS?
ENS is similar to Domain Name Service(DNS) on the internet. Ethereum Name Service (ENS) is a decentralized, open-source naming system built on the Ethereum blockchain. It allows users to register human-readable names (like vitalik.eth
) and map them to addresses (eg: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
).
What is The Graph?
The Graph is a decentralized protocol for indexing and querying blockchain data. On The Graph Network, we can build decentralized APIs called Subgraphs. From the subgraph, we can query data from the blockchain using GraphQL in a fast and efficient way. In simple words, querying data from blockchain is hard. The Graph makes it easier.
Learn more about The Graph from here.
Now that we've learned about ENS & The Graph, let's do some queries.
Head on to the ENS Subgraph playground and let's learn queries one by one.
Fetch address pointed by an ENS
For this example, we can resolve vitalik.eth
and fetch the address.
Below is the sample query. Copy this query and try it out on the playground of the ENS subgraph. Also, give a try different ENS.
query Address {
domains(where: {name: "vitalik.eth"}) {
resolvedAddress {
id
}
}
}
From the above query, you'll get the result below.
{
"data": {
"domains": [
{
"resolvedAddress": {
"id": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
}
}
]
}
}
Fetch all ENS pointed to an address
Now that we've got an address from an ENS, let's fetch all ENS pointed to an address.
For this example, we'll be using 0xd8da6bf26964af9d7eed9e03e53415d37aa96045
.
query ENS {
domains(where: {
resolvedAddress: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
}){
name
}
}
From the above query, you'll be getting a bunch of ENS that are pointed to the address in the query.
Resolve multiple ENS in a single query
We learned to resolve the address from ENS and vice versa. Now, let's do the ultimate query!
query DomainLookups(
$domains: [String!]! = [
"vitalik.eth"
"messari.eth"
]
) {
domains(where: { name_in: $domains }) {
name
resolvedAddress {
id
}
}
}
In this single query, an array of domains is passed as a variable and you can query resolvedAddress
in a fast, efficient & decentralized way. Below is an example of the result from the above query.
{
"data": {
"domains": [
{
"name": "messari.eth",
"resolvedAddress": {
"id": "0x6fa2bacf752dab6cb6e4b922321f03b4cb61d141"
}
},
{
"name": "vitalik.eth",
"resolvedAddress": {
"id": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
}
}
]
}
}
If you want to query from The Graph Network to your dApp, do check out the docs here.
Thanks for reading!
If you have any queries, feel free to DM me on X @0xShiyasmohd
Subscribe to my newsletter
Read articles from Shiyas Mohammed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shiyas Mohammed
Shiyas Mohammed
Engineer @Edge & Node | Working on The Graph