Extracting data from Uniswap using TheGraph and Python

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@santigs·
0.000 HBD
Extracting data from Uniswap using TheGraph and Python
![Screenshot 20210214 at 09.44.14.png](https://files.peakd.com/file/peakd-hive/santigs/fvsdA24B-Screenshot202021-02-1420at2009.44.14.png)

If you are intending to get information from Ethereum or any EVM blockchains [TheGraph](https://thegraph.com/) 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

https://www.youtube.com/watch?v=cizLhxSKrAc

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](https://files.peakd.com/file/peakd-hive/santigs/xo6ozdPn-Screenshot202021-02-1420at2010.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](https://files.peakd.com/file/peakd-hive/santigs/2c5NXGPV-Screenshot202021-02-1420at2010.08.54.png)

We then create our pandas dataframe from the response variable

![Screenshot 20210214 at 09.59.14.png](https://files.peakd.com/file/peakd-hive/santigs/TIJNDKqS-Screenshot202021-02-1420at2009.59.14.png)

This is how our dataframe looks like


![Screenshot 20210214 at 10.10.34.png](https://files.peakd.com/file/peakd-hive/santigs/fAr8aSAd-Screenshot202021-02-1420at2010.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](https://files.peakd.com/file/peakd-hive/santigs/DEyZTqI8-Screenshot202021-02-1420at2010.14.31.png)

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


![Screenshot 20210214 at 10.15.51.png](https://files.peakd.com/file/peakd-hive/santigs/o6JyjPq8-Screenshot202021-02-1420at2010.15.51.png)

Here's the result


![newplot 3.png](https://files.peakd.com/file/peakd-hive/santigs/G0KBDQB2-newplot203.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. 
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , ,