How to Track liquidity for token pairs on Uniswap
Tags: beginners, tutorial, graphql, blockchain
Published: 2023-09-05 09:30:00

## **Introduction**

Today we will explore different aspects of the liquidity pools of any token pair on Uniswap. Uniswap is the most popular dapp in the crypto ecosystem; it is a decentralized exchange (DEX) that allows you to trade tokens on a blockchain. Out of the different types of DEX, Uniswap is called AMM (Automated money market), which relies on the concept of liquidity pools. Let’s refresh on liquidity pools and then explore data using Bitquery V2 APIs.

Our [V2 APIs](https://docs.bitquery.io/docs/intro/) allow us to access blockchain data in real-time using GraphQL subscriptions. You can try out V2 APIs [here](https://ide.bitquery.io/streaming)

## **Understanding Token-Pair Liquidity**

There are different types of DEX, which you can divide mainly into AMM DEX and Order Book DEX. An [AMM DEX](https://academy.binance.com/en/articles/what-is-an-automated-market-maker-amm) like Uniswap, Sushiswap, etc. relies on algorithmic pricing and liquidity pools, while an order book DEX matches buyers and sellers directly based on their submitted orders.

Liquidity, in the context of [Uniswap](https://uniswap.org/) and similar AMM DEXs, refers to the availability of tokens in liquidity pools that facilitate trades. The pools are created by liquidity providers, who provide tokens to liquidity pools in exchange for earning part of trading fees. The depth of liquidity pools plays an important role in determining how easily traders can execute trades without causing significant fluctuations in market prices.

Now that we have clarified what liquidity pools are and how they affect DEX working, let’s see how to fetch data using the [DEX API](https://bitquery.io/products/dex).

## **Get All Liquidity Pools For a Token**

As anyone can create a liquidity pool for any token, there will be many pools for a single token, so let’s try to fetch a list of all the pools.

In this query, we will be fetching a list of liquidity pools created for USDT, for which we need to set buy currency to the [address of USDT token](https://explorer.bitquery.io/ethereum/token/0xdAC17F958D2ee523a2206206994597C13D831ec7).

Open [this query](https://ide.bitquery.io/uniswap-streaming-get-all-liquidity-pools-for-a-token/) in the GraphQL IDE.

```graphql
{
  EVM(dataset: combined, network: eth) {
    DEXTrades(
        where: {
            Trade: {
                Buy: {
                    Currency: {
                        SmartContract: {
                            is: "0xdAC17F958D2ee523a2206206994597C13D831ec7"
                        }
                    }
                }
            }
        }
        limit: {count: 10}
        limitBy: {by: Trade_Sell_Currency_SmartContract, count: 1}
    ) {
      Trade {
        Dex {
          ProtocolName
          OwnerAddress
          ProtocolVersion
          PairTokenAddress: SmartContract
          Pair {
            SmartContract
            Name
            Symbol
          }
        }
        Buy {
          Currency {
            Name
            SmartContract
          }
        }
        Sell {
          Currency {
            Name
            SmartContract
          }
        }
      }
    }
  }
}
```

## **Get all pools for tokens on a certain DEX**

When we run the above query, you will notice all those pairs are from [Uniswap V3](https://community.bitquery.io/t/introducing-uniswap-v3-protocol-on-eth/336). This is because the API sends data for the default DEX if there is no DEX specified in the query.

In this query, we have defined the name of the DEX from which we need data, which is “[Uniswap V2](https://explorer.bitquery.io/ethereum/smart_contract/0x7a250d5630b4cf539739df2c5dacb4c659f2488d)”.

Open [this query](https://ide.bitquery.io/DEX-Streaming-Get-Pools-from-Any-DEX) in the GraphQL IDE.

```graphql
{
  EVM(dataset: combined, network: eth) {
    DEXTrades(
        where: {
            Trade: {
                Buy: {
                    Currency: {
                        SmartContract: {
                            is: "0xdAC17F958D2ee523a2206206994597C13D831ec7"
                        }
                    }
                }, 
                Dex: {
                    Pair: {
                        Name: {
                            is: "Uniswap V2"
                        }
                    }
                }
            }
        }
        limit: {count: 10}
        limitBy: {by: Trade_Sell_Currency_SmartContract, count: 1}
    ) {
      Trade {
        Dex {
          ProtocolName
          OwnerAddress
          ProtocolVersion
          PairTokenAddress: SmartContract
          Pair {
            SmartContract
            Name
            Symbol
          }
        }
        Buy {
          Currency {
            Name
            SmartContract
          }
        }
        Sell {
          Currency {
            Name
            SmartContract
          }
        }
      }
    }
  }
}
```

You can also use the smart contract address of the DEX instead of the protocol name, as the API might not recognize the name of some DEXs. Instead of selecting the protocolName field in the DEX filter, select the SmartContract field and input the smart contract address. Here is an example query that uses the smart contract address of DEX to filter.

## **Get liquidity of a liquidity pool**

Now that we can fetch a list of liquidity pools, we can look into fetching details about each pool using the address of a specific liquidity pool.

This query fetches the balance of tokens in the pool, which is the liquidity of that liquidity pool.

Open [this query](https://ide.bitquery.io/Uniswap-Streaming-Get-Depth-of-Liquidity-Pool) in the GraphQL IDE.

```graphql
{
  EVM(dataset: combined, network: eth) {
    BalanceUpdates(
        where: {
            BalanceUpdate: {
                Address: {
                    is: "0x6ca298d2983ab03aa1da7679389d955a4efee15c"
                }
            }
        }
        orderBy: {descendingByField: "balance"}
    ) {
      Currency {
        Name
      }
      balance: sum(of: BalanceUpdate_Amount)
      BalanceUpdate {
        Address
      }
    }
  }
}
```

**Also Read: [OpenSea NFT API](https://bitquery.io/blog/opensea-nft-api)**

## **Get number of trades for a liquidity pool**

We can also get the number of trades that happened in a pool, which will tell us how frequently that pool is being used.

This query allows you to count all the successful trades in that pool. We have to filter using the address of the pool as an argument for Trade.Pair. SmartContract and using the count method to count distinct Transaction_Hash values.

Open [this query](https://ide.bitquery.io/Uniswap-Streaming-Get-Number-of-Trades-in-a-Pool) in the GraphQL IDE.

```graphql
{
  EVM(network: eth, dataset: combined) {
    DEXTrades(
        orderBy: {descending: Block_Date}
        where: {
            Trade: {
                Dex: {
                    Pair: {
                        SmartContract: {
                            is: "0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852"
                        }
                    }
                }
            }
        }
        limit: {count: 10}
    ) {
      Block {
        Time(interval: {in: days})
      }
      Trades: count(
        distinct: Transaction_Hash
        if: {TransactionStatus: {Success: true}}
      )
    }
  }
}
```

## **Get all the recent token pairs created on Uniswap**

As anyone can create a liquidity pool without any restrictions, there are many pools created every day, so let’s see how to fetch details about created pools.

Whenever a new pool is created using the [Uniswap Factory Smart Contract](https://explorer.bitquery.io/ethereum/smart_contract/0x1f98431c8ad98523631ae4a59f267346ea31f984), an event called PoolCreated is emitted. We can fetch all those events in order to get details about the newly created pool.

To fetch events, we will use the [Events API](https://docs.bitquery.io/docs/examples/events/events_api/) with the address of the Factory contract and the name of the event passed as arguments to the Log.SmartContract and Log.Signature.Name filters, respectively.

Open [this query](https://ide.bitquery.io/Uniswap-Streaming-Get-Recently-Created-Pools-by-PoolCreated-Event) in the GraphQL IDE.

```graphql
{
  EVM {
    Events(
        where: {
            Log: {
                SmartContract: {
                    is: "0x1F98431c8aD98523631AE4a59f267346ea31F984"
                }, 
                Signature: {
                    Name: {
                        is: "PoolCreated"
                    }
                }
            }
        }
        limit: {count: 10}
        orderBy: {descending: Block_Date}
    ) {
      Arguments {
        Name
        Value {
          ... on EVM_ABI_Integer_Value_Arg {
            integer
          }
          ... on EVM_ABI_String_Value_Arg {
            string
          }
          ... on EVM_ABI_Address_Value_Arg {
            address
          }
          ... on EVM_ABI_BigInt_Value_Arg {
            bigInteger
          }
          ... on EVM_ABI_Bytes_Value_Arg {
            hex
          }
          ... on EVM_ABI_Boolean_Value_Arg {
            bool
          }
        }
        Type
        Index
      }
      Block {
        Hash
      }
    }
  }
}
```

**Also Read:  [Ethereum DEX GraphQL APIs with Examples](https://bitquery.io/blog/ethereum-dex-graphql-api)**

## **Get all pools for token pair**

As anyone can create liquidity pools, there can be many pools for the same token pairs. As some pools will have low liquidity and some might have high liquidity, getting that data is important.

To get a list of liquidity pools for a token pair, we will pass the token addresses for both tokens in the pair to Trade.Buy.Currency.SmartContract and Trade.Sell.Currency. SmartContract respectively. Finally, we will limit the result by setting the value of Trade_Dex_Pair_SmartContract to 1, so we will get all the unique pools for the particular token pair.

Open [this query](https://ide.bitquery.io/DEX-Streaming-Get-All-Pools-for-Token-Pair) in the GraphQL IDE.

```graphql
{
  EVM(dataset: combined, network: eth) {
    DEXTrades(
        where: {
            Trade: {
                Buy: {
                    Currency: {
                        SmartContract: {
                            is: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
                        }
                    }
                }, 
                Sell: {
                    Currency: {
                        SmartContract: {
                            is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
                        }
                    }
                }
            }
        }
        limit: {count: 10}
        limitBy: {by: Trade_Dex_Pair_SmartContract, count: 1}
    ) {
      Trade {
        Dex {
          ProtocolName
          OwnerAddress
          ProtocolVersion
          PairTokenAddress: SmartContract
          Pair {
            SmartContract
            Name
            Symbol
          }
        }
        Buy {
          Currency {
            Name
            SmartContract
          }
        }
        Sell {
          Currency {
            Name
            SmartContract
          }
        }
      }
    }
  }
}
```

## **Get Uniswap Data From Arbitrum**

For demonstration, we have fetched the data from Uniswap Contracts deployed on Ethereum, but you can also fetch data from Uniswap Contracts deployed on Arbitrum by changing the value of the network field. Just make sure you change the addresses for all the tokens or DEXs to appropriate Arbitrum addresses.

## **Get Liquidity Pool Data From DEXs Besides Uniswap**

We explored Uniswap V3 and V2 data here, but you can also fetch data from [SushiSwap](https://explorer.bitquery.io/ethereum/token/0x6b3595068778dd592e39a122f4f5a5cf09c90fe2), [Curve](https://explorer.bitquery.io/ethereum/token/0xd533a949740bb3306d119cc777fa900ba034cd52), [Balancer](https://explorer.bitquery.io/ethereum/token/0xba100000625a3754423978a60c9317c58a424e3d), and many other DEXs.

The Bitquery V2 API currently supports Ethereum, Arbitrum, and BSC, so you will be able to fetch data from many DEXs on these chains.

## Also Read:

- [Top Uniswap APIs](https://bitquery.io/blog/top-uniswap-APIs)
- [Tron DEX APIs with Bitquery | GraphQL APIs](https://bitquery.io/blog/tron-dex-api)