The Graph vs Alethio vs web3JS
Tags: defi, cryptodata, cryptomarketdata
Published: 2020-07-03 17:25:42

---
title: The Graph vs Alethio vs web3JS
published: true
date: 2020-06-16 18:13:41 UTC
tags: Defi,cryptodata,cryptomarketdata,defi
canonical_url: https://blog.coincodecap.com/the-graph-vs-alethio-vs-web3js?utm_source=rss&utm_medium=rss&utm_campaign=the-graph-vs-alethio-vs-web3js
---

### A practical exercise using the Aave protocol

## Introduction

There is always talk about the clarity of [blockchain](https://blog.coincodecap.com/what-is-blockchain-a-simple-guide-for-dummies/), that everything is traceable and everyone can see what was written but is it easy to access this information? How can I get data to do my analysis in a simple way?

In this post we are going to see three ways to do it, using the DeFi [Aavesome](https://medium.com/u/13bfa9f22920) protocol as an example, in which we are going to obtain the history of deposits made on the platform, using three different methods:

- [The Graph](https://medium.com/u/c2f061b81210): A protocol for building decentralized applications using GraphQL.
- [alethio](https://medium.com/u/57ffb51f1e81): An API rest to obtain data from the blockchain in a friendly way.
- [Web3 Foundation Team](https://medium.com/u/d6954f1a46d9): Reading the events directly on the blockchain using the web3js library

## Aave Data

In order to access certain data on the blockchain, the easiest way is to query the events. When a function in a [smart contract](https://blog.coincodecap.com/tag/smart-contact) is called, if a certain action is executed, an event can be emitted, which can listen at that time by clients who are listening for that particular event, or in the future, accessing their history, which is recorded on the blockchain.

In this case we want to see the history of deposits made in the Aave protocol, so we look inside its code if there is an event related to this.

In its documentation, we see that if there is an event for this and it contains the following data:

![Aave doc](https://cdn-images-1.medium.com/max/800/0*ue5U7Zrj_OMV388D)

But if we don’t want to trust the documentation and see it in the code, let’s see the contract [deployed on mainnet](https://etherscan.io/address/0x398eC7346DcD622eDc5ae82352F02bE94C62d119#code) and look in code in the part:

![Etherscan](https://cdn-images-1.medium.com/max/800/0*bK9RMn1q4BeisSC5)

Which contains this event, if we want to see in detail the moment in which it is generated we see that it is done once the deposit function is finished, therefore it is what we are looking for:

![](https://cdn-images-1.medium.com/max/800/0*ECsh6B6c-nWUosT1)

## Getting the data

Once we are clear about what we are looking for, we will obtain these data in three different ways.

### **The Graph**

Aave’s team has created a subgraph that can be tested [in this playground.](https://thegraph.com/explorer/subgraph/aave/protocol-raw)

As an example, we want to extract the deposit id, and the amount that was deposited in it, to then do our analysis, for this we use the query

```
{
  deposits(first:1000, skip:0){
   id,
   amount
  }
}
```

that returns the data of the same

![transaction](https://cdn-images-1.medium.com/max/800/0*TcpbQtM_k1yCcP2D)

We would have to repeat this query n times, advancing the skip parameter from 1000 to 1000 to bring up the next block of data.

### **Alethio**

Now we are going to obtain it using the Alethio api, for this we will use the endpoint:

```
[https://api.aleth.io/v1/log-entries?filter[loggedBy]=0x398eC7346DcD622eDc5ae82352F02bE94C62d119&filter[hasLogTopics.0]=0xc12c57b1c73a2c3a2ea4613e9476abb3d8d146857aab7329e24243fb59710c82](https://api.aleth.io/v1/log-entries?filter%5BloggedBy%5D=0x398eC7346DcD622eDc5ae82352F02bE94C62d119&filter%5BhasLogTopics.0%5D=0xc12c57b1c73a2c3a2ea4613e9476abb3d8d146857aab7329e24243fb59710c82)
```

Where we query the logs of the Aave’s smart contract address, and filter the logs, with the hash of the Deposit function

The object returned by this endpoint indicates the event, with its parameters and the link to the “next” pages to bring in a new data block.

![Alethio api](https://cdn-images-1.medium.com/max/800/0*knxIwG80FqHw2_Kr)

We iterate all of these “next” links by increasing the number of existing “Deposit” events.

### **web3.js**

As a third option we will read the events directly from the blockchain, to do that, we will use the web3js library and we will get all the events of type “deposit” in the Aave contract, with a script like the following:

```
const contract = new web3.eth.Contract(LendingPoolABI, LendingPoolAddress)
return contract.getPastEvents('Deposit', {
   fromBlock: initBlock,
   toBlock: endBLock
}, function (error, events) { return events })
```

## Comparing results

Once we have created the three methods, we are going to execute them at the same time, to see that we have the same number of deposits and the information is correct, from three different sources, for this we execute the three methods and write the total number of events:

```
console.log('Number of events with Alethio:', (await getDepositsAlethio()).length)
console.log('Number of events with web3js:', (await getEventsWeb3js()).length)
console.log('Number of events with The graph:', (await getDepositsGQL()).length)
```

The output shows us:

```
Number of events with Alethio: 31810

Number of events with web3js: 31809

Number of events with The graph: 31682
```

Oops…. We have a discrepancy, in the case of Alethio and web3js, it is only one event, which occurred between executions, but the difference with The graph is bigger.

Doing several tests I have verified that there is a certain delay from the generation of an event, until it is indexed in The graph, so if this event is consulted, nothing else is not found in The graph, but a few minutes later if it is, hence the discrepancy in the number of events recorded.

In these three ways, you can consult the data, creating your own datasets with which build models, and verify that the information shown by these protocols is true. Do you think your bank would allow you to access the deposit history?

- [Ethereum Name Service (ENS)](https://blog.coincodecap.com/ethereum-name-service-ens)
- [Understanding Cryptocurrency Trading Bots](https://blog.coincodecap.com/a-guide-to-cryptocurrency-trading-bots)
- [Flash Loans — Borrow Without Collateral](https://blog.coincodecap.com/what-are-flash-loans-on-ethereum)
- [Ewasm – Ethereum WebAssembly](https://blog.coincodecap.com/ewasm-ethereum-webassembly)
- [Non-Fungible Tokens Explained](https://blog.coincodecap.com/non-fungible-tokens-explained)
- [Benefits of Crypto payroll for Employees and Companies](https://blog.coincodecap.com/benefits-of-crypto-payroll-for-employees-and-companies)
- [Ethereum versus Bitcoin](https://blog.coincodecap.com/ethereum-versus-bitcoin)
- [A review of DeFi Money Market Project(DMM)](https://blog.coincodecap.com/a-review-of-defi-money-market-project-dmm)

The post [The Graph vs Alethio vs web3JS](https://blog.coincodecap.com/the-graph-vs-alethio-vs-web3js) appeared first on [CoinCodeCap Blog](https://blog.coincodecap.com).