DEV Community

Cover image for How to Track liquidity for token pairs on Uniswap
Nikhil for Bitquery

Posted on • Originally published at bitquery.io

How to Track liquidity for token pairs on Uniswap

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 allow us to access blockchain data in real-time using GraphQL subscriptions. You can try out V2 APIs here

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 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 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.

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.

Open this query in the GraphQL IDE.

{
  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
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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. 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”.

Open this query in the GraphQL IDE.

{
  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
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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 in the GraphQL IDE.

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

Also Read: 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 in the GraphQL IDE.

{
  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}}
      )
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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, 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 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 in the GraphQL IDE.

{
  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
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Also Read:  Ethereum DEX GraphQL APIs with Examples

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 in the GraphQL IDE.

{
  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
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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, Curve, Balancer, 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 comments (0)