Extracting data from Uniswap using TheGraph and Python

in STEMGeeks5 years ago

Screenshot 20210214 at 09.44.14.png

If you are intending to get information from Ethereum or any EVM blockchains TheGraph is a great tool. It is a decentralized service providing data subgraphs using GraphQL query language. In this tutorial, I will be showing how you can get data from Uniswap using python
You can get a clear idea of how TheGraph works in this video

In order to search and extract data, you need to install gql library in your Python environment. I will also be using Plotly and Pandas to create a bar chart of the query results so you may also need to install those libraries.

pip install gql
pip install plotly
pip install pandas

Once you have both libraries installed you can import them into your code

Screenshot 20210214 at 10.01.56.png

We extract data using this piece of code. In this example, we will be searching for the first 10 Uniswap pairs with a pool volume bigger than $10M and will store the result in the response variable. The data is fetched as JSON notation so our response variable will be a dictionary,

Screenshot 20210214 at 10.08.54.png

We then create our pandas dataframe from the response variable

Screenshot 20210214 at 09.59.14.png

This is how our dataframe looks like

Screenshot 20210214 at 10.10.34.png

We add column name, change the type of the volume column from string to float, and add a new column showing the token pairs.

Screenshot 20210214 at 10.14.31.png

And finally, create our bar chart with these two lines of code

Screenshot 20210214 at 10.15.51.png

Here's the result

newplot 3.png

You can use this method to keep an eye on trades in a specific liquidity pool and launch a signal to decide if you want to invest.