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:
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:
We will get the following 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();
.
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:
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
- Uniswap APIs — Get Pools Data, Tokens, and Create Charts
- Learn about Ethereum and Web3
- Crypto Trading Bot
- 3Commas Review
- AAX Exchange Review | Referral Code, Trading Fee, Pros and Cons
- Deribit Review | Options, Fees, APIs and Testnet
- FTX Crypto Exchange Review
- NGRAVE ZERO review
- Bybit Exchange Review
- 3Commas vs Cryptohopper
- The Best Bitcoin Hardware wallet
- Crypto Copy Trading Platforms
- Best monero wallet
- ledger nano s vs x
- Bitsgap vs 3Commas vs Quadency
- The Best Crypto Tax Software
- Best Crypto Trading Platforms
- Best Crypto Lending Platforms
- Ledger Nano S vs Trezor one vs Trezor T vs Ledger Nano X
- BlockFi vs Celsius vs Hodlnaut
- Bitsgap review — A Crypto Trading Bot That Makes Easy Money
- Quadency Review- A Crypto Trading Bot Made For Professionals
- PrimeXBT Review | Leverage Trading, Fee and Covesting
- Altrady review
- Ellipal Titan Review
- SecuX Stone Review
- BlockFi Review | Earn up to 8.6% interests on your Crypto
- Coinrule review
- Best Blockchain Analysis Tools
- Crypto arbitrage guide: How to make money as a beginner
- Best Crypto Charting Tool
- What are the best books to learn about Bitcoin?