Table of Contents
A practical exercise using the Aave protocol
Introduction
There is always talk about the clarity of blockchain, 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 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: A protocol for building decentralized applications using GraphQL.
- alethio: An API rest to obtain data from the blockchain in a friendly way.
- Web3 Foundation Team: 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 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:
But if we donโt want to trust the documentation and see it in the code, letโs see the contract deployed on mainnet and look in code in the part:
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:
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.
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
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
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.
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?