How to Access Indexed Ethereum Data with the Graph

When you’re trying to access Ethereum data generated by smart contracts and dapps on the blockchain you might find it hard to transform the data to a readable format. The Graph is an indexing protocol for querying networks like Ethereum and IPFS. Anyone can build and publish open APIs, called subgraphs, making data easily accessible. In this tutorial, we’ll see how to use The Graph to query data about Aave using Graphql, Javascript, and Nodejs.

The first step for this tutorial will be to visit The Graph website and check the list of already supported protocols ranging from Uniswap, ERC20, Aave, Compound… Once you find the protocol you want to get data from (in our case Aave) click on the protocol and you’ll see it’s page:

The first thing you need to get from the page is the address where you’ll need to do the queries (1). In our case “https://api.thegraph.com/subgraphs/name/aave/protocol”.

The left box (2) gives you an example query that you can execute using the play button. You can easily use this text field to replace the content with your own query and test easily your queries while you build them.

The right part (3) give you a description of the entities you are able to query inside of that API. Clicking on any entity will describe it in more details with it’s fields and relationships with other entities.

Building the query

In our example will make a simple query that lists the latest flash loans that were made on the Aave platform. Writing queries for The Graph requires to be written in GraphQL, if you are not familiar with GraphQL check this tutorial. To start writing our query we’ll need to know how is a Flash Loan defined in the graph. For this we can search the FlashLoan entity on the left and see what it contains:

Our query that looks for the latest 10 flash loans that where made on Aave looks like this:

{
  flashLoans(first: 10, orderBy: timestamp, orderDirection: desc) {
    id
    reserve {
      name
      symbol
    }
    amount,
    target,
    timestamp
  }
}

Note that the flash loan object refers to another entity (the reserve) which is basically the pool containing the tokens borrowed.

Making the query with NodeJS

The Graph user interface offers a really good way to test and develop your own queries. Now it’s time to move our query to JavaScript (or your preferred stack) in order to be able to store the data or run scripts over it. The process is really easy, we just need to send the query through a post request to the https endpoint provided by The Graph.

In our case we’ll use axios to handle the request:

npm install axios --save

Then our complete code will looks like this:

const axios = require('axios')

axios.post('https://api.thegraph.com/subgraphs/name/aave/protocol', {
  query: `
  {
    flashLoans(first: 10, orderBy: timestamp, orderDirection: desc) {
      id
      reserve {
        name
        symbol
      }
      amount,
      target,
      timestamp
    }
  }  
  `
})
.then((res) => {
  for (const flashsloan of res.data.data.flashLoans) {
    console.log(flashsloan)
  }
})
.catch((error) => {
  console.error(error)
})

Which shoud give something like this when executed:

As you can see with the result is that it can remove a lot of work for you to listen the events on the blockchain, decode them and even work with token decimals..

Going further

Leave a Reply

Your email address will not be published. Required fields are marked *