Uniswap Subgraph on The Graph GraphQL API

Get Uniswap data using The Graph GraphQL API

Build a dotnet sdk to call Uniswap’s subgraph using GraphQL

Reitter
Published in
4 min readDec 20, 2020

--

Uniswap is a decentralized protocol built on Ethereum where users can swap ERC-20 tokens without the need for buyers and sellers to create a demand. It is the most popular decentralized exchange (DEX) with over 1.4 billion dollars of Total Value Locked at time of writing.

Uniswap uses the y = k*x market maker mechanism to determine the price of a token, where this product is held constant and used to determine the price of the transactions.

The Graph is an indexing protocol for querying data from Ethereum and IPFS. Anyone can contribute and create what is called a subgraph, thus making the access to blockchain data easy.

The Graph has multiple subgraphs such as Aave, ENS, Balancer and MakerDAO. In order to query data from these subgraphs we will use GraphQL.

GraphQL is an open-source data query and manipulation language for APIs created by Facebook.

The Uniswap subgraph can be found below:

Bitquery has a very comprehensive Uniswap API; you should also try it.

Building the Uniswap sdk

In order to make GraphQL queries, we will need two packages, one for making the GraphQL queries and the other to deserialize the data using the new and high-performant System.Text.Json. In order to add the packages, we can run the cli commands:

dotnet add package GraphQL.Client --version 3.2.0

dotnet add package GraphQL.Cliente.Serializer.SystemTextJson --version 3.2.0

Now, we can create our Uniswap.cs class that will receive by constructor injection the IGraphQLClient:

Uniswap.cs

Get Most Liquid Market Pairs Endpoint

We can now call the Uniswap V2 subgraph. Let’s create a method called GetMostLiquidMarketPairs and make our first query using GraphQL. In order to create the query, we instanciate the GraphQLRequest class and set the property Query with our desired GraphQL query:

Now we can call the API by using the method SendQueryAsync from the GraphQL client:

Call the Uniswap V2 subgraph

We will get the following JSON response:

GetMostLiquidMarketPairs JSON response

Add Uniswap class to the Dependency Injection Container

In order to have acess to the Uniswap class we have built, we will add it to the DI Container. In order to do so, we will create the IUniswap interface and create the extension method AddUniswap as we can see in the code below. We create an extension method for the IServiceCollection interface because when using this sdk, we will simply add in the StartUp.cs class the services.AddUniswap();.

AddUniswap.cs

In the code above we have used the HttpClient typed client that is just an HttpClient that's pre-configured for some specific use.

Use the Uniswap sdk

Now that we have built the sdk, we can use it in our own APIs. In the example below, we will get the IUniwap interface via constructor injection and then we are able to call its methods as seen in the sample controller below:

Use IUniswap.cs

Uniswap dotnet standard library

This complete library is free and can be downloaded and added to your project by running the cli command:

dotnet add package Uniswap.dotnet --version 1.0.1

You can also add this package by going to nuget, the official package manager for .NET or by going to GitHub:

Conclusion

In this article we have built a dotnet wrapper for the Uniswap V2 subgraph in order to get the Decentralized Exchange analytics, such as the Most Liquid Market Pairs.

References

Also, Read

Get Best Software Deals Directly In Your Inbox

--

--